Skip to content

Commit

Permalink
Tweaks with some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarranzan committed Oct 22, 2024
1 parent d68cc83 commit 3eb6b48
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
5 changes: 0 additions & 5 deletions opentelemetry-quickstart/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@
<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,7 +1,7 @@
package org.acme.opentelemetry;

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

@Inject
OpenTelemetry openTelemetry;

private Meter meter;

private AtomicDouble xValue = new AtomicDouble(0.0);
private LongCounter longCounter;

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

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

@GET
Expand All @@ -59,13 +62,14 @@ public String chain() {
@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\"}";
public String setMetric(@QueryParam("value") long value) {
if (value <= 0) {
LOG.warn("Attempted to set metric with a non-positive value.");
return "{\"status\":\"failure\", \"message\":\"Value parameter must be positive\"}";
}
xValue.set(value);
LOG.infof("Metric 'xvalue' set to: %f", value);
longCounter.add(value);

LOG.infof("Metric 'xvalue' set to: %d", value);
return "{\"status\":\"success\", \"xvalue\":" + value + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public void testChainEndpoint() {

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

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

}

0 comments on commit 3eb6b48

Please sign in to comment.