Skip to content

Commit

Permalink
Adding TraceSampling to Examplar Sampler - Signed-off-by: Fabrice Sce…
Browse files Browse the repository at this point in the history
…llos <fabrice.scellos@soprasteria.com>

Signed-off-by: Fabrice Scellos <fabrice.scellos@soprasteria.com>
  • Loading branch information
fscellos committed Feb 16, 2022
1 parent 95872fc commit 9c40fc9
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ simpleclient_pushgateway/mockserver.log
simpleclient_pushgateway/mockserver_request.log
nb-configuration.xml
dependency-reduced-pom.xml

**/.classpath
**.project
**/.settings/
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,10 @@ public String getTraceId() {
public String getSpanId() {
return "span-id";
}

@Override
public boolean isSampled() {
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ public Exemplar sample(double value, double bucketFrom, double bucketTo, Exempla

private Exemplar doSample(double value, Exemplar previous) {
long timestampMs = clock.currentTimeMillis();
if (previous == null || previous.getTimestampMs() == null
|| timestampMs - previous.getTimestampMs() > minRetentionIntervalMs) {
if ((previous == null || previous.getTimestampMs() == null
|| timestampMs - previous.getTimestampMs() > minRetentionIntervalMs)
&& spanContextSupplier.isSampled()) {
String spanId = spanContextSupplier.getSpanId();
String traceId = spanContextSupplier.getTraceId();
if (traceId != null && spanId != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package io.prometheus.client.exemplars;

import io.prometheus.client.exemplars.tracer.common.SpanContextSupplier;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static java.lang.Double.NEGATIVE_INFINITY;
import static java.lang.Double.POSITIVE_INFINITY;

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import static java.lang.Double.NEGATIVE_INFINITY;
import static java.lang.Double.POSITIVE_INFINITY;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

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

public class DefaultExemplarSamplerTest {

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

final SpanContextSupplier testContext = new SpanContextSupplier() {
@Override
Expand All @@ -31,7 +33,30 @@ public String getTraceId() {
public String getSpanId() {
return spanId.get();
}
};

@Override
public boolean isSampled() {
return true;
}
};

final SpanContextSupplier testContextNoSample = new SpanContextSupplier() {

@Override
public String getTraceId() {
return traceId.get();
}

@Override
public String getSpanId() {
return spanId.get();
}

@Override
public boolean isSampled() {
return false;
}
};

final DefaultExemplarSampler.Clock testClock = new DefaultExemplarSampler.Clock() {
@Override
Expand All @@ -46,6 +71,7 @@ public void setUp() {
spanId.set("span-1");
timestamp.set(System.currentTimeMillis());
defaultSampler = new DefaultExemplarSampler(testContext, testClock);
defaultSamplerWithNoSample = new DefaultExemplarSampler(testContextNoSample, testClock);
}

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

@Test
public void testCounterWithNoSample() {
CounterExemplarSampler sampler = defaultSamplerWithNoSample;
Assert.assertEquals(null, sampler.sample(2.0, null));
}

@Test
public void testHistogramWithNoSample() {
HistogramExemplarSampler sampler = defaultSamplerWithNoSample;
Assert.assertEquals(null, sampler.sample(2.0, NEGATIVE_INFINITY, POSITIVE_INFINITY, null));
}

@Test
public void testHistogram() {
HistogramExemplarSampler sampler = defaultSampler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ public interface SpanContextSupplier {
* @return the current span id, or {@code null} if this call is not happening within a span context.
*/
String getSpanId();

/**
* @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
* 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
* sampling process
*/
boolean isSampled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public String getSpanId() {
String spanId = Span.current().getSpanContext().getSpanId();
return SpanId.isValid(spanId) ? spanId : null;
}

@Override
public boolean isSampled() {
return Span.current().getSpanContext().isSampled();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public String getSpanId() {
String spanId = Span.current().getSpanContext().getSpanId();
return SpanId.isValid(spanId) ? spanId : null;
}

@Override
public boolean isSampled() {
return Span.current().getSpanContext().isSampled();
}
}

0 comments on commit 9c40fc9

Please sign in to comment.