From 45b0321d88dc12f1838de1ffb4d0cd8a3b220f56 Mon Sep 17 00:00:00 2001 From: jcarranzan Date: Tue, 22 Oct 2024 11:23:17 +0200 Subject: [PATCH] Add use of Dev Services wgth Grafana LGTM and Opentelemetry metrics --- opentelemetry-quickstart/README.md | 8 +++++ opentelemetry-quickstart/pom.xml | 4 +++ .../acme/opentelemetry/TracedResource.java | 36 +++++++++++++++++++ .../src/main/resources/application.properties | 5 +-- .../opentelemetry/TracedResourceTest.java | 13 +++++++ 5 files changed, 64 insertions(+), 2 deletions(-) diff --git a/opentelemetry-quickstart/README.md b/opentelemetry-quickstart/README.md index 3fdfce7bf1..ec5783bff3 100644 --- a/opentelemetry-quickstart/README.md +++ b/opentelemetry-quickstart/README.md @@ -1 +1,9 @@ Quarkus guide: https://quarkus.io/guides/opentelemetry + +In this opentelemetry-quickstart is implemented OpenTelemetry Metrics detailed in the guide: https://quarkus.io/guides/opentelemetry-metrics . + +Additionally, it integrates the LGTM DevService for telemetry visualization by adding the `quarkus-observability-devservices-lgtm` dependency. + +Usage and configuration are explained in the guide: https://quarkus.io/guides/opentelemetry-tracing#grafana-otel-lgtm-option + + diff --git a/opentelemetry-quickstart/pom.xml b/opentelemetry-quickstart/pom.xml index 670080ebd3..f99a54563e 100644 --- a/opentelemetry-quickstart/pom.xml +++ b/opentelemetry-quickstart/pom.xml @@ -48,6 +48,10 @@ io.quarkus quarkus-rest-client + + io.quarkus + quarkus-observability-devservices-lgtm + diff --git a/opentelemetry-quickstart/src/main/java/org/acme/opentelemetry/TracedResource.java b/opentelemetry-quickstart/src/main/java/org/acme/opentelemetry/TracedResource.java index a59eb3a65b..7304048a43 100644 --- a/opentelemetry-quickstart/src/main/java/org/acme/opentelemetry/TracedResource.java +++ b/opentelemetry-quickstart/src/main/java/org/acme/opentelemetry/TracedResource.java @@ -1,8 +1,14 @@ package org.acme.opentelemetry; +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.metrics.LongCounter; +import io.opentelemetry.api.metrics.Meter; +import jakarta.annotation.PostConstruct; +import jakarta.inject.Inject; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; +import jakarta.ws.rs.QueryParam; import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.UriInfo; @@ -18,6 +24,24 @@ public class TracedResource { @Context UriInfo uriInfo; + @Inject + OpenTelemetry openTelemetry; + + @Inject + Meter meter; + + private LongCounter longCounter; + + @PostConstruct + public void init() { + meter = openTelemetry.getMeter("myservice"); + + longCounter = meter.counterBuilder("service.xvalue") + .setDescription("Current value of X in the service") + .setUnit("units") + .build(); + } + @GET @Path("/hello") @Produces(MediaType.TEXT_PLAIN) @@ -35,4 +59,16 @@ public String chain() { .build(ResourceClient.class); return "chain -> " + resourceClient.hello(); } + + @GET + @Path("/metrics/set") + @Produces(MediaType.APPLICATION_JSON) + public String setMetric(@QueryParam("value") long value) { + if (value <= 0) { + return "{\"status\":\"failure\", \"message\":\"Value parameter must be positive\"}"; + } + longCounter.add(value); + + return "{\"status\":\"success\", \"xvalue\":" + value + "}"; + } } diff --git a/opentelemetry-quickstart/src/main/resources/application.properties b/opentelemetry-quickstart/src/main/resources/application.properties index 9e25685f64..b383c7add3 100644 --- a/opentelemetry-quickstart/src/main/resources/application.properties +++ b/opentelemetry-quickstart/src/main/resources/application.properties @@ -1,5 +1,6 @@ quarkus.application.name=myservice -quarkus.otel.exporter.otlp.traces.endpoint=http://localhost:4317 +quarkus.otel.metrics.enabled=true quarkus.otel.exporter.otlp.traces.headers=Authorization=Bearer my_secret quarkus.log.console.format=%d{HH:mm:ss} %-5p traceId=%X{traceId}, parentId=%X{parentId}, spanId=%X{spanId}, sampled=%X{sampled} [%c{2.}] (%t) %s%e%n - +# jfr must be enabled if you need OpenTelemetry metrics on native mode. +quarkus.native.monitoring=jfr \ No newline at end of file diff --git a/opentelemetry-quickstart/src/test/java/org/acme/opentelemetry/TracedResourceTest.java b/opentelemetry-quickstart/src/test/java/org/acme/opentelemetry/TracedResourceTest.java index 9031c4ecb2..e54bf63781 100644 --- a/opentelemetry-quickstart/src/test/java/org/acme/opentelemetry/TracedResourceTest.java +++ b/opentelemetry-quickstart/src/test/java/org/acme/opentelemetry/TracedResourceTest.java @@ -28,4 +28,17 @@ public void testChainEndpoint() { .body(is("chain -> hello")); } + @Test + public void testSetMetricEndpoint() { + long testValue = 42L; + + given() + .queryParam("value", testValue) + .when().get("/metrics/set") + .then() + .statusCode(200) + .body("status", is("success")) + .body("xvalue", is((int)testValue)); + } + }