Skip to content

Commit a799410

Browse files
committed
Dealing with comments
1 parent c202f8b commit a799410

3 files changed

Lines changed: 50 additions & 33 deletions

File tree

util/opentelemetry-util-genai/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ classifiers = [
2626
"Programming Language :: Python :: 3.14",
2727
]
2828
dependencies = [
29-
"opentelemetry-instrumentation ~= 0.60b1",
30-
"opentelemetry-semantic-conventions ~= 0.60b1",
29+
"opentelemetry-instrumentation ~= 0.61b0.dev",
30+
"opentelemetry-semantic-conventions ~= 0.61b0.dev",
3131
"opentelemetry-api>=1.31.0",
3232
]
3333

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

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,30 @@ def llm(
225225
raise
226226
self.stop_llm(invocation)
227227

228+
@contextmanager
229+
def embedding(
230+
self, invocation: EmbeddingInvocation | None = None
231+
) -> Iterator[EmbeddingInvocation]:
232+
"""Context manager for Embedding invocations.
233+
234+
Only set data attributes on the invocation object, do not modify the span or context.
235+
236+
Starts the span on entry. On normal exit, finalizes the invocation and ends the span.
237+
If an exception occurs inside the context, marks the span as error, ends it, and
238+
re-raises the original exception.
239+
"""
240+
if invocation is None:
241+
invocation = EmbeddingInvocation()
242+
self.start_embedding(invocation)
243+
try:
244+
yield invocation
245+
except Exception as exc:
246+
self.fail_embedding(
247+
invocation, Error(message=str(exc), type=type(exc))
248+
)
249+
raise
250+
self.stop_embedding(invocation)
251+
228252
def start_embedding(
229253
self, invocation: EmbeddingInvocation
230254
) -> EmbeddingInvocation:
@@ -249,11 +273,13 @@ def stop_embedding(
249273
return invocation
250274

251275
span = invocation.span
252-
_apply_embedding_finish_attributes(span, invocation)
253-
self._record_embedding_metrics(invocation, span)
254-
# Detach context and end span
255-
otel_context.detach(invocation.context_token)
256-
span.end()
276+
try:
277+
_apply_embedding_finish_attributes(span, invocation)
278+
self._record_embedding_metrics(invocation, span)
279+
finally:
280+
# Detach context and end span even if finishing fails
281+
otel_context.detach(invocation.context_token)
282+
span.end()
257283
return invocation
258284

259285
def fail_embedding(
@@ -265,13 +291,17 @@ def fail_embedding(
265291
return invocation
266292

267293
span = invocation.span
268-
_apply_embedding_finish_attributes(invocation.span, invocation)
269-
_apply_error_attributes(invocation.span, error)
270-
error_type = getattr(error.type, "__qualname__", None)
271-
self._record_embedding_metrics(invocation, span, error_type=error_type)
272-
# Detach context and end span
273-
otel_context.detach(invocation.context_token)
274-
span.end()
294+
try:
295+
_apply_embedding_finish_attributes(invocation.span, invocation)
296+
_apply_error_attributes(invocation.span, error)
297+
error_type = getattr(error.type, "__qualname__", None)
298+
self._record_embedding_metrics(
299+
invocation, span, error_type=error_type
300+
)
301+
finally:
302+
# Detach context and end span even if finishing fails
303+
otel_context.detach(invocation.context_token)
304+
span.end()
275305
return invocation
276306

277307

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

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -264,32 +264,18 @@ class EmbeddingInvocation(GenAIInvocation):
264264
and context_token attributes are set by the TelemetryHandler.
265265
"""
266266

267-
operation_name: str = field(
268-
default=GenAI.GenAiOperationNameValues.EMBEDDINGS.value,
269-
metadata={"semconv": GenAI.GEN_AI_OPERATION_NAME},
270-
)
271-
provider: str | None = None # e.g., azure.ai.openai, openai, aws.bedrock
267+
operation_name: str = GenAI.GenAiOperationNameValues.EMBEDDINGS.value
272268

273-
request_model: str | None = field(
274-
default=None,
275-
metadata={"semconv": GenAI.GEN_AI_REQUEST_MODEL},
276-
)
269+
provider: str | None = None # e.g., azure.ai.openai, openai, aws.bedrock
277270

271+
request_model: str | None = None
278272
server_address: str | None = None
279273
server_port: int | None = None
280-
error_type: str | None = None
281274

282275
# encoding_formats can be multi-value -> combinational cardinality risk.
283276
# Keep on spans/events only.
284-
encoding_formats: list[str] = field(
285-
default_factory=list,
286-
metadata={"semconv": GenAI.GEN_AI_REQUEST_ENCODING_FORMATS},
287-
)
288-
289-
input_tokens: int | None = field(
290-
default=None,
291-
metadata={"semconv": GenAI.GEN_AI_USAGE_INPUT_TOKENS},
292-
)
277+
encoding_formats: list[str] | None = None
278+
input_tokens: int | None = None
293279
dimension_count: int | None = None
294280

295281
attributes: dict[str, Any] = field(default_factory=_new_str_any_dict)
@@ -305,6 +291,7 @@ class EmbeddingInvocation(GenAIInvocation):
305291
Additional attributes to set on metrics. Must be of a low cardinality.
306292
These attributes will not be set on spans or events.
307293
"""
294+
monotonic_start_s: float | None = None
308295

309296

310297
@dataclass

0 commit comments

Comments
 (0)