Skip to content

Commit 74fc926

Browse files
authored
fix(anthropic): temp disable beta apis instrumentation (#3258)
1 parent 57b67b1 commit 74fc926

File tree

2 files changed

+108
-71
lines changed

2 files changed

+108
-71
lines changed

packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -81,32 +81,32 @@
8181
"method": "stream",
8282
"span_name": "anthropic.chat",
8383
},
84-
# Beta API methods (regular Anthropic SDK)
85-
{
86-
"package": "anthropic.resources.beta.messages.messages",
87-
"object": "Messages",
88-
"method": "create",
89-
"span_name": "anthropic.chat",
90-
},
91-
{
92-
"package": "anthropic.resources.beta.messages.messages",
93-
"object": "Messages",
94-
"method": "stream",
95-
"span_name": "anthropic.chat",
96-
},
97-
# Beta API methods (Bedrock SDK)
98-
{
99-
"package": "anthropic.lib.bedrock._beta_messages",
100-
"object": "Messages",
101-
"method": "create",
102-
"span_name": "anthropic.chat",
103-
},
104-
{
105-
"package": "anthropic.lib.bedrock._beta_messages",
106-
"object": "Messages",
107-
"method": "stream",
108-
"span_name": "anthropic.chat",
109-
},
84+
# # Beta API methods (regular Anthropic SDK)
85+
# {
86+
# "package": "anthropic.resources.beta.messages.messages",
87+
# "object": "Messages",
88+
# "method": "create",
89+
# "span_name": "anthropic.chat",
90+
# },
91+
# {
92+
# "package": "anthropic.resources.beta.messages.messages",
93+
# "object": "Messages",
94+
# "method": "stream",
95+
# "span_name": "anthropic.chat",
96+
# },
97+
# # Beta API methods (Bedrock SDK)
98+
# {
99+
# "package": "anthropic.lib.bedrock._beta_messages",
100+
# "object": "Messages",
101+
# "method": "create",
102+
# "span_name": "anthropic.chat",
103+
# },
104+
# {
105+
# "package": "anthropic.lib.bedrock._beta_messages",
106+
# "object": "Messages",
107+
# "method": "stream",
108+
# "span_name": "anthropic.chat",
109+
# },
110110
]
111111

112112
WRAPPED_AMETHODS = [
@@ -122,32 +122,32 @@
122122
"method": "create",
123123
"span_name": "anthropic.chat",
124124
},
125-
# Beta API async methods (regular Anthropic SDK)
126-
{
127-
"package": "anthropic.resources.beta.messages.messages",
128-
"object": "AsyncMessages",
129-
"method": "create",
130-
"span_name": "anthropic.chat",
131-
},
132-
{
133-
"package": "anthropic.resources.beta.messages.messages",
134-
"object": "AsyncMessages",
135-
"method": "stream",
136-
"span_name": "anthropic.chat",
137-
},
138-
# Beta API async methods (Bedrock SDK)
139-
{
140-
"package": "anthropic.lib.bedrock._beta_messages",
141-
"object": "AsyncMessages",
142-
"method": "create",
143-
"span_name": "anthropic.chat",
144-
},
145-
{
146-
"package": "anthropic.lib.bedrock._beta_messages",
147-
"object": "AsyncMessages",
148-
"method": "stream",
149-
"span_name": "anthropic.chat",
150-
},
125+
# # Beta API async methods (regular Anthropic SDK)
126+
# {
127+
# "package": "anthropic.resources.beta.messages.messages",
128+
# "object": "AsyncMessages",
129+
# "method": "create",
130+
# "span_name": "anthropic.chat",
131+
# },
132+
# {
133+
# "package": "anthropic.resources.beta.messages.messages",
134+
# "object": "AsyncMessages",
135+
# "method": "stream",
136+
# "span_name": "anthropic.chat",
137+
# },
138+
# # Beta API async methods (Bedrock SDK)
139+
# {
140+
# "package": "anthropic.lib.bedrock._beta_messages",
141+
# "object": "AsyncMessages",
142+
# "method": "create",
143+
# "span_name": "anthropic.chat",
144+
# },
145+
# {
146+
# "package": "anthropic.lib.bedrock._beta_messages",
147+
# "object": "AsyncMessages",
148+
# "method": "stream",
149+
# "span_name": "anthropic.chat",
150+
# },
151151
]
152152

153153

@@ -183,6 +183,7 @@ async def _aset_token_usage(
183183
choice_counter: Counter = None,
184184
):
185185
from opentelemetry.instrumentation.anthropic.utils import _aextract_response_data
186+
186187
response = await _aextract_response_data(response)
187188

188189
if usage := response.get("usage"):
@@ -276,6 +277,7 @@ def _set_token_usage(
276277
choice_counter: Counter = None,
277278
):
278279
from opentelemetry.instrumentation.anthropic.utils import _extract_response_data
280+
279281
response = _extract_response_data(response)
280282

