@@ -2062,207 +2062,3 @@ def test_that_generation_like_properties_are_actually_created():
20622062 assert (
20632063 obs .completion_start_time is not None
20642064 ), f"{ obs_type } should persist completion_start_time property"
2065-
2066-
2067- def test_context_manager_user_propagation ():
2068- """Test that user context manager propagates user_id to child spans."""
2069- langfuse = Langfuse ()
2070-
2071- user_id = "test_user_123"
2072-
2073- with langfuse .start_as_current_span (name = "parent-span" ) as parent_span :
2074- with langfuse .propagate_attributes ({"user_id" : user_id }):
2075- trace_id = parent_span .trace_id
2076-
2077- # Create child spans that should inherit user_id
2078- child_span = langfuse .start_span (name = "child-span" )
2079- child_span .end ()
2080-
2081- # Create generation that should inherit user_id
2082- generation = parent_span .start_generation (name = "child-generation" )
2083- generation .end ()
2084-
2085- langfuse .flush ()
2086- sleep (2 )
2087-
2088- # Verify trace has user_id (child spans inherit via context propagation)
2089- trace = get_api ().trace .get (trace_id )
2090- assert trace .user_id == user_id
2091-
2092- # Verify child observations were created and have user_id
2093- child_observations = [
2094- obs
2095- for obs in trace .observations
2096- if obs .name in ["child-span" , "child-generation" ]
2097- # Skip user.id validation as we currently drop it from the visible attributes server-side.
2098- # and obs.metadata["attributes"]["user.id"] == user_id
2099- ]
2100- assert len (child_observations ) == 2
2101-
2102-
2103- def test_context_manager_session_propagation ():
2104- """Test that session context manager propagates session_id to child spans."""
2105- langfuse = Langfuse ()
2106-
2107- session_id = "test_session_456"
2108-
2109- with langfuse .start_as_current_span (name = "parent-span" ) as parent_span :
2110- with langfuse .propagate_attributes ({"session_id" : session_id }):
2111- trace_id = parent_span .trace_id
2112-
2113- # Create child spans that should inherit session_id
2114- child_span = langfuse .start_span (name = "child-span" )
2115- child_span .end ()
2116-
2117- # Create nested context to test multiple levels
2118- with langfuse .start_as_current_span (name = "nested-span" ):
2119- grandchild_span = langfuse .start_span (name = "grandchild-span" )
2120- grandchild_span .end ()
2121-
2122- langfuse .flush ()
2123- sleep (2 )
2124-
2125- # Verify trace has session_id
2126- trace = get_api ().trace .get (trace_id )
2127- assert trace .session_id == session_id
2128-
2129- # Verify nested spans were created
2130- nested_observations = [
2131- obs
2132- for obs in trace .observations
2133- if "span" in obs .name
2134- # Skip session.id validation as we currently drop it from the visible attributes server-side.
2135- # and obs.metadata["attributes"]["session.id"] == session_id
2136- ]
2137- assert len (nested_observations ) >= 2
2138-
2139-
2140- def test_context_manager_metadata_propagation ():
2141- """Test that metadata context manager propagates metadata to child spans."""
2142- langfuse = Langfuse ()
2143-
2144- with langfuse .start_as_current_span (name = "parent-span" ) as parent_span :
2145- with langfuse .propagate_attributes (
2146- {
2147- "experiment" : "A/B" ,
2148- "version" : "1.2.3" ,
2149- "feature_flag" : "enabled" ,
2150- }
2151- ):
2152- trace_id = parent_span .trace_id
2153-
2154- # Create child spans that should inherit metadata
2155- child_span = langfuse .start_span (name = "child-span" )
2156- child_span .end ()
2157-
2158- # Create generation that should inherit metadata
2159- generation = parent_span .start_generation (name = "child-generation" )
2160- generation .end ()
2161-
2162- langfuse .flush ()
2163- sleep (2 )
2164-
2165- # Verify trace has metadata
2166- trace = get_api ().trace .get (trace_id )
2167- assert trace .metadata ["experiment" ] == "A/B"
2168- assert trace .metadata ["version" ] == "1.2.3"
2169- assert trace .metadata ["feature_flag" ] == "enabled"
2170-
2171- # Verify all observations have the metadata distributed as individual keys
2172- for obs in trace .observations :
2173- if obs .name in ["child-span" , "child-generation" , "parent-span" ]:
2174- # Check that metadata was set on the observation
2175- assert hasattr (obs , "metadata" ), f"Observation { obs .name } missing metadata"
2176- assert (
2177- obs .metadata ["experiment" ] == "A/B"
2178- ), f"Observation { obs .name } missing experiment metadata"
2179- assert (
2180- obs .metadata ["version" ] == "1.2.3"
2181- ), f"Observation { obs .name } missing version metadata"
2182- assert (
2183- obs .metadata ["feature_flag" ] == "enabled"
2184- ), f"Observation { obs .name } missing feature_flag metadata"
2185-
2186-
2187- def test_context_manager_nested_contexts ():
2188- """Test nested context managers with overrides and merging."""
2189- langfuse = Langfuse ()
2190-
2191- with langfuse .start_as_current_span (name = "outer-span" ) as outer_span :
2192- with langfuse .propagate_attributes (
2193- {"user_id" : "user_1" , "session_id" : "session_1" }
2194- ):
2195- with langfuse .propagate_attributes ({"env" : "prod" , "region" : "us-east" }):
2196- outer_trace_id = outer_span .trace_id
2197-
2198- # Create span in outer context
2199- outer_child = langfuse .start_span (name = "outer-child" )
2200- outer_child .end ()
2201-
2202- nested_span = langfuse .start_span (name = "nested-span" )
2203- nested_span .end ()
2204-
2205- langfuse .flush ()
2206- sleep (2 )
2207-
2208- # Verify trace was created with nested spans
2209- trace = get_api ().trace .get (outer_trace_id )
2210-
2211- # Verify trace-level properties from the context
2212- assert trace .user_id == "user_1"
2213- assert trace .session_id == "session_1"
2214- assert trace .metadata ["env" ] == "prod"
2215- assert trace .metadata ["region" ] == "us-east"
2216-
2217- # Verify child observations were created
2218- child_observations = [
2219- obs for obs in trace .observations if "child" in obs .name or "nested" in obs .name
2220- ]
2221- assert len (child_observations ) >= 2
2222-
2223- # Verify specific child spans exist and have correct metadata
2224- outer_child_obs = [obs for obs in trace .observations if obs .name == "outer-child" ]
2225- nested_span_obs = [obs for obs in trace .observations if obs .name == "nested-span" ]
2226-
2227- assert len (outer_child_obs ) == 1 , "outer-child span should exist"
2228- assert len (nested_span_obs ) == 1 , "nested-span should exist"
2229-
2230-
2231- def test_context_manager_baggage_propagation ():
2232- """Test context managers with as_baggage=True for cross-service propagation."""
2233- langfuse = Langfuse ()
2234-
2235- # Test with baggage enabled (careful with sensitive data)
2236- with langfuse .start_as_current_span (name = "service-span" ) as span :
2237- with langfuse .propagate_attributes (
2238- {"session_id" : "public_session_789" }, as_baggage = True
2239- ):
2240- with langfuse .propagate_attributes (
2241- {"service" : "api" , "version" : "v1.0" }, as_baggage = True
2242- ):
2243- trace_id = span .trace_id
2244-
2245- # Create child spans that inherit baggage context
2246- child_span = langfuse .start_span (name = "external-call-span" )
2247- child_span .end ()
2248-
2249- langfuse .flush ()
2250- sleep (2 )
2251-
2252- # Verify trace properties were set
2253- trace = get_api ().trace .get (trace_id )
2254- assert trace .session_id == "public_session_789"
2255- assert trace .metadata ["service" ] == "api"
2256- assert trace .metadata ["version" ] == "v1.0"
2257-
2258- # Verify all observations have the metadata and session_id
2259- for obs in trace .observations :
2260- if obs .name in ["external-call-span" , "service-span" ]:
2261- # Check that metadata was set on the observation
2262- assert hasattr (obs , "metadata" ), f"Observation { obs .name } missing metadata"
2263- assert (
2264- obs .metadata ["service" ] == "api"
2265- ), f"Observation { obs .name } missing service metadata"
2266- assert (
2267- obs .metadata ["version" ] == "v1.0"
2268- ), f"Observation { obs .name } missing version metadata"
0 commit comments