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

Add exemplars support for Micrometer Tracing #805

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
99 changes: 99 additions & 0 deletions integration_tests/it_exemplars_micrometer_tracing/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.prometheus</groupId>
<artifactId>integration_tests</artifactId>
<version>0.16.1-SNAPSHOT</version>
</parent>

<artifactId>it_exemplars_micrometer_tracing</artifactId>
<name>Integration Tests - Exemplars with Micrometer Tracing</name>

<dependencies>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-test</artifactId>
<version>1.0.0-M7</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_httpserver</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>it_common</artifactId>
<type>test-jar</type>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
<repository>
<id>spring-milestone</id>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>

<build>
<finalName>example-micrometer-tracing-app</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.prometheus.client.it.exemplars_micrometer_tracing;

import io.micrometer.tracing.Span;
import io.micrometer.tracing.Tracer;
import io.micrometer.tracing.test.simple.SimpleSpan;
import io.micrometer.tracing.test.simple.SimpleTracer;
import io.prometheus.client.Counter;
import io.prometheus.client.exemplars.DefaultExemplarSampler;
import io.prometheus.client.exemplars.ExemplarConfig;
import io.prometheus.client.exemplars.ExemplarSampler;
import io.prometheus.client.exemplars.tracer.micrometer.tracing.MicrometerTracingSpanContextSupplier;
import io.prometheus.client.exporter.HTTPServer;

import java.io.IOException;

/**
* Example application using Micrometer Tracing.
*/
public class ExampleApplication {

public static void main(String[] args) throws IOException, InterruptedException {
SimpleTracer tracer = new SimpleTracer();
ExemplarSampler exemplarSampler = new DefaultExemplarSampler(new MicrometerTracingSpanContextSupplier(tracer));
ExemplarConfig.setCounterExemplarSampler(exemplarSampler);
ExemplarConfig.setHistogramExemplarSampler(exemplarSampler);
ExemplarConfig.enableExemplars();
Comment on lines +23 to +26
Copy link
Member

@fstab fstab Sep 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just read your comment above again. Yes, the current implementation in Tracer.findSpanContextSupplier() assumes that it can get a tracer from a static variable. In that case client_java will use that tracer automatically, and users don't need any explicit code for that.

However, if the tracer is not available via a static variable, you need to set it explicitly as in your example above. I don't see any other way how to do it.


Counter counter = Counter.build()
.name("test")
.help("help")
.register();

Span span = nextSpan(tracer);
try (Tracer.SpanInScope ws = tracer.withSpan(span.start())) {
counter.inc(1);
}
finally {
span.end();
}

new HTTPServer(9000);
Thread.currentThread().join(); // sleep forever
}

private static Span nextSpan(SimpleTracer tracer) {
SimpleSpan span = tracer.nextSpan().name("testSpan");
span.context().setSampled(true);
span.context().setTraceId("45e09ee1c39e1f8f");
span.context().setSpanId("22f69a0e2c0ab635");

return span;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.prometheus.client.it.exemplars_micrometer_tracing;

import io.prometheus.client.it.common.LogConsumer;
import io.prometheus.client.it.common.Scraper;
import io.prometheus.client.it.common.Volume;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.GenericContainer;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;

/**
* Test if traces from Micrometer Tracing are picked up as Exemplars.
**/
public class ExemplarsMicrometerTracingIT {

private Volume volume;
private GenericContainer<?> javaContainer;

private final String appJar = "example-micrometer-tracing-app.jar";
private final String image = "openjdk:11-jre";
private final String[] cmd = new String[] {"java", "-cp", appJar + ":dependency/*", ExampleApplication.class.getName()};

@Before
public void setUp() throws IOException, URISyntaxException {
volume = Volume.create("exemplars-micrometer-tracing-test")
.copyFromTargetDirectory(appJar)
.copyFromTargetDirectory("dependency");
javaContainer = new GenericContainer<>(image)
.withFileSystemBind(volume.getHostPath(), "/app", BindMode.READ_ONLY)
.withWorkingDirectory("/app")
.withLogConsumer(LogConsumer.withPrefix(image))
.withExposedPorts(9000)
.withCommand(cmd);
System.out.println("Java image: " + image);
System.out.println("Temp dir: " + volume.getHostPath());
System.out.println("cmd: " + String.join(" ", cmd));
}

@After
public void tearDown() throws IOException {
javaContainer.stop();
volume.remove();
}

@Test
public void exemplarsShouldBeFound() {
javaContainer.start();
List<String> metrics = Scraper.scrape("http://localhost:" + javaContainer.getMappedPort(9000) + "/metrics", 10_000);
boolean testTotalWithExemplarFound = false;
boolean testTotalWithoutExemplarFound = false;
for (String metric : metrics) {
System.out.println(metric);
if (metric.matches("^test_total 1\\.0 # \\{span_id=\"22f69a0e2c0ab635\",trace_id=\"45e09ee1c39e1f8f\"} 1.0 [0-9.]+$")) {
testTotalWithExemplarFound = true;
}
if (metric.matches("^test_total 1\\.0$")) {
testTotalWithoutExemplarFound = true;
}
}

Assert.assertTrue("test_total metric with exemplars expected", testTotalWithExemplarFound);
Assert.assertFalse("test_total without exemplar should not be there", testTotalWithoutExemplarFound);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT"/>
</root>

<logger name="org.testcontainers" level="INFO"/>
<logger name="com.github.dockerjava" level="WARN"/>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<ruleset comparisonMethod="maven"
xmlns="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<rules>
</rules>
</ruleset>
1 change: 1 addition & 0 deletions integration_tests/it_exemplars_otel_agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.3</version>
<executions>
<execution>
<goals>
Expand Down
1 change: 1 addition & 0 deletions integration_tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
</licenses>

<modules>
<module>it_exemplars_micrometer_tracing</module>
<module>it_exemplars_otel_sdk</module>
<module>it_exemplars_otel_agent</module>
<module>it_java_versions</module>
Expand Down
5 changes: 5 additions & 0 deletions simpleclient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<!-- The simpleclient has no required 3rd party runtime dependencies by design, so that
it can be included into any other project without having to worry about conflicts.
All transitive dependencies of tracer libraries are scoped as provided and optional at runtime. -->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_tracer_micrometer_tracing</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_tracer_otel</artifactId>
Expand Down
15 changes: 15 additions & 0 deletions simpleclient_tracer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,23 @@
<artifactId>opentelemetry-api</artifactId>
<version>1.15.0</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bom</artifactId>
<version>1.0.0-M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<repositories>
<repository>
<id>spring-milestone</id>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>

<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
Expand All @@ -33,6 +47,7 @@

<modules>
<module>simpleclient_tracer_common</module>
<module>simpleclient_tracer_micrometer_tracing</module>
<module>simpleclient_tracer_otel</module>
<module>simpleclient_tracer_otel_agent</module>
</modules>
Expand Down
69 changes: 69 additions & 0 deletions simpleclient_tracer/simpleclient_tracer_micrometer_tracing/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_tracer</artifactId>
<version>0.16.1-SNAPSHOT</version>
</parent>

<artifactId>simpleclient_tracer_micrometer_tracing</artifactId>
<packaging>bundle</packaging>

<name>Prometheus Java Span Context Supplier - Micrometer Tracing</name>

<dependencies>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_tracer_common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing</artifactId>
<version>1.0.0-M7</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.23.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
</project>
Loading