Skip to content

Commit

Permalink
Add use of Dev Services wgth Grafana LGTM and Opentelemetry metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarranzan committed Oct 22, 2024
1 parent edc470a commit d68cc83
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
9 changes: 9 additions & 0 deletions opentelemetry-quickstart/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-observability-devservices-lgtm</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-opentelemetry-exporter-otlp</artifactId>
<version>3.9.5</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package org.acme.opentelemetry;

import com.google.common.util.concurrent.AtomicDouble;
import io.opentelemetry.api.GlobalOpenTelemetry;
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;
Expand All @@ -18,6 +24,20 @@ public class TracedResource {
@Context
UriInfo uriInfo;

private Meter meter;

private AtomicDouble xValue = new AtomicDouble(0.0);

@PostConstruct
public void init() {
meter = GlobalOpenTelemetry.getMeter("myservice");

meter.gaugeBuilder("service.xvalue")
.setDescription("Current value of X in the service")
.setUnit("units")
.buildWithCallback(measurement -> measurement.record(xValue.get()));
}

@GET
@Path("/hello")
@Produces(MediaType.TEXT_PLAIN)
Expand All @@ -35,4 +55,17 @@ public String chain() {
.build(ResourceClient.class);
return "chain -> " + resourceClient.hello();
}

@GET
@Path("/metrics/set")
@Produces(MediaType.APPLICATION_JSON)
public String setMetric(@QueryParam("value") Double value) {
if (value == null) {
LOG.warn("Attempted to set metric without providing a value.");
return "{\"status\":\"failure\", \"message\":\"Value parameter is missing\"}";
}
xValue.set(value);
LOG.infof("Metric 'xvalue' set to: %f", value);
return "{\"status\":\"success\", \"xvalue\":" + value + "}";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
quarkus.application.name=myservice
quarkus.otel.exporter.otlp.traces.endpoint=http://localhost:4317
quarkus.opentelemetry.enabled=true
quarkus.opentelemetry.tracer.enabled=true
quarkus.opentelemetry.metrics.enabled=true
quarkus.opentelemetry.exporter.otlp.endpoint=http://localhost:4317
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

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
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,17 @@ public void testChainEndpoint() {
.body(is("chain -> hello"));
}

@Test
public void testSetMetricEndpoint() {
Double testValue = 42.0;

given()
.queryParam("value", testValue)
.when().get("/metrics/set")
.then()
.statusCode(200)
.body("status", is("success"))
.body("xvalue", is(testValue.floatValue()));
}

}

0 comments on commit d68cc83

Please sign in to comment.