Skip to content

Instantly share code, notes, and snippets.

@ImDevinC
Last active August 29, 2023 16:17
Show Gist options
  • Save ImDevinC/de8266758a1d732caa36b91dc3110a8a to your computer and use it in GitHub Desktop.
Save ImDevinC/de8266758a1d732caa36b91dc3110a8a to your computer and use it in GitHub Desktop.
Serverless OTEL Example
// index.ts
import { initializeTelemetry } from './initialize';
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { SQSClient, SendMessageCommand, SendMessageCommandInput } from '@aws-sdk/client-sqs';
const sqsClient = new SQSClient({});
const publishMessage = async () => {
const input: SendMessageCommandInput = {
QueueUrl: process.env.QUEUE_URL,
MessageBody: 'test-message'
};
const command = new SendMessageCommand(input);
await sqsClient.send(command)
}
export const handler = async (event: APIGatewayProxyEvent) : Promise<APIGatewayProxyResult> => {
//const sdk = initializeTelemetry();
let resp: APIGatewayProxyResult
try {
await publishMessage();
resp = {
statusCode: 200
}
} catch (err) {
resp = {
statusCode: 500
}
}
//await sdk.shutdown();
return resp;
}
// initialize.ts
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { NodeSDK } from '@opentelemetry/sdk-node';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-proto';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
export const initializeTelemetry = () => {
const sdk = new NodeSDK({
autoDetectResources: true,
instrumentations: [getNodeAutoInstrumentations({
'@opentelemetry/instrumentation-aws-lambda': {
disableAwsContextPropagation: true
},
})],
traceExporter: new OTLPTraceExporter(),
metricReader: new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter()
})
});
sdk.start();
return sdk;
}
//setup.ts
import { initializeTelemetry } from './initialize'
initializeTelemetry()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment