Skip to content

Commit 3a37f43

Browse files
committed
Additional span attributes
1 parent 381d38d commit 3a37f43

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

util/opentelemetry-util-genai/README.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ This package provides these span attributes.
2121
-> gen_ai.usage.input_tokens: Int(24)
2222
-> gen_ai.usage.output_tokens: Int(7)
2323
-> gen_ai.input.messages: Str("[{\"role\": \"user\", \"content\": \"hello world\"}]")
24+
-> gen_ai.request.top_p: Double(0.9)
25+
-> gen_ai.request.frequency_penalty: Double(0.5)
26+
-> gen_ai.request.presence_penalty: Double(0.5)
27+
-> gen_ai.request.stop_sequences: Slice(["\n","Human:","AI:"])
28+
-> gen_ai.request.seed: Int(100)
29+
-> gen_ai.request.max_tokens: Int(100)
30+
-> gen_ai.request.temperature: Double(0.1)
2431

2532

2633
Installation

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,52 @@ def _maybe_set_span_output_messages(
158158
)
159159

160160

161+
def _maybe_set_additional_span_attributes(
162+
span: Span, invocation: LLMInvocation
163+
) -> None:
164+
if "top_p" in invocation.attributes:
165+
span.set_attribute(
166+
GenAI.GEN_AI_REQUEST_TOP_P,
167+
invocation.attributes["top_p"],
168+
)
169+
170+
if "frequency_penalty" in invocation.attributes:
171+
span.set_attribute(
172+
GenAI.GEN_AI_REQUEST_FREQUENCY_PENALTY,
173+
invocation.attributes["frequency_penalty"],
174+
)
175+
176+
if "presence_penalty" in invocation.attributes:
177+
span.set_attribute(
178+
GenAI.GEN_AI_REQUEST_PRESENCE_PENALTY,
179+
invocation.attributes["presence_penalty"],
180+
)
181+
182+
if "stop" in invocation.attributes:
183+
span.set_attribute(
184+
GenAI.GEN_AI_REQUEST_STOP_SEQUENCES,
185+
invocation.attributes["stop"],
186+
)
187+
188+
if "seed" in invocation.attributes:
189+
span.set_attribute(
190+
GenAI.GEN_AI_REQUEST_SEED,
191+
invocation.attributes["seed"],
192+
)
193+
194+
if "temperature" in invocation.attributes:
195+
span.set_attribute(
196+
GenAI.GEN_AI_REQUEST_TEMPERATURE,
197+
invocation.attributes["temperature"],
198+
)
199+
200+
if "max_completion_tokens" in invocation.attributes:
201+
span.set_attribute(
202+
GenAI.GEN_AI_REQUEST_MAX_TOKENS,
203+
invocation.attributes["max_completion_tokens"],
204+
)
205+
206+
161207
class BaseTelemetryGenerator:
162208
"""
163209
Abstract base for emitters mapping GenAI types -> OpenTelemetry.
@@ -270,6 +316,7 @@ def _apply_common_span_attributes(
270316
prompt_tokens,
271317
completion_tokens,
272318
)
319+
_maybe_set_additional_span_attributes(span, invocation)
273320
genai_attributes = _get_genai_attributes(
274321
request_model,
275322
response_model,

0 commit comments

Comments
 (0)