-
Notifications
You must be signed in to change notification settings - Fork 846
feat(anthropic): add Messages.create sync instrumentation #4034
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
base: main
Are you sure you want to change the base?
feat(anthropic): add Messages.create sync instrumentation #4034
Conversation
- Removed unused trace import from conftest.py and updated tracer_provider fixture to return the provider instead of yielding it. - Enhanced test_instrument_with_no_providers to ensure proper instrument/uninstrument cycle while passing providers for a clean test environment.
- Improved readability by restructuring the assignment of request_model in the messages_create function. - Ensured consistent handling of model attributes from both the attributes dictionary and kwargs.
…s function - Updated the return type to include AttributeValue for better type safety. - Expanded the docstring to detail the OpenTelemetry semantic conventions for LLM requests and server attributes. - Included descriptions for GenAI attributes and server attributes to improve clarity and documentation.
e011a91 to
643a282
Compare
- Introduced a new test file for integration tests that require a valid ANTHROPIC_API_KEY. - Implemented a pytest hook to skip integration tests if the API key is not set or is a placeholder. - Updated pyproject.toml to include pytest configuration for test paths and markers for integration tests. - Enhanced conftest.py to support skipping integration tests based on API key availability.
- Removed commented-out imports and improved assertion readability in test_integration.py. - Streamlined attribute checks for spans to enhance clarity and maintainability. - Ensured consistent validation of response attributes in integration tests.
- Added a pylint directive to disable the redefined-outer-name warning, improving code quality and maintainability.
- Updated the messages_create function to ensure request_model is explicitly converted to a string for better type consistency. - Cleaned up the docstring in get_llm_request_attributes to remove unnecessary whitespace and enhance readability. - Improved clarity in the documentation of GenAI and server attributes included in the returned dictionary.
- Added a comment to the AnthropicInstrumentor class to indicate the need for a logger_provider in the TelemetryHandler to capture content events.
|
@aabmass Can you please take another look? I updated the pr with the suggested changes. |
…nstrumentation package. The integration tests have been deleted, and the pytest configuration has been updated to reflect these changes.
…strumentation test configuration.
| def is_content_enabled() -> bool: | ||
| """Check if content capture is enabled via environment variable.""" | ||
| capture_content = environ.get( | ||
| OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "false" | ||
| ) | ||
| return capture_content.lower() == "true" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like something we should expose in genai utils if it's not already.
Looking through this PR, it doesn't look like it's actually used? I see it's passed to the wrapper function, but the _capture_content is not accessed. Can this just be removed and let GenAI utils take care of removing it from the spans/events?
…d related references. Updated messages_create function to eliminate unused parameter.
Description
Implements OpenTelemetry instrumentation for Anthropic's
Messages.createAPI (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 attributesutils.py: Helper functions for attribute extraction, error handling, and content capture configuration__init__.py: Wiring for patching/unpatching viawraptSemantic Convention Attributes Captured
Request:
gen_ai.operation.name(chat)gen_ai.system(anthropic)gen_ai.request.modelgen_ai.request.max_tokensgen_ai.request.temperaturegen_ai.request.top_pgen_ai.request.top_kgen_ai.request.stop_sequencesserver.addressResponse:
gen_ai.response.idgen_ai.response.modelgen_ai.response.finish_reasonsgen_ai.usage.input_tokensgen_ai.usage.output_tokensError:
error.type(on exceptions)Fixes #(#3949)
Type of change
How Has This Been Tested?
Ran the full test suite with VCR cassettes for mocked API responses:
Test cases:
Results: 15 tests passed (8 new + 7 existing instrumentor tests)
Does This PR Require a Core Repo Change?
Checklist: