- 
                Notifications
    You must be signed in to change notification settings 
- Fork 821
Add exemplars support for Micrometer Tracing #805
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
          
     Open
      
      
            jonatan-ivanov
  wants to merge
  1
  commit into
  prometheus:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
jonatan-ivanov:exemplars-support-for-micrometer-tracing
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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> | 
        
          
          
            53 changes: 53 additions & 0 deletions
          
          53 
        
  ...rc/main/java/io/prometheus/client/it/exemplars_micrometer_tracing/ExampleApplication.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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(); | ||
|  | ||
| 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; | ||
| } | ||
| } | ||
        
          
          
            70 changes: 70 additions & 0 deletions
          
          70 
        
  ...va/io/prometheus/client/it/exemplars_micrometer_tracing/ExemplarsMicrometerTracingIT.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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); | ||
| } | ||
| } | 
        
          
          
            14 changes: 14 additions & 0 deletions
          
          14 
        
  integration_tests/it_exemplars_micrometer_tracing/src/test/resources/logback-test.xml
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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> | 
        
          
          
            6 changes: 6 additions & 0 deletions
          
          6 
        
  integration_tests/it_exemplars_micrometer_tracing/version-rules.xml
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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> | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            69 changes: 69 additions & 0 deletions
          
          69 
        
  simpleclient_tracer/simpleclient_tracer_micrometer_tracing/pom.xml
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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> | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 caseclient_javawill 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.