Skip to content

Commit 898d476

Browse files
committed
Add Simpleclient Backwards-Compatibility Module
Signed-off-by: Fabian Stäber <[email protected]>
1 parent 0f0d302 commit 898d476

File tree

12 files changed

+888
-9
lines changed

12 files changed

+888
-9
lines changed

examples/example-exporter-httpserver/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<configuration>
6565
<transformers>
6666
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
67-
<mainClass>io.prometheus.metrics.examples.httpserver.Main</mainClass>
67+
<mainClass>io.prometheus.metrics.examples.simpleclient.Main</mainClass>
6868
</transformer>
6969
</transformers>
7070
</configuration>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Built-in HTTPServer for Exposing Metrics
2+
3+
## Build
4+
5+
This example is built as part of the `client_java` project.
6+
7+
```
8+
./mvnw package
9+
```
10+
11+
## Run
12+
13+
The build creates a JAR file with the example application in `./examples/example-simpleclient-bridge/target/`.
14+
15+
```
16+
java -jar ./examples/example-simpleclient-bridge/target/example-simpleclient-bridge.jar
17+
```
18+
19+
This should expose a metrics endpoint on [http://localhost:9400/metrics](http://localhost:9400/metrics).
20+
The `events_total` counter should be exposed.with a Web browser should yield an example of a counter metric.
21+
22+
```
23+
# HELP events_total total number of events
24+
# TYPE events_total counter
25+
events_total 1.0
26+
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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>examples</artifactId>
8+
<version>1.0.0-alpha-5-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>example-simpleclient-bridge</artifactId>
12+
13+
<name>Example - Simpleclient Bridge</name>
14+
<description>
15+
Prometheus Metrics Example of the Simpleclient Backwards Compatibility module
16+
</description>
17+
18+
<licenses>
19+
<license>
20+
<name>The Apache Software License, Version 2.0</name>
21+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
22+
<distribution>repo</distribution>
23+
</license>
24+
</licenses>
25+
26+
<developers>
27+
<developer>
28+
<id>fstab</id>
29+
<name>Fabian Stäber</name>
30+
<email>[email protected]</email>
31+
</developer>
32+
</developers>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>io.prometheus</groupId>
37+
<artifactId>simpleclient</artifactId>
38+
<version>0.16.0</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>io.prometheus</groupId>
42+
<artifactId>prometheus-metrics-simpleclient-bridge</artifactId>
43+
<version>${project.version}</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>io.prometheus</groupId>
47+
<artifactId>prometheus-metrics-exporter-httpserver</artifactId>
48+
<version>${project.version}</version>
49+
</dependency>
50+
</dependencies>
51+
52+
<build>
53+
<finalName>${project.artifactId}</finalName>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<artifactId>maven-shade-plugin</artifactId>
58+
<executions>
59+
<execution>
60+
<phase>package</phase>
61+
<goals>
62+
<goal>shade</goal>
63+
</goals>
64+
<configuration>
65+
<transformers>
66+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
67+
<mainClass>io.prometheus.metrics.examples.simpleclient.Main</mainClass>
68+
</transformer>
69+
</transformers>
70+
</configuration>
71+
</execution>
72+
</executions>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.prometheus.metrics.examples.simpleclient;
2+
3+
import io.prometheus.client.Counter;
4+
import io.prometheus.metrics.exporter.httpserver.HTTPServer;
5+
import io.prometheus.metrics.simpleclient.bridge.SimpleclientCollector;
6+
7+
import java.io.IOException;
8+
9+
/**
10+
* Simple example of the simpleclient backwards compatibility module.
11+
*/
12+
public class Main {
13+
14+
public static void main(String[] args) throws IOException, InterruptedException {
15+
16+
// The following call will register all metrics from the old CollectorRegistry.defaultRegistry
17+
// with the new PrometheusRegistry.defaultRegistry.
18+
19+
SimpleclientCollector.newBuilder().register();
20+
21+
// Register a counter with the old CollectorRegistry.
22+
// It doesn't matter whether the counter is registered before or after bridging with PrometheusRegistry.
23+
24+
Counter simpleclientCounter = Counter.build()
25+
.name("events_total")
26+
.help("total number of events")
27+
.register();
28+
29+
simpleclientCounter.inc();
30+
31+
// Expose metrics from the new PrometheusRegistry. This should contain the events_total metric.
32+
33+
HTTPServer server = HTTPServer.newBuilder()
34+
.withPort(9400)
35+
.buildAndStart();
36+
37+
System.out.println("HTTPServer listening on port http://localhost:" + server.getPort() + "/metrics");
38+
39+
Thread.currentThread().join();
40+
}
41+
}

examples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<module>example-exporter-servlet-tomcat</module>
3838
<module>example-exporter-httpserver</module>
3939
<module>example-exporter-opentelemetry</module>
40+
<module>example-simpleclient-bridge</module>
4041
</modules>
4142

4243
</project>

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<module>prometheus-metrics-exporter-httpserver</module>
5858
<module>prometheus-metrics-exporter-opentelemetry</module>
5959
<module>prometheus-metrics-instrumentation-jvm</module>
60+
<module>prometheus-metrics-simpleclient-bridge</module>
6061
<!-- <module>prometheus-metrics-shaded-dependencies</module> -->
6162
<module>examples</module>
6263
<module>integration-tests</module>

prometheus-metrics-exposition-formats/src/test/java/io/prometheus/metrics/expositionformats/ExpositionFormatsTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ public void testClassicHistogramComplete() throws Exception {
741741
"response_size_bytes_count{status=\"200\"} 3 " + scrapeTimestamp1s + "\n" +
742742
"response_size_bytes_sum{status=\"200\"} 4.1 " + scrapeTimestamp1s + "\n" +
743743
"response_size_bytes_created{status=\"200\"} " + createdTimestamp1s + " " + scrapeTimestamp1s + "\n" +
744-
"response_size_bytes_bucket{status=\"500\",le=\"0.0\"} 3 " + scrapeTimestamp2s + "\n" +
744+
"response_size_bytes_bucket{status=\"500\",le=\"1.0\"} 3 " + scrapeTimestamp2s + "\n" +
745745
"response_size_bytes_bucket{status=\"500\",le=\"2.2\"} 5 " + scrapeTimestamp2s + " # " + exemplar1String + "\n" +
746746
"response_size_bytes_bucket{status=\"500\",le=\"+Inf\"} 5 " + scrapeTimestamp2s + " # " + exemplar2String + "\n" +
747747
"response_size_bytes_count{status=\"500\"} 5 " + scrapeTimestamp2s + "\n" +
@@ -755,7 +755,7 @@ public void testClassicHistogramComplete() throws Exception {
755755
"response_size_bytes_bucket{status=\"200\",le=\"+Inf\"} 3 " + scrapeTimestamp1s + "\n" +
756756
"response_size_bytes_count{status=\"200\"} 3 " + scrapeTimestamp1s + "\n" +
757757
"response_size_bytes_sum{status=\"200\"} 4.1 " + scrapeTimestamp1s + "\n" +
758-
"response_size_bytes_bucket{status=\"500\",le=\"0.0\"} 3 " + scrapeTimestamp2s + "\n" +
758+
"response_size_bytes_bucket{status=\"500\",le=\"1.0\"} 3 " + scrapeTimestamp2s + "\n" +
759759
"response_size_bytes_bucket{status=\"500\",le=\"2.2\"} 5 " + scrapeTimestamp2s + "\n" +
760760
"response_size_bytes_bucket{status=\"500\",le=\"+Inf\"} 5 " + scrapeTimestamp2s + "\n" +
761761
"response_size_bytes_count{status=\"500\"} 5 " + scrapeTimestamp2s + "\n" +
@@ -772,7 +772,7 @@ public void testClassicHistogramComplete() throws Exception {
772772
"response_size_bytes_bucket{status=\"200\",le=\"+Inf\"} 3 " + scrapeTimestamp1s + " # " + exemplar2String + "\n" +
773773
"response_size_bytes_count{status=\"200\"} 3 " + scrapeTimestamp1s + "\n" +
774774
"response_size_bytes_sum{status=\"200\"} 4.1 " + scrapeTimestamp1s + "\n" +
775-
"response_size_bytes_bucket{status=\"500\",le=\"0.0\"} 3 " + scrapeTimestamp2s + "\n" +
775+
"response_size_bytes_bucket{status=\"500\",le=\"1.0\"} 3 " + scrapeTimestamp2s + "\n" +
776776
"response_size_bytes_bucket{status=\"500\",le=\"2.2\"} 5 " + scrapeTimestamp2s + " # " + exemplar1String + "\n" +
777777
"response_size_bytes_bucket{status=\"500\",le=\"+Inf\"} 5 " + scrapeTimestamp2s + " # " + exemplar2String + "\n" +
778778
"response_size_bytes_count{status=\"500\"} 5 " + scrapeTimestamp2s + "\n" +
@@ -785,7 +785,7 @@ public void testClassicHistogramComplete() throws Exception {
785785
"response_size_bytes_bucket{status=\"200\",le=\"+Inf\"} 3 " + scrapeTimestamp1s + "\n" +
786786
"response_size_bytes_count{status=\"200\"} 3 " + scrapeTimestamp1s + "\n" +
787787
"response_size_bytes_sum{status=\"200\"} 4.1 " + scrapeTimestamp1s + "\n" +
788-
"response_size_bytes_bucket{status=\"500\",le=\"0.0\"} 3 " + scrapeTimestamp2s + "\n" +
788+
"response_size_bytes_bucket{status=\"500\",le=\"1.0\"} 3 " + scrapeTimestamp2s + "\n" +
789789
"response_size_bytes_bucket{status=\"500\",le=\"2.2\"} 5 " + scrapeTimestamp2s + "\n" +
790790
"response_size_bytes_bucket{status=\"500\",le=\"+Inf\"} 5 " + scrapeTimestamp2s + "\n" +
791791
"response_size_bytes_count{status=\"500\"} 5 " + scrapeTimestamp2s + "\n" +
@@ -819,7 +819,7 @@ public void testClassicHistogramComplete() throws Exception {
819819
"sample_sum: 3.2 " +
820820
"bucket { " +
821821
"cumulative_count: 3 " +
822-
"upper_bound: 0.0 " +
822+
"upper_bound: 1.0 " +
823823
"} bucket { " +
824824
"cumulative_count: 5 " +
825825
"upper_bound: 2.2 " +
@@ -839,7 +839,7 @@ public void testClassicHistogramComplete() throws Exception {
839839
.addDataPoint(HistogramSnapshot.HistogramDataPointSnapshot.newBuilder()
840840
.withSum(3.2)
841841
.withClassicHistogramBuckets(ClassicHistogramBuckets.newBuilder()
842-
.addBucket(0.0, 3)
842+
.addBucket(1.0, 3)
843843
.addBucket(2.2, 2)
844844
.addBucket(Double.POSITIVE_INFINITY, 0)
845845
.build())

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Unit.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ public static double nanosToSeconds(long nanos) {
4141
return nanos / 1E9;
4242
}
4343

44-
public static double millisToSeconds(long nanos) {
45-
return nanos / 1E3;
44+
public static double millisToSeconds(long millis) {
45+
return millis / 1E3;
46+
}
47+
48+
public static double secondsToMillis(double seconds) {
49+
return seconds * 1E3;
4650
}
4751

4852
public static double kiloBytesToBytes(double kilobytes) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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>client_java</artifactId>
8+
<version>1.0.0-alpha-5-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>prometheus-metrics-simpleclient-bridge</artifactId>
12+
<packaging>bundle</packaging>
13+
14+
<name>Prometheus Metrics - Simpleclient Bridge</name>
15+
<description>
16+
Bridge the old simpleclient CollectorRegistry to the new PrometheusRegistry
17+
</description>
18+
19+
<licenses>
20+
<license>
21+
<name>The Apache Software License, Version 2.0</name>
22+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
23+
<distribution>repo</distribution>
24+
</license>
25+
</licenses>
26+
27+
<developers>
28+
<developer>
29+
<id>fstab</id>
30+
<name>Fabian Stäber</name>
31+
<email>[email protected]</email>
32+
</developer>
33+
</developers>
34+
35+
<dependencies>
36+
<dependency>
37+
<groupId>io.prometheus</groupId>
38+
<artifactId>prometheus-metrics-model</artifactId>
39+
<version>${project.version}</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>io.prometheus</groupId>
43+
<artifactId>simpleclient</artifactId>
44+
<version>0.16.0</version>
45+
<scope>provided</scope><!-- provided so that this can be used with older simpleclient versions -->
46+
</dependency>
47+
48+
<!-- test dependencies -->
49+
<dependency>
50+
<groupId>junit</groupId>
51+
<artifactId>junit</artifactId>
52+
<version>4.13.2</version>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>io.prometheus</groupId>
57+
<artifactId>prometheus-metrics-exposition-formats</artifactId>
58+
<version>${project.version}</version>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>io.prometheus</groupId>
63+
<artifactId>simpleclient_common</artifactId>
64+
<version>0.16.0</version>
65+
<scope>test</scope>
66+
</dependency>
67+
</dependencies>
68+
</project>

0 commit comments

Comments
 (0)