dd-trace 6.26.0 → 6.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +58 -1
- package/initialize.mjs +5 -3
- package/package.json +9 -8
- package/packages/datadog-instrumentations/src/ai.js +88 -127
- package/packages/datadog-instrumentations/src/aws-lambda.js +9 -0
- package/packages/datadog-instrumentations/src/helpers/hooks.js +0 -1
- package/packages/datadog-instrumentations/src/helpers/instrument.js +1 -3
- package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentation-registry.js +4 -1
- package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/ai.js +2 -0
- package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/graphql-jit.js +5 -3
- package/packages/datadog-instrumentations/src/modelcontextprotocol-sdk.js +26 -2
- package/packages/datadog-instrumentations/src/modelcontextprotocol-server.js +3 -1
- package/packages/datadog-instrumentations/src/path-to-regexp.js +11 -11
- package/packages/datadog-instrumentations/src/router.js +17 -1
- package/packages/datadog-instrumentations/src/vitest-main-no-worker-init.js +1 -1
- package/packages/datadog-plugin-aws-lambda/src/context/events.js +167 -0
- package/packages/datadog-plugin-aws-lambda/src/context/extractor.js +153 -0
- package/packages/datadog-plugin-aws-lambda/src/context/http.js +50 -0
- package/packages/datadog-plugin-aws-lambda/src/context/span-context.js +26 -0
- package/packages/datadog-plugin-aws-lambda/src/context/step-functions.js +142 -0
- package/packages/datadog-plugin-aws-lambda/src/context/types.d.ts +17 -0
- package/packages/datadog-plugin-aws-lambda/src/context/xray.js +67 -0
- package/packages/datadog-plugin-aws-lambda/src/index.js +52 -4
- package/packages/datadog-plugin-azure-functions/src/index.js +68 -12
- package/packages/datadog-plugin-fetch/src/index.js +7 -0
- package/packages/datadog-plugin-http/src/client.js +12 -0
- package/packages/datadog-plugin-http/src/server.js +18 -4
- package/packages/dd-trace/src/aiguard/client.js +7 -9
- package/packages/dd-trace/src/aiguard/evaluation.js +7 -2
- package/packages/dd-trace/src/aiguard/messages/utils.js +2 -2
- package/packages/dd-trace/src/aiguard/sdk.js +7 -2
- package/packages/dd-trace/src/appsec/api_security/normalized-route.js +273 -167
- package/packages/dd-trace/src/appsec/channels.js +1 -0
- package/packages/dd-trace/src/appsec/iast/taint-tracking/plugin.js +21 -14
- package/packages/dd-trace/src/appsec/index.js +3 -0
- package/packages/dd-trace/src/appsec/rasp/index.js +57 -3
- package/packages/dd-trace/src/appsec/rasp/utils.js +14 -0
- package/packages/dd-trace/src/carrier.js +7 -1
- package/packages/dd-trace/src/ci-visibility/dynamic-instrumentation/index.js +4 -2
- package/packages/dd-trace/src/ci-visibility/dynamic-instrumentation/worker/index.js +31 -19
- package/packages/dd-trace/src/config/generated-config-types.d.ts +10 -3
- package/packages/dd-trace/src/config/index.js +28 -2
- package/packages/dd-trace/src/config/parsers.js +25 -0
- package/packages/dd-trace/src/config/supported-configurations.json +27 -7
- package/packages/dd-trace/src/datastreams/http.js +57 -0
- package/packages/dd-trace/src/datastreams/index.js +24 -0
- package/packages/dd-trace/src/datastreams/pathway.js +33 -4
- package/packages/dd-trace/src/datastreams/processor.js +22 -3
- package/packages/dd-trace/src/lambda/handler-paths.js +1 -1
- package/packages/dd-trace/src/llmobs/constants/tags.js +1 -0
- package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/index.js +33 -1
- package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/utils.js +102 -1
- package/packages/dd-trace/src/llmobs/span_processor.js +3 -0
- package/packages/dd-trace/src/llmobs/tagger.js +9 -0
- package/packages/dd-trace/src/noop/span.js +0 -4
- package/packages/dd-trace/src/openfeature/flagging_provider.js +17 -0
- package/packages/dd-trace/src/opentelemetry/logs/otlp_transformer.js +2 -1
- package/packages/dd-trace/src/opentelemetry/metrics/otlp_transformer.js +2 -1
- package/packages/dd-trace/src/opentelemetry/otlp/otlp_transformer_base.js +16 -1
- package/packages/dd-trace/src/opentelemetry/trace/index.js +5 -4
- package/packages/dd-trace/src/opentelemetry/trace/otlp_http_trace_exporter.js +10 -6
- package/packages/dd-trace/src/opentelemetry/trace/otlp_transformer.js +42 -26
- package/packages/dd-trace/src/opentracing/propagation/text_map.js +4 -4
- package/packages/dd-trace/src/plugin_manager.js +2 -0
- package/packages/dd-trace/src/plugins/util/inferred_proxy.js +3 -5
- package/packages/dd-trace/src/plugins/util/url.js +1 -1
- package/packages/dd-trace/src/register-loader.js +43 -0
- package/register.js +10 -44
- package/register.mjs +27 -0
|
@@ -593,6 +593,7 @@ addHook({ name: 'router', file: 'lib/layer.js', versions: ['>=1 <2'] }, Layer =>
|
|
|
593
593
|
})
|
|
594
594
|
|
|
595
595
|
const queryParserReadCh = channel('datadog:query:read:finish')
|
|
596
|
+
const routeCreateCh = channel('datadog:router:route:create')
|
|
596
597
|
|
|
597
598
|
addHook({ name: 'router', versions: ['>=2'] }, Router => {
|
|
598
599
|
const wrapRouterMethod = createWrapRouterMethod('router', getCompileToRegexp())
|
|
@@ -620,11 +621,26 @@ addHook({ name: 'router', versions: ['>=2'] }, Router => {
|
|
|
620
621
|
})
|
|
621
622
|
|
|
622
623
|
shimmer.wrap(WrappedRouter.prototype, 'use', wrapRouterMethod)
|
|
623
|
-
shimmer.wrap(WrappedRouter.prototype, 'route', wrapRouterMethod)
|
|
624
|
+
shimmer.wrap(WrappedRouter.prototype, 'route', original => wrapRouterMethod(publishRouteCreate(original)))
|
|
624
625
|
|
|
625
626
|
return WrappedRouter
|
|
626
627
|
})
|
|
627
628
|
|
|
629
|
+
/**
|
|
630
|
+
* Publish the matching options of the Router that owns each new Route (a Router does not inherit
|
|
631
|
+
* the app's settings), once at registration.
|
|
632
|
+
* @param {(this: { caseSensitive?: boolean, strict?: boolean }, path: string) => object} original
|
|
633
|
+
*/
|
|
634
|
+
function publishRouteCreate (original) {
|
|
635
|
+
return function (...args) {
|
|
636
|
+
const route = original.apply(this, args)
|
|
637
|
+
if (routeCreateCh.hasSubscribers) {
|
|
638
|
+
routeCreateCh.publish({ route, sensitive: this.caseSensitive === true, strict: this.strict === true })
|
|
639
|
+
}
|
|
640
|
+
return route
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
628
644
|
const routerParamStartCh = channel('datadog:router:param:start')
|
|
629
645
|
const visitedParams = new WeakSet()
|
|
630
646
|
|
|
@@ -110,7 +110,7 @@ function noop () {}
|
|
|
110
110
|
* @param {string} url
|
|
111
111
|
*/
|
|
112
112
|
function removeVitestBrowserUrlMetadata (url) {
|
|
113
|
-
const locationMatch =
|
|
113
|
+
const locationMatch = VITEST_BROWSER_STACK_LOCATION_RE.exec(url)
|
|
114
114
|
const location = locationMatch?.[0] || ''
|
|
115
115
|
const urlWithoutLocation = location ? url.slice(0, -location.length) : url
|
|
116
116
|
const queryStart = urlWithoutLocation.indexOf('?')
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const log = require('../../../dd-trace/src/log')
|
|
4
|
+
|
|
5
|
+
/** @typedef {import('../../../dd-trace/src/opentracing/span_context')} SpanContext */
|
|
6
|
+
/** @typedef {import('../../../dd-trace/src/opentracing/tracer')} Tracer */
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param {Record<string, unknown>|undefined} record SNS record.
|
|
10
|
+
* @returns {Record<string, string>|null}
|
|
11
|
+
*/
|
|
12
|
+
function getSNSHeaders (record) {
|
|
13
|
+
try {
|
|
14
|
+
const attribute = record?.Sns?.MessageAttributes?._datadog
|
|
15
|
+
if (!attribute?.Value) return null
|
|
16
|
+
const value = attribute.Type === 'String'
|
|
17
|
+
? attribute.Value
|
|
18
|
+
: Buffer.from(attribute.Value, 'base64').toString('ascii')
|
|
19
|
+
return JSON.parse(value)
|
|
20
|
+
} catch {
|
|
21
|
+
return null
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {Record<string, unknown>|undefined} record SNS-over-SQS record.
|
|
27
|
+
* @returns {Record<string, string>|null}
|
|
28
|
+
*/
|
|
29
|
+
function getSNSSQSHeaders (record) {
|
|
30
|
+
if (!record) return null
|
|
31
|
+
try {
|
|
32
|
+
if (record.body) {
|
|
33
|
+
const body = JSON.parse(record.body)
|
|
34
|
+
const attribute = body?.MessageAttributes?._datadog
|
|
35
|
+
if (attribute?.Value) {
|
|
36
|
+
const value = attribute.Type === 'String'
|
|
37
|
+
? attribute.Value
|
|
38
|
+
: Buffer.from(attribute.Value, 'base64').toString('ascii')
|
|
39
|
+
return JSON.parse(value)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const attribute = record.messageAttributes?._datadog
|
|
43
|
+
return attribute?.stringValue ? JSON.parse(attribute.stringValue) : null
|
|
44
|
+
} catch {
|
|
45
|
+
return null
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {Record<string, unknown>|undefined} record SQS record.
|
|
51
|
+
* @returns {Record<string, string>|null}
|
|
52
|
+
*/
|
|
53
|
+
function getSQSHeaders (record) {
|
|
54
|
+
try {
|
|
55
|
+
let headers = record?.messageAttributes?._datadog?.stringValue
|
|
56
|
+
if (!headers) {
|
|
57
|
+
const encoded = record?.messageAttributes?._datadog?.binaryValue
|
|
58
|
+
if (encoded) headers = Buffer.from(encoded, 'base64').toString('ascii')
|
|
59
|
+
}
|
|
60
|
+
return headers ? JSON.parse(headers) : null
|
|
61
|
+
} catch {
|
|
62
|
+
return null
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @param {Record<string, unknown>|undefined} record EventBridge-over-SQS record.
|
|
68
|
+
* @returns {Record<string, string>|null}
|
|
69
|
+
*/
|
|
70
|
+
function getEventBridgeSQSHeaders (record) {
|
|
71
|
+
try {
|
|
72
|
+
return record?.body ? JSON.parse(record.body)?.detail?._datadog ?? null : null
|
|
73
|
+
} catch {
|
|
74
|
+
return null
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @param {Record<string, unknown>} event Kinesis event.
|
|
80
|
+
* @param {Tracer} tracer Core tracer.
|
|
81
|
+
* @returns {SpanContext|null}
|
|
82
|
+
*/
|
|
83
|
+
function extractKinesis (event, tracer) {
|
|
84
|
+
const data = event?.Records?.[0]?.kinesis.data
|
|
85
|
+
if (data === undefined) return null
|
|
86
|
+
try {
|
|
87
|
+
if (!data) return null
|
|
88
|
+
const headers = JSON.parse(Buffer.from(data, 'base64').toString('ascii'))?._datadog
|
|
89
|
+
return headers ? tracer.extract('text_map', headers) : null
|
|
90
|
+
} catch (error) {
|
|
91
|
+
log.debug('Unable to extract trace context from Kinesis event: %s', error)
|
|
92
|
+
return null
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @param {Record<string, unknown>} event MSK event.
|
|
98
|
+
* @param {Tracer} tracer Core tracer.
|
|
99
|
+
* @returns {SpanContext|null}
|
|
100
|
+
*/
|
|
101
|
+
function extractMSK (event, tracer) {
|
|
102
|
+
if (!event?.records) return null
|
|
103
|
+
// Unlike SQS/SNS/Kinesis, MSK selects the first traced record across partitions.
|
|
104
|
+
for (const records of Object.values(event.records)) {
|
|
105
|
+
if (!Array.isArray(records)) continue
|
|
106
|
+
for (const record of records) {
|
|
107
|
+
try {
|
|
108
|
+
if (!Array.isArray(record?.headers)) continue
|
|
109
|
+
let headers = null
|
|
110
|
+
for (const entry of record.headers) {
|
|
111
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) continue
|
|
112
|
+
for (const [name, value] of Object.entries(entry)) {
|
|
113
|
+
if (Array.isArray(value)) {
|
|
114
|
+
headers ??= {}
|
|
115
|
+
headers[name.toLowerCase()] = Buffer.from(value).toString('utf8')
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (!headers) continue
|
|
120
|
+
const context = tracer.extract('text_map', headers)
|
|
121
|
+
if (context) return context
|
|
122
|
+
} catch (error) {
|
|
123
|
+
log.debug('Unable to extract trace context from MSK event: %s', error)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return null
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @param {Record<string, unknown>} event Durable Execution event.
|
|
132
|
+
* @param {Tracer} tracer Core tracer.
|
|
133
|
+
* @returns {SpanContext|null}
|
|
134
|
+
*/
|
|
135
|
+
function extractDurableExecution (event, tracer) {
|
|
136
|
+
if (!event?.DurableExecutionArn) return null
|
|
137
|
+
const operations = event.InitialExecutionState?.Operations
|
|
138
|
+
if (!operations || operations.length === 0) return null
|
|
139
|
+
let best = null
|
|
140
|
+
for (const op of operations) {
|
|
141
|
+
if (typeof op?.Name !== 'string' || !op.Name.startsWith('_datadog_')) continue
|
|
142
|
+
const suffix = op.Name.slice('_datadog_'.length)
|
|
143
|
+
const number = Number.parseInt(suffix, 10)
|
|
144
|
+
if (Number.isNaN(number) || String(number) !== suffix) continue
|
|
145
|
+
if (best === null || number > best.number) best = { number, op }
|
|
146
|
+
}
|
|
147
|
+
if (best === null) return null
|
|
148
|
+
const raw = best.op.Payload ?? best.op.StepDetails?.Result
|
|
149
|
+
if (!raw || typeof raw !== 'string') return null
|
|
150
|
+
let headers
|
|
151
|
+
try {
|
|
152
|
+
headers = JSON.parse(raw)
|
|
153
|
+
} catch (error) {
|
|
154
|
+
log.debug('Failed to parse trace checkpoint payload: %s', error)
|
|
155
|
+
}
|
|
156
|
+
return headers && typeof headers === 'object' ? tracer.extract('text_map', headers) : null
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
module.exports = {
|
|
160
|
+
getSNSHeaders,
|
|
161
|
+
getSNSSQSHeaders,
|
|
162
|
+
getSQSHeaders,
|
|
163
|
+
getEventBridgeSQSHeaders,
|
|
164
|
+
extractKinesis,
|
|
165
|
+
extractMSK,
|
|
166
|
+
extractDurableExecution,
|
|
167
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
// Ported from datadog-lambda-js at da9fc3c0; propagation itself stays core-owned.
|
|
4
|
+
const log = require('../../../dd-trace/src/log')
|
|
5
|
+
const guards = require('../event-type-guards')
|
|
6
|
+
const {
|
|
7
|
+
getSNSHeaders,
|
|
8
|
+
getSNSSQSHeaders,
|
|
9
|
+
getSQSHeaders,
|
|
10
|
+
getEventBridgeSQSHeaders,
|
|
11
|
+
extractKinesis,
|
|
12
|
+
extractMSK,
|
|
13
|
+
extractDurableExecution,
|
|
14
|
+
} = require('./events')
|
|
15
|
+
const { extractHTTP } = require('./http')
|
|
16
|
+
const { fromTraceContext } = require('./span-context')
|
|
17
|
+
const { getStepFunctionContext, extractStepFunctionContext } = require('./step-functions')
|
|
18
|
+
const { extractDatadogContext, extractXrayContext, getAWSTraceHeader } = require('./xray')
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @typedef {object} ExtractedContext
|
|
22
|
+
* @property {import('../../../dd-trace/src/opentracing/span_context')|null} spanContext
|
|
23
|
+
* @property {'event'|'xray'|'ddtrace'|undefined} source
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
class TraceContextExtractor {
|
|
27
|
+
#tracer
|
|
28
|
+
#config
|
|
29
|
+
#stepFunction
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* One extractor per invocation keeps source first-use Step Functions caching invocation-local.
|
|
33
|
+
* @param {import('../../../dd-trace/src/opentracing/tracer')} tracer Core tracer.
|
|
34
|
+
* @param {import('./types').ExtractorConfig} config Lambda configuration.
|
|
35
|
+
*/
|
|
36
|
+
constructor (tracer, config) {
|
|
37
|
+
this.#tracer = tracer
|
|
38
|
+
this.#config = config
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @param {Record<string, unknown>} event Lambda event.
|
|
43
|
+
* @param {Record<string, unknown>} context Lambda context.
|
|
44
|
+
* @returns {ExtractedContext|Promise<ExtractedContext>}
|
|
45
|
+
*/
|
|
46
|
+
extract (event, context) {
|
|
47
|
+
if (this.#config.traceExtractor) return this.#extractCustom(event, context)
|
|
48
|
+
return this.#extractFallback(event, context)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Preserve the shim callback contract: pass the original event (of any shape) and AWS Lambda Context unchanged.
|
|
53
|
+
* Existing typed extractors return a trace context synchronously or through a Promise. It contains decimal
|
|
54
|
+
* traceId/parentId strings, numeric sampleMode (-1, 0, 1, 2) and source ('event', 'xray', 'ddtrace'); legacy
|
|
55
|
+
* traceID/parentID names remain accepted. Preparation awaits extraction before the invocation span starts.
|
|
56
|
+
* Failed extraction resumes the event, Step Functions, Lambda client-context and X-Ray fallback chain.
|
|
57
|
+
*
|
|
58
|
+
* @param {Record<string, unknown>} event Lambda event.
|
|
59
|
+
* @param {Record<string, unknown>} context Lambda context.
|
|
60
|
+
* @returns {Promise<ExtractedContext>}
|
|
61
|
+
*/
|
|
62
|
+
async #extractCustom (event, context) {
|
|
63
|
+
try {
|
|
64
|
+
const custom = await this.#config.traceExtractor(event, context)
|
|
65
|
+
const spanContext = fromTraceContext(custom)
|
|
66
|
+
if (spanContext !== null) return { spanContext, source: custom.source }
|
|
67
|
+
} catch (error) {
|
|
68
|
+
log.error('Unable to extract trace context. Custom extractor function failed: %s', error)
|
|
69
|
+
}
|
|
70
|
+
return this.#extractFallback(event, context)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @param {Record<string, unknown>} event Lambda event.
|
|
75
|
+
* @param {Record<string, unknown>} context Lambda context.
|
|
76
|
+
* @returns {ExtractedContext}
|
|
77
|
+
*/
|
|
78
|
+
#extractFallback (event, context) {
|
|
79
|
+
let spanContext = this.#extractEvent(event)
|
|
80
|
+
if (spanContext === null) spanContext = this.#extractStepFunction(event)
|
|
81
|
+
if (spanContext === null && context && typeof context === 'object') {
|
|
82
|
+
const custom = context.clientContext?.custom
|
|
83
|
+
if (custom && typeof custom === 'object') {
|
|
84
|
+
spanContext = this.#tracer.extract('text_map', custom._datadog === undefined ? custom : custom._datadog)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (spanContext !== null) return { spanContext, source: 'event' }
|
|
88
|
+
return { spanContext: extractXrayContext(), source: 'xray' }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @param {Record<string, unknown>} event Lambda event.
|
|
93
|
+
* @returns {import('../../../dd-trace/src/opentracing/span_context')|null}
|
|
94
|
+
*/
|
|
95
|
+
#extractEvent (event) {
|
|
96
|
+
if (!event || typeof event !== 'object') return null
|
|
97
|
+
if (guards.isDurableExecutionEvent(event)) return extractDurableExecution(event, this.#tracer)
|
|
98
|
+
const headers = event.headers ?? event.multiValueHeaders
|
|
99
|
+
if (headers !== null && typeof headers === 'object') {
|
|
100
|
+
return extractHTTP(event, this.#tracer, this.#config.decodeAuthorizerContext)
|
|
101
|
+
}
|
|
102
|
+
if (guards.isSNSEvent(event)) {
|
|
103
|
+
return this.#extractMessage(getSNSHeaders(event.Records[0]), getAWSTraceHeader())
|
|
104
|
+
}
|
|
105
|
+
if (guards.isSNSSQSEvent(event)) {
|
|
106
|
+
return this.#extractMessage(getSNSSQSHeaders(event.Records[0]), event.Records[0]?.attributes?.AWSTraceHeader)
|
|
107
|
+
}
|
|
108
|
+
if (guards.isEventBridgeSQSEvent(event)) {
|
|
109
|
+
return this.#extractMessage(getEventBridgeSQSHeaders(event.Records[0]))
|
|
110
|
+
}
|
|
111
|
+
if (guards.isAppSyncResolverEvent(event)) {
|
|
112
|
+
event.headers = event.request.headers
|
|
113
|
+
return extractHTTP(event, this.#tracer, false)
|
|
114
|
+
}
|
|
115
|
+
if (guards.isMSKEvent(event)) return extractMSK(event, this.#tracer)
|
|
116
|
+
if (guards.isSQSEvent(event)) {
|
|
117
|
+
return this.#extractMessage(getSQSHeaders(event.Records[0]), event.Records[0]?.attributes?.AWSTraceHeader)
|
|
118
|
+
}
|
|
119
|
+
if (guards.isKinesisStreamEvent(event)) return extractKinesis(event, this.#tracer)
|
|
120
|
+
if (guards.isEventBridgeEvent(event)) return this.#extractMessage(event?.detail?._datadog)
|
|
121
|
+
return null
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Message carriers may contain Step Functions context instead of propagation headers.
|
|
126
|
+
* @param {Record<string, string>|null|undefined} headers Message carrier.
|
|
127
|
+
* @param {string} [awsTraceHeader] Optional Java-injected Datadog context.
|
|
128
|
+
* @returns {import('../../../dd-trace/src/opentracing/span_context')|null}
|
|
129
|
+
*/
|
|
130
|
+
#extractMessage (headers, awsTraceHeader) {
|
|
131
|
+
try {
|
|
132
|
+
if (headers) {
|
|
133
|
+
const spanContext = this.#tracer.extract('text_map', headers) || this.#extractStepFunction(headers)
|
|
134
|
+
if (spanContext) return spanContext
|
|
135
|
+
}
|
|
136
|
+
if (awsTraceHeader !== undefined) return extractDatadogContext(awsTraceHeader)
|
|
137
|
+
} catch (error) {
|
|
138
|
+
log.debug('Unable to extract trace context from message: %s', error)
|
|
139
|
+
}
|
|
140
|
+
return null
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @param {Record<string, unknown>} event Event or message carrier.
|
|
145
|
+
* @returns {import('../../../dd-trace/src/opentracing/span_context')|null}
|
|
146
|
+
*/
|
|
147
|
+
#extractStepFunction (event) {
|
|
148
|
+
this.#stepFunction ??= { context: getStepFunctionContext(event) }
|
|
149
|
+
return extractStepFunctionContext(this.#stepFunction.context)
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
module.exports = TraceContextExtractor
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const log = require('../../../dd-trace/src/log')
|
|
4
|
+
const { isAPIGatewayEventV2, isAPIGatewayEvent } = require('../event-type-guards')
|
|
5
|
+
|
|
6
|
+
const AUTHORIZING_REQUEST_ID_HEADER = 'x-datadog-authorizing-requestid'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Decodes the authorizer's carrier only for an uncached authorization.
|
|
10
|
+
* @param {Record<string, unknown>} event HTTP event.
|
|
11
|
+
* @returns {Record<string, string>|null}
|
|
12
|
+
*/
|
|
13
|
+
function getInjectedAuthorizerHeaders (event) {
|
|
14
|
+
const authorizer = event?.requestContext?.authorizer
|
|
15
|
+
if (!authorizer) return null
|
|
16
|
+
let raw = authorizer._datadog
|
|
17
|
+
// Keep REST classification ahead of HTTP API classification for overlapping shapes.
|
|
18
|
+
if (!isAPIGatewayEvent(event) && isAPIGatewayEventV2(event)) raw = authorizer.lambda._datadog
|
|
19
|
+
if (!raw) return null
|
|
20
|
+
const headers = JSON.parse(Buffer.from(raw, 'base64').toString())
|
|
21
|
+
if (authorizer.integrationLatency > 0 ||
|
|
22
|
+
event.requestContext.requestId === headers[AUTHORIZING_REQUEST_ID_HEADER]) return headers
|
|
23
|
+
return null
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {Record<string, unknown>} event HTTP event.
|
|
28
|
+
* @param {import('../../../dd-trace/src/opentracing/tracer')} tracer Core tracer.
|
|
29
|
+
* @param {boolean} [decodeAuthorizerContext] Decode authorizer metadata.
|
|
30
|
+
* @returns {import('../../../dd-trace/src/opentracing/span_context')|null}
|
|
31
|
+
*/
|
|
32
|
+
function extractHTTP (event, tracer, decodeAuthorizerContext = true) {
|
|
33
|
+
if (decodeAuthorizerContext) {
|
|
34
|
+
try {
|
|
35
|
+
const injected = getInjectedAuthorizerHeaders(event)
|
|
36
|
+
if (injected !== null) return tracer.extract('text_map', injected)
|
|
37
|
+
} catch (error) {
|
|
38
|
+
log.debug('Unable to extract trace context from authorizer event: %s', error)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const headers = event.headers ?? event.multiValueHeaders
|
|
42
|
+
const carrier = {}
|
|
43
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
44
|
+
if (Array.isArray(value)) carrier[key.toLowerCase()] = value[0] ?? ''
|
|
45
|
+
else if (typeof value === 'string') carrier[key.toLowerCase()] = value
|
|
46
|
+
}
|
|
47
|
+
return tracer.extract('text_map', carrier)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
module.exports = { extractHTTP, getInjectedAuthorizerHeaders, AUTHORIZING_REQUEST_ID_HEADER }
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { AUTO_KEEP } = require('../../../../ext/priority')
|
|
4
|
+
const id = require('../../../dd-trace/src/id')
|
|
5
|
+
const SpanContext = require('../../../dd-trace/src/opentracing/span_context')
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Converts the shim's custom context contract to the core tracer's context.
|
|
9
|
+
* Numeric sampling follows datadog-lambda-js #850 (13e2f4ca).
|
|
10
|
+
*
|
|
11
|
+
* @param {import('./types').TraceContext} context Custom extractor result.
|
|
12
|
+
* @returns {SpanContext|null}
|
|
13
|
+
*/
|
|
14
|
+
function fromTraceContext (context) {
|
|
15
|
+
const traceId = context.traceId || context.traceID
|
|
16
|
+
const spanId = context.parentId || context.parentID
|
|
17
|
+
if (traceId === undefined || spanId === undefined) return null
|
|
18
|
+
|
|
19
|
+
return new SpanContext({
|
|
20
|
+
traceId: id(traceId, 10),
|
|
21
|
+
spanId: id(spanId, 10),
|
|
22
|
+
sampling: { priority: context.sampleMode },
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = { fromTraceContext, AUTO_KEEP }
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { createHash } = require('node:crypto')
|
|
4
|
+
|
|
5
|
+
const { readDatadogTraceId, readDatadogTags } = require('../../../dd-trace/src/carrier')
|
|
6
|
+
const id = require('../../../dd-trace/src/id')
|
|
7
|
+
const { fromTraceContext, AUTO_KEEP } = require('./span-context')
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {object} StepFunctionContext
|
|
11
|
+
* @property {string} execution_id
|
|
12
|
+
* @property {string} redrive_count
|
|
13
|
+
* @property {string} retry_count
|
|
14
|
+
* @property {string} state_entered_time
|
|
15
|
+
* @property {string} state_name
|
|
16
|
+
* @property {string} [root_execution_id]
|
|
17
|
+
* @property {string} [serverless_version]
|
|
18
|
+
* @property {string} [trace_id]
|
|
19
|
+
* @property {string} [dd_p_tid]
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** @param {Record<string, unknown>} context Step Functions payload. */
|
|
23
|
+
function isValidContext (context) {
|
|
24
|
+
return typeof context?.Execution?.Id === 'string' &&
|
|
25
|
+
typeof context?.State?.EnteredTime === 'string' && typeof context?.State?.Name === 'string'
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Preserve source envelope handling, including its null/object semantics.
|
|
29
|
+
/* eslint-disable eslint-rules/eslint-safe-typeof-object */
|
|
30
|
+
/**
|
|
31
|
+
* @param {Record<string, unknown>} event Lambda event or message carrier.
|
|
32
|
+
* @returns {StepFunctionContext|undefined}
|
|
33
|
+
*/
|
|
34
|
+
function getStepFunctionContext (event) {
|
|
35
|
+
if (typeof event !== 'object') return
|
|
36
|
+
if (typeof event?.Payload?._datadog === 'object' || isValidContext(event?.Payload)) event = event.Payload
|
|
37
|
+
if (typeof event._datadog === 'object') event = event._datadog
|
|
38
|
+
if (!isValidContext(event)) return
|
|
39
|
+
|
|
40
|
+
const context = {
|
|
41
|
+
execution_id: event.Execution.Id,
|
|
42
|
+
redrive_count: (event.Execution.RedriveCount ?? '0').toString(),
|
|
43
|
+
retry_count: (event.State.RetryCount ?? '0').toString(),
|
|
44
|
+
state_entered_time: event.State.EnteredTime,
|
|
45
|
+
state_name: event.State.Name,
|
|
46
|
+
}
|
|
47
|
+
if (event['serverless-version'] === 'v1') {
|
|
48
|
+
if (typeof event.RootExecutionId === 'string') {
|
|
49
|
+
return { ...context, root_execution_id: event.RootExecutionId, serverless_version: 'v1' }
|
|
50
|
+
}
|
|
51
|
+
const trace = readStepFunctionTraceContext(event)
|
|
52
|
+
if (trace) {
|
|
53
|
+
return {
|
|
54
|
+
...context,
|
|
55
|
+
trace_id: trace.traceId,
|
|
56
|
+
dd_p_tid: parseTraceIdHigh(trace.tags),
|
|
57
|
+
serverless_version: 'v1',
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const headers = event.Execution?.Input?._datadog
|
|
64
|
+
const trace = typeof headers === 'object' && headers !== null && readStepFunctionTraceContext(headers)
|
|
65
|
+
if (trace) {
|
|
66
|
+
return {
|
|
67
|
+
...context,
|
|
68
|
+
trace_id: trace.traceId,
|
|
69
|
+
dd_p_tid: parseTraceIdHigh(trace.tags),
|
|
70
|
+
serverless_version: 'legacy-lambda-root',
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return context
|
|
74
|
+
}
|
|
75
|
+
/* eslint-enable eslint-rules/eslint-safe-typeof-object */
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* These are scalar Step Functions schema fields, not list-valued HTTP propagation headers.
|
|
79
|
+
* @param {Record<string, unknown>} context Step Functions payload.
|
|
80
|
+
* @returns {{ traceId: string, tags: string }|undefined}
|
|
81
|
+
*/
|
|
82
|
+
function readStepFunctionTraceContext (context) {
|
|
83
|
+
const fields = {}
|
|
84
|
+
for (const field of Object.keys(context)) {
|
|
85
|
+
const value = context[field]
|
|
86
|
+
if (typeof value === 'string') fields[field] = value
|
|
87
|
+
}
|
|
88
|
+
const traceId = readDatadogTraceId(fields)
|
|
89
|
+
const tags = readDatadogTags(fields)
|
|
90
|
+
if (typeof traceId === 'string' && typeof tags === 'string') return { traceId, tags }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** @param {string} tags Datadog propagation tags. */
|
|
94
|
+
function parseTraceIdHigh (tags) {
|
|
95
|
+
for (const tag of tags.split(',')) {
|
|
96
|
+
if (tag.includes('_dd.p.tid=')) return tag.split('=')[1]
|
|
97
|
+
}
|
|
98
|
+
return ''
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Uses the same SHA-256 bytes, sign-bit masking and zero fallback as the shim.
|
|
103
|
+
* @param {string} value Value to hash.
|
|
104
|
+
* @param {number} offset Byte offset of the 64-bit identifier.
|
|
105
|
+
*/
|
|
106
|
+
function hashId (value, offset) {
|
|
107
|
+
const hash = createHash('sha256').update(value).digest()
|
|
108
|
+
return ((hash.readBigUInt64BE(offset) & 0x7F_FF_FF_FF_FF_FF_FF_FFn) || 1n).toString(10)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @param {StepFunctionContext|undefined} context Parsed Step Functions context.
|
|
113
|
+
* @returns {import('../../../dd-trace/src/opentracing/span_context')|null}
|
|
114
|
+
*/
|
|
115
|
+
function extractStepFunctionContext (context) {
|
|
116
|
+
if (!context) return null
|
|
117
|
+
let traceId
|
|
118
|
+
let high
|
|
119
|
+
if (typeof context.root_execution_id === 'string' && typeof context.serverless_version === 'string') {
|
|
120
|
+
traceId = hashId(context.root_execution_id, 8)
|
|
121
|
+
high = hashId(context.root_execution_id, 0)
|
|
122
|
+
} else if (typeof context.trace_id === 'string' && typeof context.dd_p_tid === 'string' &&
|
|
123
|
+
typeof context.serverless_version === 'string') {
|
|
124
|
+
traceId = context.trace_id
|
|
125
|
+
high = context.dd_p_tid
|
|
126
|
+
} else if (context.serverless_version === undefined) {
|
|
127
|
+
traceId = hashId(context.execution_id, 8)
|
|
128
|
+
high = hashId(context.execution_id, 0)
|
|
129
|
+
} else {
|
|
130
|
+
return null
|
|
131
|
+
}
|
|
132
|
+
const counts = context.retry_count === '0' && context.redrive_count === '0'
|
|
133
|
+
? ''
|
|
134
|
+
: `#${context.retry_count}#${context.redrive_count}`
|
|
135
|
+
const parentId = hashId(`${context.execution_id}#${context.state_name}#${context.state_entered_time}${counts}`, 0)
|
|
136
|
+
const spanContext = fromTraceContext({ traceId, parentId, sampleMode: AUTO_KEEP, source: 'event' })
|
|
137
|
+
// Preserve the source's radix, including for Lambda-root propagated tags.
|
|
138
|
+
spanContext._trace.tags['_dd.p.tid'] = id(high, 10).toString(16)
|
|
139
|
+
return spanContext
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
module.exports = { getStepFunctionContext, extractStepFunctionContext }
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Private Lambda extraction contract; the shim owns the customer-facing callback types. */
|
|
2
|
+
export interface TraceContext {
|
|
3
|
+
traceId?: string;
|
|
4
|
+
parentId?: string;
|
|
5
|
+
/** @deprecated Use traceId. */
|
|
6
|
+
traceID?: string;
|
|
7
|
+
/** @deprecated Use parentId. */
|
|
8
|
+
parentID?: string;
|
|
9
|
+
sampleMode: -1 | 0 | 1 | 2;
|
|
10
|
+
source: 'event' | 'xray' | 'ddtrace';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Event and context types come from the caller, without requiring AWS types in the tracer. */
|
|
14
|
+
export interface ExtractorConfig<TEvent = unknown, TContext = unknown> {
|
|
15
|
+
traceExtractor?: (event: TEvent, context: TContext) => TraceContext | Promise<TraceContext>;
|
|
16
|
+
decodeAuthorizerContext?: boolean;
|
|
17
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
// Context primitives ported from datadog-lambda-js at da9fc3c0.
|
|
4
|
+
const { getEnvironmentVariable } = require('../../../dd-trace/src/config/helper')
|
|
5
|
+
const { fromTraceContext } = require('./span-context')
|
|
6
|
+
|
|
7
|
+
/** Reads invocation-specific X-Ray context on every call, without caching tracer configuration. */
|
|
8
|
+
function getAWSTraceHeader () {
|
|
9
|
+
// eslint-disable-next-line eslint-rules/eslint-non-prefix-env-names -- Per-invocation input; not cached config.
|
|
10
|
+
return getEnvironmentVariable('_X_AMZN_TRACE_ID')
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {string} header AWS trace header.
|
|
15
|
+
* @returns {{ traceId: string, parentId: string, sampled: string }|undefined}
|
|
16
|
+
*/
|
|
17
|
+
function parseAWSTraceHeader (header) {
|
|
18
|
+
const [root, parent, sampling] = header.split(';')
|
|
19
|
+
if (parent === undefined || sampling === undefined) return
|
|
20
|
+
const [, traceId] = root.split('=')
|
|
21
|
+
const [, parentId] = parent.split('=')
|
|
22
|
+
const [, sampled] = sampling.split('=')
|
|
23
|
+
if (traceId === undefined || parentId === undefined || sampled === undefined) return
|
|
24
|
+
return { traceId, parentId, sampled }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Extracts the Datadog context embedded by upstream Java instrumentation.
|
|
29
|
+
* This remains an event parent, independently of the X-Ray merge setting.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} header AWS trace header.
|
|
32
|
+
* @returns {import('../../../dd-trace/src/opentracing/span_context')|null}
|
|
33
|
+
*/
|
|
34
|
+
function extractDatadogContext (header) {
|
|
35
|
+
const context = parseAWSTraceHeader(header)
|
|
36
|
+
if (!context) return null
|
|
37
|
+
const parts = context.traceId.split('-')
|
|
38
|
+
if (parts.length <= 2 || !parts[2].startsWith('00000000')) return null
|
|
39
|
+
return fromTraceContext({
|
|
40
|
+
traceId: BigInt('0x' + parts[2].slice(8)).toString(10),
|
|
41
|
+
parentId: BigInt('0x' + context.parentId).toString(10),
|
|
42
|
+
sampleMode: parseInt(context.sampled, 10),
|
|
43
|
+
source: 'event',
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** @returns {import('../../../dd-trace/src/opentracing/span_context')|null} */
|
|
48
|
+
function extractXrayContext () {
|
|
49
|
+
const header = getAWSTraceHeader()
|
|
50
|
+
if (header === undefined) return null
|
|
51
|
+
const context = parseAWSTraceHeader(header)
|
|
52
|
+
if (!context || context.parentId.length !== 16) return null
|
|
53
|
+
const parts = context.traceId.split('-')
|
|
54
|
+
if (parts.length < 3 || parts[2].length !== 24) return null
|
|
55
|
+
try {
|
|
56
|
+
return fromTraceContext({
|
|
57
|
+
traceId: (BigInt('0x' + parts[2]) % 0x80_00_00_00_00_00_00_00n).toString(10),
|
|
58
|
+
parentId: BigInt('0x' + context.parentId).toString(10),
|
|
59
|
+
sampleMode: parseInt(context.sampled, 10) === 1 ? 2 : -1,
|
|
60
|
+
source: 'xray',
|
|
61
|
+
})
|
|
62
|
+
} catch {
|
|
63
|
+
return null
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
module.exports = { parseAWSTraceHeader, extractDatadogContext, extractXrayContext, getAWSTraceHeader }
|