Skip to content
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
71 changes: 71 additions & 0 deletions opentracing-metrics-prometheus-spring-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2017 The OpenTracing Authors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License.

-->
<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>
<artifactId>opentracing-metrics-parent</artifactId>
<groupId>io.opentracing.contrib</groupId>
<version>0.1.3-SNAPSHOT</version>
</parent>

<artifactId>opentracing-metrics-prometheus-spring-autoconfigure</artifactId>

<dependencies>
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-metrics-spring-autoconfigure</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-metrics-prometheus</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_servlet</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${version.javax.servlet-api}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${version.org.springframework.boot}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${version.org.springframework.boot}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright 2017 The OpenTracing Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.opentracing.contrib.metrics.prometheus.spring.autoconfigure;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.prometheus.client.CollectorRegistry;

@Configuration
public class PrometheusConfiguration {

@Bean
@ConditionalOnMissingBean
public CollectorRegistry defaultCollectorRegistry() {
return CollectorRegistry.defaultRegistry;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright 2017 The OpenTracing Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.opentracing.contrib.metrics.prometheus.spring.autoconfigure;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.opentracing.contrib.metrics.MetricLabel;
import io.opentracing.contrib.metrics.MetricsReporter;
import io.opentracing.contrib.metrics.prometheus.PrometheusMetricsReporter;
import io.prometheus.client.CollectorRegistry;

@Configuration
public class PrometheusMetricsReporterConfiguration {

@Autowired
private CollectorRegistry collectorRegistry;

@Value("${OPENTRACING_METRICS_NAME:}")
private String metricsName;

@Autowired(required=false)
private Set<MetricLabel> metricLabels;

@Bean
public MetricsReporter prometheusMetricsReporter() {
PrometheusMetricsReporter.Builder builder = PrometheusMetricsReporter.newMetricsReporter();
if (metricsName != null && !metricsName.isEmpty()) {
builder.withName(metricsName);
}
builder.withCollectorRegistry(collectorRegistry);
if (metricLabels != null && !metricLabels.isEmpty()) {
for (MetricLabel label : metricLabels) {
builder.withCustomLabel(label);
}
}
return builder.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2017 The OpenTracing Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.opentracing.contrib.metrics.prometheus.spring.autoconfigure;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.MetricsServlet;

@Configuration
@ConditionalOnClass(value = {MetricsServlet.class})
public class PrometheusServletConfiguration {

@Value("${OPENTRACING_METRICS_EXPORTER_HTTP_PATH:false}")
private String metricsPath;

@Bean
@ConditionalOnProperty(name="OPENTRACING_METRICS_EXPORTER_HTTP_PATH")
ServletRegistrationBean registerPrometheusExporterServlet(CollectorRegistry metricRegistry) {
return new ServletRegistrationBean(new MetricsServlet(metricRegistry), metricsPath);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
io.opentracing.contrib.metrics.prometheus.spring.autoconfigure.PrometheusConfiguration,\
io.opentracing.contrib.metrics.prometheus.spring.autoconfigure.PrometheusMetricsReporterConfiguration,\
io.opentracing.contrib.metrics.prometheus.spring.autoconfigure.PrometheusServletConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright 2017 The OpenTracing Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.opentracing.contrib.metrics.prometheus.spring.autoconfigure;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import io.opentracing.contrib.api.SpanData;
import io.opentracing.contrib.metrics.MetricsReporter;
import io.opentracing.tag.Tags;
import io.prometheus.client.Collector.MetricFamilySamples;
import io.prometheus.client.CollectorRegistry;

@SpringBootTest(
classes = {PrometheusMetricsReporterConfigurationTest.SpringConfiguration.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class PrometheusMetricsReporterConfigurationTest {

private static final CollectorRegistry testCollectorRegistry = new CollectorRegistry();

@Autowired
private MetricsReporter metricsReporter;

@Configuration
@EnableAutoConfiguration
public static class SpringConfiguration {
@Bean
public CollectorRegistry testCollectorRegistry() {
return testCollectorRegistry;
}
}

@Test
public void testMetricsReporter() {
MetricFamilySamples samples = testCollectorRegistry.metricFamilySamples().nextElement();
assertTrue(samples.samples.isEmpty());

SpanData metricSpanData=Mockito.mock(SpanData.class);
Mockito.when(metricSpanData.getOperationName()).thenReturn("testOp");
Map<String,Object> testTags = new HashMap<String,Object>();
testTags.put(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT);
Mockito.when(metricSpanData.getTags()).thenReturn(testTags);
Mockito.when(metricSpanData.getDuration()).thenReturn(500L);
metricsReporter.reportSpan(metricSpanData);

samples = testCollectorRegistry.metricFamilySamples().nextElement();
assertFalse(samples.samples.isEmpty());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright 2017 The OpenTracing Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.opentracing.contrib.metrics.prometheus.spring.autoconfigure;

import static org.junit.Assert.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import io.opentracing.contrib.metrics.MetricsReporter;
import io.prometheus.client.Collector.MetricFamilySamples;
import io.prometheus.client.CollectorRegistry;

@SpringBootTest(
classes = {PrometheusMetricsReporterConfigurationWithNameTest.SpringConfiguration.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class PrometheusMetricsReporterConfigurationWithNameTest {

private static final String METRICS_NAME = "testmetrics";

private static final CollectorRegistry testCollectorRegistry = new CollectorRegistry();

@Autowired
private MetricsReporter metricsReporter;

@Configuration
@EnableAutoConfiguration
public static class SpringConfiguration {
@Bean
public CollectorRegistry testCollectorRegistry() {
return testCollectorRegistry;
}
}

@BeforeClass
public static void init() {
System.setProperty("OPENTRACING_METRICS_NAME", METRICS_NAME);
}

@AfterClass
public static void close() {
System.clearProperty("OPENTRACING_METRICS_NAME");
}

@Test
public void testMetricsReporterName() {
assertNotNull(metricsReporter);

MetricFamilySamples samples = testCollectorRegistry.metricFamilySamples().nextElement();
assertEquals(METRICS_NAME, samples.name);
}

}
Loading