|
18 | 18 | from timeit import default_timer
|
19 | 19 | from typing import TYPE_CHECKING, Optional
|
20 | 20 |
|
| 21 | +from opentelemetry import context |
21 | 22 | from opentelemetry._logs import Logger, LogRecord
|
| 23 | +from opentelemetry.metrics import Histogram |
22 | 24 | from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import (
|
23 | 25 | GEN_AI_OPENAI_REQUEST_SERVICE_TIER,
|
24 | 26 | GEN_AI_OPENAI_RESPONSE_SERVICE_TIER,
|
25 | 27 | GEN_AI_OPERATION_NAME,
|
26 | 28 | GEN_AI_OUTPUT_TYPE,
|
27 | 29 | GEN_AI_REQUEST_CHOICE_COUNT,
|
| 30 | + GEN_AI_REQUEST_ENCODING_FORMATS, |
28 | 31 | GEN_AI_REQUEST_FREQUENCY_PENALTY,
|
29 | 32 | GEN_AI_REQUEST_MAX_TOKENS,
|
30 | 33 | GEN_AI_REQUEST_MODEL,
|
|
43 | 46 | )
|
44 | 47 | from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
|
45 | 48 | from opentelemetry.semconv.attributes.server_attributes import SERVER_ADDRESS, SERVER_PORT
|
46 |
| - |
47 |
| -try: |
48 |
| - from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import GEN_AI_REQUEST_ENCODING_FORMATS |
49 |
| -except ImportError: |
50 |
| - # available since 1.29.0 |
51 |
| - GEN_AI_REQUEST_ENCODING_FORMATS = "gen_ai.request.encoding_formats" |
52 |
| - |
53 |
| -from opentelemetry.metrics import Histogram |
54 | 49 | from opentelemetry.trace import Span
|
| 50 | +from opentelemetry.trace.propagation import set_span_in_context |
55 | 51 | from opentelemetry.util.types import Attributes
|
56 | 52 |
|
57 | 53 | EVENT_GEN_AI_ASSISTANT_MESSAGE = "gen_ai.assistant.message"
|
@@ -380,15 +376,13 @@ def _send_logs_from_stream_choices(
|
380 | 376 | "message": message,
|
381 | 377 | }
|
382 | 378 | # StreamWrapper is consumed after start_as_current_span exits, so capture the current span
|
383 |
| - ctx = span.get_span_context() |
| 379 | + ctx = set_span_in_context(span, context.get_current()) |
384 | 380 | # keep compat on the exported attributes with Event
|
385 | 381 | log = LogRecord(
|
386 | 382 | event_name=EVENT_GEN_AI_CHOICE,
|
387 | 383 | body=body,
|
388 | 384 | attributes={**attributes, "event.name": EVENT_GEN_AI_CHOICE},
|
389 |
| - trace_id=ctx.trace_id, |
390 |
| - span_id=ctx.span_id, |
391 |
| - trace_flags=ctx.trace_flags, |
| 385 | + context=ctx, |
392 | 386 | )
|
393 | 387 | logger.emit(log)
|
394 | 388 |
|
|
0 commit comments