Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ddtrace/appsec/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ class AI_GUARD(metaclass=Constant_Class):
TARGET_TAG: str = TAG + ".target"
BLOCKED_TAG: str = TAG + ".blocked"
TOOL_NAME_TAG: str = TAG + ".tool_name"
EVENT_TAG: str = TAG + ".event"

# meta struct
STRUCT: Literal["ai_guard"] = "ai_guard"
Expand Down
1 change: 1 addition & 0 deletions ddtrace/appsec/ai_guard/_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ def evaluate(self, messages: list[Message], options: Optional[Options] = None) -
root_span = core.get_root_span()
if root_span:
_aiguard_manual_keep(root_span)
root_span.set_tag(AI_GUARD.EVENT_TAG, "true")
Comment thread
manuel-alvarez-alvarez marked this conversation as resolved.
if should_block:
span.set_tag(AI_GUARD.BLOCKED_TAG, "true")
raise AIGuardAbortError(
Expand Down
11 changes: 11 additions & 0 deletions tests/appsec/ai_guard/api/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,14 @@ def test_endpoint_discovery(site, config, param, expected):
with override_ai_guard_config(dict(_ai_guard_endpoint=config)):
client = new_ai_guard_client(endpoint=param)
assert client._endpoint == expected


@patch("ddtrace.appsec.ai_guard._api_client.AIGuardClient._execute_request")
def test_event_tag_in_root_span(mock_execute_request, ai_guard_client, tracer):
"""Test that AI Guard event tag is set on the root span of the trace."""
mock_execute_request.return_value = mock_evaluate_response("ALLOW", reason="It's fine")

with tracer.trace("root_span") as root_span:
ai_guard_client.evaluate(PROMPT, Options(block=False))

assert root_span.get_tag(AI_GUARD.EVENT_TAG) == "true"
Loading