Project: Grafana Tempo (Observability and Analysis / Observability)
File: span_metrics.md

aliases: /docs/tempo/latest/serversidemetrics/span_metrics/ /docs/tempo/latest/metrics-generator/span_metrics/ title: Span metrics description: The span metrics processor generates metrics from ingested tracing data, including request, error, and duration (RED) metrics. weight: 200 The span metrics processor generates metrics from ingested tracing data, including request, error, and duration (RED) metrics. Span metrics generate two metrics: A counter that computes requests A histogram that tracks the distribution of durations of all requests Span metrics are of particular interest if your system is not monitored with metrics, but it has distributed tracing implemented. You get out-of-the-box metrics from your tracing pipeline. {{< admonition type="note" >}} Metrics generation is disabled by default. Contact Grafana Support to enable metrics generation in your organization. {{% /admonition %}} Even if you already have metrics, span metrics can provide in-depth monitoring of your system. The generated metrics will show application level insight into your monitoring, as far as tracing gets propagated through your applications. Last but not least, span metrics lower the entry barrier for using . An exemplar is a specific trace representative of measurement taken in a given time interval. Since traces and metrics co-exist in the metrics-generator, exemplars can be automatically added, providing additional value to these metrics. To enable service graphs in Tempo/GET, enable the metrics generator and add an overrides section which enables the `span-metrics` generator. See . The span metrics processor works by inspecting every received span and computing the total count and the duration of spans for every unique combination of dimensions. Dimensions can be the service name, the operation, the span kind, the status code and any attribute present in the span. This processor is designed with the goal to mirror the implementation from the OpenTelemetry Collector of the with the same name. {{< admonition type="note" >}} To learn more about cardinality and how to perform a dry run of the metrics generator, see the . {{% /admonition %}} The following metrics are exported: | Metric | Type | Labels | Description | | | | - | - | | tracesspanmetricslatency | Histogram | Dimensions | Duration of the span | | tracesspanmetricscalls_total | Counter | Dimensions | Total count of the span | | tracesspanmetricssize_total | Counter | Dimensions | Total size of spans ingested | {{< admonition type="note" >}} In Tempo 1.4 and 1.4.1, the histogram metric was called `tracesspanmetricsduration_seconds`. This was changed later to be consistent with the metrics generated by Grafana Agent and the OpenTelemetry Collector. {{% /admonition %}} By default, the metrics processor adds the following labels to each metric: `service`, `spanname`, `spankind`, `statuscode`, `statusmessage`, `job`, and

`service` - The name of the service that generated the span `span_name` - The unique name of the span `span_kind` - The type of span, this can be one of five values: `SPANKINDSERVER` - The span was generated by a call from another service `SPANKINDCLIENT` - The span made a call to another service `SPANKINDINTERNAL` - The span does not have interaction outside of the service it was generated in `SPANKINDPUBLISHER` - The span created data that was pushed onto a bus or message broker `SPANKINDCONSUMER` - The span consumed data that was on a bus or messaging system `status_code` - The result of the span, this can be one of three values: `STATUSCODEUNSET` - Result of the span was unset/unknown `STATUSCODEOK` - The span operation completed successfully `STATUSCODEERROR` - The span operation completed with an error `statusmessage` (optionally enabled) - The message that details the reason for the `statuscode` label `job` - The name of the job, a combination of namespace and service; only added if `metricsgenerator.processor.spanmetrics.enabletargetinfo: true` `instance` - The instance ID; only added if `metricsgenerator.processor.spanmetrics.enabletargetinfo: true` Additional user defined labels can be created using the . When a configured dimension collides with one of the default labels (e.g. `statuscode`), the label for the respective dimension is prefixed with double underscore (i.e. `statuscode`). Custom labeling of dimensions is also supported using the . An optional metric called `tracestargetinfo` using all resource level attributes as dimensions can be enabled in the . If you use a ratio-based sampler, you can use the custom sampler below to not lose metric information. However, you also need to set `metricsgenerator.processor.spanmetrics.spanmultiplierkey` to `"X-SampleRatio"`. ```go package tracer import ( "go.opentelemetry.io/otel/attribute" tracesdk "go.opentelemetry.io/otel/sdk/trace" ) type RatioBasedSampler struct { innerSampler tracesdk.Sampler sampleRateAttribute attribute.KeyValue } func NewRatioBasedSampler(fraction float64) RatioBasedSampler { innerSampler := tracesdk.TraceIDRatioBased(fraction) return RatioBasedSampler{ innerSampler: innerSampler, sampleRateAttribute: attribute.Float64("X-SampleRatio", fraction), } } func (ds RatioBasedSampler) ShouldSample(parameters tracesdk.SamplingParameters) tracesdk.SamplingResult { sampler := ds.innerSampler result := sampler.ShouldSample(parameters) if result.Decision == tracesdk.RecordAndSample { result.Attributes = append(result.Attributes, ds.sampleRateAttribute) } return result } func (ds RatioBasedSampler) Description() string { return "Ratio Based Sampler which gives information about sampling ratio" } ``` In some cases, you may want to reduce the number of metrics produced by the `spanmetrics` processor. You can configure the processor to use an `include` filter to match criteria that must be present in the span in order to be included. Following the include filter, you can use an `exclude` filter to reject portions of what was previously included by the filter policy. Currently, only filtering by resource and span attributes with the following value types is supported. `bool` `double` `int` `string` Additionally, these intrinsic span attributes may be filtered upon: `name` `status` (code) `kind` The following intrinsic kinds are available for filtering. `SPANKINDSERVER` `SPANKINDINTERNAL` `SPANKINDCLIENT` `SPANKINDPRODUCER` `SPANKINDCONSUMER` Intrinsic keys can be acted on directly when implementing a filter policy. For example: ```yaml metrics_generator: processor: span_metrics: filter_policies: include: match_type: strict attributes: key: kind value: SPANKINDSERVER ``` In this example, spans which are of `kind` "server" are included for metrics export. When selecting spans based on non-intrinsic attributes, it is required to specify the scope of the attribute, similar to how it is specified in TraceQL. For example, if the `resource` contains a `location` attribute which is to be used in a filter policy, then the reference needs to be specified as `resource.location`. This requires users to know and specify which scope an attribute is to be found and avoids the ambiguity of conflicting values at differing scopes. The following may help illustrate. ```yaml metrics_generator: processor: span_metrics: filter_policies: include: match_type: strict attributes: key: resource.location value: earth ``` In the above examples,