Skip to content

Commit db4de18

Browse files
committed
chore: set properties on current span
1 parent 10d9c85 commit db4de18

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

langfuse/_client/client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3506,6 +3506,11 @@ def session(
35063506
new_context = otel_context_api.set_value(LANGFUSE_CTX_SESSION_ID, id)
35073507
token = otel_context_api.attach(new_context)
35083508

3509+
# Set attribute on currently active span if exists
3510+
current_span = otel_trace_api.get_current_span()
3511+
if current_span is not None and current_span.is_recording():
3512+
current_span.set_attribute("session.id", id)
3513+
35093514
# Set baggage if requested
35103515
baggage_token = None
35113516
if as_baggage:
@@ -3558,6 +3563,11 @@ def user(self, id: str, *, as_baggage: bool = False) -> Generator[None, None, No
35583563
new_context = otel_context_api.set_value(LANGFUSE_CTX_USER_ID, id)
35593564
token = otel_context_api.attach(new_context)
35603565

3566+
# Set attribute on currently active span if exists
3567+
current_span = otel_trace_api.get_current_span()
3568+
if current_span is not None and current_span.is_recording():
3569+
current_span.set_attribute("user.id", id)
3570+
35613571
# Set baggage if requested
35623572
baggage_token = None
35633573
if as_baggage:
@@ -3618,6 +3628,21 @@ def metadata(
36183628
new_context = otel_context_api.set_value(LANGFUSE_CTX_METADATA, kwargs)
36193629
token = otel_context_api.attach(new_context)
36203630

3631+
# Set attributes on currently active span if exists
3632+
current_span = otel_trace_api.get_current_span()
3633+
if current_span is not None and current_span.is_recording():
3634+
import json as json_module
3635+
3636+
for key, value in kwargs.items():
3637+
attr_key = f"langfuse.metadata.{key}"
3638+
# Convert value to appropriate type for span attribute
3639+
if isinstance(value, (str, int, float, bool)):
3640+
attr_value = value
3641+
else:
3642+
# For complex types, convert to JSON string
3643+
attr_value = json_module.dumps(value)
3644+
current_span.set_attribute(attr_key, attr_value)
3645+
36213646
# Set baggage if requested
36223647
baggage_tokens = []
36233648
if as_baggage:

langfuse/_client/span.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,11 @@ def session(
11681168
new_context = otel_context_api.set_value(LANGFUSE_CTX_SESSION_ID, id)
11691169
token = otel_context_api.attach(new_context)
11701170

1171+
# Set attribute on currently active span if exists
1172+
current_span = otel_trace_api.get_current_span()
1173+
if current_span is not None and current_span.is_recording():
1174+
current_span.set_attribute("session.id", id)
1175+
11711176
# Set baggage if requested
11721177
baggage_token = None
11731178
if as_baggage:
@@ -1219,6 +1224,11 @@ def user(self, id: str, *, as_baggage: bool = False) -> Generator[None, None, No
12191224
new_context = otel_context_api.set_value(LANGFUSE_CTX_USER_ID, id)
12201225
token = otel_context_api.attach(new_context)
12211226

1227+
# Set attribute on currently active span if exists
1228+
current_span = otel_trace_api.get_current_span()
1229+
if current_span is not None and current_span.is_recording():
1230+
current_span.set_attribute("user.id", id)
1231+
12221232
# Set baggage if requested
12231233
baggage_token = None
12241234
if as_baggage:
@@ -1278,6 +1288,21 @@ def metadata(
12781288
new_context = otel_context_api.set_value(LANGFUSE_CTX_METADATA, kwargs)
12791289
token = otel_context_api.attach(new_context)
12801290

1291+
# Set attributes on currently active span if exists
1292+
current_span = otel_trace_api.get_current_span()
1293+
if current_span is not None and current_span.is_recording():
1294+
import json as json_module
1295+
1296+
for key, value in kwargs.items():
1297+
attr_key = f"langfuse.metadata.{key}"
1298+
# Convert value to appropriate type for span attribute
1299+
if isinstance(value, (str, int, float, bool)):
1300+
attr_value = value
1301+
else:
1302+
# For complex types, convert to JSON string
1303+
attr_value = json_module.dumps(value)
1304+
current_span.set_attribute(attr_key, attr_value)
1305+
12811306
# Set baggage if requested
12821307
baggage_tokens = []
12831308
if as_baggage:

0 commit comments

Comments
 (0)