Skip to content

Commit 301c926

Browse files
chore: add the tags returned by the service to the ai_guard span
1 parent 5db793a commit 301c926

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

dd-java-agent/agent-aiguard/src/main/java/com/datadog/aiguard/AIGuardInternal.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,17 @@ public Evaluation evaluate(final List<Message> messages, final Options options)
224224
}
225225
final Action action = Action.valueOf(actionStr);
226226
final String reason = (String) result.get("reason");
227+
@SuppressWarnings("unchecked")
228+
final List<String> tags = (List<String>) result.get("tags");
227229
span.setTag(ACTION_TAG, action);
228-
span.setTag(REASON_TAG, reason);
230+
if (reason != null) {
231+
span.setTag(REASON_TAG, reason);
232+
}
233+
if (tags != null) {
234+
for (final String tag : tags) {
235+
span.setTag("ai_guard.tag." + tag, true);
236+
}
237+
}
229238
final boolean shouldBlock =
230239
isBlockingEnabled(options, result.get("is_blocking_enabled")) && action != Action.ALLOW;
231240
WafMetricCollector.get().aiGuardRequest(action, shouldBlock);

dd-java-agent/agent-aiguard/src/test/groovy/com/datadog/aiguard/AIGuardInternalTests.groovy

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class AIGuardInternalTests extends DDSpecification {
163163
return mockResponse(
164164
request,
165165
200,
166-
[data: [attributes: [action: suite.action, reason: suite.reason, is_blocking_enabled: suite.blocking]]]
166+
[data: [attributes: [action: suite.action, reason: suite.reason, tags: suite.tags ?: [], is_blocking_enabled: suite.blocking]]]
167167
)
168168
}
169169
}
@@ -193,6 +193,11 @@ class AIGuardInternalTests extends DDSpecification {
193193
if (throwAbortError) {
194194
1 * span.addThrowable(_ as AIGuard.AIGuardAbortError)
195195
}
196+
if (suite.tags) {
197+
suite.tags.each {
198+
1 * span.setTag("ai_guard.tag.${it}", true)
199+
}
200+
}
196201

197202
assertRequest(request, suite.messages)
198203
if (throwAbortError) {
@@ -497,22 +502,28 @@ class AIGuardInternalTests extends DDSpecification {
497502
private static class TestSuite {
498503
private final AIGuard.Action action
499504
private final String reason
505+
private final List<String> tags
500506
private final boolean blocking
501507
private final String description
502508
private final String target
503509
private final List<AIGuard.Message> messages
504510

505-
TestSuite(AIGuard.Action action, String reason, boolean blocking, String description, String target, List<AIGuard.Message> messages) {
511+
TestSuite(AIGuard.Action action, String reason, List<String> tags, boolean blocking, String description, String target, List<AIGuard.Message> messages) {
506512
this.action = action
507513
this.reason = reason
514+
this.tags = tags
508515
this.blocking = blocking
509516
this.description = description
510517
this.target = target
511518
this.messages = messages
512519
}
513520

514521
static List<TestSuite> build() {
515-
def actionValues = [[ALLOW, 'Go ahead'], [DENY, 'Nope'], [ABORT, 'Kill it with fire']]
522+
def actionValues = [
523+
[ALLOW, 'Go ahead', []],
524+
[DENY, 'Nope', ['deny_everything', 'test_deny']],
525+
[ABORT, 'Kill it with fire', ['alarm_tag', 'abort_everything']]
526+
]
516527
def blockingValues = [true, false]
517528
def suiteValues = [
518529
['tool call', 'tool', TOOL_CALL],
@@ -521,7 +532,7 @@ class AIGuardInternalTests extends DDSpecification {
521532
]
522533
return combinations([actionValues, blockingValues, suiteValues] as Iterable)
523534
.collect { action, blocking, suite ->
524-
new TestSuite(action[0], action[1], blocking, suite[0], suite[1], suite[2])
535+
new TestSuite(action[0], action[1], action[2], blocking, suite[0], suite[1], suite[2])
525536
}
526537
}
527538

0 commit comments

Comments
 (0)