Skip to content

Commit d467eb1

Browse files
committed
set attributes only when span is recording
1 parent 8b58f27 commit d467eb1

File tree

2 files changed

+7
-7
lines changed
  • instrumentation/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2

2 files changed

+7
-7
lines changed

Diff for: instrumentation/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/patch.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ def traced_method(wrapped, instance, args, kwargs):
5151
span_name = f"{span_attributes[GenAIAttributes.GEN_AI_OPERATION_NAME]} {span_attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL]}"
5252

5353
span = tracer.start_span(name=span_name, kind=SpanKind.CLIENT)
54-
_set_input_attributes(span, span_attributes)
55-
set_event_prompt(span, json.dumps(llm_prompts))
54+
if span.is_recording():
55+
_set_input_attributes(span, span_attributes)
56+
set_event_prompt(span, json.dumps(llm_prompts))
5657

5758
try:
5859
result = wrapped(*args, **kwargs)

Diff for: instrumentation/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/utils.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from opentelemetry.semconv._incubating.attributes import (
2222
gen_ai_attributes as GenAIAttributes,
2323
)
24-
from opentelemetry.trace import Span
2524

2625

2726
def silently_fail(func):
@@ -101,7 +100,7 @@ def extract_tools_prompt(item):
101100
return calls
102101

103102

104-
def set_event_prompt(span: Span, prompt):
103+
def set_event_prompt(span, prompt):
105104
span.add_event(
106105
name="gen_ai.content.prompt",
107106
attributes={
@@ -110,12 +109,12 @@ def set_event_prompt(span: Span, prompt):
110109
)
111110

112111

113-
def set_span_attributes(span: Span, attributes: dict):
112+
def set_span_attributes(span, attributes: dict):
114113
for field, value in attributes.model_dump(by_alias=True).items():
115114
set_span_attribute(span, field, value)
116115

117116

118-
def set_event_completion(span: Span, result_content):
117+
def set_event_completion(span, result_content):
119118
span.add_event(
120119
name="gen_ai.content.completion",
121120
attributes={
@@ -124,7 +123,7 @@ def set_event_completion(span: Span, result_content):
124123
)
125124

126125

127-
def set_span_attribute(span: Span, name, value):
126+
def set_span_attribute(span, name, value):
128127
if non_numerical_value_is_set(value) is False:
129128
return
130129

0 commit comments

Comments
 (0)