Skip to content

Commit 4b4e710

Browse files
committed
Dealing with comments
1 parent c4f4bbb commit 4b4e710

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
@@ -25,8 +25,8 @@ classifiers = [
2525
"Programming Language :: Python :: 3.13",
2626
]
2727
dependencies = [
28-
"opentelemetry-instrumentation ~= 0.60b1",
29-
"opentelemetry-semantic-conventions ~= 0.60b1",
28+
"opentelemetry-instrumentation ~= 0.61b0.dev",
29+
"opentelemetry-semantic-conventions ~= 0.61b0.dev",
3030
"opentelemetry-api>=1.31.0",
3131
]
3232

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
@@ -244,32 +244,18 @@ class EmbeddingInvocation(GenAIInvocation):
244244
and context_token attributes are set by the TelemetryHandler.
245245
"""
246246

247-
operation_name: str = field(
248-
default=GenAI.GenAiOperationNameValues.EMBEDDINGS.value,
249-
metadata={"semconv": GenAI.GEN_AI_OPERATION_NAME},
250-
)
251-
provider: str | None = None # e.g., azure.ai.openai, openai, aws.bedrock
247+
operation_name: str = GenAI.GenAiOperationNameValues.EMBEDDINGS.value
252248

253-
request_model: str | None = field(
254-
default=None,
255-
metadata={"semconv": GenAI.GEN_AI_REQUEST_MODEL},
256-
)
249+
provider: str | None = None # e.g., azure.ai.openai, openai, aws.bedrock
257250

251+
request_model: str | None = None
258252
server_address: str | None = None
259253
server_port: int | None = None
260-
error_type: str | None = None
261254

262255
# encoding_formats can be multi-value -> combinational cardinality risk.
263256
# Keep on spans/events only.
264-
encoding_formats: list[str] = field(
265-
default_factory=list,
266-
metadata={"semconv": GenAI.GEN_AI_REQUEST_ENCODING_FORMATS},
267-
)
268-
269-
input_tokens: int | None = field(
270-
default=None,
271-
metadata={"semconv": GenAI.GEN_AI_USAGE_INPUT_TOKENS},
272-
)
257+
encoding_formats: list[str] | None = None
258+
input_tokens: int | None = None
273259
dimension_count: int | None = None
274260

275261
attributes: dict[str, Any] = field(default_factory=_new_str_any_dict)
@@ -285,6 +271,7 @@ class EmbeddingInvocation(GenAIInvocation):
285271
Additional attributes to set on metrics. Must be of a low cardinality.
286272
These attributes will not be set on spans or events.
287273
"""
274+
monotonic_start_s: float | None = None
288275

289276

290277
@dataclass

0 commit comments

Comments
 (0)