Skip to content
This repository was archived by the owner on Nov 17, 2025. It is now read-only.

Commit 8232f8c

Browse files
cnnradamsMrAliasc24tbogdandrutu
authored
Integrate Exemplars with Metrics SDK (#113)
* Exemplar OTEP * Wording fixes Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * stats updates, specify parameters/output format * aggregation -> aggregator, other small changes * gauge -> lastvalue * Update text/metrics/0113-exemplars.md Co-authored-by: Chris Kleinknecht <libc@google.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Chris Kleinknecht <libc@google.com> Co-authored-by: Bogdan Drutu <bogdandrutu@gmail.com>
1 parent b2385e3 commit 8232f8c

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

text/metrics/0113-exemplars.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Integrate Exemplars with Metrics
2+
3+
This OTEP adds exemplar support to aggregations defined in the Metrics SDK.
4+
5+
## Definition
6+
7+
Exemplars are example data points for aggregated data. They provide specific context to otherwise general aggregations. For histogram-type metrics, exemplars are points associated with each bucket in the histogram giving an example of what was aggregated into the bucket. Exemplars are augmented beyond just measurements with references to the sampled trace where the measurement was recorded and labels that were attached to the measurement.
8+
9+
## Motivation
10+
11+
Defining exemplar behaviour for aggregations allows OpenTelemetry to support exemplars in Google Cloud Monitoring.
12+
13+
Exemplars provide a link between metrics and traces. Consider a user using a Histogram aggregation to track response latencies over time for a high QPS server. The histogram is composed of buckets based on the speed of the request, for example, "there were 55 requests that took 400-500 milliseconds". The user wants to troubleshoot slow requests, so they would need to find a trace where the latency was high. With exemplars, the user is able to get an exemplar trace from a high latency bucket, an exemplar trace from a low latency bucket, and compare them to figure out the reason for the high latency.
14+
15+
Exemplars are meaningful for all aggregations where relevant traces can provide more context to the aggregation, as well as when exemplars can display specific information not otherwise shown in the aggregation (for example, the full set of labels where they otherwise might be aggregated away).
16+
17+
## Internal details
18+
19+
An exemplar is a `RawValue`, which is defined as:
20+
21+
```
22+
message RawValue {
23+
// Numerical value of the measurement that was recorded. Only one of these two fields is
24+
// used for the data, depending on its type
25+
double double_value = 0;
26+
int64 int64_value = 1;
27+
28+
// Exact time that the measurement was recorded
29+
fixed64 time_unix_nano = 2;
30+
31+
// 'label:value' map of all labels that were provided by the user recording the measurement
32+
repeated opentelemetry.proto.common.v1.StringKeyValue labels = 3;
33+
34+
// Span ID of the current trace
35+
optional bytes span_id = 4;
36+
37+
// Trace ID of the current trace
38+
optional bytes trace_id = 5;
39+
40+
// When sample_count is non-zero, this exemplar has been chosen in a statistically
41+
// unbiased way such that the exemplar is representative of `sample_count` individual events
42+
optional double sample_count = 6;
43+
}
44+
```
45+
46+
Exemplar collection should be enabled through an optional parameter (disabled by default), and when not enabled, there should be no collection/logic performed related to exemplars. This is to ensure that when necessary, aggregators are as high performance as possible. Aggregators should also have a parameter to determine whether exemplars should only be collected if they are recorded during a sampled trace, or if tracing should have no effect on which exemplars are sampled. This allows aggregations to prioritize either the link between metrics and traces or the statistical significance of exemplars, when necessary.
47+
48+
[#347](https://github.com/open-telemetry/opentelemetry-specification/pull/347) describes a set of standard aggregators in the metrics SDK. Here we describe how exemplars could be implemented for each aggregator.
49+
50+
### Exemplar behaviour for standard aggregators
51+
52+
#### HistogramAggregator
53+
54+
The HistogramAggregator MUST (when enabled) maintain a list of exemplars whose values are distributed across all buckets of the histogram (there should be one or more exemplars in every bucket that has a population of at least one sample-able measurement). Implementations SHOULD NOT retain an unbounded number of exemplars.
55+
56+
#### Sketch
57+
58+
A Sketch aggregator SHOULD maintain a list of exemplars whose values are spaced out across the distribution. There is no specific number of exemplars that should be retained (although the amount SHOULD NOT be unbounded), but the implementation SHOULD pick exemplars that represent as much of the distribution as possible. (Specific details not defined, see open questions.)
59+
60+
#### Last-Value
61+
62+
Most (if not all) Last-Value aggregators operate asynchronously and do not ever interact with context. Since the value of a Last-Value is the last measurement (essentially the other parts of an exemplar), exemplars are not worth implementing for Last-Value.
63+
64+
#### Exact
65+
66+
The Exact aggregator will function by maintaining a list of `RawValue`s, which contain all of the information exemplars would carry. Therefore the Exact aggregator will not need to maintain any exemplars.
67+
68+
#### Counter
69+
70+
Exemplars give value to counter aggregations in two ways: One, by tying metric and trace data together, and two, by providing necessary information to re-create the input distribution. When enabled, the aggregator will retain a bounded list of exemplars at each checkpoint, sampled from across the distribution of the data. Exemplars should be sampled in a statistically significant way.
71+
72+
#### MinMaxSumCount
73+
74+
Similar to Counter, MinMaxSumCount should retain a bounded list of exemplars that were sampled from across the input distribution in a statistically significant way.
75+
76+
#### Custom Aggregators
77+
78+
Custom aggregators MAY support exemplars by maintaining a list of exemplars that can be retrieved by exporters. Custom aggregators should select exemplars based on their usage by the connected exporter (for example, exemplars recorded for Google Cloud Monitoring should only be retained if they were recorded within a sampled trace).
79+
80+
Exemplars will always be retrieved from aggregations (by the exporter) as a list of RawValue objects. They will be communicated via a
81+
82+
```
83+
optional repeated RawValue exemplars = 6
84+
```
85+
86+
attribute on the `Metric` object.
87+
88+
## Trade-offs and mitigations
89+
90+
Performance (in terms of memory usage and to some extent time complexity) is the main concern of implementing exemplars. However, by making recording exemplars optional, there should be minimal overhead when exemplars are not enabled.
91+
92+
## Prior art and alternatives
93+
94+
Exemplars are implemented in [OpenCensus](https://github.com/census-instrumentation/opencensus-specs/blob/master/stats/Exemplars.md#exemplars), but only for HistogramAggregator. This OTEP is largely a port from the OpenCensus definition of exemplars, but it also adds exemplar support to other aggregators.
95+
96+
[Cloud monitoring API doc for exemplars](https://cloud.google.com/monitoring/api/ref_v3/rpc/google.api#google.api.Distribution.Exemplar)
97+
98+
## Open questions
99+
100+
- Exemplars usually refer to a span in a sampled trace. While using the collector to perform tail-sampling, the sampling decision may be deferred until after the metric would be exported. How do we create exemplars in this case?
101+
102+
- We don’t have a strong grasp on how the sketch aggregator works in terms of implementation - so we don’t have enough information to design how exemplars should work properly.
103+
104+
- The spec doesn't yet define a standard set of aggregations, just default aggregations for standard metric instruments. Since exemplars are always attached to particular aggregations, it's impossible to fully specify the behavior of exemplars.

0 commit comments

Comments
 (0)