Skip to content

How rules run

A rule has four parts. Its target is the set of spans it looks at. Its stage is when it runs. Its conditions are what a span must match, and its actions are what to do with a match. In the editor, a rule reads as a sentence: “When [any span] is [completed] and matches…, then…”.

Target Looks at
any span every span
a root span spans with no parent in this process: a trace’s true root, or the entry span of a request that came from another service
a child span spans whose parent is in this process

“Root” always means root within one process. When service A calls service B, B’s server span counts as a root for B’s rules.

Stage Runs Can see
created when the span starts its name, kind, start attributes, service and library, whether it’s a root, the current time
completed when the span ends, and again when its trace’s root ends everything above, plus end attributes, Span Duration and Has Error. Trace conditions run only in the root-end pass.

Use completed for anything you only know at the end. For example, HTTP instrumentation sets http.route and renames its span only as the span ends.

The trace conditions (Trace Duration, Trace Span Count and Spans In Trace) describe the whole trace. SpanSlice can check them only once the root ends, so the editor offers them only on completed rules.

Conditions sit in groups. Each group joins its conditions with and or or, and groups can nest. A rule with no conditions matches every span its target covers.

Condition Operators
Service Name, Span Name, Library Name is, is not, in, not in, contains, does not contain, starts with, matches regex
Span Attribute the text operators, plus exists, does not exist, =, >, >=, <, <=, between
Resource Attribute as Span Attribute, on the resource of the process that made the span
Span Attribute Key is, contains, starts with, matches regex, against every attribute key of the span and its trace
Span Kind is, is not, in, not in: Internal, Server, Client, Producer or Consumer
Span Duration, Trace Duration =, >, >=, <, <=, between
Has Error is true, is false
Trace Span Count =, >, >=, <, <=
Spans In Trace any, at least n, at most n, none, all, of the spans matching a nested condition
Current Time between two times of day, in the process’s local time zone

in and not in take a list of exact values. To fill the list, paste a column or a comma-separated list. Like the other negative operators, not in also matches a span that doesn’t have the value at all.

matches regex uses RE2 syntax. The pattern matches anywhere in the value unless you anchor it with ^ or $, and (?i) makes it case-insensitive. RE2 matches in time linear in the length of the value, so no pattern can stall your service on a crafted request. The trade-off is that RE2 has no lookahead, lookbehind or backreferences. SpanSlice rejects a pattern that uses them when you save the rule.

Library Name is the span’s instrumentation scope, such as @opentelemetry/instrumentation-knex.

Span Attribute reads the span’s own attribute first, then the trace’s attributes. Trace attributes come from Set Trace Attribute and from setTraceAttributes in your code. The rule sees the value the span would leave with. SpanSlice counts a trace attribute from the moment it’s set. A span that started before the attribute was set doesn’t see it in its created rules, but its completed rules do.

Span Attribute Key with is looks up one key, which costs the same as a Span Attribute exists. With contains, starts with or matches regex, it checks every attribute key on every span it reaches, which makes it the most expensive condition. SpanSlice evaluates those after the other conditions in its group. To limit the spans they run on, narrow the rule by span name or library first.

Rules run in order: the service’s own rules first, then the project’s. Every matching rule runs its actions. A later rule can override what an earlier one set. See sampling for how sample rates combine. Stop Rule Evaluation ends rule processing for that span at that stage.

The SDK holds every span in a trace until the trace’s local root ends. That lets a completed rule on the root see the whole trace, and lets sampling keep or drop the trace as one unit.

To bound memory, the SDK sends a trace early when it reaches 5,000 spans, when it’s 120 seconds old, or when more than 1,000 traces are open. An early-sent trace skips the root-end pass and uses the rate its created rules set. Spans that end after their trace was sent follow the trace’s decision for 30 seconds.

The SDK stamps each span with the rules it applied:

Attribute Meaning
spanslice.rules / spanslice.rule_names every rule that matched the span
spanslice.dropped_by / spanslice.dropped_by_name the rule whose Drop Span or Collapse Span removed it
spanslice.sampled_by / spanslice.sampled_by_name the rule that set the trace’s sample rate or dropped the trace, on every span of the trace

A rule’s name is optional. An unnamed rule uses a title generated from what it does, such as Drop traces with http.route = /healthz. The title shows in the app and in the *_name attributes, and it changes as you edit the rule. If a query or board depends on a rule’s name, give the rule a name.