Skip to content

Commit a6d1707

Browse files
Move matching tags to meta struct
1 parent e100df7 commit a6d1707

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static datadog.trace.api.telemetry.WafMetricCollector.AIGuardTruncationType.CONTENT;
55
import static datadog.trace.api.telemetry.WafMetricCollector.AIGuardTruncationType.MESSAGES;
66
import static datadog.trace.util.Strings.isBlank;
7-
import static java.util.Collections.singletonMap;
87

98
import com.squareup.moshi.JsonAdapter;
109
import com.squareup.moshi.JsonReader;
@@ -69,7 +68,8 @@ public BadConfigurationException(final String message) {
6968
static final String REASON_TAG = "ai_guard.reason";
7069
static final String BLOCKED_TAG = "ai_guard.blocked";
7170
static final String META_STRUCT_TAG = "ai_guard";
72-
static final String META_STRUCT_KEY = "messages";
71+
static final String META_STRUCT_MESSAGES = "messages";
72+
static final String META_STRUCT_MATCHING_RULES = "matching_rules";
7373

7474
public static void install() {
7575
final Config config = Config.get();
@@ -208,8 +208,8 @@ public Evaluation evaluate(final List<Message> messages, final Options options)
208208
} else {
209209
span.setTag(TARGET_TAG, "prompt");
210210
}
211-
final Map<String, Object> metaStruct =
212-
singletonMap(META_STRUCT_KEY, messagesForMetaStruct(messages));
211+
final Map<String, Object> metaStruct = new HashMap<>(2);
212+
metaStruct.put(META_STRUCT_MESSAGES, messagesForMetaStruct(messages));
213213
span.setMetaStruct(META_STRUCT_TAG, metaStruct);
214214
final Request.Builder request =
215215
new Request.Builder()
@@ -230,10 +230,8 @@ public Evaluation evaluate(final List<Message> messages, final Options options)
230230
if (reason != null) {
231231
span.setTag(REASON_TAG, reason);
232232
}
233-
if (tags != null) {
234-
for (final String tag : tags) {
235-
span.setTag("ai_guard.tags." + tag, true);
236-
}
233+
if (tags != null && !tags.isEmpty()) {
234+
metaStruct.put(META_STRUCT_MATCHING_RULES, tags);
237235
}
238236
final boolean shouldBlock =
239237
isBlockingEnabled(options, result.get("is_blocking_enabled")) && action != Action.ALLOW;

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class AIGuardInternalTests extends DDSpecification {
157157
Request request = null
158158
Throwable error = null
159159
AIGuard.Evaluation eval = null
160+
Map<String, Object> receivedMeta = null
160161
final throwAbortError = suite.blocking && suite.action != ALLOW
161162
final call = Mock(Call) {
162163
execute() >> {
@@ -189,16 +190,18 @@ class AIGuardInternalTests extends DDSpecification {
189190
}
190191
1 * span.setTag(AIGuardInternal.ACTION_TAG, suite.action)
191192
1 * span.setTag(AIGuardInternal.REASON_TAG, suite.reason)
192-
1 * span.setMetaStruct(AIGuardInternal.META_STRUCT_TAG, [messages: suite.messages])
193+
1 * span.setMetaStruct(AIGuardInternal.META_STRUCT_TAG, _ as Map) >> {
194+
receivedMeta = it[1] as Map<String, Object>
195+
return span
196+
}
193197
if (throwAbortError) {
194198
1 * span.addThrowable(_ as AIGuard.AIGuardAbortError)
195199
}
200+
201+
receivedMeta.messages == suite.messages
196202
if (suite.tags) {
197-
suite.tags.each {
198-
1 * span.setTag("ai_guard.tags.${it}", true)
199-
}
203+
receivedMeta.matching_rules == suite.tags
200204
}
201-
202205
assertRequest(request, suite.messages)
203206
if (throwAbortError) {
204207
error instanceof AIGuard.AIGuardAbortError

0 commit comments

Comments
 (0)