@@ -51,7 +51,6 @@ const (
5151 SeparatorColon = ':'
5252
5353 traceIDTag = "trace_id"
54- spanIDTag = "span_id"
5554)
5655
5756// Option is a function that sets some option for the Factory constructor.
@@ -141,8 +140,16 @@ func (f *Factory) Counter(options metrics.Options) metrics.Counter {
141140 Help : help ,
142141 }
143142 cv := f .cache .getOrMakeCounterVec (opts , labelNames )
143+
144+ ctr := cv .WithLabelValues (f .tagsAsLabelValues (labelNames , tags )... )
145+ exemplarAdder , ok := ctr .(prometheus.ExemplarAdder )
146+ if ! ok {
147+ exemplarAdder = nil
148+ }
149+
144150 return & counter {
145- counter : cv .WithLabelValues (f .tagsAsLabelValues (labelNames , tags )... ),
151+ counter : ctr ,
152+ exemplarAdder : exemplarAdder ,
146153 }
147154}
148155
@@ -180,8 +187,14 @@ func (f *Factory) Timer(options metrics.TimerOptions) metrics.Timer {
180187 Buckets : asFloatBuckets (options .Buckets ),
181188 }
182189 hv := f .cache .getOrMakeHistogramVec (opts , labelNames )
190+ hist := hv .WithLabelValues (f .tagsAsLabelValues (labelNames , tags )... )
191+ exemplarObserver , ok := hist .(prometheus.ExemplarObserver )
192+ if ! ok {
193+ exemplarObserver = nil
194+ }
183195 return & timer {
184- histogram : hv .WithLabelValues (f .tagsAsLabelValues (labelNames , tags )... ),
196+ histogram : hist ,
197+ exemplarObserver : exemplarObserver ,
185198 }
186199}
187200
@@ -208,8 +221,14 @@ func (f *Factory) Histogram(options metrics.HistogramOptions) metrics.Histogram
208221 Buckets : options .Buckets ,
209222 }
210223 hv := f .cache .getOrMakeHistogramVec (opts , labelNames )
224+ hist := hv .WithLabelValues (f .tagsAsLabelValues (labelNames , tags )... )
225+ exemplarObserver , ok := hist .(prometheus.ExemplarObserver )
226+ if ! ok {
227+ exemplarObserver = nil
228+ }
211229 return & histogram {
212- histogram : hv .WithLabelValues (f .tagsAsLabelValues (labelNames , tags )... ),
230+ histogram : hist ,
231+ exemplarObserver : exemplarObserver ,
213232 }
214233}
215234
@@ -219,19 +238,20 @@ func (f *Factory) Namespace(scope metrics.NSOptions) metrics.Factory {
219238}
220239
221240type counter struct {
222- counter prometheus.Counter
241+ counter prometheus.Counter
242+ exemplarAdder prometheus.ExemplarAdder
223243}
224244
225245func (c * counter ) Inc (v int64 ) {
226246 c .counter .Add (float64 (v ))
227247}
228248
229249func (c * counter ) IncWithExemplar (v int64 , traceID string ) {
230- ctr , ok := c .counter .(prometheus.ExemplarAdder )
231- if ok {
250+ if c .exemplarAdder != nil {
232251 labels := make (map [string ]string , 1 )
233252 labels [traceIDTag ] = traceID
234- ctr .AddWithExemplar (float64 (v ), labels )
253+ c .exemplarAdder .AddWithExemplar (float64 (v ), labels )
254+ return
235255 }
236256 c .Inc (v )
237257}
@@ -249,38 +269,38 @@ type observer interface {
249269}
250270
251271type timer struct {
252- histogram observer
272+ histogram observer
273+ exemplarObserver prometheus.ExemplarObserver
253274}
254275
255276func (t * timer ) Record (v time.Duration ) {
256277 t .histogram .Observe (float64 (v .Nanoseconds ()) / float64 (time .Second / time .Nanosecond ))
257278}
258279
259280func (t * timer ) RecordWithExemplar (v time.Duration , traceID string ) {
260- hist , ok := t .histogram .(prometheus.ExemplarObserver )
261- if ok {
281+ if t .exemplarObserver != nil {
262282 labels := make (map [string ]string , 1 )
263283 labels [traceIDTag ] = traceID
264- hist .ObserveWithExemplar (float64 (v .Nanoseconds ())/ float64 (time .Second / time .Nanosecond ), labels )
284+ t . exemplarObserver .ObserveWithExemplar (float64 (v .Nanoseconds ())/ float64 (time .Second / time .Nanosecond ), labels )
265285 return
266286 }
267287 t .Record (v )
268288}
269289
270290type histogram struct {
271- histogram observer
291+ histogram observer
292+ exemplarObserver prometheus.ExemplarObserver
272293}
273294
274295func (h * histogram ) Record (v float64 ) {
275296 h .histogram .Observe (v )
276297}
277298
278299func (h * histogram ) RecordWithExemplar (v float64 , traceID string ) {
279- hist , ok := h .histogram .(prometheus.ExemplarObserver )
280- if ok {
300+ if h .exemplarObserver != nil {
281301 labels := make (map [string ]string , 1 )
282302 labels [traceIDTag ] = traceID
283- hist .ObserveWithExemplar (v , labels )
303+ h . exemplarObserver .ObserveWithExemplar (v , labels )
284304 return
285305 }
286306 h .Record (v )
0 commit comments