@@ -339,7 +339,7 @@ private long durationUntilNextExemplarExpires(long now) {
339339
340340 private long updateCustomExemplar (int index , double value , Labels labels , long now ) {
341341 if (!labels .contains (Exemplar .TRACE_ID ) && !labels .contains (Exemplar .SPAN_ID )) {
342- labels = mergeLabels (labels , doSampleExemplar ());
342+ labels = mergeLabels (labels , sampleTraceContextLabels ());
343343 }
344344 customExemplars [index ] =
345345 Exemplar .builder ().value (value ).labels (labels ).timestampMillis (now ).build ();
@@ -358,6 +358,19 @@ private long updateExemplar(int index, double value, long now) {
358358 }
359359
360360 private Labels doSampleExemplar () {
361+ Labels labels = sampleTraceContextLabels ();
362+ if (labels .isEmpty ()) {
363+ return labels ;
364+ }
365+ // Per-metric supplier first (more specific), then the global supplier. On a name
366+ // collision the earlier (more specific) value is kept; the reserved trace_id/span_id
367+ // labels always win over both.
368+ labels = mergeAdditionalLabels (labels , additionalLabelsSupplier );
369+ labels = mergeAdditionalLabels (labels , ExemplarLabelsSupplier .getExemplarLabelsSupplier ());
370+ return labels ;
371+ }
372+
373+ private Labels sampleTraceContextLabels () {
361374 // Using the qualified name so that Micrometer can exclude the dependency on
362375 // prometheus-metrics-tracer-initializer
363376 // as they provide their own implementation of SpanContextSupplier.
@@ -374,14 +387,7 @@ private Labels doSampleExemplar() {
374387 String traceId = spanContext .getCurrentTraceId ();
375388 if (spanId != null && traceId != null ) {
376389 spanContext .markCurrentSpanAsExemplar ();
377- Labels labels = Labels .of (Exemplar .TRACE_ID , traceId , Exemplar .SPAN_ID , spanId );
378- // Per-metric supplier first (more specific), then the global supplier. On a name
379- // collision the earlier (more specific) value is kept; the reserved trace_id/span_id
380- // labels always win over both.
381- labels = mergeAdditionalLabels (labels , additionalLabelsSupplier );
382- labels =
383- mergeAdditionalLabels (labels , ExemplarLabelsSupplier .getExemplarLabelsSupplier ());
384- return labels ;
390+ return Labels .of (Exemplar .TRACE_ID , traceId , Exemplar .SPAN_ID , spanId );
385391 }
386392 }
387393 }
0 commit comments