opentelemetry-python-contrib
opentelemetry-python-contrib copied to clipboard
feat(anthropic): add Messages.create sync instrumentation
Description
Implements OpenTelemetry instrumentation for Anthropic's Messages.create API (sync, non-streaming). This adds automatic tracing for Anthropic SDK calls, capturing GenAI semantic convention attributes for observability.
P.S: LLM help was used for writing the code.
What's Implemented
-
patch.py: Wrapper function forMessages.createthat creates spans with request/response attributes -
utils.py: Helper functions for attribute extraction, error handling, and content capture configuration -
__init__.py: Wiring for patching/unpatching viawrapt - Tests: Comprehensive test suite with VCR cassettes for API mocking
- Updated minimum anthropic version to 0.16.0 - the instrumentation requires the modern SDK structure (anthropic.resources.messages module) which doesn't exist in versions prior to 0.18.0.
Semantic Convention Attributes Captured
Request:
-
gen_ai.operation.name(chat) -
gen_ai.system(anthropic) -
gen_ai.request.model -
gen_ai.request.max_tokens -
gen_ai.request.temperature -
gen_ai.request.top_p -
gen_ai.request.top_k -
gen_ai.request.stop_sequences -
server.address
Response:
-
gen_ai.response.id -
gen_ai.response.model -
gen_ai.response.finish_reasons -
gen_ai.usage.input_tokens -
gen_ai.usage.output_tokens
Error:
-
error.type(on exceptions)
Fixes #(https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3949)
Type of change
- [x] New feature (non-breaking change which adds functionality)
How Has This Been Tested?
Ran the full test suite with VCR cassettes for mocked API responses:
pytest instrumentation-genai/opentelemetry-instrumentation-anthropic/tests/ -v
Test cases:
- [x] Basic sync message creation with correct span attributes
- [x] All optional parameters (temperature, top_p, top_k, stop_sequences)
- [x] Token usage capture (input/output tokens)
- [x] Stop reason captured as finish_reasons array
- [x] Connection error handling (APIConnectionError)
- [x] API error handling (404 NotFoundError)
- [x] Uninstrumentation removes patching
- [x] Multiple instrument/uninstrument cycles
Results: 15 tests passed (8 new + 7 existing instrumentor tests)
Does This PR Require a Core Repo Change?
- [x] No.
Checklist:
- [x] Followed the style guidelines of this project
- [x] Changelogs have been updated
- [x] Unit tests have been added
- [x] Documentation has been updated