Skip to content

Commit e16f49f

Browse files
committed
Refactoring to remove duplicate codes
1 parent a799410 commit e16f49f

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,21 @@ def _get_embedding_common_attributes(
8888
}
8989

9090

91+
def _get_span_name(
92+
invocation: LLMInvocation | EmbeddingInvocation,
93+
) -> str:
94+
"""Get the span name for a GenAI invocation."""
95+
return f"{invocation.operation_name} {invocation.request_model}".strip()
96+
97+
9198
def _get_llm_span_name(invocation: LLMInvocation) -> str:
9299
"""Get the span name for an LLM invocation."""
93-
return f"{invocation.operation_name} {invocation.request_model}".strip()
100+
return _get_span_name(invocation)
94101

95102

96103
def _get_embedding_span_name(invocation: EmbeddingInvocation) -> str:
97104
"""Get the span name for an Embedding invocation."""
98-
return f"{invocation.operation_name} {invocation.request_model}".strip()
105+
return _get_span_name(invocation)
99106

100107

101108
def _get_llm_messages_attributes_for_span(

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ class GenAIInvocation:
201201
context_token: ContextToken | None = None
202202
span: Span | None = None
203203
attributes: dict[str, Any] = field(default_factory=_new_str_any_dict)
204+
# Monotonic start time in seconds (from timeit.default_timer) used
205+
# for duration calculations to avoid mixing clock sources. This is
206+
# populated by the TelemetryHandler when starting an invocation.
207+
monotonic_start_s: float | None = None
204208

205209

206210
@dataclass
@@ -250,10 +254,6 @@ class LLMInvocation(GenAIInvocation):
250254
seed: int | None = None
251255
server_address: str | None = None
252256
server_port: int | None = None
253-
# Monotonic start time in seconds (from timeit.default_timer) used
254-
# for duration calculations to avoid mixing clock sources. This is
255-
# populated by the TelemetryHandler when starting an invocation.
256-
monotonic_start_s: float | None = None
257257

258258

259259
@dataclass
@@ -267,7 +267,6 @@ class EmbeddingInvocation(GenAIInvocation):
267267
operation_name: str = GenAI.GenAiOperationNameValues.EMBEDDINGS.value
268268

269269
provider: str | None = None # e.g., azure.ai.openai, openai, aws.bedrock
270-
271270
request_model: str | None = None
272271
server_address: str | None = None
273272
server_port: int | None = None
@@ -291,7 +290,6 @@ class EmbeddingInvocation(GenAIInvocation):
291290
Additional attributes to set on metrics. Must be of a low cardinality.
292291
These attributes will not be set on spans or events.
293292
"""
294-
monotonic_start_s: float | None = None
295293

296294

297295
@dataclass

0 commit comments

Comments
 (0)