281283
if usage := response.get("usage"):
@@ -443,7 +445,10 @@ async def _ahandle_response(span: Span, event_logger: Optional[EventLogger], res
443445
else:
444446
if not span.is_recording():
445447
return
446-
from opentelemetry.instrumentation.anthropic.span_utils import aset_response_attributes
448+
from opentelemetry.instrumentation.anthropic.span_utils import (
449+
aset_response_attributes,
450+
)
451+
447452
await aset_response_attributes(span, response)
448453

449454

@@ -669,7 +674,10 @@ async def _awrap(
669674
kwargs,
670675
)
671676
elif response:
672-
from opentelemetry.instrumentation.anthropic.utils import ashared_metrics_attributes
677+
from opentelemetry.instrumentation.anthropic.utils import (
678+
ashared_metrics_attributes,
679+
)
680+
673681
metric_attributes = await ashared_metrics_attributes(response)
674682

675683
if duration_histogram:
@@ -774,9 +782,13 @@ def _instrument(self, **kwargs):
774782
wrapped_method,
775783
),
776784
)
777-
logger.debug(f"Successfully wrapped {wrap_package}.{wrap_object}.{wrap_method}")
785+
logger.debug(
786+
f"Successfully wrapped {wrap_package}.{wrap_object}.{wrap_method}"
787+
)
778788
except Exception as e:
779-
logger.debug(f"Failed to wrap {wrap_package}.{wrap_object}.{wrap_method}: {e}")
789+
logger.debug(
790+
f"Failed to wrap {wrap_package}.{wrap_object}.{wrap_method}: {e}"
791+
)
780792
pass # that's ok, we don't want to fail if some methods do not exist
781793

782794
for wrapped_method in WRAPPED_AMETHODS:

packages/opentelemetry-instrumentation-anthropic/tests/test_bedrock_with_raw_response.py

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@ def async_anthropic_bedrock_client(instrument_legacy):
2121
return AsyncAnthropicBedrock(
2222
aws_region=aws_region,
2323
aws_access_key=aws_access_key,
24-
aws_secret_key=aws_secret_key
24+
aws_secret_key=aws_secret_key,
2525
)
2626

2727

28+
@pytest.mark.skip
2829
@pytest.mark.asyncio
2930
@pytest.mark.vcr
3031
async def test_async_anthropic_bedrock_with_raw_response(
31-
instrument_legacy, async_anthropic_bedrock_client, span_exporter, log_exporter, reader
32+
instrument_legacy,
33+
async_anthropic_bedrock_client,
34+
span_exporter,
35+
log_exporter,
36+
reader,
3237
):
3338
"""Test that AsyncAnthropicBedrock with_raw_response.create generates spans"""
3439
response = await async_anthropic_bedrock_client.messages.with_raw_response.create(
@@ -53,7 +58,11 @@ async def test_async_anthropic_bedrock_with_raw_response(
5358
)
5459
assert (anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.0.role"]) == "user"
5560
# For raw response, content is accessed differently
56-
response_content = response.parse().content[0].text if hasattr(response, 'parse') else response.content[0].text
61+
response_content = (
62+
response.parse().content[0].text
63+
if hasattr(response, "parse")
64+
else response.content[0].text
65+
)
5766
assert (
5867
anthropic_span.attributes.get(f"{SpanAttributes.LLM_COMPLETIONS}.0.content")
5968
== response_content
@@ -71,10 +80,15 @@ async def test_async_anthropic_bedrock_with_raw_response(
7180
)
7281

7382

83+
@pytest.mark.skip
7484
@pytest.mark.asyncio
7585
@pytest.mark.vcr
7686
async def test_async_anthropic_bedrock_regular_create(
77-
instrument_legacy, async_anthropic_bedrock_client, span_exporter, log_exporter, reader
87+
instrument_legacy,
88+
async_anthropic_bedrock_client,
89+
span_exporter,
90+
log_exporter,
91+
reader,
7892
):
7993
"""Test that regular AsyncAnthropicBedrock create works (for comparison)"""
8094
response = await async_anthropic_bedrock_client.messages.create(
@@ -115,21 +129,28 @@ async def test_async_anthropic_bedrock_regular_create(
115129
)
116130

117131

132+
@pytest.mark.skip
118133
@pytest.mark.asyncio
119134
@pytest.mark.vcr
120135
async def test_async_anthropic_bedrock_beta_with_raw_response(
121-
instrument_legacy, async_anthropic_bedrock_client, span_exporter, log_exporter, reader
136+
instrument_legacy,
137+
async_anthropic_bedrock_client,
138+
span_exporter,
139+
log_exporter,
140+
reader,
122141
):
123142
"""Test that AsyncAnthropicBedrock beta.messages.with_raw_response.create generates spans"""
124-
response = await async_anthropic_bedrock_client.beta.messages.with_raw_response.create(
125-
max_tokens=1024,
126-
messages=[
127-
{
128-
"role": "user",
129-
"content": "Tell me a joke about OpenTelemetry",
130-
}
131-
],
132-
model="anthropic.claude-3-haiku-20240307-v1:0",
143+
response = (
144+
await async_anthropic_bedrock_client.beta.messages.with_raw_response.create(
145+
max_tokens=1024,
146+
messages=[
147+
{
148+
"role": "user",
149+
"content": "Tell me a joke about OpenTelemetry",
150+
}
151+
],
152+
model="anthropic.claude-3-haiku-20240307-v1:0",
153+
)
133154
)
134155

135156
spans = span_exporter.get_finished_spans()
@@ -143,7 +164,11 @@ async def test_async_anthropic_bedrock_beta_with_raw_response(
143164
)
144165
assert (anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.0.role"]) == "user"
145166
# For raw response, content is accessed differently
146-
response_content = response.parse().content[0].text if hasattr(response, 'parse') else response.content[0].text
167+
response_content = (
168+
response.parse().content[0].text
169+
if hasattr(response, "parse")
170+
else response.content[0].text
171+
)
147172
assert (
148173
anthropic_span.attributes.get(f"{SpanAttributes.LLM_COMPLETIONS}.0.content")
149174
== response_content

0 commit comments

Comments
 (0)