-
Notifications
You must be signed in to change notification settings - Fork 855
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added prometheus exporter example * tidying up * Update examples/prometheus/README.md Co-authored-by: Giovanni Liva <giovanni.liva@dynatrace.com> * Update examples/prometheus/README.md Co-authored-by: Giovanni Liva <giovanni.liva@dynatrace.com> * added port arg check as per code review suggestion Co-authored-by: Giovanni Liva <giovanni.liva@dynatrace.com>
- Loading branch information
Showing
5 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,41 @@ | ||
# Prometheus Example | ||
|
||
This example demonstrates how to use the OpenTelemetry SDK | ||
to instrument a simple application using Prometheus as the metric exporter and expose the metrics via HTTP. | ||
|
||
These are collected by a Prometheus instance which is configured to pull these metrics via HTTP. | ||
|
||
# How to run | ||
|
||
## Prerequisites | ||
* Java 1.7 | ||
* Docker 19.03 | ||
|
||
## 1 - Compile | ||
```shell script | ||
../gradlew fatJar | ||
``` | ||
## 2 - Run Prometheus | ||
|
||
Start Prometheus instance with a configuration that sets up a HTTP collection job for ```127.0.0.1:19090``` | ||
|
||
See [prometheus.yml](prometheus.yml) | ||
|
||
```shell script | ||
docker run --network="host" --rm -it \ | ||
--name prometheus \ | ||
-v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml \ | ||
prom/prometheus | ||
|
||
``` | ||
|
||
## 3 - Start the Application | ||
```shell script | ||
java -cp build/libs/opentelemetry-examples-prometheus-all-0.1.0-SNAPSHOT.jar io.opentelemetry.example.PrometheusExample 19090 | ||
``` | ||
## 4 - Open the Prometheus UI | ||
|
||
Navigate to: | ||
|
||
http://localhost:9090/graph?g0.range_input=15m&g0.expr=incoming_messages&g0.tab=0 | ||
|
This file contains 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 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
description = 'OpenTelemetry Example for Prometheus Exporter' | ||
ext.moduleName = "io.opentelemetry.examples.prometheus" | ||
|
||
dependencies { | ||
compile("io.opentelemetry:opentelemetry-api:${opentelemetryVersion}") | ||
compile("io.opentelemetry:opentelemetry-sdk:${opentelemetryVersion}") | ||
compile("io.opentelemetry:opentelemetry-exporters-prometheus:${opentelemetryVersion}") | ||
compile("io.prometheus:simpleclient:0.8.1") | ||
compile("io.prometheus:simpleclient_httpserver:0.8.1") | ||
} |
This file contains 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,30 @@ | ||
global: | ||
scrape_interval: 15s | ||
scrape_timeout: 10s | ||
evaluation_interval: 15s | ||
alerting: | ||
alertmanagers: | ||
- static_configs: | ||
- targets: [] | ||
scheme: http | ||
timeout: 10s | ||
api_version: v1 | ||
scrape_configs: | ||
- job_name: prometheus | ||
honor_timestamps: true | ||
scrape_interval: 15s | ||
scrape_timeout: 10s | ||
metrics_path: /metrics | ||
scheme: http | ||
static_configs: | ||
- targets: | ||
- localhost:9090 | ||
- job_name: otel_java_prometheus_example | ||
honor_timestamps: true | ||
scrape_interval: 15s | ||
scrape_timeout: 10s | ||
metrics_path: /metrics | ||
scheme: http | ||
static_configs: | ||
- targets: | ||
- 127.0.0.1:19090 |
86 changes: 86 additions & 0 deletions
86
examples/prometheus/src/main/java/io/opentelemetry/example/PrometheusExample.java
This file contains 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,86 @@ | ||
package io.opentelemetry.example; | ||
|
||
import io.opentelemetry.common.Labels; | ||
import io.opentelemetry.exporters.prometheus.PrometheusCollector; | ||
import io.opentelemetry.metrics.AsynchronousInstrument; | ||
import io.opentelemetry.metrics.LongValueObserver; | ||
import io.opentelemetry.metrics.Meter; | ||
import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
import io.opentelemetry.sdk.metrics.MeterSdkProvider; | ||
import io.prometheus.client.exporter.HTTPServer; | ||
import java.io.IOException; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
/** | ||
* Example of using the {@link PrometheusCollector} to convert OTel metrics to Prometheus format and | ||
* expose these to a Prometheus instance via a {@link HTTPServer} exporter. | ||
* | ||
* <p>A {@link LongValueObserver} is used to periodically measure how many incoming messages are | ||
* awaiting processing. The {@link LongValueObserver.Callback} gets executed every collection | ||
* interval. | ||
*/ | ||
public class PrometheusExample { | ||
|
||
private final MeterSdkProvider meterSdkProvider = OpenTelemetrySdk.getMeterProvider(); | ||
private final Meter meter = meterSdkProvider.get("PrometheusExample", "0.7"); | ||
private final HTTPServer server; | ||
private long incomingMessageCount; | ||
|
||
public PrometheusExample(int port) throws IOException { | ||
|
||
LongValueObserver observer = | ||
meter | ||
.longValueObserverBuilder("incoming.messages") | ||
.setDescription("No of incoming messages awaiting processing") | ||
.setUnit("message") | ||
.build(); | ||
|
||
observer.setCallback( | ||
new LongValueObserver.Callback<LongValueObserver.LongResult>() { | ||
@Override | ||
public void update(AsynchronousInstrument.LongResult result) { | ||
result.observe(incomingMessageCount, Labels.empty()); | ||
} | ||
}); | ||
|
||
PrometheusCollector.newBuilder() | ||
.setMetricProducer(meterSdkProvider.getMetricProducer()) | ||
.buildAndRegister(); | ||
|
||
server = new HTTPServer(port); | ||
} | ||
|
||
void shutdown() { | ||
server.stop(); | ||
} | ||
|
||
void simulate() { | ||
for (int i = 300; i > 0; i--) { | ||
try { | ||
System.out.println( | ||
i + " Iterations to go, current incomingMessageCount is: " + incomingMessageCount); | ||
incomingMessageCount = ThreadLocalRandom.current().nextLong(100); | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
} | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
|
||
int port = 0; | ||
try { | ||
port = Integer.parseInt(args[0]); | ||
} catch (Exception e) { | ||
System.out.println("Port not set, or is invalid. Exiting"); | ||
System.exit(1); | ||
} | ||
|
||
PrometheusExample prometheusExample = new PrometheusExample(port); | ||
|
||
prometheusExample.simulate(); | ||
prometheusExample.shutdown(); | ||
|
||
System.out.println("Exiting"); | ||
} | ||
} |
This file contains 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