From 49b418ab568d141e637dc6c9603f58b6d4f8e521 Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Mon, 4 Nov 2024 10:51:37 -0800 Subject: [PATCH 1/2] GenAI - Use common env var for content recording --- .../src/opentelemetry/instrumentation/openai_v2/utils.py | 6 +++--- .../tests/conftest.py | 6 +++--- .../tests/test_chat_completions.py | 1 + 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/utils.py b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/utils.py index ffbf3db5aa..a3a2d317ce 100644 --- a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/utils.py +++ b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/utils.py @@ -27,14 +27,14 @@ server_attributes as ServerAttributes, ) -OTEL_INSTRUMENTATION_OPENAI_CAPTURE_MESSAGE_CONTENT = ( - "OTEL_INSTRUMENTATION_OPENAI_CAPTURE_MESSAGE_CONTENT" +OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = ( + "OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT" ) def is_content_enabled() -> bool: capture_content = environ.get( - OTEL_INSTRUMENTATION_OPENAI_CAPTURE_MESSAGE_CONTENT, "false" + OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "false" ) return capture_content.lower() == "true" diff --git a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/conftest.py b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/conftest.py index 1b79947cc5..f35dfbacf2 100644 --- a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/conftest.py +++ b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/conftest.py @@ -7,7 +7,7 @@ from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor from opentelemetry.instrumentation.openai_v2.utils import ( - OTEL_INSTRUMENTATION_OPENAI_CAPTURE_MESSAGE_CONTENT, + OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, ) from opentelemetry.sdk._events import EventLoggerProvider from opentelemetry.sdk._logs import LoggerProvider @@ -85,7 +85,7 @@ def instrument_no_content(tracer_provider, event_logger_provider): @pytest.fixture(scope="function") def instrument_with_content(tracer_provider, event_logger_provider): os.environ.update( - {OTEL_INSTRUMENTATION_OPENAI_CAPTURE_MESSAGE_CONTENT: "True"} + {OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT: "True"} ) instrumentor = OpenAIInstrumentor() instrumentor.instrument( @@ -94,7 +94,7 @@ def instrument_with_content(tracer_provider, event_logger_provider): ) yield instrumentor - os.environ.pop(OTEL_INSTRUMENTATION_OPENAI_CAPTURE_MESSAGE_CONTENT, None) + os.environ.pop(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, None) instrumentor.uninstrument() diff --git a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/test_chat_completions.py b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/test_chat_completions.py index be7bfc09b3..4369aa6a38 100644 --- a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/test_chat_completions.py +++ b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/test_chat_completions.py @@ -713,6 +713,7 @@ def assert_message_in_logs(log, event_name, expected_content, parent_span): if not expected_content: assert not log.log_record.body else: + assert log.log_record.body assert dict(log.log_record.body) == remove_none_values( expected_content ) From 6ed94c65660d597f5f8515080e70c9cf8b8d8939 Mon Sep 17 00:00:00 2001 From: Liudmila Molkova Date: Mon, 4 Nov 2024 11:26:03 -0800 Subject: [PATCH 2/2] changelog --- .../opentelemetry-instrumentation-openai-v2/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/CHANGELOG.md b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/CHANGELOG.md index 3509eb5b79..16ecbdaefe 100644 --- a/instrumentation-genai/opentelemetry-instrumentation-openai-v2/CHANGELOG.md +++ b/instrumentation-genai/opentelemetry-instrumentation-openai-v2/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +- Use generic `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT` environment variable + to control if content of prompt, completion, and other messages is captured. + ([#2947](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2947)) + - Update OpenAI instrumentation to Semantic Conventions v1.28.0: add new attributes and switch prompts and completions to log-based events. ([#2925](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2925))