Skip to content

Commit

Permalink
chore: Correlate logs with traces in Datadog
Browse files Browse the repository at this point in the history
  • Loading branch information
stevoland committed Feb 19, 2024
1 parent 8b3be68 commit 8f3556d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"@opentelemetry/api": "^1.7.0",
"@opentelemetry/auto-instrumentations-node": "^0.41.1",
"@opentelemetry/instrumentation-pino": "^0.35.0",
"@operate-first/probot-metrics": "^1.0.0",
"@probot/adapter-aws-lambda-serverless": "^3.0.2",
"deepmerge": "^4.3.1",
Expand Down
20 changes: 20 additions & 0 deletions tracing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Configuring manually due to https://github.com/open-telemetry/opentelemetry-js-contrib/issues/1773
const process = require('process')
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http')
const { PinoInstrumentation } = require('@opentelemetry/instrumentation-pino')
const opentelemetry = require('@opentelemetry/sdk-node')
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc')
const { Resource } = require('@opentelemetry/resources')
Expand All @@ -18,6 +19,25 @@ if (OTEL_EXPORTER_OTLP_ENDPOINT) {
textMapPropagator: new W3CTraceContextPropagator(),
traceExporter,
instrumentations: [
new PinoInstrumentation({
logHook: (span, record) => {
const spanContext = span?.spanContext()

if (!spanContext) {
return
}

// Transform IDs from opentelemetry format to Datadog format
// https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/opentelemetry/?tab=nodejs)
const { spanId, traceId } = spanContext
const traceIdEnd = traceId.slice(traceId.length / 2)
const datadogTraceId = BigInt(`0x${traceIdEnd}`).toString()
const datadogSpanId = BigInt(`0x${spanId}`).toString()

record['dd.trace_id'] = datadogTraceId
record['dd.span_id'] = datadogSpanId
}
}),
new HttpInstrumentation({
ignoreIncomingPaths: ['/health', '/metrics', '/ping']
})
Expand Down

0 comments on commit 8f3556d

Please sign in to comment.