Skip to content

Commit

Permalink
set attributes only when span is recording
Browse files Browse the repository at this point in the history
  • Loading branch information
alizenhom committed Oct 17, 2024
1 parent 8b58f27 commit d467eb1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ def traced_method(wrapped, instance, args, kwargs):
span_name = f"{span_attributes[GenAIAttributes.GEN_AI_OPERATION_NAME]} {span_attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL]}"

span = tracer.start_span(name=span_name, kind=SpanKind.CLIENT)
_set_input_attributes(span, span_attributes)
set_event_prompt(span, json.dumps(llm_prompts))
if span.is_recording():
_set_input_attributes(span, span_attributes)
set_event_prompt(span, json.dumps(llm_prompts))

try:
result = wrapped(*args, **kwargs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from opentelemetry.semconv._incubating.attributes import (
gen_ai_attributes as GenAIAttributes,
)
from opentelemetry.trace import Span


def silently_fail(func):
Expand Down Expand Up @@ -101,7 +100,7 @@ def extract_tools_prompt(item):
return calls


def set_event_prompt(span: Span, prompt):
def set_event_prompt(span, prompt):
span.add_event(
name="gen_ai.content.prompt",
attributes={
Expand All @@ -110,12 +109,12 @@ def set_event_prompt(span: Span, prompt):
)


def set_span_attributes(span: Span, attributes: dict):
def set_span_attributes(span, attributes: dict):
for field, value in attributes.model_dump(by_alias=True).items():
set_span_attribute(span, field, value)


def set_event_completion(span: Span, result_content):
def set_event_completion(span, result_content):
span.add_event(
name="gen_ai.content.completion",
attributes={
Expand All @@ -124,7 +123,7 @@ def set_event_completion(span: Span, result_content):
)


def set_span_attribute(span: Span, name, value):
def set_span_attribute(span, name, value):
if non_numerical_value_is_set(value) is False:
return

Expand Down

0 comments on commit d467eb1

Please sign in to comment.