Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
)
from opentelemetry.trace.status import Status, StatusCode
from opentelemetry.util.genai.types import (
ContentCapturingMode,
InputMessage,
LLMInvocation,
OutputMessage,
Expand All @@ -55,7 +56,16 @@ def is_content_enabled() -> bool:
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "false"
)

return capture_content.lower() == "true"
lower = capture_content.lower()
if lower == "true":
return True

# Support newer ContentCapturingMode enum values (e.g. span_and_event)
try:
mode = ContentCapturingMode[lower.upper()]
return mode != ContentCapturingMode.NO_CONTENT
except KeyError:
return False


def extract_tool_calls(item, capture_content):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Unit tests for is_content_enabled() covering all ContentCapturingMode values."""

import pytest

from opentelemetry.instrumentation.openai_v2.utils import (
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT,
is_content_enabled,
)


@pytest.mark.parametrize(
"env_value",
["true", "True", "TRUE"],
)
def test_is_content_enabled_true(monkeypatch, env_value):
monkeypatch.setenv(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, env_value)
assert is_content_enabled() is True


def test_is_content_enabled_span_and_event(monkeypatch):
monkeypatch.setenv(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "span_and_event")
assert is_content_enabled() is True


def test_is_content_enabled_span_only(monkeypatch):
monkeypatch.setenv(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "span_only")
assert is_content_enabled() is True


def test_is_content_enabled_event_only(monkeypatch):
monkeypatch.setenv(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "event_only")
assert is_content_enabled() is True


@pytest.mark.parametrize(
"env_value",
["false", "False", "FALSE"],
)
def test_is_content_enabled_false(monkeypatch, env_value):
monkeypatch.setenv(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, env_value)
assert is_content_enabled() is False


def test_is_content_enabled_empty(monkeypatch):
monkeypatch.setenv(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "")
assert is_content_enabled() is False


def test_is_content_enabled_random_string(monkeypatch):
monkeypatch.setenv(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "random")
assert is_content_enabled() is False


def test_is_content_enabled_unset(monkeypatch):
monkeypatch.delenv(OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, raising=False)
assert is_content_enabled() is False