Skip to content

feat(anthropic): support user_id metadata tag #13790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions ddtrace/contrib/internal/anthropic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ def tag_params_on_span(span, kwargs, integration):
for k, v in kwargs.items():
if k == "system" and integration.is_pc_sampled_span(span):
span.set_tag_str("anthropic.request.system", integration.trunc(str(v)))
elif k == "metadata" and isinstance(v, dict):
Copy link
Author

@emhagman emhagman Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does remove it from anthropic.request.params, not sure if we should include it for legacy reasons in case anyone was using string matching here in their traces (@anthropic.request.params:"...") or if it's OK to change this.

user_id = _get_attr(v, "user_id", None)
if user_id:
span.set_tag_str("anthropic.request.user_id", str(user_id))
elif k not in ("messages", "model"):
tagged_params[k] = v
span.set_tag_str("anthropic.request.parameters", safe_json(tagged_params))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
features:
- |
tracing: Add ``anthropic.request.user_id`` tag to Anthropic spans from ``metadata.user_id`` parameter.
16 changes: 16 additions & 0 deletions tests/contrib/anthropic/test_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ def test_global_tags(ddtrace_config_anthropic, anthropic, request_vcr, mock_trac
assert span.get_tag("anthropic.request.api_key") == "sk-...key>"


def test_user_id_tag(ddtrace_config_anthropic, anthropic, request_vcr, mock_tracer):
llm = anthropic.Anthropic()
with override_global_config(dict(service="test-svc", env="staging", version="1234")):
cassette_name = "anthropic_completion.yaml"
with request_vcr.use_cassette(cassette_name):
llm.messages.create(
model="claude-3-opus-20240229",
max_tokens=15,
messages=[{"role": "user", "content": "What does Nietzsche mean by 'God is dead'?"}],
metadata={"user_id": "1234567890"},
)

span = mock_tracer.pop_traces()[0][0]
assert span.get_tag("anthropic.request.user_id") == "1234567890"


@pytest.mark.snapshot(token="tests.contrib.anthropic.test_anthropic.test_anthropic_llm", ignores=["resource"])
def test_anthropic_llm_sync_create(anthropic, request_vcr):
llm = anthropic.Anthropic()
Expand Down