Skip to content

Commit 8ce5a53

Browse files
committed
add Exemplar support for OpenTelemetry tracing (review)
1 parent d8fbf66 commit 8ce5a53

File tree

14 files changed

+141
-132
lines changed

14 files changed

+141
-132
lines changed

benchmark/src/main/java/io/prometheus/benchmark/CounterBenchmark.java benchmark/src/main/java/io/prometheus/client/benchmark/CounterBenchmark.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.prometheus.benchmark;
1+
package io.prometheus.client.benchmark;
22

33
import com.codahale.metrics.MetricRegistry;
44

benchmark/src/main/java/io/prometheus/benchmark/ExemplarsBenchmark.java benchmark/src/main/java/io/prometheus/client/benchmark/ExemplarsBenchmark.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.prometheus.benchmark;
1+
package io.prometheus.client.benchmark;
22

33
import io.prometheus.client.Counter;
44
import io.prometheus.client.exemplars.DefaultExemplarSampler;

benchmark/src/main/java/io/prometheus/benchmark/GaugeBenchmark.java benchmark/src/main/java/io/prometheus/client/benchmark/GaugeBenchmark.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.prometheus.benchmark;
1+
package io.prometheus.client.benchmark;
22

33
import com.codahale.metrics.MetricRegistry;
44

benchmark/src/main/java/io/prometheus/benchmark/SummaryBenchmark.java benchmark/src/main/java/io/prometheus/client/benchmark/SummaryBenchmark.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.prometheus.benchmark;
1+
package io.prometheus.client.benchmark;
22

33
import com.codahale.metrics.MetricRegistry;
44

integration_tests/pom.xml

+11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@
2828
<module>java_versions</module>
2929
</modules>
3030

31+
<build>
32+
<pluginManagement>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-failsafe-plugin</artifactId>
37+
<version>2.22.2</version>
38+
</plugin>
39+
</plugins>
40+
</pluginManagement>
41+
</build>
3142
<dependencyManagement>
3243
<dependencies>
3344
<dependency>

