Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws-sdk): lambda client instrumentation #916

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Specific service logic currently implemented for:

- [SQS](./docs/sqs.md)
- [SNS](./docs/sns.md)
- [Lambda](./docs/lambda.md)
- DynamoDb

## Potential Side Effects
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Lambda

Lambda is Amazon's function-as-a-service (FaaS) platform. Thus, it should follow the [OpenTelemetry specification for FaaS systems](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/faas.md).
chrisrichardsevergreen marked this conversation as resolved.
Show resolved Hide resolved

## Specific trace semantics

The following methods are automatically enhanced:

### invoke
chrisrichardsevergreen marked this conversation as resolved.
Show resolved Hide resolved

- [Outgoing Attributes](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/faas.md#outgoing-invocations) are added by this instrumentation according to the spec.
chrisrichardsevergreen marked this conversation as resolved.
Show resolved Hide resolved
- OpenTelemetry trace context is injected into the `ClientContext` parameter, allowing functions to extract this using the `Custom` property within the function.
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,32 @@
"dependencies": {
"@opentelemetry/core": "^1.0.0",
"@opentelemetry/instrumentation": "^0.27.0",
"@opentelemetry/propagation-utils": "^0.26.0",
"@opentelemetry/semantic-conventions": "^1.0.0",
"@opentelemetry/propagation-utils": "^0.26.0"
"aws-lambda": "^1.0.7"
blumamir marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"@aws-sdk/client-dynamodb": "3.37.0",
"@aws-sdk/client-lambda": "3.37.0",
"@aws-sdk/client-s3": "3.37.0",
"@aws-sdk/client-sqs": "3.37.0",
"@aws-sdk/types": "3.37.0",
"@opentelemetry/api": "1.0.1",
"@opentelemetry/contrib-test-utils": "^0.29.0",
"@opentelemetry/sdk-trace-base": "1.0.1",
"@types/aws-lambda": "^8.10.92",
"@types/mocha": "8.2.3",
"@types/node": "16.11.21",
"@types/sinon": "10.0.6",
"aws-sdk": "2.1008.0",
"eslint": "8.7.0",
"expect": "27.4.2",
"gts": "3.1.0",
"mocha": "7.2.0",
"nock": "13.2.1",
"nyc": "15.1.0",
"rimraf": "3.0.2",
"sinon": "13.0.1",
"gts": "3.1.0",
"@opentelemetry/contrib-test-utils": "^0.29.0",
"test-all-versions": "5.0.1",
"ts-mocha": "8.0.0",
"typescript": "4.3.4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '../types';
import { DynamodbServiceExtension } from './dynamodb';
import { SnsServiceExtension } from './sns';
import { LambdaServiceExtension } from './lambda';

export class ServicesExtensions implements ServiceExtension {
services: Map<string, ServiceExtension> = new Map();
Expand All @@ -31,6 +32,7 @@ export class ServicesExtensions implements ServiceExtension {
this.services.set('SQS', new SqsServiceExtension());
this.services.set('SNS', new SnsServiceExtension());
this.services.set('DynamoDB', new DynamodbServiceExtension());
this.services.set('Lambda', new LambdaServiceExtension());
}

requestPreSpanHook(request: NormalizedRequest): RequestMetadata {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Span, SpanKind, Tracer } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
AwsSdkInstrumentationConfig,
NormalizedRequest,
NormalizedResponse,
} from '../types';
import { RequestMetadata, ServiceExtension } from './ServiceExtension';
import { TextMapSetter, context, propagation } from '@opentelemetry/api';
import type { ClientContext } from 'aws-lambda';

export class LambdaServiceExtension implements ServiceExtension {
requestPreSpanHook(request: NormalizedRequest): RequestMetadata {
const functionName = this.extractFunctionName(request.commandInput);

let spanAttributes = {};
let spanName: string | undefined;

switch (request.commandName) {
case 'Invoke':
spanAttributes = {
[SemanticAttributes.FAAS_INVOKED_NAME]: functionName,
[SemanticAttributes.FAAS_INVOKED_PROVIDER]: 'aws',
};
if (request.region) {
spanAttributes = {
...spanAttributes,
[SemanticAttributes.FAAS_INVOKED_REGION]: request.region,
};
blumamir marked this conversation as resolved.
Show resolved Hide resolved
}
spanName = `${functionName} invoke`;
chrisrichardsevergreen marked this conversation as resolved.
Show resolved Hide resolved
break;
}
return {
isIncoming: false,
spanAttributes,
spanKind: SpanKind.CLIENT,
spanName,
};
}

requestPostSpanHook = (request: NormalizedRequest) => {
switch (request.commandName) {
case 'Invoke':
chrisrichardsevergreen marked this conversation as resolved.
Show resolved Hide resolved
{
request.commandInput = injectPropagationContext(request.commandInput);
}
break;
}
};

responseHook(
response: NormalizedResponse,
span: Span,
tracer: Tracer,
config: AwsSdkInstrumentationConfig
) {
const operation = response.request.commandName;

if (operation === 'Invoke') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a switch statement too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

if (response.data && '$metadata' in response.data) {
blumamir marked this conversation as resolved.
Show resolved Hide resolved
span.setAttribute(
SemanticAttributes.FAAS_EXECUTION,
response.data['$metadata'].requestId
);
}
}
}

extractFunctionName = (commandInput: Record<string, any>): string => {
return commandInput?.FunctionName;
};
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a space below this would make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linter keeps removing that, so unfortunately I can't


class ContextSetter implements TextMapSetter<Record<string, any>> {
set(carrier: Record<string, any>, key: string, value: string): void {
const parsedClientContext: ClientContext = JSON.parse(
blumamir marked this conversation as resolved.
Show resolved Hide resolved
carrier.ClientContext !== undefined
blumamir marked this conversation as resolved.
Show resolved Hide resolved
? Buffer.from(carrier.ClientContext, 'base64').toString('utf8')
: '{"Custom":{}}'
);
const updatedPayload = {
...parsedClientContext,
Custom: {
blumamir marked this conversation as resolved.
Show resolved Hide resolved
...parsedClientContext.Custom,
blumamir marked this conversation as resolved.
Show resolved Hide resolved
[key]: value,
},
};
const encodedPayload = Buffer.from(JSON.stringify(updatedPayload)).toString(
'base64'
);
if (encodedPayload.length <= 3583) {
blumamir marked this conversation as resolved.
Show resolved Hide resolved
carrier.ClientContext = encodedPayload;
}
blumamir marked this conversation as resolved.
Show resolved Hide resolved
}
}
const contextSetter = new ContextSetter();

const injectPropagationContext = (
invocationRequest: Record<string, any>
blumamir marked this conversation as resolved.
Show resolved Hide resolved
): Record<string, any> => {
propagation.inject(context.active(), invocationRequest, contextSetter);
return invocationRequest;
};
Loading