Skip to content

Commit fb30d45

Browse files
committed
check env for content capture
1 parent b3bb75c commit fb30d45

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

util/opentelemetry-util-genai/src/opentelemetry/util/genai/generators.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040

4141
from opentelemetry import trace
4242
from opentelemetry.context import Context, get_current
43+
from opentelemetry.instrumentation._semconv import (
44+
_OpenTelemetrySemanticConventionStability,
45+
_OpenTelemetryStabilitySignalType,
46+
_StabilityMode,
47+
)
4348
from opentelemetry.metrics import Histogram, Meter, get_meter
4449
from opentelemetry.semconv._incubating.attributes import (
4550
gen_ai_attributes as GenAI,
@@ -55,6 +60,10 @@
5560
use_span,
5661
)
5762
from opentelemetry.trace.status import Status, StatusCode
63+
from opentelemetry.util.genai.utils import (
64+
ContentCapturingMode,
65+
get_content_capturing_mode,
66+
)
5867
from opentelemetry.util.types import AttributeValue
5968

6069
from .instruments import Instruments
@@ -79,11 +88,10 @@ def _get_metric_attributes(
7988
framework: Optional[str],
8089
) -> Dict[str, AttributeValue]:
8190
attributes: Dict[str, AttributeValue] = {}
82-
# TODO: add below to opentelemetry.semconv._incubating.attributes.gen_ai_attributes
8391
if framework is not None:
8492
attributes["gen_ai.framework"] = framework
8593
if system:
86-
attributes["gen_ai.provider.name"] = system
94+
attributes[GenAI.GEN_AI_PROVIDER_NAME] = system
8795
if operation_name:
8896
attributes[GenAI.GEN_AI_OPERATION_NAME] = operation_name
8997
if request_model:
@@ -108,9 +116,8 @@ def _set_initial_span_attributes(
108116
if framework is not None:
109117
span.set_attribute("gen_ai.framework", framework)
110118
if system is not None:
111-
# Deprecated: use "gen_ai.provider.name"
112-
span.set_attribute(GenAI.GEN_AI_SYSTEM, system)
113-
span.set_attribute("gen_ai.provider.name", system)
119+
# TODO: clean system name to match GenAiProviderNameValues?
120+
span.set_attribute(GenAI.GEN_AI_PROVIDER_NAME, system)
114121

115122

116123
def _set_response_and_usage_attributes(
@@ -137,10 +144,21 @@ def _collect_finish_reasons(generations: List[OutputMessage]) -> List[str]:
137144
return finish_reasons
138145

139146

140-
def _maybe_set_input_messages(
141-
span: Span, messages: List[InputMessage], capture: bool
147+
def _maybe_set_span_input_messages(
148+
span: Span, messages: List[InputMessage]
142149
) -> None:
143-
if not capture:
150+
# if GEN_AI stability mode is DEFAULT, do not capture message content
151+
if (
152+
_OpenTelemetrySemanticConventionStability._get_opentelemetry_stability_opt_in_mode(
153+
_OpenTelemetryStabilitySignalType.GEN_AI,
154+
)
155+
== _StabilityMode.DEFAULT
156+
):
157+
return
158+
if get_content_capturing_mode() not in (
159+
ContentCapturingMode.SPAN_ONLY,
160+
ContentCapturingMode.SPAN_AND_EVENT,
161+
):
144162
return
145163
message_parts: List[Dict[str, Any]] = [
146164
asdict(message) for message in messages
@@ -221,14 +239,12 @@ def __init__(
221239
self,
222240
tracer: Optional[Tracer] = None,
223241
meter: Optional[Meter] = None,
224-
capture_content: bool = False,
225242
):
226243
self._tracer: Tracer = tracer or trace.get_tracer(__name__)
227244
_meter: Meter = meter or get_meter(__name__)
228245
instruments = Instruments(_meter)
229246
self._duration_histogram = instruments.operation_duration_histogram
230247
self._token_histogram = instruments.token_usage_histogram
231-
self._capture_content = capture_content
232248

233249
# Map from run_id -> _SpanState, to keep track of spans and parent/child relationships
234250
self.spans: Dict[UUID, _SpanState] = {}
@@ -352,9 +368,7 @@ def finish(self, invocation: LLMInvocation):
352368
metric_attributes, prompt_tokens, completion_tokens = (
353369
self._apply_common_span_attributes(span, invocation)
354370
)
355-
_maybe_set_input_messages(
356-
span, invocation.messages, self._capture_content
357-
)
371+
_maybe_set_span_input_messages(span, invocation.messages)
358372
_set_chat_generation_attrs(span, invocation.chat_generations)
359373
_record_token_metrics(
360374
self._token_histogram,

util/opentelemetry-util-genai/src/opentelemetry/util/genai/handler.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,26 +89,15 @@ def __init__(self, emitter_type_full: bool = True, **kwargs: Any):
8989
schema_url=Schemas.V1_36_0.value,
9090
)
9191

92-
# TODO: trigger span+metric+event generation based on the content capturing mode
92+
# TODO: trigger span+metric+event generation based on the full emitter flag
9393
self._generator = SpanMetricGenerator(
9494
tracer=self._tracer,
9595
meter=self._meter,
96-
capture_content=self._should_collect_content(),
9796
)
9897

9998
self._llm_registry: dict[UUID, LLMInvocation] = {}
10099
self._lock = Lock()
101100

102-
@staticmethod
103-
def _should_collect_content() -> bool:
104-
# TODO: Get the content capturing mode from the environment variable
105-
# from .utils import get_content_capturing_mode
106-
# from opentelemetry.instrumentation._semconv import (
107-
# OTEL_SEMCONV_STABILITY_OPT_IN,
108-
# _OpenTelemetrySemanticConventionStability,
109-
# )
110-
return True # Placeholder for future config
111-
112101
def start_llm(
113102
self,
114103
prompts: List[InputMessage],

util/opentelemetry-util-genai/tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def test_llm_start_and_stop_creates_span(self): # pylint: disable=no-self-use
139139
assert span.attributes is not None
140140
span_attrs = span.attributes
141141
assert span_attrs.get("gen_ai.operation.name") == "chat"
142-
assert span_attrs.get("gen_ai.system") == "test-system"
142+
assert span_attrs.get("gen_ai.provider.name") == "test-system"
143143
assert span.start_time is not None
144144
assert span.end_time is not None
145145
assert span.end_time > span.start_time

0 commit comments

Comments
 (0)