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 3 commits into
base: main
Choose a base branch
from

Conversation

emhagman
Copy link

@emhagman emhagman commented Jun 26, 2025

Summary

Add tag support for metdata.user_id in Anthropic message calls (seen here https://docs.anthropic.com/en/api/messages#body-metadata) so that you can filter traces by user_id properly.

Currently anthropic.request.paramters contains a string of the dict which can't be filtered. Similar to user being tracked as a tag in openai.request.user but for Anthropic.

ddtrace_anthropic_userid

Risks

See PR comment; Removes user_id from anthropic.request.params based on how it's coded. If people have trace filters, APM Metrics, etc by looking for user_id in their string already, it would break that. We could keep it in there for legacy reasons if needed.

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

@emhagman emhagman requested review from a team as code owners June 26, 2025 17:53
@emhagman emhagman requested review from avara1986 and sabrenner June 26, 2025 17:53
@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants