This allows you to filter and search for traces by user ID later.
Copy
import { Laminar, observe } from '@lmnr-ai/lmnr';// Option 1. directly pass userId to observeawait observe({ name: 'processUserRequest', userId: 'user_123'}, async () => { // Process user request here});// Option 2.Use Laminar.setTraceUserIdawait observe({ name: 'processUserRequest',}, async () => { Laminar.setTraceUserId('user_123'); // Process user request here});
Copy
import { Laminar, observe } from '@lmnr-ai/lmnr';// Option 1. directly pass userId to observeawait observe({ name: 'processUserRequest', userId: 'user_123'}, async () => { // Process user request here});// Option 2.Use Laminar.setTraceUserIdawait observe({ name: 'processUserRequest',}, async () => { Laminar.setTraceUserId('user_123'); // Process user request here});
Copy
from lmnr import Laminar, observe# Option 1: Directly in the observe decorator# Note that this is static, i.e. you can't change this at runtime.@observe(user_id='user_123')def process_user_request(): # Process user request here pass# Option 2: Use Laminar.set_trace_user_id@observe()def process_user_request(req): # IMPORTANT: set_trace_user_id must be called within an active span context # Here, the @observe decorator creates that context Laminar.set_trace_user_id('user_123') # Process user request here
Another approach using start_as_current_span:
Copy
from lmnr import Laminardef process_user_request(user_id): with Laminar.start_as_current_span(name="process_request") as span: # Now we have an active span context Laminar.set_trace_user_id('user_123') # Your code here
❌ Incorrect usage (will not work):
Copy
# This won't work because it's outside any span contextLaminar.set_trace_user_id('user_123')@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_user_id('user_123') # Your code here
When including user IDs in traces, consider the following privacy practices:
Use anonymous or pseudonymous IDs rather than personally identifiable information
Consider your data retention policies
Use Laminar’s tracing level settings to control sensitive data
Follow relevant data protection regulations
Copy
// Use TracingLevel.META_ONLY for sensitive user operationsimport { withTracingLevel, TracingLevel } from "@lmnr-ai/lmnr";withTracingLevel(TracingLevel.META_ONLY, () => { // Sensitive operations here will only record metadata // Input/output content won't be saved});
Copy
// Use TracingLevel.META_ONLY for sensitive user operationsimport { withTracingLevel, TracingLevel } from "@lmnr-ai/lmnr";withTracingLevel(TracingLevel.META_ONLY, () => { // Sensitive operations here will only record metadata // Input/output content won't be saved});
Copy
from lmnr import Laminar, TracingLevelwith Laminar.set_tracing_level(TracingLevel.META_ONLY): # Sensitive operations here will only record metadata # Input/output content won't be saved