Skip to content

Commit e951d7b

Browse files
authored
Merge pull request #708 from prometheus/beorn7/exemplars
Pull out ...WithExemplar methods into separate interfaces
2 parents ba79017 + f34b098 commit e951d7b

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

examples/random/main.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,13 @@ func main() {
9090
for {
9191
v := (rand.NormFloat64() * *normDomain) + *normMean
9292
rpcDurations.WithLabelValues("normal").Observe(v)
93-
rpcDurationsHistogram.ObserveWithExemplar(
94-
// Demonstrate exemplar support with a dummy ID. This would be
95-
// something like a trace ID in a real application.
93+
// Demonstrate exemplar support with a dummy ID. This
94+
// would be something like a trace ID in a real
95+
// application. Note the necessary type assertion. We
96+
// already know that rpcDurationsHistogram implements
97+
// the ExemplarObserver interface and thus don't need to
98+
// check the outcome of the tipe assertion.
99+
rpcDurationsHistogram.(prometheus.ExemplarObserver).ObserveWithExemplar(
96100
v, prometheus.Labels{"dummyID": fmt.Sprint(rand.Intn(100000))},
97101
)
98102
time.Sleep(time.Duration(75*oscillationFactor()) * time.Millisecond)

prometheus/counter.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,18 @@ type Counter interface {
4141
// Add adds the given value to the counter. It panics if the value is <
4242
// 0.
4343
Add(float64)
44-
// AddWithExemplar works like Add but also replaces the currently saved
45-
// exemplar (if any) with a new one, created from the provided value,
46-
// the current time as timestamp, and the provided labels. Empty Labels
47-
// will lead to a valid (label-less) exemplar. But if Labels is nil, the
48-
// current exemplar is left in place. This method panics if the value is
49-
// < 0, if any of the provided labels are invalid, or if the provided
50-
// labels contain more than 64 runes in total.
44+
}
45+
46+
// ExemplarAdder is implemented by Counters that offer the option of adding a
47+
// value to the Counter together with an exemplar. Its AddWithExemplar method
48+
// works like the Add method of the Counter interface but also replaces the
49+
// currently saved exemplar (if any) with a new one, created from the provided
50+
// value, the current time as timestamp, and the provided labels. Empty Labels
51+
// will lead to a valid (label-less) exemplar. But if Labels is nil, the current
52+
// exemplar is left in place. AddWithExemplar panics if the value is < 0, if any
53+
// of the provided labels are invalid, or if the provided labels contain more
54+
// than 64 runes in total.
55+
type ExemplarAdder interface {
5156
AddWithExemplar(value float64, exemplar Labels)
5257
}
5358

@@ -56,6 +61,9 @@ type CounterOpts Opts
5661

5762
// NewCounter creates a new Counter based on the provided CounterOpts.
5863
//
64+
// The returned implementation also implements ExemplarAdder. It is safe to
65+
// perform the corresponding type assertion.
66+
//
5967
// The returned implementation tracks the counter value in two separate
6068
// variables, a float64 and a uint64. The latter is used to track calls of the
6169
// Inc method and calls of the Add method with a value that can be represented

prometheus/histogram.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,6 @@ type Histogram interface {
4848

4949
// Observe adds a single observation to the histogram.
5050
Observe(float64)
51-
// ObserveWithExemplar works like Observe but also replaces the
52-
// currently saved exemplar for the relevant bucket (possibly none) with
53-
// a new one, created from the provided value, the current time as
54-
// timestamp, and the provided Labels. Empty Labels will lead to a valid
55-
// (label-less) exemplar. But if Labels is nil, the current exemplar in
56-
// the relevant bucket is left in place. This method panics if any of
57-
// the provided labels are invalid or if the provided labels contain
58-
// more than 64 runes in total.
59-
ObserveWithExemplar(value float64, exemplar Labels)
6051
}
6152

6253
// bucketLabel is used for the label that defines the upper bound of a
@@ -161,6 +152,10 @@ type HistogramOpts struct {
161152

162153
// NewHistogram creates a new Histogram based on the provided HistogramOpts. It
163154
// panics if the buckets in HistogramOpts are not in strictly increasing order.
155+
//
156+
// The returned implementation also implements ExemplarObserver. It is safe to
157+
// perform the corresponding type assertion. Exemplars are tracked separately
158+
// for each bucket.
164159
func NewHistogram(opts HistogramOpts) Histogram {
165160
return newHistogram(
166161
NewDesc(

prometheus/histogram_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func TestHistogramConcurrency(t *testing.T) {
189189
if n%2 == 0 {
190190
sum.Observe(v)
191191
} else {
192-
sum.ObserveWithExemplar(v, Labels{"foo": "bar"})
192+
sum.(ExemplarObserver).ObserveWithExemplar(v, Labels{"foo": "bar"})
193193
}
194194
}
195195
end.Done()

prometheus/observer.go

+12
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,15 @@ type ObserverVec interface {
5050

5151
Collector
5252
}
53+
54+
// ExemplarObserver is implemented by Observers that offer the option of
55+
// observing a value together with an exemplar. Its ObserveWithExemplar method
56+
// works like the Observe method of an Observer but also replaces the currently
57+
// saved exemplar (if any) with a new one, created from the provided value, the
58+
// current time as timestamp, and the provided Labels. Empty Labels will lead to
59+
// a valid (label-less) exemplar. But if Labels is nil, the current exemplar is
60+
// left in place. ObserveWithExemplar panics if any of the provided labels are
61+
// invalid or if the provided labels contain more than 64 runes in total.
62+
type ExemplarObserver interface {
63+
ObserveWithExemplar(value float64, exemplar Labels)
64+
}

0 commit comments

Comments
 (0)