Use the Laminar.withMetadata wrapper to add metadata to all traces created within its scope:
import { Laminar } from '@lmnr-ai/lmnr';Laminar.withMetadata({ environment: 'production', featureFlag: 'new-algorithm-v2', region: 'us-west'}, () => { // All traces created inside this function will have the metadata});
Alternatively, you can add metadata directly in the observe function:
Use the Laminar.withMetadata wrapper to add metadata to all traces created within its scope:
import { Laminar } from '@lmnr-ai/lmnr';Laminar.withMetadata({ environment: 'production', featureFlag: 'new-algorithm-v2', region: 'us-west'}, () => { // All traces created inside this function will have the metadata});
Alternatively, you can add metadata directly in the observe function:
Option 1: Using set_metadata 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_metadata({ 'environment': 'production', 'feature_flag': 'new-algorithm-v2', 'region': 'us-west' }) # your code here # Optionally, at the end of the trace, you can clear the metadata Laminar.clear_metadata()
Option 2: Directly in the observe decorator (preferred method)
from lmnr import Laminar, observe@observe(metadata={ 'environment': 'production', 'feature_flag': 'new-algorithm-v2', 'region': 'us-west'})def my_function(): # Your code here pass
In Python, Laminar.set_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 Laminardef process_request(): with Laminar.start_as_current_span(name="process_request") as span: # Now we have an active span context Laminar.set_metadata({ 'environment': 'production', 'feature_flag': 'new-algorithm-v2' }) # Your code here # Clear when done if needed Laminar.clear_metadata()
❌ Incorrect usage (will not work):
# This won't work because it's outside any span contextLaminar.set_metadata({'environment': 'production'})@observe()def my_function(): # The metadata set above won't be applied here pass
✅ Correct usage:
@observe()def my_function(): # Set metadata inside the span context Laminar.set_metadata({'environment': 'production'}) # Your code here
Laminar.withMetadata({ environment: 'production', // or 'staging', 'development', etc. region: 'us-west', deploymentId: 'deploy-123'}, () => { // Your code here});
Laminar.withMetadata({ environment: 'production', // or 'staging', 'development', etc. region: 'us-west', deploymentId: 'deploy-123'}, () => { // Your code here});
@observe()def my_function(): Laminar.set_metadata({ 'environment': 'production', # or 'staging', 'development', etc. 'region': 'us-west', 'deployment_id': 'deploy-123' }) # Your code here