From b144213e6750f963f481a5e0fbb76462e0df6cc3 Mon Sep 17 00:00:00 2001 From: Gal Kleinman Date: Thu, 14 Aug 2025 17:05:16 +0300 Subject: [PATCH 1/2] temp(anthropic): disable beta apis instrumentation --- .../instrumentation/anthropic/__init__.py | 124 ++++++++++-------- 1 file changed, 68 insertions(+), 56 deletions(-) diff --git a/packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py b/packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py index d3275df1ba..88140f2fc6 100644 --- a/packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py +++ b/packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py @@ -81,32 +81,32 @@ "method": "stream", "span_name": "anthropic.chat", }, - # Beta API methods (regular Anthropic SDK) - { - "package": "anthropic.resources.beta.messages.messages", - "object": "Messages", - "method": "create", - "span_name": "anthropic.chat", - }, - { - "package": "anthropic.resources.beta.messages.messages", - "object": "Messages", - "method": "stream", - "span_name": "anthropic.chat", - }, - # Beta API methods (Bedrock SDK) - { - "package": "anthropic.lib.bedrock._beta_messages", - "object": "Messages", - "method": "create", - "span_name": "anthropic.chat", - }, - { - "package": "anthropic.lib.bedrock._beta_messages", - "object": "Messages", - "method": "stream", - "span_name": "anthropic.chat", - }, + # # Beta API methods (regular Anthropic SDK) + # { + # "package": "anthropic.resources.beta.messages.messages", + # "object": "Messages", + # "method": "create", + # "span_name": "anthropic.chat", + # }, + # { + # "package": "anthropic.resources.beta.messages.messages", + # "object": "Messages", + # "method": "stream", + # "span_name": "anthropic.chat", + # }, + # # Beta API methods (Bedrock SDK) + # { + # "package": "anthropic.lib.bedrock._beta_messages", + # "object": "Messages", + # "method": "create", + # "span_name": "anthropic.chat", + # }, + # { + # "package": "anthropic.lib.bedrock._beta_messages", + # "object": "Messages", + # "method": "stream", + # "span_name": "anthropic.chat", + # }, ] WRAPPED_AMETHODS = [ @@ -122,32 +122,32 @@ "method": "create", "span_name": "anthropic.chat", }, - # Beta API async methods (regular Anthropic SDK) - { - "package": "anthropic.resources.beta.messages.messages", - "object": "AsyncMessages", - "method": "create", - "span_name": "anthropic.chat", - }, - { - "package": "anthropic.resources.beta.messages.messages", - "object": "AsyncMessages", - "method": "stream", - "span_name": "anthropic.chat", - }, - # Beta API async methods (Bedrock SDK) - { - "package": "anthropic.lib.bedrock._beta_messages", - "object": "AsyncMessages", - "method": "create", - "span_name": "anthropic.chat", - }, - { - "package": "anthropic.lib.bedrock._beta_messages", - "object": "AsyncMessages", - "method": "stream", - "span_name": "anthropic.chat", - }, + # # Beta API async methods (regular Anthropic SDK) + # { + # "package": "anthropic.resources.beta.messages.messages", + # "object": "AsyncMessages", + # "method": "create", + # "span_name": "anthropic.chat", + # }, + # { + # "package": "anthropic.resources.beta.messages.messages", + # "object": "AsyncMessages", + # "method": "stream", + # "span_name": "anthropic.chat", + # }, + # # Beta API async methods (Bedrock SDK) + # { + # "package": "anthropic.lib.bedrock._beta_messages", + # "object": "AsyncMessages", + # "method": "create", + # "span_name": "anthropic.chat", + # }, + # { + # "package": "anthropic.lib.bedrock._beta_messages", + # "object": "AsyncMessages", + # "method": "stream", + # "span_name": "anthropic.chat", + # }, ] @@ -183,6 +183,7 @@ async def _aset_token_usage( choice_counter: Counter = None, ): from opentelemetry.instrumentation.anthropic.utils import _aextract_response_data + response = await _aextract_response_data(response) if usage := response.get("usage"): @@ -276,6 +277,7 @@ def _set_token_usage( choice_counter: Counter = None, ): from opentelemetry.instrumentation.anthropic.utils import _extract_response_data + response = _extract_response_data(response) if usage := response.get("usage"): @@ -443,7 +445,10 @@ async def _ahandle_response(span: Span, event_logger: Optional[EventLogger], res else: if not span.is_recording(): return - from opentelemetry.instrumentation.anthropic.span_utils import aset_response_attributes + from opentelemetry.instrumentation.anthropic.span_utils import ( + aset_response_attributes, + ) + await aset_response_attributes(span, response) @@ -669,7 +674,10 @@ async def _awrap( kwargs, ) elif response: - from opentelemetry.instrumentation.anthropic.utils import ashared_metrics_attributes + from opentelemetry.instrumentation.anthropic.utils import ( + ashared_metrics_attributes, + ) + metric_attributes = await ashared_metrics_attributes(response) if duration_histogram: @@ -774,9 +782,13 @@ def _instrument(self, **kwargs): wrapped_method, ), ) - logger.debug(f"Successfully wrapped {wrap_package}.{wrap_object}.{wrap_method}") + logger.debug( + f"Successfully wrapped {wrap_package}.{wrap_object}.{wrap_method}" + ) except Exception as e: - logger.debug(f"Failed to wrap {wrap_package}.{wrap_object}.{wrap_method}: {e}") + logger.debug( + f"Failed to wrap {wrap_package}.{wrap_object}.{wrap_method}: {e}" + ) pass # that's ok, we don't want to fail if some methods do not exist for wrapped_method in WRAPPED_AMETHODS: From 17280945e62464c3d6cbb6c14675d81199624bc0 Mon Sep 17 00:00:00 2001 From: Gal Kleinman Date: Thu, 14 Aug 2025 17:08:39 +0300 Subject: [PATCH 2/2] disable related tests --- .../tests/test_bedrock_with_raw_response.py | 55 ++++++++++++++----- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/packages/opentelemetry-instrumentation-anthropic/tests/test_bedrock_with_raw_response.py b/packages/opentelemetry-instrumentation-anthropic/tests/test_bedrock_with_raw_response.py index dc950a7f9a..183743474e 100644 --- a/packages/opentelemetry-instrumentation-anthropic/tests/test_bedrock_with_raw_response.py +++ b/packages/opentelemetry-instrumentation-anthropic/tests/test_bedrock_with_raw_response.py @@ -21,14 +21,19 @@ def async_anthropic_bedrock_client(instrument_legacy): return AsyncAnthropicBedrock( aws_region=aws_region, aws_access_key=aws_access_key, - aws_secret_key=aws_secret_key + aws_secret_key=aws_secret_key, ) +@pytest.mark.skip @pytest.mark.asyncio @pytest.mark.vcr async def test_async_anthropic_bedrock_with_raw_response( - instrument_legacy, async_anthropic_bedrock_client, span_exporter, log_exporter, reader + instrument_legacy, + async_anthropic_bedrock_client, + span_exporter, + log_exporter, + reader, ): """Test that AsyncAnthropicBedrock with_raw_response.create generates spans""" response = await async_anthropic_bedrock_client.messages.with_raw_response.create( @@ -53,7 +58,11 @@ async def test_async_anthropic_bedrock_with_raw_response( ) assert (anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.0.role"]) == "user" # For raw response, content is accessed differently - response_content = response.parse().content[0].text if hasattr(response, 'parse') else response.content[0].text + response_content = ( + response.parse().content[0].text + if hasattr(response, "parse") + else response.content[0].text + ) assert ( anthropic_span.attributes.get(f"{SpanAttributes.LLM_COMPLETIONS}.0.content") == response_content @@ -71,10 +80,15 @@ async def test_async_anthropic_bedrock_with_raw_response( ) +@pytest.mark.skip @pytest.mark.asyncio @pytest.mark.vcr async def test_async_anthropic_bedrock_regular_create( - instrument_legacy, async_anthropic_bedrock_client, span_exporter, log_exporter, reader + instrument_legacy, + async_anthropic_bedrock_client, + span_exporter, + log_exporter, + reader, ): """Test that regular AsyncAnthropicBedrock create works (for comparison)""" response = await async_anthropic_bedrock_client.messages.create( @@ -115,21 +129,28 @@ async def test_async_anthropic_bedrock_regular_create( ) +@pytest.mark.skip @pytest.mark.asyncio @pytest.mark.vcr async def test_async_anthropic_bedrock_beta_with_raw_response( - instrument_legacy, async_anthropic_bedrock_client, span_exporter, log_exporter, reader + instrument_legacy, + async_anthropic_bedrock_client, + span_exporter, + log_exporter, + reader, ): """Test that AsyncAnthropicBedrock beta.messages.with_raw_response.create generates spans""" - response = await async_anthropic_bedrock_client.beta.messages.with_raw_response.create( - max_tokens=1024, - messages=[ - { - "role": "user", - "content": "Tell me a joke about OpenTelemetry", - } - ], - model="anthropic.claude-3-haiku-20240307-v1:0", + response = ( + await async_anthropic_bedrock_client.beta.messages.with_raw_response.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Tell me a joke about OpenTelemetry", + } + ], + model="anthropic.claude-3-haiku-20240307-v1:0", + ) ) spans = span_exporter.get_finished_spans() @@ -143,7 +164,11 @@ async def test_async_anthropic_bedrock_beta_with_raw_response( ) assert (anthropic_span.attributes[f"{SpanAttributes.LLM_PROMPTS}.0.role"]) == "user" # For raw response, content is accessed differently - response_content = response.parse().content[0].text if hasattr(response, 'parse') else response.content[0].text + response_content = ( + response.parse().content[0].text + if hasattr(response, "parse") + else response.content[0].text + ) assert ( anthropic_span.attributes.get(f"{SpanAttributes.LLM_COMPLETIONS}.0.content") == response_content