Skip to content

Commit

Permalink
add metrics example for demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
chenmudu committed Jul 12, 2024
1 parent 5c4584f commit d87149a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
15 changes: 15 additions & 0 deletions otel-simple-webmvc/docker-compose-java-demo.yaml
Original file line number Diff line number Diff line change
@@ -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"

1 change: 1 addition & 0 deletions otel-simple-webmvc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<artifactId>opentelemetry-logback-appender-1.0</artifactId>
</dependency>


</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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 !";
}

Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
<artifactId>opentelemetry-logback-appender-1.0</artifactId>
</dependency>



<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down

0 comments on commit d87149a

Please sign in to comment.