Skip to content

Commit 68cdf76

Browse files
Add exemplars support for Micrometer Tracing
1 parent 1966186 commit 68cdf76

File tree

13 files changed

+430
-0
lines changed

13 files changed

+430
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>io.prometheus</groupId>
7+
<artifactId>integration_tests</artifactId>
8+
<version>0.16.1-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>it_exemplars_micrometer_tracing</artifactId>
12+
<name>Integration Tests - Exemplars with Micrometer Tracing</name>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>io.prometheus</groupId>
17+
<artifactId>simpleclient</artifactId>
18+
<version>${project.version}</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>io.micrometer</groupId>
22+
<artifactId>micrometer-tracing-test</artifactId>
23+
<version>1.0.0-M7</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>io.prometheus</groupId>
27+
<artifactId>simpleclient_httpserver</artifactId>
28+
<version>${project.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>io.prometheus</groupId>
32+
<artifactId>it_common</artifactId>
33+
<type>test-jar</type>
34+
<version>${project.version}</version>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>com.squareup.okhttp3</groupId>
39+
<artifactId>okhttp</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.testcontainers</groupId>
44+
<artifactId>testcontainers</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>ch.qos.logback</groupId>
49+
<artifactId>logback-classic</artifactId>
50+
<scope>test</scope>
51+
</dependency>
52+
</dependencies>
53+
54+
<repositories>
55+
<repository>
56+
<id>spring-milestone</id>
57+
<url>https://repo.spring.io/milestone</url>
58+
</repository>
59+
</repositories>
60+
61+
<build>
62+
<finalName>example-micrometer-tracing-app</finalName>
63+
<plugins>
64+
<plugin>
65+
<groupId>org.apache.maven.plugins</groupId>
66+
<artifactId>maven-compiler-plugin</artifactId>
67+
<configuration>
68+
<source>1.8</source>
69+
<target>1.8</target>
70+
</configuration>
71+
</plugin>
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-failsafe-plugin</artifactId>
75+
</plugin>
76+
<plugin>
77+
<groupId>org.apache.maven.plugins</groupId>
78+
<artifactId>maven-dependency-plugin</artifactId>
79+
<executions>
80+
<execution>
81+
<id>copy-dependencies</id>
82+
<phase>package</phase>
83+
<goals>
84+
<goal>copy-dependencies</goal>
85+
</goals>
86+
</execution>
87+
</executions>
88+
</plugin>
89+
</plugins>
90+
</build>
91+
92+
<licenses>
93+
<license>
94+
<name>The Apache Software License, Version 2.0</name>
95+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
96+
<distribution>repo</distribution>
97+
</license>
98+
</licenses>
99+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.prometheus.client.it.exemplars_micrometer_tracing;
2+
3+
import io.micrometer.tracing.Span;
4+
import io.micrometer.tracing.Tracer;
5+
import io.micrometer.tracing.test.simple.SimpleSpan;
6+
import io.micrometer.tracing.test.simple.SimpleTracer;
7+
import io.prometheus.client.Counter;
8+
import io.prometheus.client.exemplars.DefaultExemplarSampler;
9+
import io.prometheus.client.exemplars.ExemplarConfig;
10+
import io.prometheus.client.exemplars.ExemplarSampler;
11+
import io.prometheus.client.exemplars.tracer.micrometer.tracing.MicrometerTracingSpanContextSupplier;
12+
import io.prometheus.client.exporter.HTTPServer;
13+
14+
import java.io.IOException;
15+
16+
/**
17+
* Example application using Micrometer Tracing.
18+
*/
19+
public class ExampleApplication {
20+
21+
public static void main(String[] args) throws IOException, InterruptedException {
22+
SimpleTracer tracer = new SimpleTracer();
23+
ExemplarSampler exemplarSampler = new DefaultExemplarSampler(new MicrometerTracingSpanContextSupplier(tracer));
24+
ExemplarConfig.setCounterExemplarSampler(exemplarSampler);
25+
ExemplarConfig.setHistogramExemplarSampler(exemplarSampler);
26+
ExemplarConfig.enableExemplars();
27+
28+
Counter counter = Counter.build()
29+
.name("test")
30+
.help("help")
31+
.register();
32+
33+
Span span = nextSpan(tracer);
34+
try (Tracer.SpanInScope ws = tracer.withSpan(span.start())) {
35+
counter.inc(1);
36+
}
37+
finally {
38+
span.end();
39+
}
40+
41+
new HTTPServer(9000);
42+
Thread.currentThread().join(); // sleep forever
43+
}
44+
45+
private static Span nextSpan(SimpleTracer tracer) {
46+
SimpleSpan span = tracer.nextSpan().name("testSpan");
47+
span.context().setSampled(true);
48+
span.context().setTraceId("45e09ee1c39e1f8f");
49+
span.context().setSpanId("22f69a0e2c0ab635");
50+
51+
return span;
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package io.prometheus.client.it.exemplars_micrometer_tracing;
2+
3+
import io.prometheus.client.it.common.LogConsumer;
4+
import io.prometheus.client.it.common.Scraper;
5+
import io.prometheus.client.it.common.Volume;
6+
import org.junit.After;
7+
import org.junit.Assert;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.testcontainers.containers.BindMode;
11+
import org.testcontainers.containers.GenericContainer;
12+
13+
import java.io.IOException;
14+
import java.net.URISyntaxException;
15+
import java.util.List;
16+
17+
/**
18+
* Test if traces from Micrometer Tracing are picked up as Exemplars.
19+
**/
20+
public class ExemplarsMicrometerTracingIT {
21+
22+
private Volume volume;
23+
private GenericContainer<?> javaContainer;
24+
25+
private final String appJar = "example-micrometer-tracing-app.jar";
26+
private final String image = "openjdk:11-jre";
27+
private final String[] cmd = new String[] {"java", "-cp", appJar + ":dependency/*", ExampleApplication.class.getName()};
28+
29+
@Before
30+
public void setUp() throws IOException, URISyntaxException {
31+
volume = Volume.create("exemplars-micrometer-tracing-test")
32+
.copyFromTargetDirectory(appJar)
33+
.copyFromTargetDirectory("dependency");
34+
javaContainer = new GenericContainer<>(image)
35+
.withFileSystemBind(volume.getHostPath(), "/app", BindMode.READ_ONLY)
36+
.withWorkingDirectory("/app")
37+
.withLogConsumer(LogConsumer.withPrefix(image))
38+
.withExposedPorts(9000)
39+
.withCommand(cmd);
40+
System.out.println("Java image: " + image);
41+
System.out.println("Temp dir: " + volume.getHostPath());
42+
System.out.println("cmd: " + String.join(" ", cmd));
43+
}
44+
45+
@After
46+
public void tearDown() throws IOException {
47+
javaContainer.stop();
48+
volume.remove();
49+
}
50+
51+
@Test
52+
public void exemplarsShouldBeFound() {
53+
javaContainer.start();
54+
List<String> metrics = Scraper.scrape("http://localhost:" + javaContainer.getMappedPort(9000) + "/metrics", 10_000);
55+
boolean testTotalWithExemplarFound = false;
56+
boolean testTotalWithoutExemplarFound = false;
57+
for (String metric : metrics) {
58+
System.out.println(metric);
59+
if (metric.matches("^test_total 1\\.0 # \\{span_id=\"22f69a0e2c0ab635\",trace_id=\"45e09ee1c39e1f8f\"} 1.0 [0-9.]+$")) {
60+
testTotalWithExemplarFound = true;
61+
}
62+
if (metric.matches("^test_total 1\\.0$")) {
63+
testTotalWithoutExemplarFound = true;
64+
}
65+
}
66+
67+
Assert.assertTrue("test_total metric with exemplars expected", testTotalWithExemplarFound);
68+
Assert.assertFalse("test_total without exemplar should not be there", testTotalWithoutExemplarFound);
69+
}
70+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
5+
</encoder>
6+
</appender>
7+
8+
<root level="info">
9+
<appender-ref ref="STDOUT"/>
10+
</root>
11+
12+
<logger name="org.testcontainers" level="INFO"/>
13+
<logger name="com.github.dockerjava" level="WARN"/>
14+
</configuration>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<ruleset comparisonMethod="maven"
2+
xmlns="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0 https://www.mojohaus.org/versions-maven-plugin/xsd/rule-2.0.0.xsd">
4+
<rules>
5+
</rules>
6+
</ruleset>

integration_tests/it_exemplars_otel_agent/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<plugin>
7777
<groupId>org.springframework.boot</groupId>
7878
<artifactId>spring-boot-maven-plugin</artifactId>
79+
<version>2.6.3</version>
7980
<executions>
8081
<execution>
8182
<goals>

integration_tests/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
</licenses>
2323

2424
<modules>
25+
<module>it_exemplars_micrometer_tracing</module>
2526
<module>it_exemplars_otel_sdk</module>
2627
<module>it_exemplars_otel_agent</module>
2728
<module>it_java_versions</module>

simpleclient/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
<!-- The simpleclient has no required 3rd party runtime dependencies by design, so that
3838
it can be included into any other project without having to worry about conflicts.
3939
All transitive dependencies of tracer libraries are scoped as provided and optional at runtime. -->
40+
<dependency>
41+
<groupId>io.prometheus</groupId>
42+
<artifactId>simpleclient_tracer_micrometer_tracing</artifactId>
43+
<version>${project.version}</version>
44+
</dependency>
4045
<dependency>
4146
<groupId>io.prometheus</groupId>
4247
<artifactId>simpleclient_tracer_otel</artifactId>

simpleclient_tracer/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,23 @@
2020
<artifactId>opentelemetry-api</artifactId>
2121
<version>1.15.0</version>
2222
</dependency>
23+
<dependency>
24+
<groupId>io.micrometer</groupId>
25+
<artifactId>micrometer-tracing-bom</artifactId>
26+
<version>1.0.0-M7</version>
27+
<type>pom</type>
28+
<scope>import</scope>
29+
</dependency>
2330
</dependencies>
2431
</dependencyManagement>
2532

33+
<repositories>
34+
<repository>
35+
<id>spring-milestone</id>
36+
<url>https://repo.spring.io/milestone</url>
37+
</repository>
38+
</repositories>
39+
2640
<licenses>
2741
<license>
2842
<name>The Apache Software License, Version 2.0</name>
@@ -33,6 +47,7 @@
3347

3448
<modules>
3549
<module>simpleclient_tracer_common</module>
50+
<module>simpleclient_tracer_micrometer_tracing</module>
3651
<module>simpleclient_tracer_otel</module>
3752
<module>simpleclient_tracer_otel_agent</module>
3853
</modules>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>io.prometheus</groupId>
7+
<artifactId>simpleclient_tracer</artifactId>
8+
<version>0.16.1-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>simpleclient_tracer_micrometer_tracing</artifactId>
12+
<packaging>bundle</packaging>
13+
14+
<name>Prometheus Java Span Context Supplier - Micrometer Tracing</name>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>io.prometheus</groupId>
19+
<artifactId>simpleclient_tracer_common</artifactId>
20+
<version>${project.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>io.micrometer</groupId>
24+
<artifactId>micrometer-tracing</artifactId>
25+
<version>1.0.0-M7</version>
26+
<scope>provided</scope>
27+
<optional>true</optional>
28+
</dependency>
29+
<dependency>
30+
<groupId>junit</groupId>
31+
<artifactId>junit</artifactId>
32+
<version>4.13.2</version>
33+
<scope>test</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.mockito</groupId>
37+
<artifactId>mockito-core</artifactId>
38+
<version>4.6.1</version>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.assertj</groupId>
43+
<artifactId>assertj-core</artifactId>
44+
<version>3.23.1</version>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.apache.maven.plugins</groupId>
53+
<artifactId>maven-compiler-plugin</artifactId>
54+
<configuration>
55+
<source>1.8</source>
56+
<target>1.8</target>
57+
</configuration>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
62+
<licenses>
63+
<license>
64+
<name>The Apache Software License, Version 2.0</name>
65+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
66+
<distribution>repo</distribution>
67+
</license>
68+
</licenses>
69+
</project>

0 commit comments

Comments
 (0)