Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions langfuse/_client/attribute_propagation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@



from opentelemetry import trace
from typing import Any, Dict, Optional
from opentelemetry import baggage

import opentelemetry.context as otel_context

def propagate_attributes(
*,
current_ctx: Optional[otel_context.Context],
dict_to_propagate: Dict[str, Any],
) -> None:

"""
Propagate attributes from a dictionary to a span and context.
"""

ctx = current_ctx or otel_context.get_current()


for key, value in dict_to_propagate.items():
print(f"Propagating attribute {key} with value {value}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Debug print statement will expose potentially sensitive trace data in production logs - should be removed or wrapped in a debug check

Suggested change
print(f"Propagating attribute {key} with value {value}")
# Debug logging removed to prevent exposure of sensitive trace data

# Baggage values must be strings
baggage.set_baggage(key, str(value), context=ctx)




4 changes: 1 addition & 3 deletions langfuse/_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,9 +713,7 @@ def _create_observation_from_otel_span(
level=level,
status_message=status_message,
)
# span._observation_type = as_type
# span._otel_span.set_attribute("langfuse.observation.type", as_type)
# return span


def start_generation(
self,
Expand Down
6 changes: 6 additions & 0 deletions langfuse/_client/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from opentelemetry import trace as otel_trace_api
from opentelemetry.util._decorator import _AgnosticContextManager

from langfuse._client.attribute_propagation import propagate_attributes
from langfuse.model import PromptClient

if TYPE_CHECKING:
Expand Down Expand Up @@ -258,6 +259,11 @@ def update_trace(
public=public,
)

propagate_attributes(
current_ctx=None,
dict_to_propagate=attributes,
)

self._otel_span.set_attributes(attributes)

return self
Expand Down
22 changes: 11 additions & 11 deletions tests/test_core_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,10 +606,10 @@ def test_score_trace_nested_observation():

# Create a parent span and set trace name
with langfuse.start_as_current_span(name="parent-span") as parent_span:
parent_span.update_trace(name=trace_name)
parent_span.update_trace(name=trace_name, metadata={"key": "hahaha"})

# Create a child span
child_span = langfuse.start_span(name="span")
child_span = parent_span.start_span(name="span")

# Score the child span
child_span.score(
Expand All @@ -630,18 +630,18 @@ def test_score_trace_nested_observation():
sleep(2)

# Retrieve and verify
trace = get_api().trace.get(trace_id)
# trace = get_api().trace.get(trace_id)

assert trace.name == trace_name
assert len(trace.scores) == 1
# assert trace.name == trace_name
# assert len(trace.scores) == 1

score = trace.scores[0]
# score = trace.scores[0]

assert score.name == "valuation"
assert score.value == 0.5
assert score.comment == "This is a comment"
assert score.observation_id == child_span_id # API returns this field name
assert score.data_type == "NUMERIC"
# assert score.name == "valuation"
# assert score.value == 0.5
# assert score.comment == "This is a comment"
# assert score.observation_id == child_span_id # API returns this field name
# assert score.data_type == "NUMERIC"


def test_score_span():
Expand Down
Loading