simpleclient/src/main/java/io/prometheus/client/Counter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public void inc(double amt) {
207207
*
208208
* @param amt same as in {@link #inc(double)}
209209
* @param exemplarLabels list of name/value pairs, as documented in {@link Exemplar#Exemplar(double, String...)}.
210-
* A commonly used name is {@link Exemplar#TRACE_ID}.
210+
* A commonly used name is {@code "trace_id"}.
211211
* Calling {@code incWithExemplar(amt)} means that an exemplar without labels will be created.
212212
* Calling {@code incWithExemplar(amt, (String[]) null)} is equivalent
213213
* to calling {@code inc(amt)}.

simpleclient/src/main/java/io/prometheus/client/Histogram.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ public void observe(double amt) {
366366
*
367367
* @param amt same as in {@link #observe(double)} (double)}
368368
* @param exemplarLabels list of name/value pairs, as documented in {@link Exemplar#Exemplar(double, String...)}.
369-
* A commonly used name is {@link Exemplar#TRACE_ID}.
369+
* A commonly used name is {@code "trace_id"}.
370370
* Calling {@code observeWithExemplar(amt)} means that an exemplar without labels is created.
371371
* Calling {@code observeWithExemplar(amt, (String[]) null)} is equivalent
372372
* to calling {@code observe(amt)}.

simpleclient/src/main/java/io/prometheus/client/exemplars/DefaultExemplarSampler.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
import io.prometheus.client.exemplars.tracer.common.SpanContextSupplier;
44

5-
import static io.prometheus.client.exemplars.Exemplar.SPAN_ID;
6-
import static io.prometheus.client.exemplars.Exemplar.TRACE_ID;
7-
85
/**
96
* Default Exemplar sampler.
107
* <p/>
118
* Keeps each Exemplar for a minimum of ~7 seconds, then samples a new one.
129
*/
1310
public class DefaultExemplarSampler implements ExemplarSampler {
1411

12+
private static final String SPAN_ID = "span_id";
13+
private static final String TRACE_ID = "trace_id";
14+
1515
private final SpanContextSupplier spanContextSupplier;
1616
// Choosing a prime number for the retention interval makes behavior more predictable,
1717
// because it is unlikely that retention happens at the exact same time as a Prometheus scrape.
@@ -43,10 +43,10 @@ private Exemplar doSample(double value, Exemplar previous) {
4343
long timestampMs = clock.currentTimeMillis();
4444
if (previous == null || previous.getTimestampMs() == null
4545
|| timestampMs - previous.getTimestampMs() > minRetentionIntervalMs) {
46-
String traceId = spanContextSupplier.getTraceId();
4746
String spanId = spanContextSupplier.getSpanId();
47+
String traceId = spanContextSupplier.getTraceId();
4848
if (traceId != null && spanId != null) {
49-
return new Exemplar(value, timestampMs, TRACE_ID, traceId, SPAN_ID, spanId);
49+
return new Exemplar(value, timestampMs, SPAN_ID, spanId, TRACE_ID, traceId);
5050
}
5151
}
5252
return null;

simpleclient/src/main/java/io/prometheus/client/exemplars/Exemplar.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@
99
*/
1010
public class Exemplar {
1111

12-
public static final String TRACE_ID = "trace_id";
13-
public static final String SPAN_ID = "span_id";
14-
1512
private final String[] labels;
1613
private final double value;
1714
private final Long timestampMs;
1815

19-
private static final Pattern labelNameRegex = Pattern.compile("[a-zA-Z_]\\w*"); // \w is [a-zA-Z_0-9]
16+
private static final Pattern labelNameRegex = Pattern.compile("[a-zA-Z_][a-zA-Z_0-9]*");
2017

2118
/**
2219
* Create an Exemplar without a timestamp

simpleclient/src/main/java/io/prometheus/client/exemplars/ExemplarConfig.java

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public class ExemplarConfig {
1818
}
1919

2020
/**
21+
* Set the exemplar sampler used when building Counters.
22+
* <p>
23+
* This must be called before a Counter is created.
24+
*
2125
* @param counterExemplarSampler will be used by default when creating new {@code Counter} metrics,
2226
* unless {@link #disableExemplarSamplers()} was called.
2327
*/
@@ -29,6 +33,10 @@ public static void setCounterExemplarSampler(CounterExemplarSampler counterExemp
2933
}
3034

3135
/**
36+
* Set the exemplar sampler used when building Histograms.
37+
* <p>
38+
* This must be called before a Histogram is created.
39+
*
3240
* @param histogramExemplarSampler will be used by default when creating new {@code Histogram} metrics,
3341
* unless {@link #disableExemplarSamplers()} was called.
3442
*/
@@ -41,8 +49,11 @@ public static void setHistogramExemplarSampler(HistogramExemplarSampler histogra
4149

4250
/**
4351
* Disable the implicit exemplar samplers by default.
52+
* <p>
4453
* Exemplars can still be enabled for individual metrics in the metric builder,
4554
* or they can be provided explicitly for individual observations with the {@code ...withExemplar()} methods.
55+
* <p>
56+
* This must be called before a Counter or a Histogram is created.
4657
*/
4758
public static void disableExemplarSamplers() {
4859
enabled = false;

simpleclient/src/test/java/io/prometheus/client/exemplars/DefaultExemplarSamplerTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
import java.util.concurrent.atomic.AtomicLong;
99
import java.util.concurrent.atomic.AtomicReference;
1010

11-
import static io.prometheus.client.exemplars.Exemplar.SPAN_ID;
12-
import static io.prometheus.client.exemplars.Exemplar.TRACE_ID;
1311
import static java.lang.Double.NEGATIVE_INFINITY;
1412
import static java.lang.Double.POSITIVE_INFINITY;
1513

1614
public class DefaultExemplarSamplerTest {
1715

16+
private static final String SPAN_ID = "span_id";
17+
private static final String TRACE_ID = "trace_id";
18+
1819
final AtomicReference<String> traceId = new AtomicReference<String>();
1920
final AtomicReference<String> spanId = new AtomicReference<String>();
2021
final AtomicLong timestamp = new AtomicLong();

simpleclient/src/test/java/io/prometheus/client/exemplars/ExemplarTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
import java.util.Map;
88
import java.util.TreeMap;
99

10-
import static io.prometheus.client.exemplars.Exemplar.SPAN_ID;
11-
import static io.prometheus.client.exemplars.Exemplar.TRACE_ID;
12-
1310
public class ExemplarTest {
1411

12+
private final String SPAN_ID = "span_id";
13+
private final String TRACE_ID = "trace_id";
14+
1515
@Test
1616
public void testCompleteExemplar() {
1717
double value = 42;

simpleclient_common/src/main/java/io/prometheus/client/exporter/common/TextFormat.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public static void writeOpenMetrics100(Writer writer, Enumeration<Collector.Metr
267267
writer.write("# EOF\n");
268268
}
269269

270-
private static void omWriteTimestamp(Writer writer, long timestampMs) throws IOException {
270+
static void omWriteTimestamp(Writer writer, long timestampMs) throws IOException {
271271
writer.write(Long.toString(timestampMs / 1000L));
272272
writer.write(".");
273273
long ms = timestampMs % 1000;

0 commit comments

Comments
 (0)