Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding TraceSampling to Examplar Sampler #766

Merged
merged 3 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
12 changes: 7 additions & 5 deletions integration_tests/it_exemplars_otel_agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_bom</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand All @@ -33,11 +40,6 @@
<artifactId>okhttp</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public ExemplarsOpenTelemetryAgentIT() throws IOException, URISyntaxException {
"java",
"-Dotel.traces.exporter=logging",
"-Dotel.metrics.exporter=none",
"-Dotel.traces.sampler=always_on",
"-javaagent:/app/" + agentJar,
"-jar",
"/app/" + appJar
Expand Down
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
5 changes: 5 additions & 0 deletions simpleclient_bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
<artifactId>simpleclient_httpserver</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_tracer_common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_jetty</artifactId>
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();
fscellos marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

public class OpenTelemetrySpanContextSupplier implements SpanContextSupplier {

public static boolean isAvailable() {
public static boolean isAvailable() {
try {
if ("inactive".equalsIgnoreCase(System.getProperties().getProperty("io.prometheus.otelExemplars"))) {
return false;
}
OpenTelemetrySpanContextSupplier test = new OpenTelemetrySpanContextSupplier();
test.getSpanId();
test.getTraceId();
test.isSampled();
return true;
} catch (LinkageError ignored) {
// NoClassDefFoundError:
Expand All @@ -25,15 +26,20 @@ public static boolean isAvailable() {
}
}

@Override
public String getTraceId() {
String traceId = Span.current().getSpanContext().getTraceId();
return TraceId.isValid(traceId) ? traceId : null;
}
@Override
public String getTraceId() {
String traceId = Span.current().getSpanContext().getTraceId();
return TraceId.isValid(traceId) ? traceId : null;
}

@Override
public String getSpanId() {
String spanId = Span.current().getSpanContext().getSpanId();
return SpanId.isValid(spanId) ? spanId : null;
}
@Override
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 @@ -20,6 +20,7 @@ public static boolean isAvailable() {
OpenTelemetryAgentSpanContextSupplier test = new OpenTelemetryAgentSpanContextSupplier();
test.getSpanId();
test.getTraceId();
test.isSampled();
return true;
} catch (LinkageError ignored) {
// NoClassDefFoundError:
Expand All @@ -41,4 +42,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();
}
}