Skip to content

Commit d2e7e2d

Browse files
authored
fix(environments): correctly propagate client attribute (#1188)
1 parent 8054094 commit d2e7e2d

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

langfuse/_client/client.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ def start_span(
297297
input=input,
298298
output=output,
299299
metadata=metadata,
300+
environment=self._environment,
300301
)
301302

302303
otel_span = self._otel_tracer.start_span(name=name, attributes=attributes)
@@ -307,6 +308,7 @@ def start_span(
307308
input=input,
308309
output=output,
309310
metadata=metadata,
311+
environment=self._environment,
310312
)
311313

312314
def start_as_current_span(
@@ -697,6 +699,7 @@ def _start_as_current_otel_span_with_processed_media(
697699
input=input,
698700
output=output,
699701
metadata=metadata,
702+
environment=self._environment,
700703
)
701704
if as_type == "span"
702705
else LangfuseGeneration(
@@ -705,6 +708,7 @@ def _start_as_current_otel_span_with_processed_media(
705708
input=input,
706709
output=output,
707710
metadata=metadata,
711+
environment=self._environment,
708712
)
709713
)
710714

@@ -849,7 +853,11 @@ def update_current_span(
849853
current_otel_span = self._get_current_otel_span()
850854

851855
if current_otel_span is not None:
852-
span = LangfuseSpan(otel_span=current_otel_span, langfuse_client=self)
856+
span = LangfuseSpan(
857+
otel_span=current_otel_span,
858+
langfuse_client=self,
859+
environment=self._environment,
860+
)
853861

854862
span.update(
855863
input=input,
@@ -919,7 +927,11 @@ def update_current_trace(
919927
current_otel_span = self._get_current_otel_span()
920928

921929
if current_otel_span is not None:
922-
span = LangfuseSpan(otel_span=current_otel_span, langfuse_client=self)
930+
span = LangfuseSpan(
931+
otel_span=current_otel_span,
932+
langfuse_client=self,
933+
environment=self._environment,
934+
)
923935

924936
span.update_trace(
925937
name=name,

langfuse/_client/span.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def __init__(
6868
input: Optional[Any] = None,
6969
output: Optional[Any] = None,
7070
metadata: Optional[Any] = None,
71+
environment: Optional[str] = None,
7172
):
7273
"""Initialize a new Langfuse span wrapper.
7374
@@ -78,6 +79,7 @@ def __init__(
7879
input: Input data for the span (any JSON-serializable object)
7980
output: Output data from the span (any JSON-serializable object)
8081
metadata: Additional metadata to associate with the span
82+
environment: The tracing environment
8183
"""
8284
self._otel_span = otel_span
8385
self._otel_span.set_attribute(
@@ -88,6 +90,12 @@ def __init__(
8890
self.trace_id = self._langfuse_client._get_otel_trace_id(otel_span)
8991
self.id = self._langfuse_client._get_otel_span_id(otel_span)
9092

93+
self._environment = environment
94+
if self._environment is not None:
95+
self._otel_span.set_attribute(
96+
LangfuseOtelSpanAttributes.ENVIRONMENT, self._environment
97+
)
98+
9199
# Handle media only if span is sampled
92100
if self._otel_span.is_recording:
93101
media_processed_input = self._process_media_and_apply_mask(
@@ -490,6 +498,7 @@ def __init__(
490498
input: Optional[Any] = None,
491499
output: Optional[Any] = None,
492500
metadata: Optional[Any] = None,
501+
environment: Optional[str] = None,
493502
):
494503
"""Initialize a new LangfuseSpan.
495504
@@ -499,6 +508,7 @@ def __init__(
499508
input: Input data for the span (any JSON-serializable object)
500509
output: Output data from the span (any JSON-serializable object)
501510
metadata: Additional metadata to associate with the span
511+
environment: The tracing environment
502512
"""
503513
super().__init__(
504514
otel_span=otel_span,
@@ -507,6 +517,7 @@ def __init__(
507517
input=input,
508518
output=output,
509519
metadata=metadata,
520+
environment=environment,
510521
)
511522

512523
def update(
@@ -643,7 +654,9 @@ def start_span(
643654
)
644655

645656
return LangfuseSpan(
646-
otel_span=new_otel_span, langfuse_client=self._langfuse_client
657+
otel_span=new_otel_span,
658+
langfuse_client=self._langfuse_client,
659+
environment=self._environment,
647660
)
648661

649662
def start_as_current_span(
@@ -818,7 +831,9 @@ def start_generation(
818831
)
819832

820833
return LangfuseGeneration(
821-
otel_span=new_otel_span, langfuse_client=self._langfuse_client
834+
otel_span=new_otel_span,
835+
langfuse_client=self._langfuse_client,
836+
environment=self._environment,
822837
)
823838

824839
def start_as_current_generation(
@@ -936,6 +951,7 @@ def __init__(
936951
input: Optional[Any] = None,
937952
output: Optional[Any] = None,
938953
metadata: Optional[Any] = None,
954+
environment: Optional[str] = None,
939955
):
940956
"""Initialize a new LangfuseGeneration span.
941957
@@ -945,6 +961,7 @@ def __init__(
945961
input: Input data for the generation (e.g., prompts)
946962
output: Output from the generation (e.g., completions)
947963
metadata: Additional metadata to associate with the generation
964+
environment: The tracing environment
948965
"""
949966
super().__init__(
950967
otel_span=otel_span,
@@ -953,6 +970,7 @@ def __init__(
953970
input=input,
954971
output=output,
955972
metadata=metadata,
973+
environment=environment,
956974
)
957975

958976
def update(

0 commit comments

Comments
 (0)