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
25 changes: 25 additions & 0 deletions ddtrace/contrib/internal/anthropic/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ def patch() -> None:
)
wrap("anthropic", "resources.beta.messages.messages.AsyncMessages.stream", traced_chat_model_generate)

# AnthropicBedrock / AsyncAnthropicBedrock define their own beta Messages classes in
# anthropic.lib.bedrock._beta_messages whose `create` attribute is bound to the first-party
# function at module import time, before patch() runs. The wrap() above on the first-party
# class does not affect those captured references, so Bedrock callers see no spans unless we
# also wrap the Bedrock module paths directly.
try:
import anthropic.lib.bedrock._beta_messages # noqa: F401
except ImportError:
pass
else:
wrap("anthropic", "lib.bedrock._beta_messages.Messages.create", traced_chat_model_generate)
wrap(
"anthropic",
"lib.bedrock._beta_messages.AsyncMessages.create",
traced_async_chat_model_generate,
)


def unpatch() -> None:
if not getattr(anthropic, "_datadog_patch", False):
Expand All @@ -136,4 +153,12 @@ def unpatch() -> None:
unwrap(anthropic.resources.beta.messages.messages.AsyncMessages, "create")
unwrap(anthropic.resources.beta.messages.messages.AsyncMessages, "stream")

try:
import anthropic.lib.bedrock._beta_messages as _bedrock_beta_messages
except ImportError:
pass
else:
unwrap(_bedrock_beta_messages.Messages, "create")
unwrap(_bedrock_beta_messages.AsyncMessages, "create")

delattr(anthropic, "_datadog_integration")
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
anthropic: Fixes missing APM and LLM Observability spans for ``AnthropicBedrock`` / ``AsyncAnthropicBedrock`` clients when calling the beta messages API.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'll suggest that this is a feature rather than a fix

``anthropic.lib.bedrock._beta_messages`` defines its own ``Messages`` / ``AsyncMessages`` classes whose ``create`` attribute is bound to the first-party
function at import time, before the integration's ``patch()`` runs, so wrapping only the first-party class left Bedrock callers untraced.
Loading