Skip to content

Commit 9c40fc9

Browse files
committed
Adding TraceSampling to Examplar Sampler - Signed-off-by: Fabrice Scellos <fabrice.scellos@soprasteria.com>
Signed-off-by: Fabrice Scellos <fabrice.scellos@soprasteria.com>
1 parent 95872fc commit 9c40fc9

File tree

7 files changed

+74
-9
lines changed

7 files changed

+74
-9
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ simpleclient_pushgateway/mockserver.log
1313
simpleclient_pushgateway/mockserver_request.log
1414
nb-configuration.xml
1515
dependency-reduced-pom.xml
16+
17+
**/.classpath
18+
**.project
19+
**/.settings/

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,10 @@ public String getTraceId() {
7676
public String getSpanId() {
7777
return "span-id";
7878
}
79+
80+
@Override
81+
public boolean isSampled() {
82+
return true;
83+
}
7984
}
8085
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ public Exemplar sample(double value, double bucketFrom, double bucketTo, Exempla
4141

4242
private Exemplar doSample(double value, Exemplar previous) {
4343
long timestampMs = clock.currentTimeMillis();
44-
if (previous == null || previous.getTimestampMs() == null
45-
|| timestampMs - previous.getTimestampMs() > minRetentionIntervalMs) {
44+
if ((previous == null || previous.getTimestampMs() == null
45+
|| timestampMs - previous.getTimestampMs() > minRetentionIntervalMs)
46+
&& spanContextSupplier.isSampled()) {
4647
String spanId = spanContextSupplier.getSpanId();
4748
String traceId = spanContextSupplier.getTraceId();
4849
if (traceId != null && spanId != null) {

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

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package io.prometheus.client.exemplars;
22

3-
import io.prometheus.client.exemplars.tracer.common.SpanContextSupplier;
4-
import org.junit.Assert;
5-
import org.junit.Before;
6-
import org.junit.Test;
3+
import static java.lang.Double.NEGATIVE_INFINITY;
4+
import static java.lang.Double.POSITIVE_INFINITY;
75

86
import java.util.concurrent.atomic.AtomicLong;
97
import java.util.concurrent.atomic.AtomicReference;
108

11-
import static java.lang.Double.NEGATIVE_INFINITY;
12-
import static java.lang.Double.POSITIVE_INFINITY;
9+
import org.junit.Assert;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
import io.prometheus.client.exemplars.tracer.common.SpanContextSupplier;
1314

1415
public class DefaultExemplarSamplerTest {
1516

@@ -20,6 +21,7 @@ public class DefaultExemplarSamplerTest {
2021
final AtomicReference<String> spanId = new AtomicReference<String>();
2122
final AtomicLong timestamp = new AtomicLong();
2223
DefaultExemplarSampler defaultSampler;
24+
DefaultExemplarSampler defaultSamplerWithNoSample;
2325

2426
final SpanContextSupplier testContext = new SpanContextSupplier() {
2527
@Override
@@ -31,7 +33,30 @@ public String getTraceId() {
3133
public String getSpanId() {
3234
return spanId.get();
3335
}
34-
};
36+
37+
@Override
38+
public boolean isSampled() {
39+
return true;
40+
}
41+
};
42+
43+
final SpanContextSupplier testContextNoSample = new SpanContextSupplier() {
44+
45+
@Override
46+
public String getTraceId() {
47+
return traceId.get();
48+
}
49+
50+
@Override
51+
public String getSpanId() {
52+
return spanId.get();
53+
}
54+
55+
@Override
56+
public boolean isSampled() {
57+
return false;
58+
}
59+
};
3560

3661
final DefaultExemplarSampler.Clock testClock = new DefaultExemplarSampler.Clock() {
3762
@Override
@@ -46,6 +71,7 @@ public void setUp() {
4671
spanId.set("span-1");
4772
timestamp.set(System.currentTimeMillis());
4873
defaultSampler = new DefaultExemplarSampler(testContext, testClock);
74+
defaultSamplerWithNoSample = new DefaultExemplarSampler(testContextNoSample, testClock);
4975
}
5076

5177
@Test
@@ -68,6 +94,18 @@ public void testCounter() {
6894
Assert.assertNull(sampler.sample(4.0, null));
6995
}
7096

97+
@Test
98+
public void testCounterWithNoSample() {
99+
CounterExemplarSampler sampler = defaultSamplerWithNoSample;
100+
Assert.assertEquals(null, sampler.sample(2.0, null));
101+
}
102+
103+
@Test
104+
public void testHistogramWithNoSample() {
105+
HistogramExemplarSampler sampler = defaultSamplerWithNoSample;
106+
Assert.assertEquals(null, sampler.sample(2.0, NEGATIVE_INFINITY, POSITIVE_INFINITY, null));
107+
}
108+
71109
@Test
72110
public void testHistogram() {
73111
HistogramExemplarSampler sampler = defaultSampler;

simpleclient_tracer/simpleclient_tracer_common/src/main/java/io/prometheus/client/exemplars/tracer/common/SpanContextSupplier.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,11 @@ public interface SpanContextSupplier {
1111
* @return the current span id, or {@code null} if this call is not happening within a span context.
1212
*/
1313
String getSpanId();
14+
15+
/**
16+
* @return the state of the current Span. If this value is false a component before in the chain take the decision to not record it. Subsequent calling service have
17+
* to respect this value in order not to have partial TraceID with only some Span in it. This value is important to be sure to choose a recorded Trace in Examplar
18+
* sampling process
19+
*/
20+
boolean isSampled();
1421
}

simpleclient_tracer/simpleclient_tracer_otel/src/main/java/io/prometheus/client/exemplars/tracer/otel/OpenTelemetrySpanContextSupplier.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ public String getSpanId() {
3636
String spanId = Span.current().getSpanContext().getSpanId();
3737
return SpanId.isValid(spanId) ? spanId : null;
3838
}
39+
40+
@Override
41+
public boolean isSampled() {
42+
return Span.current().getSpanContext().isSampled();
43+
}
3944
}

simpleclient_tracer/simpleclient_tracer_otel_agent/src/main/java/io/prometheus/client/exemplars/tracer/otel_agent/OpenTelemetryAgentSpanContextSupplier.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ public String getSpanId() {
4141
String spanId = Span.current().getSpanContext().getSpanId();
4242
return SpanId.isValid(spanId) ? spanId : null;
4343
}
44+
45+
@Override
46+
public boolean isSampled() {
47+
return Span.current().getSpanContext().isSampled();
48+
}
4449
}

0 commit comments

Comments
 (0)