Last active
August 29, 2023 16:17
-
-
Save ImDevinC/de8266758a1d732caa36b91dc3110a8a to your computer and use it in GitHub Desktop.
Serverless OTEL Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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