Adding metadata to traces

It is often useful to add additional information to traces. For example, you may want to add the user ID or the environment name (staging, production, etc.) to the trace.

You can do this by adding metadata to the trace.

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

Laminar.withMetadata({
    userId: '123',
    environment: 'production'
}, () => {
    // your code here
    // you can `observe` multiple spans here
})

Filtering traces by metadata

You can filter traces by metadata in the Laminar UI. Go to the traces page and select the spans tab. Add a filter by metadata, and enter the name and value in the format metadata_key=metadata_value.

Metadata vs labels

Adding metadata to a trace is different from adding a label. To learn more about adding labels programmatically, see labels.

Here’s a brief comparison of the two:

MetadataLabels
ScopeApplies to entire traceApplies to individual spans
PurposeGeneral trace context (e.g., environment, user ID)Specific span classification or tagging
ConfigurationSet directly in codeRequires pre-configured label classes in UI
ValidationAny string key-value pairs allowedMust match predefined label names and values
UI LocationShown in trace overviewShown in individual span details
Common UsesEnvironment info, user context, request IDsQuality scores, data categories