Published on 00/00/0000
Last updated on 00/00/0000
Published on 00/00/0000
Last updated on 00/00/0000
Share
Share
INSIGHTS
4 min read
Share
npm init -y
Then add the following dependencies to the resulting package.json:
"dependencies": {
"@opentelemetry/api": "^1.1.0",
"@opentelemetry/auto-instrumentations-node": "^0.30.0",
"@opentelemetry/exporter-trace-otlp-http": "^0.28.0",
"@opentelemetry/instrumentation": "^0.28.0",
"@opentelemetry/sdk-trace-base": "^1.2.0",
"@opentelemetry/sdk-trace-node": "^1.2.0"
}
After adding the required OpenTelemetry dependencies, run the following command:
npm install
const api = require("@opentelemetry/api");
const { BatchSpanProcessor } = require("@opentelemetry/sdk-trace-base");
const {
OTLPTraceExporter
} = require("@opentelemetry/exporter-trace-otlp-http");
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const { registerInstrumentations } = require("@opentelemetry/instrumentation");
const {
getNodeAutoInstrumentations
} = require("@opentelemetry/auto-instrumentations-node");
api.diag.setLogger(new api.DiagConsoleLogger(), api.DiagLogLevel.ALL);
const provider = new NodeTracerProvider();
const collectorOptions = {
url: "<backend_url>"
};
const spanProcessor = new BatchSpanProcessor(
new OTLPTraceExporter(collectorOptions)
);
provider.addSpanProcessor(spanProcessor);
provider.register();
registerInstrumentations({
instrumentations: [
getNodeAutoInstrumentations({
"@opentelemetry/instrumentation-aws-lambda": {
disableAwsContextPropagation: true
}
})
]
});
Replace <backend_url> with the URL of your favorite backend to send all tracing data to it.
Note that disableAwsContextPropagation is set to true. The reason for this is that the Lambda instrumentation tries to use the X-Ray context headers by default, this results in a non-sampled context, which creates a NonRecordingSpan.
More details can be found in the instrumentation documentation.
"use strict";
const http = require("http");
module.exports.hello = async (event) => {
http.get("http://aws.amazon.com");
return {
statusCode: 200,
body: "Success!"
};
};
service: lambda-otel-native
frameworkVersion: '3'provider:
name: aws
runtime: nodejs14.x
region: '<your-region>'
environment:
NODE_OPTIONS: --require lambda-wrapperfunctions:
lambda-otel-test:
handler: handler.hello
For OpenTelemetry to work properly, lambda-wrapper.js must be included before any other file. That’s why we have added the environment variable NODE_OPTIONS:--require lambda-wrapper which preloads the wrapper at startup.
Note if you are not using Serverless Framework to deploy your Lambda function, you must manually add this environment variable using the AWS Console UI.
Finally, run the following command to deploy the project to AWS:
serverless deploy
Get emerging insights on innovative technology straight to your inbox.
Discover how AI assistants can revolutionize your business, from automating routine tasks and improving employee productivity to delivering personalized customer experiences and bridging the AI skills gap.
The Shift is Outshift’s exclusive newsletter.
The latest news and updates on generative AI, quantum computing, and other groundbreaking innovations shaping the future of technology.