Skip to content

Commit 5d82d94

Browse files
committed
fix(openai): Handle content capture with span_and_event mode
The is_content_enabled() function only recognized 'true' as a valid value for OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, causing newer enum values like 'span_and_event' to be treated as disabled. Updated is_content_enabled() to recognize all valid ContentCapturingMode enum values (span_only, event_only, span_and_event) in addition to the legacy 'true' value. Added dedicated unit tests for is_content_enabled() covering all ContentCapturingMode values instead of fixture-based testing. Fixes #4038
1 parent 7f107df commit 5d82d94

2 files changed

Lines changed: 81 additions & 1 deletion

File tree

instrumentation-genai/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
)
3838
from opentelemetry.trace.status import Status, StatusCode
3939
from opentelemetry.util.genai.types import (
40+
ContentCapturingMode,
4041
InputMessage,
4142
LLMInvocation,
4243
OutputMessage,
@@ -55,7 +56,16 @@ def is_content_enabled() -> bool:
5556
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "false"
5657
)
5758

58-
return capture_content.lower() == "true"
59+
lower = capture_content.lower()
60+
if lower == "true":
61+
return True
62+
63+
# Support newer ContentCapturingMode enum values (e.g. span_and_event)
64+
try:
65+
mode = ContentCapturingMode[lower.upper()]
66+
return mode != ContentCapturingMode.NO_CONTENT
67+
except KeyError:
68+
return False
5969

6070

6171
def extract_tool_calls(item, capture_content):
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Unit tests for is_content_enabled() covering all ContentCapturingMode values."""
16+
17+
import pytest
18+
19+
from opentelemetry.instrumentation.openai_v2.utils import (
20+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT,
21+
is_content_enabled,
22+
)
23+
24+
25+
@pytest.mark.parametrize(
26+
"env_value",
27+
["true", "True", "TRUE"],
28+
)
29+
def test_is_content_enabled_true(monkeypatch, env_value):
30+
monkeypatch.setenv(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, env_value)
31+
assert is_content_enabled() is True
32+
33+
34+
def test_is_content_enabled_span_and_event(monkeypatch):
35+
monkeypatch.setenv(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "span_and_event")
36+
assert is_content_enabled() is True
37+
38+
39+
def test_is_content_enabled_span_only(monkeypatch):
40+
monkeypatch.setenv(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "span_only")
41+
assert is_content_enabled() is True
42+
43+
44+
def test_is_content_enabled_event_only(monkeypatch):
45+
monkeypatch.setenv(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "event_only")
46+
assert is_content_enabled() is True
47+
48+
49+
@pytest.mark.parametrize(
50+
"env_value",
51+
["false", "False", "FALSE"],
52+
)
53+
def test_is_content_enabled_false(monkeypatch, env_value):
54+
monkeypatch.setenv(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, env_value)
55+
assert is_content_enabled() is False
56+
57+
58+
def test_is_content_enabled_empty(monkeypatch):
59+
monkeypatch.setenv(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "")
60+
assert is_content_enabled() is False
61+
62+
63+
def test_is_content_enabled_random_string(monkeypatch):
64+
monkeypatch.setenv(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "random")
65+
assert is_content_enabled() is False
66+
67+
68+
def test_is_content_enabled_unset(monkeypatch):
69+
monkeypatch.delenv(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, raising=False)
70+
assert is_content_enabled() is False

0 commit comments

Comments
 (0)