Skip to content

Commit fe703a9

Browse files
chore: add the tags returned by the service to the ai_guard span
1 parent 7c26d49 commit fe703a9

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

ddtrace/appsec/ai_guard/_api_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ def evaluate(self, messages: List[Message], options: Optional[Options] = None) -
224224
attributes = result["data"]["attributes"]
225225
action = attributes["action"]
226226
reason = attributes.get("reason", None)
227+
tags = attributes.get("tags", [])
227228
blocking_enabled = attributes.get("is_blocking_enabled", False)
228229
except Exception as e:
229230
value = json.dumps(result, indent=2)[:500]
@@ -239,6 +240,8 @@ def evaluate(self, messages: List[Message], options: Optional[Options] = None) -
239240
)
240241

241242
span.set_tag(AI_GUARD.ACTION_TAG, action)
243+
for tag in tags:
244+
span.set_tag(AI_GUARD.TAG + ".tag." + tag, "true")
242245
if reason:
243246
span.set_tag(AI_GUARD.REASON_TAG, reason)
244247
else:

tests/appsec/ai_guard/api/test_api_client.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646

4747
def _build_test_params():
4848
actions = [
49-
{"action": "ALLOW", "reason": "Go ahead"},
50-
{"action": "DENY", "reason": "Nope"},
51-
{"action": "ABORT", "reason": "Kill it with fire"},
49+
{"action": "ALLOW", "reason": "Go ahead", "tags": []},
50+
{"action": "DENY", "reason": "Nope", "tags": ["deny_everything", "test_deny"]},
51+
{"action": "ABORT", "reason": "Kill it with fire", "tags": ["alarm_tag", "abort_everything"]},
5252
]
5353
block = [True, False]
5454
suites = [
@@ -63,6 +63,7 @@ def _build_test_params():
6363
pytest.param(
6464
action["action"],
6565
action["reason"],
66+
action["tags"],
6667
block,
6768
suite["suite"],
6869
suite["target"],
@@ -78,14 +79,24 @@ def assert_telemetry(mocked, metric, tags):
7879
assert ("count", "appsec", metric, 1, tags) in metrics
7980

8081

81-
@pytest.mark.parametrize("action,reason,blocking,suite,target,messages", _build_test_params())
82+
@pytest.mark.parametrize("action,reason,tags,blocking,suite,target,messages", _build_test_params())
8283
@patch("ddtrace.internal.telemetry.telemetry_writer._namespace")
8384
@patch("ddtrace.appsec.ai_guard._api_client.AIGuardClient._execute_request")
8485
def test_evaluate_method(
85-
mock_execute_request, telemetry_mock, ai_guard_client, tracer, action, reason, blocking, suite, target, messages
86+
mock_execute_request,
87+
telemetry_mock,
88+
ai_guard_client,
89+
tracer,
90+
action,
91+
reason,
92+
tags,
93+
blocking,
94+
suite,
95+
target,
96+
messages,
8697
):
8798
"""Test different combinations of evaluations."""
88-
mock_execute_request.return_value = mock_evaluate_response(action, reason, blocking)
99+
mock_execute_request.return_value = mock_evaluate_response(action, reason, tags, blocking)
89100
should_block = blocking and action != "ALLOW"
90101

91102
if should_block:
@@ -103,6 +114,8 @@ def test_evaluate_method(
103114
expected_tags.update({"ai_guard.tool_name": "calc"})
104115
if action != "ALLOW" and blocking:
105116
expected_tags.update({"ai_guard.blocked": "true"})
117+
for tag in tags:
118+
expected_tags.update({"ai_guard.tag." + tag: "true"})
106119
assert_ai_guard_span(
107120
tracer,
108121
messages,

tests/appsec/ai_guard/utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,18 @@ def assert_ai_guard_span(tracer: DummyTracer, messages: List[Message], tags: Dic
3838
assert struct["messages"] == messages
3939

4040

41-
def mock_evaluate_response(action: str, reason: str = "", block: bool = True) -> Mock:
41+
def mock_evaluate_response(action: str, reason: str = "", tags: List[str] = None, block: bool = True) -> Mock:
4242
mock_response = Mock()
4343
mock_response.status = 200
4444
mock_response.get_json.return_value = {
45-
"data": {"attributes": {"action": action, "reason": reason, "is_blocking_enabled": block}}
45+
"data": {
46+
"attributes": {
47+
"action": action,
48+
"reason": reason,
49+
"tags": tags if tags else [],
50+
"is_blocking_enabled": block,
51+
}
52+
}
4653
}
4754
return mock_response
4855

0 commit comments

Comments
 (0)