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
|
@@ -4,11 +4,59 @@ const dc = require('dc-polyfill')
|
|
|
4
4
|
|
|
5
5
|
const { getEnvironmentVariable } = require('../../dd-trace/src/config/helper')
|
|
6
6
|
const TracingPlugin = require('../../dd-trace/src/plugins/tracing')
|
|
7
|
+
const log = require('../../dd-trace/src/log')
|
|
8
|
+
const TraceContextExtractor = require('./context/extractor')
|
|
7
9
|
|
|
8
10
|
const invocationStartChannel = dc.channel('datadog:aws-lambda:invocation:start')
|
|
9
11
|
const invocationEndChannel = dc.channel('datadog:aws-lambda:invocation:end')
|
|
10
12
|
|
|
11
13
|
class AwsLambdaPlugin extends TracingPlugin {
|
|
14
|
+
/** @param {...object} args Plugin constructor arguments. */
|
|
15
|
+
constructor (...args) {
|
|
16
|
+
super(...args)
|
|
17
|
+
this.addSub('datadog:aws-lambda:invocation:prepare', this.prepare)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Subscribers publish synchronously; the instrumentation awaits this explicit preparation promise
|
|
22
|
+
* before publishing span start. No invocation span is created during extraction.
|
|
23
|
+
* @param {object} context Invocation channel context.
|
|
24
|
+
*/
|
|
25
|
+
prepare (context) {
|
|
26
|
+
const config = {
|
|
27
|
+
traceExtractor: context.config?.traceExtractor ?? this.config.traceExtractor,
|
|
28
|
+
decodeAuthorizerContext: context.config?.decodeAuthorizerContext ?? this.config.decodeAuthorizerContext,
|
|
29
|
+
}
|
|
30
|
+
const extractor = new TraceContextExtractor(this.tracer, config)
|
|
31
|
+
const apply = extracted => {
|
|
32
|
+
const merge = context.config?.mergeXrayTraces ??
|
|
33
|
+
context.config?.mergeDatadogXrayTraces ?? this.config.mergeXrayTraces
|
|
34
|
+
if (extracted.source === 'event' || merge) {
|
|
35
|
+
context.parent = extracted.spanContext
|
|
36
|
+
context.parentSource = extracted.source
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// Match the shim's stale-span cleanup before detaching the extraction scope.
|
|
40
|
+
try {
|
|
41
|
+
const activeSpan = this.tracer.scope().active()
|
|
42
|
+
if (activeSpan && typeof activeSpan.finish === 'function') activeSpan.finish()
|
|
43
|
+
} catch (error) {
|
|
44
|
+
log.debug('Unable to finish stale Lambda span: %s', error)
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
const extracted = this.tracer.scope().activate(null, () => extractor.extract(context.event, context.context))
|
|
48
|
+
if (config.traceExtractor) {
|
|
49
|
+
context.preparation = extracted.then(apply).catch(error => {
|
|
50
|
+
log.error('Unable to prepare Lambda trace context: %s', error)
|
|
51
|
+
})
|
|
52
|
+
} else {
|
|
53
|
+
apply(extracted)
|
|
54
|
+
}
|
|
55
|
+
} catch (error) {
|
|
56
|
+
log.error('Unable to prepare Lambda trace context: %s', error)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
12
60
|
/**
|
|
13
61
|
* Starts the invocation span and publishes the invocation start boundary.
|
|
14
62
|
*
|
|
@@ -22,11 +70,11 @@ class AwsLambdaPlugin extends TracingPlugin {
|
|
|
22
70
|
bindStart (context) {
|
|
23
71
|
const functionName = context.context?.functionName ||
|
|
24
72
|
getEnvironmentVariable('AWS_LAMBDA_FUNCTION_NAME')
|
|
25
|
-
// TODO(migration PR 8): the extracted upstream context becomes the parent. Until the context
|
|
26
|
-
// extractors land, pinning `childOf` keeps the span from adopting a stale active span left
|
|
27
|
-
// behind by a previous invocation in the same warm container.
|
|
28
73
|
const span = this.startSpan('aws.lambda', {
|
|
29
|
-
childOf: null,
|
|
74
|
+
childOf: context.parent || null,
|
|
75
|
+
meta: context.parent && (context.parentSource === 'event' || context.parentSource === 'xray')
|
|
76
|
+
? { '_dd.parent_source': context.parentSource }
|
|
77
|
+
: undefined,
|
|
30
78
|
kind: 'server',
|
|
31
79
|
resource: functionName,
|
|
32
80
|
service: resolveServiceName(functionName, this.config, context.config),
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
const fs = require('node:fs')
|
|
4
|
+
const path = require('node:path')
|
|
5
|
+
|
|
6
|
+
const { getEnvironmentVariable } = require('../../dd-trace/src/config/helper')
|
|
7
|
+
const log = require('../../dd-trace/src/log')
|
|
3
8
|
const TracingPlugin = require('../../dd-trace/src/plugins/tracing')
|
|
4
9
|
const web = require('../../dd-trace/src/plugins/util/web')
|
|
5
10
|
const { getIsAzureContainerApp } = require('../../dd-trace/src/serverless')
|
|
@@ -24,6 +29,9 @@ class AzureFunctionsPlugin extends TracingPlugin {
|
|
|
24
29
|
static type = 'serverless'
|
|
25
30
|
static prefix = 'tracing:datadog:azure:functions:invoke'
|
|
26
31
|
|
|
32
|
+
/** @type {string | undefined} */
|
|
33
|
+
#routePrefix
|
|
34
|
+
|
|
27
35
|
bindStart (ctx) {
|
|
28
36
|
const meta = getMetaForTrigger(ctx)
|
|
29
37
|
const triggerType = triggerMap[ctx.methodName]
|
|
@@ -34,7 +42,8 @@ class AzureFunctionsPlugin extends TracingPlugin {
|
|
|
34
42
|
|
|
35
43
|
if (isHttpTrigger) {
|
|
36
44
|
const { httpRequest } = ctx
|
|
37
|
-
const
|
|
45
|
+
const url = new URL(httpRequest.url)
|
|
46
|
+
const path = url.pathname
|
|
38
47
|
const req = {
|
|
39
48
|
method: httpRequest.method,
|
|
40
49
|
headers: Object.fromEntries(httpRequest.headers),
|
|
@@ -42,21 +51,40 @@ class AzureFunctionsPlugin extends TracingPlugin {
|
|
|
42
51
|
}
|
|
43
52
|
// Patch the request to create web context
|
|
44
53
|
const webContext = web.patch(req)
|
|
45
|
-
|
|
46
|
-
webContext.tracer = this.tracer
|
|
47
|
-
webContext.paths = [path]
|
|
54
|
+
const operationName = this.operationName()
|
|
48
55
|
// Creates a standard span and an inferred proxy span if headers are present
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
+
if (this.config.DD_TRACE_OTEL_SEMANTICS_ENABLED) {
|
|
57
|
+
req.headers.host = url.host
|
|
58
|
+
req.url = path + getTraceQuery(url)
|
|
59
|
+
req.socket = { encrypted: url.protocol === 'https:' }
|
|
60
|
+
span = web.startSpan(
|
|
61
|
+
this.tracer,
|
|
62
|
+
this.config,
|
|
63
|
+
req,
|
|
64
|
+
undefined,
|
|
65
|
+
operationName,
|
|
66
|
+
ctx
|
|
67
|
+
)
|
|
68
|
+
const route = ctx.invocationContext.options.trigger.route?.trim() || ctx.functionName
|
|
69
|
+
const prefix = this.#routePrefix ??= getRoutePrefix()
|
|
70
|
+
web.setRoute(req, prefix + (route.startsWith('/') ? route : `/${route}`))
|
|
71
|
+
} else {
|
|
72
|
+
webContext.config = this.config
|
|
73
|
+
webContext.tracer = this.tracer
|
|
74
|
+
webContext.paths = [path]
|
|
75
|
+
span = web.startServerlessSpanWithInferredProxy(
|
|
76
|
+
this.tracer,
|
|
77
|
+
this.config,
|
|
78
|
+
operationName,
|
|
79
|
+
req,
|
|
80
|
+
ctx
|
|
81
|
+
)
|
|
82
|
+
webContext.span = span
|
|
83
|
+
}
|
|
84
|
+
|
|
56
85
|
span._integrationName = 'azure-functions'
|
|
57
86
|
span.context().setTag('component', 'azure-functions')
|
|
58
87
|
span.addTags(meta)
|
|
59
|
-
webContext.span = span
|
|
60
88
|
webContext.azureFunctionCtx = ctx
|
|
61
89
|
ctx.webContext = webContext
|
|
62
90
|
} else {
|
|
@@ -101,6 +129,34 @@ class AzureFunctionsPlugin extends TracingPlugin {
|
|
|
101
129
|
}
|
|
102
130
|
}
|
|
103
131
|
|
|
132
|
+
function getRoutePrefix () {
|
|
133
|
+
let prefix = getEnvironmentVariable('AzureFunctionsJobHost__extensions__http__routePrefix')
|
|
134
|
+
if (prefix === undefined) {
|
|
135
|
+
try {
|
|
136
|
+
const root = getEnvironmentVariable('AzureWebJobsScriptRoot') || process.cwd()
|
|
137
|
+
const contents = fs.readFileSync(path.join(root, 'host.json'), 'utf8')
|
|
138
|
+
prefix = JSON.parse(contents.replace(/^\uFEFF/, '')).extensions?.http?.routePrefix ?? 'api'
|
|
139
|
+
} catch (error) {
|
|
140
|
+
log.debug('Cannot determine Azure Functions route prefix: %s', error.code || error.name)
|
|
141
|
+
return ''
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return typeof prefix === 'string' && prefix ? `/${prefix}` : ''
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param {URL} url
|
|
149
|
+
*/
|
|
150
|
+
function getTraceQuery (url) {
|
|
151
|
+
const search = url.search
|
|
152
|
+
if (search) {
|
|
153
|
+
for (const name of url.searchParams.keys()) {
|
|
154
|
+
if (name.toLowerCase() === 'code') return ''
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return search
|
|
158
|
+
}
|
|
159
|
+
|
|
104
160
|
function getMetaForTrigger ({ functionName, methodName, invocationContext }) {
|
|
105
161
|
const prefix = getIsAzureContainerApp() ? 'aca' : 'aas'
|
|
106
162
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const HttpClientPlugin = require('../../datadog-plugin-http/src/client')
|
|
4
|
+
const { dsmBase64Name, readDsmBase64 } = require('../../dd-trace/src/carrier')
|
|
4
5
|
|
|
5
6
|
class FetchPlugin extends HttpClientPlugin {
|
|
6
7
|
static id = 'fetch'
|
|
@@ -26,6 +27,12 @@ class FetchPlugin extends HttpClientPlugin {
|
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
29
|
|
|
30
|
+
// A pathway header the caller forwards describes an upstream hop, so this request's own one replaces it.
|
|
31
|
+
const pathway = readDsmBase64(options.headers)
|
|
32
|
+
if (pathway !== undefined) {
|
|
33
|
+
req.headers.set(dsmBase64Name, pathway)
|
|
34
|
+
}
|
|
35
|
+
|
|
29
36
|
return store
|
|
30
37
|
}
|
|
31
38
|
|
|
@@ -16,6 +16,7 @@ const {
|
|
|
16
16
|
const { getClientStatusValidator } = require('../../dd-trace/src/plugins/util/status-validator')
|
|
17
17
|
const { buildClientHttpUrl, getQsObfuscator } = require('../../dd-trace/src/plugins/util/url')
|
|
18
18
|
const { stripQueryAndFragment } = require('../../dd-trace/src/util')
|
|
19
|
+
const { isAwsSdkRequest, setHttpClientCheckpoint } = require('../../dd-trace/src/datastreams')
|
|
19
20
|
const { CLIENT_PORT_KEY, COMPONENT, ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')
|
|
20
21
|
|
|
21
22
|
const HTTP_STATUS_CODE = tags.HTTP_STATUS_CODE
|
|
@@ -85,6 +86,7 @@ class HttpClientPlugin extends ClientPlugin {
|
|
|
85
86
|
span._spanContext._trace.record = false
|
|
86
87
|
}
|
|
87
88
|
|
|
89
|
+
let dataStreamsContext
|
|
88
90
|
if (this.shouldInjectTraceHeaders(options, uri)) {
|
|
89
91
|
// Clone the headers object in case an upstream lib has a reference to the original headers
|
|
90
92
|
// Implemented due to aws-sdk issue where request signing is broken if we mutate the headers
|
|
@@ -92,11 +94,21 @@ class HttpClientPlugin extends ClientPlugin {
|
|
|
92
94
|
// https://github.com/open-telemetry/opentelemetry-js-contrib/issues/1609#issuecomment-1826167348
|
|
93
95
|
options.headers = { ...options.headers }
|
|
94
96
|
this.tracer.inject(span, HTTP_HEADERS, options.headers)
|
|
97
|
+
|
|
98
|
+
if (
|
|
99
|
+
this.config.dsmEnabled &&
|
|
100
|
+
this.config.DD_DATA_STREAMS_HTTP_ENABLED &&
|
|
101
|
+
allowed &&
|
|
102
|
+
!isAwsSdkRequest(store?.span, options.headers['amz-sdk-invocation-id'])
|
|
103
|
+
) {
|
|
104
|
+
dataStreamsContext = setHttpClientCheckpoint(this.tracer, span, store?.dataStreamsContext, options.headers)
|
|
105
|
+
}
|
|
95
106
|
}
|
|
96
107
|
|
|
97
108
|
message.span = span
|
|
98
109
|
message.parentStore = store
|
|
99
110
|
message.currentStore = { ...store, span }
|
|
111
|
+
if (dataStreamsContext) message.currentStore.dataStreamsContext = dataStreamsContext
|
|
100
112
|
|
|
101
113
|
return message.currentStore
|
|
102
114
|
}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const ServerPlugin = require('../../dd-trace/src/plugins/server')
|
|
4
4
|
const { storage } = require('../../datadog-core')
|
|
5
5
|
const { withRequest } = require('../../dd-trace/src/appsec/store')
|
|
6
|
+
const { setHttpServerCheckpoint } = require('../../dd-trace/src/datastreams')
|
|
6
7
|
const web = require('../../dd-trace/src/plugins/util/web')
|
|
7
8
|
const { incomingHttpRequestStart, incomingHttpRequestEnd } = require('../../dd-trace/src/appsec/channels')
|
|
8
9
|
const { COMPONENT, SVC_SRC_KEY } = require('../../dd-trace/src/constants')
|
|
@@ -62,10 +63,17 @@ class HttpServerPlugin extends ServerPlugin {
|
|
|
62
63
|
store = withRequest(store, req)
|
|
63
64
|
}
|
|
64
65
|
|
|
66
|
+
// A reused context (HTTP/2 stream, serverless re-entry) is not a new inbound hop.
|
|
67
|
+
const dsmCheckpoint = spanCreated && this.config.dsmEnabled && this.config.DD_DATA_STREAMS_HTTP_ENABLED
|
|
68
|
+
if (dsmCheckpoint) {
|
|
69
|
+
const dataStreamsContext = setHttpServerCheckpoint(this.tracer, span, req.headers)
|
|
70
|
+
store = { ...store, dataStreamsContext }
|
|
71
|
+
}
|
|
72
|
+
|
|
65
73
|
// Skip the re-enter when web.startSpan already entered { ...store, span }
|
|
66
|
-
// and AppSec
|
|
74
|
+
// and neither AppSec nor DSM rebuilt the store: the bind would be
|
|
67
75
|
// identical to the one startSpan just made.
|
|
68
|
-
if (!spanCreated || appsecActive) {
|
|
76
|
+
if (!spanCreated || appsecActive || dsmCheckpoint) {
|
|
69
77
|
this.enter(span, store)
|
|
70
78
|
}
|
|
71
79
|
|
|
@@ -102,8 +110,14 @@ class HttpServerPlugin extends ServerPlugin {
|
|
|
102
110
|
const context = web.getContext(req)
|
|
103
111
|
const parentStore = context?.parentStore
|
|
104
112
|
|
|
105
|
-
|
|
106
|
-
|
|
113
|
+
if (parentStore) {
|
|
114
|
+
this.enter(parentStore.span, parentStore)
|
|
115
|
+
} else {
|
|
116
|
+
// Without AsyncContextFrame this store stays on the connection's async resource, where the request's body
|
|
117
|
+
// callbacks and the next request of a keep-alive connection run. They keep the request's other fields, such as
|
|
118
|
+
// AppSec's request, but must not inherit its data streams context.
|
|
119
|
+
legacyStorage.enterWith({ ...legacyStorage.getStore(), span: undefined, dataStreamsContext: undefined })
|
|
120
|
+
}
|
|
107
121
|
|
|
108
122
|
if (context) {
|
|
109
123
|
context.parentStore = undefined
|
|
@@ -88,17 +88,15 @@ class AIGuardClient {
|
|
|
88
88
|
*
|
|
89
89
|
* @param {import('../../../../index').aiguard.Message[]} messages
|
|
90
90
|
* @param {{ service: string, env: string }} meta
|
|
91
|
+
* @param {import('../../../../index').aiguard.McpServer[]} [mcpServers] Servers that the MCP tool calls reference.
|
|
91
92
|
* @returns {Promise<NonNullable<ReturnType<typeof parseEvaluationResponse>>>}
|
|
92
93
|
*/
|
|
93
|
-
evaluate (messages, meta) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
},
|
|
100
|
-
},
|
|
101
|
-
}
|
|
94
|
+
evaluate (messages, meta, mcpServers) {
|
|
95
|
+
// A request without MCP tool calls keeps the exact payload of earlier versions.
|
|
96
|
+
const attributes = mcpServers?.length
|
|
97
|
+
? { messages, meta, mcp_servers: mcpServers }
|
|
98
|
+
: { messages, meta }
|
|
99
|
+
const payload = { data: { attributes } }
|
|
102
100
|
return executeRequest(
|
|
103
101
|
payload,
|
|
104
102
|
{ url: this.#evaluateUrl, headers: this.#headers, timeout: this.#timeout }
|
|
@@ -70,6 +70,7 @@ const ALLOW = 'ALLOW'
|
|
|
70
70
|
/**
|
|
71
71
|
* @typedef {object} EvaluationMetaStruct
|
|
72
72
|
* @property {Message[]} [messages]
|
|
73
|
+
* @property {import('../../../../index').aiguard.McpServer[]} [mcp_servers]
|
|
73
74
|
* @property {Array<unknown>} [attack_categories]
|
|
74
75
|
* @property {Array<unknown>} [sds]
|
|
75
76
|
* @property {Record<string, unknown>} [tag_probs]
|
|
@@ -312,7 +313,7 @@ function isReservedExtraTag (key) {
|
|
|
312
313
|
return key.startsWith('_dd') || RESERVED_EXTRA_TAGS.has(key)
|
|
313
314
|
}
|
|
314
315
|
|
|
315
|
-
const RESERVED_EXTRA_META_STRUCT_KEYS = new Set(['messages', 'attack_categories', 'sds', 'tag_probs'])
|
|
316
|
+
const RESERVED_EXTRA_META_STRUCT_KEYS = new Set(['messages', 'mcp_servers', 'attack_categories', 'sds', 'tag_probs'])
|
|
316
317
|
|
|
317
318
|
/**
|
|
318
319
|
* Copies backend-provided extra_tags to the span and extra_metastruct to the AI Guard meta-struct,
|
|
@@ -368,7 +369,8 @@ class EvaluationReporter {
|
|
|
368
369
|
*
|
|
369
370
|
* @param {Span} span
|
|
370
371
|
* @param {Message[]} messages
|
|
371
|
-
* @param {{ source: string, integration: string, tags?: CustomTags
|
|
372
|
+
* @param {{ source: string, integration: string, tags?: CustomTags,
|
|
373
|
+
* mcpServers?: import('../../../../index').aiguard.McpServer[] }} options
|
|
372
374
|
* @returns {EvaluationReport}
|
|
373
375
|
*/
|
|
374
376
|
start (span, messages, options) {
|
|
@@ -384,7 +386,10 @@ class EvaluationReporter {
|
|
|
384
386
|
}
|
|
385
387
|
}
|
|
386
388
|
|
|
389
|
+
/** @type {EvaluationMetaStruct} */
|
|
387
390
|
const metaStruct = {}
|
|
391
|
+
// A copy of the list that the request sends, so a failed evaluation reports it too.
|
|
392
|
+
if (options.mcpServers?.length) metaStruct.mcp_servers = clone(options.mcpServers)
|
|
388
393
|
span.meta_struct = {
|
|
389
394
|
[TAGS.META_STRUCT_KEY]: metaStruct,
|
|
390
395
|
}
|
|
@@ -15,7 +15,7 @@ function stringifyIfNeeded (value) {
|
|
|
15
15
|
try {
|
|
16
16
|
return JSON.stringify(value)
|
|
17
17
|
} catch (error) {
|
|
18
|
-
log.debug('AIGuard: dropping an unserializable message field: %s', error
|
|
18
|
+
log.debug('AIGuard: dropping an unserializable message field: %s', error?.message)
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
@@ -40,7 +40,7 @@ function decode (attempt, fallback, logMessage) {
|
|
|
40
40
|
try {
|
|
41
41
|
return attempt()
|
|
42
42
|
} catch (error) {
|
|
43
|
-
log.error(logMessage, error
|
|
43
|
+
log.error(logMessage, error?.message)
|
|
44
44
|
return fallback
|
|
45
45
|
}
|
|
46
46
|
}
|
|
@@ -34,15 +34,20 @@ class AIGuard extends NoopAIGuard {
|
|
|
34
34
|
integration = TAGS.INTEGRATION_NONE,
|
|
35
35
|
childOf,
|
|
36
36
|
tags: customTags,
|
|
37
|
+
mcpServers: requestedMcpServers,
|
|
37
38
|
} = opts ?? {}
|
|
38
39
|
// Only pass `childOf` when truthy so `tracer.trace`'s default (`scope().active()`)
|
|
39
40
|
// still applies for SDK callers that don't supply an explicit parent.
|
|
40
41
|
const traceOpts = childOf ? { childOf } : {}
|
|
42
|
+
// Only a non-empty list is sent; its entries are sent as given, like the messages.
|
|
43
|
+
const mcpServers = Array.isArray(requestedMcpServers) && requestedMcpServers.length > 0
|
|
44
|
+
? requestedMcpServers
|
|
45
|
+
: undefined
|
|
41
46
|
return this.#tracer.trace(TAGS.RESOURCE, traceOpts, async (span) => {
|
|
42
|
-
const report = this.#reporter.start(span, messages, { source, integration, tags: customTags })
|
|
47
|
+
const report = this.#reporter.start(span, messages, { source, integration, tags: customTags, mcpServers })
|
|
43
48
|
let evaluation
|
|
44
49
|
try {
|
|
45
|
-
evaluation = await this.#client.evaluate(report.messages, this.#meta)
|
|
50
|
+
evaluation = await this.#client.evaluate(report.messages, this.#meta, mcpServers)
|
|
46
51
|
} catch (error) {
|
|
47
52
|
this.#reporter.fail(report, error.telemetryType ?? TAGS.ERROR_TYPE_CLIENT)
|
|
48
53
|
throw error
|