Laminar.setTraceMetadata() must be called within an active span context (such as within an observe function call). If called outside of any span context, it will have no effect.
❌ Incorrect usage (will not work):
Copy
// This won't work because it's outside any span contextLaminar.setTraceMetadata({ environment: 'production' });await observe({ name: 'myFunction' }, async () => { // The metadata set above won't be applied here});
✅ Correct usage:
Copy
await observe({ name: 'myFunction' }, async () => { // Set metadata inside the span context Laminar.setTraceMetadata({ environment: 'production' }); // Your code here});
Use the Laminar.setTraceMetadata inside a span context to add metadata to the trace:
Copy
import { Laminar, observe } from '@lmnr-ai/lmnr';await observe( { name: 'processRequest', }, async () => { Laminar.setTraceMetadata({ environment: 'production', featureFlag: 'new-algorithm-v2', region: 'us-west' }); // ...rest of your code here },);
Alternatively, you can add metadata directly in the observe function:
Laminar.setTraceMetadata() must be called within an active span context (such as within an observe function call). If called outside of any span context, it will have no effect.
❌ Incorrect usage (will not work):
Copy
// This won't work because it's outside any span contextLaminar.setTraceMetadata({ environment: 'production' });await observe({ name: 'myFunction' }, async () => { // The metadata set above won't be applied here});
✅ Correct usage:
Copy
await observe({ name: 'myFunction' }, async () => { // Set metadata inside the span context Laminar.setTraceMetadata({ environment: 'production' }); // Your code here});
In Python, there are two recommended ways to add metadata, but both must be done within a span context:
from lmnr import Laminar, observe@observe()def my_function(): # IMPORTANT: set_metadata must be called within an active span context # Here, the @observe decorator creates that context Laminar.set_trace_metadata({ 'environment': 'production', 'feature_flag': 'new-algorithm-v2', 'region': 'us-west' }) # your code here
Laminar.set_trace_metadata() must be called within an active span context (such as within a function decorated with @observe() or inside a Laminar.start_as_current_span block). If called outside of any span context, it will have no effect.
from lmnr import Laminar# Note that this is static, i.e. you can't change this at runtime.@observe(metadata={ 'environment': 'production', 'feature_flag': 'new-algorithm-v2'})def process_request(): # Your code here pass
❌ Incorrect usage (will not work):
Copy
# This won't work because it's outside any span contextLaminar.set_trace_metadata({'environment': 'production'})@observe()def my_function(): # The metadata set above won't be applied here pass
✅ Correct usage:
Copy
@observe()def my_function(): # Set metadata inside the span context Laminar.set_trace_metadata({'environment': 'production'}) # Your code here
Any new call to set metadata will overwrite the previous metadata.
❌ Incorrect usage:
Copy
await observe({ name: 'myFunction' }, async () => { Laminar.setTraceMetadata({ environment: 'production' }); // This will overwrite the previous metadata including the environment Laminar.setTraceMetadata({ region: 'us-west' });});
@observe()def my_function(): Laminar.set_trace_metadata({'environment': 'production'}) # This will overwrite the previous metadata including the environment Laminar.set_trace_metadata({'region': 'us-west'})
Laminar.setTraceMetadata({ environment: 'production', // or 'staging', 'development', etc. region: 'us-west', deploymentId: 'deploy-123'});
Copy
Laminar.setTraceMetadata({ environment: 'production', // or 'staging', 'development', etc. region: 'us-west', deploymentId: 'deploy-123'});
Copy
@observe()def my_function(): Laminar.set_trace_metadata({ 'environment': 'production', # or 'staging', 'development', etc. 'region': 'us-west', 'deployment_id': 'deploy-123' }) # Your code here