From b1bbc07aac0ad39edfca19d5ba70c38ee1248478 Mon Sep 17 00:00:00 2001 From: jarebudev <23311805+jarebudev@users.noreply.github.com> Date: Sun, 16 Aug 2020 11:22:24 +0100 Subject: [PATCH 1/5] added prometheus exporter example --- examples/prometheus/README.md | 42 ++++++++++ examples/prometheus/build.gradle | 14 ++++ examples/prometheus/prometheus.yml | 30 +++++++ .../example/PrometheusExample.java | 80 +++++++++++++++++++ examples/settings.gradle | 1 + 5 files changed, 167 insertions(+) create mode 100644 examples/prometheus/README.md create mode 100644 examples/prometheus/build.gradle create mode 100644 examples/prometheus/prometheus.yml create mode 100644 examples/prometheus/src/main/java/io/opentelemetry/example/PrometheusExample.java diff --git a/examples/prometheus/README.md b/examples/prometheus/README.md new file mode 100644 index 00000000000..9a6088f00ec --- /dev/null +++ b/examples/prometheus/README.md @@ -0,0 +1,42 @@ +# 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``` +` + +```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 + + + diff --git a/examples/prometheus/build.gradle b/examples/prometheus/build.gradle new file mode 100644 index 00000000000..140523eeaec --- /dev/null +++ b/examples/prometheus/build.gradle @@ -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") +} \ No newline at end of file diff --git a/examples/prometheus/prometheus.yml b/examples/prometheus/prometheus.yml new file mode 100644 index 00000000000..5160b3dfab4 --- /dev/null +++ b/examples/prometheus/prometheus.yml @@ -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 diff --git a/examples/prometheus/src/main/java/io/opentelemetry/example/PrometheusExample.java b/examples/prometheus/src/main/java/io/opentelemetry/example/PrometheusExample.java new file mode 100644 index 00000000000..e583b09d032 --- /dev/null +++ b/examples/prometheus/src/main/java/io/opentelemetry/example/PrometheusExample.java @@ -0,0 +1,80 @@ +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. + * + *

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() { + @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 = Integer.parseInt(args[0]); + + PrometheusExample prometheusExample = new PrometheusExample(port); + + prometheusExample.simulate(); + prometheusExample.shutdown(); + + System.out.println("Exiting"); + } +} diff --git a/examples/settings.gradle b/examples/settings.gradle index 6b395e42873..e07bd1e2d34 100644 --- a/examples/settings.gradle +++ b/examples/settings.gradle @@ -16,6 +16,7 @@ include ":opentelemetry-examples-grpc", ":opentelemetry-examples-http", ":opentelemetry-examples-jaeger", ":opentelemetry-examples-metrics", + ":opentelemetry-examples-prometheus", ":opentelemetry-examples-sdk-usage", ":opentelemetry-examples-zipkin" From a1eb7157dc71f673cf4637165f9d60e62a36ddb1 Mon Sep 17 00:00:00 2001 From: jarebudev <23311805+jarebudev@users.noreply.github.com> Date: Sun, 16 Aug 2020 11:27:39 +0100 Subject: [PATCH 2/5] tidying up --- examples/prometheus/README.md | 2 ++ examples/prometheus/build.gradle | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/prometheus/README.md b/examples/prometheus/README.md index 9a6088f00ec..cad213ab9af 100644 --- a/examples/prometheus/README.md +++ b/examples/prometheus/README.md @@ -20,6 +20,8 @@ These are collected by a Prometheus instance which is configured to pull these 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 \ diff --git a/examples/prometheus/build.gradle b/examples/prometheus/build.gradle index 140523eeaec..802d962d3c3 100644 --- a/examples/prometheus/build.gradle +++ b/examples/prometheus/build.gradle @@ -11,4 +11,4 @@ dependencies { compile("io.opentelemetry:opentelemetry-exporters-prometheus:${opentelemetryVersion}") compile("io.prometheus:simpleclient:0.8.1") compile("io.prometheus:simpleclient_httpserver:0.8.1") -} \ No newline at end of file +} From 89e9f90d507cf731f51dbee0cf3061bf5c247fac Mon Sep 17 00:00:00 2001 From: jarebudev <23311805+jarebudev@users.noreply.github.com> Date: Mon, 17 Aug 2020 08:01:33 +0100 Subject: [PATCH 3/5] Update examples/prometheus/README.md Co-authored-by: Giovanni Liva --- examples/prometheus/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/prometheus/README.md b/examples/prometheus/README.md index cad213ab9af..a39981b13fc 100644 --- a/examples/prometheus/README.md +++ b/examples/prometheus/README.md @@ -3,7 +3,7 @@ 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. +These are collected by a Prometheus instance which is configured to pull these metrics via HTTP. # How to run @@ -41,4 +41,3 @@ Navigate to: http://localhost:9090/graph?g0.range_input=15m&g0.expr=incoming_messages&g0.tab=0 - From 639e4b73a0a3be73a185ed91d09fcd043b2c2694 Mon Sep 17 00:00:00 2001 From: jarebudev <23311805+jarebudev@users.noreply.github.com> Date: Mon, 17 Aug 2020 08:01:49 +0100 Subject: [PATCH 4/5] Update examples/prometheus/README.md Co-authored-by: Giovanni Liva --- examples/prometheus/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/prometheus/README.md b/examples/prometheus/README.md index a39981b13fc..4eb5a4600e4 100644 --- a/examples/prometheus/README.md +++ b/examples/prometheus/README.md @@ -18,7 +18,6 @@ These are collected by a Prometheus instance which is configured to pull these m ## 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) @@ -40,4 +39,3 @@ Navigate to: http://localhost:9090/graph?g0.range_input=15m&g0.expr=incoming_messages&g0.tab=0 - From fa106d9ef5cb03e3524c7e71caf1090336201713 Mon Sep 17 00:00:00 2001 From: jarebudev <23311805+jarebudev@users.noreply.github.com> Date: Mon, 17 Aug 2020 08:15:26 +0100 Subject: [PATCH 5/5] added port arg check as per code review suggestion --- .../java/io/opentelemetry/example/PrometheusExample.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/examples/prometheus/src/main/java/io/opentelemetry/example/PrometheusExample.java b/examples/prometheus/src/main/java/io/opentelemetry/example/PrometheusExample.java index e583b09d032..ffedb582aca 100644 --- a/examples/prometheus/src/main/java/io/opentelemetry/example/PrometheusExample.java +++ b/examples/prometheus/src/main/java/io/opentelemetry/example/PrometheusExample.java @@ -68,7 +68,13 @@ void simulate() { public static void main(String[] args) throws IOException { - int port = Integer.parseInt(args[0]); + 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);