Skip to content

Commit c2d6232

Browse files
committed
feedback
Change-Id: Ida0c2305d950d978c31eb04a80e21e947fabdfba Co-developed-by: Cursor <[email protected]>
1 parent e0d35b2 commit c2d6232

File tree

5 files changed

+205
-33
lines changed

5 files changed

+205
-33
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616
"OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"
1717
)
1818

19+
OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT = "OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT"
20+
"""
21+
.. envvar:: OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT
22+
23+
Controls whether to emit gen_ai.client.inference.operation.details events.
24+
Must be one of ``true`` or ``false`` (case-insensitive).
25+
Defaults to ``false``.
26+
"""
27+
1928
OTEL_INSTRUMENTATION_GENAI_COMPLETION_HOOK = (
2029
"OTEL_INSTRUMENTATION_GENAI_COMPLETION_HOOK"
2130
)

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
gen_ai_json_dumps,
4343
get_content_capturing_mode,
4444
is_experimental_mode,
45+
should_emit_event,
4546
)
4647

4748

@@ -140,14 +141,11 @@ def _maybe_emit_llm_event(
140141
This function creates a LogRecord event following the semantic convention
141142
for gen_ai.client.inference.operation.details as specified in the GenAI
142143
event semantic conventions.
143-
"""
144-
if not is_experimental_mode() or get_content_capturing_mode() not in (
145-
ContentCapturingMode.EVENT_ONLY,
146-
ContentCapturingMode.SPAN_AND_EVENT,
147-
):
148-
return
149144
150-
if logger is None:
145+
For more details, see the semantic convention documentation:
146+
https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/gen-ai-events.md#event-eventgen_aiclientinferenceoperationdetails
147+
"""
148+
if not is_experimental_mode() or not should_emit_event() or logger is None:
151149
return
152150

153151
# Build event attributes by reusing the attribute getter functions

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ class ContentCapturingMode(Enum):
4040

4141
@dataclass()
4242
class ToolCall:
43+
"""Represents a tool call requested by the model
44+
45+
This model is specified as part of semconv in `GenAI messages Python models - ToolCallRequestPart
46+
<https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/non-normative/models.ipynb>`__.
47+
"""
48+
4349
arguments: Any
4450
name: str
4551
id: str | None
@@ -48,19 +54,37 @@ class ToolCall:
4854

4955
@dataclass()
5056
class ToolCallResponse:
57+
"""Represents a tool call result sent to the model or a built-in tool call outcome and details
58+
59+
This model is specified as part of semconv in `GenAI messages Python models - ToolCallResponsePart
60+
<https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/non-normative/models.ipynb>`__.
61+
"""
62+
5163
response: Any
5264
id: str | None
5365
type: Literal["tool_call_response"] = "tool_call_response"
5466

5567

5668
@dataclass()
5769
class Text:
70+
"""Represents text content sent to or received from the model
71+
72+
This model is specified as part of semconv in `GenAI messages Python models - TextPart
73+
<https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/non-normative/models.ipynb>`__.
74+
"""
75+
5876
content: str
5977
type: Literal["text"] = "text"
6078

6179

6280
@dataclass()
6381
class Reasoning:
82+
"""Represents reasoning/thinking content received from the model
83+
84+
This model is specified as part of semconv in `GenAI messages Python models - ReasoningPart
85+
<https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/non-normative/models.ipynb>`__.
86+
"""
87+
6488
content: str
6589
type: Literal["reasoning"] = "reasoning"
6690

@@ -70,6 +94,12 @@ class Reasoning:
7094

7195
@dataclass()
7296
class Blob:
97+
"""Represents blob binary data sent inline to the model
98+
99+
This model is specified as part of semconv in `GenAI messages Python models - BlobPart
100+
<https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/non-normative/models.ipynb>`__.
101+
"""
102+
73103
mime_type: str | None
74104
modality: Union[Modality, str]
75105
content: bytes
@@ -78,6 +108,12 @@ class Blob:
78108

79109
@dataclass()
80110
class File:
111+
"""Represents an external referenced file sent to the model by file id
112+
113+
This model is specified as part of semconv in `GenAI messages Python models - FilePart
114+
<https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/non-normative/models.ipynb>`__.
115+
"""
116+
81117
mime_type: str | None
82118
modality: Union[Modality, str]
83119
file_id: str
@@ -86,6 +122,12 @@ class File:
86122

87123
@dataclass()
88124
class Uri:
125+
"""Represents an external referenced file sent to the model by URI
126+
127+
This model is specified as part of semconv in `GenAI messages Python models - UriPart
128+
<https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/non-normative/models.ipynb>`__.
129+
"""
130+
89131
mime_type: str | None
90132
modality: Union[Modality, str]
91133
uri: str

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
)
2727
from opentelemetry.util.genai.environment_variables import (
2828
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT,
29+
OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT,
2930
)
3031
from opentelemetry.util.genai.types import ContentCapturingMode
3132

@@ -64,6 +65,28 @@ def get_content_capturing_mode() -> ContentCapturingMode:
6465
return ContentCapturingMode.NO_CONTENT
6566

6667

68+
def should_emit_event() -> bool:
69+
"""Check if event emission is enabled.
70+
71+
Returns True if event emission is enabled, False otherwise.
72+
Defaults to False if the environment variable is not set.
73+
"""
74+
envvar = os.environ.get(OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT)
75+
if not envvar:
76+
return False
77+
envvar_lower = envvar.lower()
78+
if envvar_lower == "true":
79+
return True
80+
if envvar_lower == "false":
81+
return False
82+
logger.warning(
83+
"%s is not a valid option for `%s` environment variable. Must be one of true or false (case-insensitive). Defaulting to `false`.",
84+
envvar,
85+
OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT,
86+
)
87+
return False
88+
89+
6790
class _GenAiJsonEncoder(json.JSONEncoder):
6891
def default(self, o: Any) -> Any:
6992
if isinstance(o, bytes):

0 commit comments

Comments
 (0)