Skip to content

Commit fb3452b

Browse files
chore: add the tags returned by the service to the ai_guard span (#6892)
1 parent 4955d6d commit fb3452b

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

packages/dd-trace/src/aiguard/sdk.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,11 @@ class AIGuard extends NoopAIGuard {
138138
span.setTag(AI_GUARD_TOOL_NAME_TAG_KEY, name)
139139
}
140140
}
141+
const metaStruct = {
142+
messages: this.#truncate(messages)
143+
}
141144
span.meta_struct = {
142-
[AI_GUARD_META_STRUCT_KEY]: {
143-
messages: this.#truncate(messages)
144-
}
145+
[AI_GUARD_META_STRUCT_KEY]: metaStruct
145146
}
146147
let response
147148
try {
@@ -166,14 +167,15 @@ class AIGuard extends NoopAIGuard {
166167
`AI Guard service call failed, status ${response.status}`,
167168
{ errors: response.body?.errors })
168169
}
169-
let action, reason, blockingEnabled
170+
let action, reason, tags, blockingEnabled
170171
try {
171172
const attr = response.body.data.attributes
172173
if (!attr.action) {
173174
throw new Error('Action missing from response')
174175
}
175176
action = attr.action
176177
reason = attr.reason
178+
tags = attr.tags
177179
blockingEnabled = attr.is_blocking_enabled ?? false
178180
} catch (e) {
179181
appsecMetrics.count(AI_GUARD_TELEMETRY_REQUESTS, { error: true }).inc(1)
@@ -182,7 +184,12 @@ class AIGuard extends NoopAIGuard {
182184
const shouldBlock = block && blockingEnabled && action !== ALLOW
183185
appsecMetrics.count(AI_GUARD_TELEMETRY_REQUESTS, { action, error: false, block: shouldBlock }).inc(1)
184186
span.setTag(AI_GUARD_ACTION_TAG_KEY, action)
185-
span.setTag(AI_GUARD_REASON_TAG_KEY, reason)
187+
if (reason) {
188+
span.setTag(AI_GUARD_REASON_TAG_KEY, reason)
189+
}
190+
if (tags?.length > 0) {
191+
metaStruct.attack_categories = tags
192+
}
186193
if (shouldBlock) {
187194
span.setTag(AI_GUARD_BLOCKED_TAG_KEY, 'true')
188195
throw new AIGuardAbortError(reason)

packages/dd-trace/test/aiguard/index.spec.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ describe('AIGuard SDK', () => {
138138
}
139139

140140
const testSuite = [
141-
{ action: 'ALLOW', reason: 'Go ahead' },
142-
{ action: 'DENY', reason: 'Nope' },
143-
{ action: 'ABORT', reason: 'Kill it with fire' }
141+
{ action: 'ALLOW', reason: 'Go ahead', tags: [] },
142+
{ action: 'DENY', reason: 'Nope', tags: ['deny_everything', 'test_deny'] },
143+
{ action: 'ABORT', reason: 'Kill it with fire', tags: ['alarm_tag', 'abort_everything'] }
144144
].flatMap(r => [
145145
{ ...r, blocking: true },
146146
{ ...r, blocking: false },
@@ -150,9 +150,9 @@ describe('AIGuard SDK', () => {
150150
{ ...r, suite: 'prompt', target: 'prompt', messages: prompt }
151151
])
152152

153-
for (const { action, reason, blocking, suite, target, messages } of testSuite) {
153+
for (const { action, reason, tags, blocking, suite, target, messages } of testSuite) {
154154
it(`test evaluate '${suite}' with ${action} action (blocking: ${blocking})`, async () => {
155-
mockFetch({ body: { data: { attributes: { action, reason, is_blocking_enabled: blocking } } } })
155+
mockFetch({ body: { data: { attributes: { action, reason, tags, is_blocking_enabled: blocking } } } })
156156
const shouldBlock = action !== 'ALLOW' && blocking
157157

158158
if (shouldBlock) {
@@ -175,7 +175,10 @@ describe('AIGuard SDK', () => {
175175
...(target === 'tool' ? { 'ai_guard.tool_name': 'calc' } : {}),
176176
...(shouldBlock ? { 'ai_guard.blocked': 'true', 'error.type': 'AIGuardAbortError' } : {})
177177
},
178-
{ messages })
178+
{
179+
messages,
180+
...(tags.length > 0 ? { attack_categories: tags } : {})
181+
})
179182
})
180183
}
181184

0 commit comments

Comments
 (0)