From d87149abed0aabc9b9f25165821c8759f7c94612 Mon Sep 17 00:00:00 2001 From: cchen Date: Fri, 12 Jul 2024 17:29:32 +0800 Subject: [PATCH] add metrics example for demo. --- .../docker-compose-java-demo.yaml | 15 +++++++++++++ otel-simple-webmvc/pom.xml | 1 + .../OtelTestSpringMvcController.java | 22 +++++++++++++++---- pom.xml | 2 ++ 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 otel-simple-webmvc/docker-compose-java-demo.yaml diff --git a/otel-simple-webmvc/docker-compose-java-demo.yaml b/otel-simple-webmvc/docker-compose-java-demo.yaml new file mode 100644 index 0000000..fe40cf5 --- /dev/null +++ b/otel-simple-webmvc/docker-compose-java-demo.yaml @@ -0,0 +1,15 @@ +version: "2.4" + +services: + otel-demo: + image: cchen1996/otel-simple-webmvc-demo:0.0.2 + container_name: otel-simple-webmvc + environment: + - OTEL.RESOURCE.ATTRIBUTES=service.name=otel-simple-webmvc,service.version=0.0.2 + - OTEL.LOGS.EXPORTER=otlp + - OTEL.METRICS.EXPORTER=otlp + - OTEL.EXPORTER.OTLP.ENDPOINT=http://192.168.117.18:4317 + restart: on-failure + ports: + - "10001:10001" + diff --git a/otel-simple-webmvc/pom.xml b/otel-simple-webmvc/pom.xml index d41025c..9e5cb77 100644 --- a/otel-simple-webmvc/pom.xml +++ b/otel-simple-webmvc/pom.xml @@ -34,6 +34,7 @@ opentelemetry-logback-appender-1.0 + \ No newline at end of file diff --git a/otel-simple-webmvc/src/main/java/org/chenmudu/otel/webmvc/controller/OtelTestSpringMvcController.java b/otel-simple-webmvc/src/main/java/org/chenmudu/otel/webmvc/controller/OtelTestSpringMvcController.java index e1248f7..8a0ec43 100644 --- a/otel-simple-webmvc/src/main/java/org/chenmudu/otel/webmvc/controller/OtelTestSpringMvcController.java +++ b/otel-simple-webmvc/src/main/java/org/chenmudu/otel/webmvc/controller/OtelTestSpringMvcController.java @@ -17,6 +17,10 @@ package org.chenmudu.otel.webmvc.controller; import ch.qos.logback.classic.Logger; +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.metrics.LongCounter; +import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.instrumentation.annotations.SpanAttribute; import io.opentelemetry.instrumentation.annotations.WithSpan; import org.slf4j.LoggerFactory; @@ -32,11 +36,20 @@ public class OtelTestSpringMvcController { private volatile long counter = 0; private static Logger log = (Logger) LoggerFactory .getLogger(OtelTestSpringMvcController.class); + private final Meter meter; + + private final LongCounter longCounter; + + { + meter = GlobalOpenTelemetry.get().getMeter(Class.class.getSimpleName()); + longCounter = meter.counterBuilder("web_mvc_success_query_counter") + .setDescription("test").build(); + } @GetMapping("/webmvc") public String webmvc() { - counter++; - if (counter % 2 == 0) { + this.counter++; + if (this.counter % 2 == 0) { try { final int i = 1 / 0; } catch (Exception e) { @@ -46,9 +59,10 @@ public String webmvc() { } log.info("OtelTestSpringMvcController webmvc!"); this.calledHi(); - if (counter > 100) { - counter = 0; + if (this.counter > 100) { + this.counter = 0; } + longCounter.add(1L); return "OtelTestSpringMvcController hello !"; } diff --git a/pom.xml b/pom.xml index 79b5a5e..31402fe 100644 --- a/pom.xml +++ b/pom.xml @@ -104,6 +104,8 @@ opentelemetry-logback-appender-1.0 + + org.apache.commons commons-lang3