What you’ll need
- An OpenTelemetry Collector instance.
- One or more instrumented services that send OTLP (gRPC or HTTP) to the collector.
- The demo services in this article (auth, flight, payment) or any services that produce spans to validate filter behavior.
Starting Collector configuration
Below is a minimal collector configuration used for this demo. It receives OTLP on the standard ports and prints telemetry to thedebug exporter so you can easily validate filtering behavior.
./app.sh) to generate spans from each service. The collector debug exporter prints spans to the console and is useful for verifying whether OTTL rules are including or dropping spans as expected.
Adding the OTTL filter processor
To filter spans with OTTL, add thefilter/ottl processor and configure rules under the traces context. The processor has an error_mode setting that determines how OTTL evaluation errors are handled:
Three
error_mode options:ignore: Log errors and continue evaluating other rules (no telemetry dropped due to OTTL evaluation errors).silent: Do not log errors and continue evaluating other rules.propagate: Stop processing and drop the telemetry when an OTTL error occurs.
traces section lists rule categories (e.g., span) and each rule is an OTTL expression. If an expression evaluates to true for a span, that span will be dropped by the filter processor.
Filter by resource attribute (service name)
Drop spans originating from a specific service by matching theresource.attributes["service.name"] resource attribute:
Filter by span name
Match spans by their name. When writing YAML, wrap the OTTL expression in single quotes and use double quotes inside as needed:validate card across all services.
Filter by span attributes
OTTL can read span attributes for flexible filtering. Examples: Match a single attribute value:- OR example (drop spans for username
johnormary):
- AND example (both conditions must match to drop the span):
Regular expression matching with IsMatch
UseIsMatch to evaluate regex patterns. For example, to drop spans where the username is exactly john or mary:
(?i)).
Negation (not)
Invert conditions usingnot. If the inner expression contains logical operators, wrap it in parentheses:
john and email john@gmail.com.
Quick reference: common OTTL span filters
| Goal | OTTL expression (YAML string) | |
|---|---|---|
Drop spans from flight-service | 'resource.attributes["service.name"] == "flight-service"' | |
Drop spans named validate card | 'name == "validate card"' | |
Drop spans with username john or mary (OR) | 'attributes["username"] == "john" or attributes["username"] == "mary"' | |
| Drop spans matching regex on username | `‘IsMatch(attributes[“username”], ”^(john | mary)$”)‘` |
Keep only flight-service (drop others) | 'resource.attributes["service.name"] != "flight-service"' |
Be careful with
error_mode: propagate. If an OTTL rule errors under propagate, telemetry may be dropped. Use ignore during testing to avoid inadvertently losing data.Putting it into the Collector pipeline (complete example)
A typical configuration that applies a simple username regex-based drop looks like this:./app.sh) to validate that matching spans are filtered. Inspect the collector logs and the debug exporter output to confirm behavior.
Summary
- OTTL lets you write expressive, targeted rules to drop spans based on span properties (name, trace ID, span ID), resource attributes (for example
service.name), and span attributes. - Use logical operators (
and,or,not) and regex viaIsMatchfor flexible matching. error_modedetermines collector behavior on OTTL evaluation errors:ignore,silent, orpropagate.- The same OTTL concepts apply to metrics and logs; use the appropriate context (
metrics,logs) within the processor configuration.
Links and References
- OpenTelemetry Collector – Processors
- OTTL (OpenTelemetry Transformation Language) reference
- OpenTelemetry Python - OTLP exporter