What is Trace Metadata?

Metadata provides additional context to your traces beyond the basic trace information. It helps you:

  • Filter and search for specific traces
  • Add business context to technical traces
  • Group related traces together
  • Categorize traces by environment, feature, or user segment

Metadata is key-value information that is attached to an entire trace, as opposed to individual spans.

Adding Metadata to Traces

You can add metadata to traces in two main ways:

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:

import { observe } from '@lmnr-ai/lmnr';

await observe({ 
    name: 'processRequest',
    metadata: { 
        environment: 'production',
        featureFlag: 'new-algorithm-v2' 
    }
}, async () => {
    // Your code here
});

Common Metadata Use Cases

Environment Information

Laminar.withMetadata({
    environment: 'production', // or 'staging', 'development', etc.
    region: 'us-west',
    deploymentId: 'deploy-123'
}, () => {
    // Your code here
});

Performance Tracking

Laminar.withMetadata({
    batchSize: 128,
    optimizerVersion: 'v2',
    modelVariant: 'high-performance'
}, () => {
    // Your code here
});

A/B Testing

Laminar.withMetadata({
    experimentId: 'exp-456',
    variant: 'treatment-B',
    cohort: 'new-users'
}, () => {
    // Your code here
});

Filtering Traces by Metadata

You can filter traces by metadata in the Laminar UI. Currently, we only support exact key-value matches, e.g. a trace with

{
    "metadata": {
        "userId": "123",
        "region": "us-west"
    }
}

can be matched by searching for userId=123 or region=us-west.

  1. Go to the Traces/Spans page
  2. Add a metadata filter and fill in the key and value

In the example below, we filter by userId key of the metadata:

Metadata vs Tags

Adding metadata to a trace is different from adding tags to a span:

MetadataTags
ScopeApplies to entire traceApplies to individual spans
PurposeGeneral trace contextSpecific span classification
ValidationAny string key-value pairsAny string
UI LocationShown in trace overviewShown in individual span details
Common UsesEnvironment info, user contextData categories, tags

To learn more about tags, see tags.

Best Practices

  1. Consistent Keys: Use consistent key names across your application
  2. Avoid Sensitive Data: Don’t include PII or sensitive data in metadata
  3. Keep It Lightweight: Only include metadata that adds meaningful context
  4. Use Hierarchical Keys: Consider using prefixes for related metadata (e.g., user_id, user_tier, user_region)
  5. Standardize Values: Use consistent formats and enumerations for values