Implements backend observability (metrics, distributed tracing, structured logs, trace-log correlation) for Micronaut services using Micrometer/Prometheus, OpenTelemetry, Logstash JSON, and Grafana Alloy shipping to Grafana Cloud (Mimir, Tempo, Loki). Use when adding observability to a Micronaut service, wiring OTel traces, setting up Prometheus scraping, configuring structured JSON logs, or connecting a Java microservice to Grafana Cloud.
Three observability pillars wired together, all flowing through Grafana Alloy in-cluster to Grafana Cloud.
See references/code-templates.md for complete copy-paste configs.
| Pillar | How |
|---|---|
| Metrics | Micrometer + Prometheus registry → /prometheus endpoint → Alloy pod-annotation scrape → Grafana Cloud Mimir |
| Distributed traces | micronaut-tracing-opentelemetry-http auto-instruments HTTP + JDBC → OTLP gRPC → Alloy → Grafana Cloud Tempo |
| Structured logs | logstash-logback-encoder (JSON to stdout) → Alloy loki.source.kubernetes → Grafana Cloud Loki |
| Trace-log correlation | OTel auto-injects / into SLF4J MDC → every JSON log line carries them; Alloy promotes them to Loki structured metadata |
traceIdspanIdAdd to the service (or shared parent) pom.xml.
Full XML → references/code-templates.md.
<!-- Micrometer + Prometheus: exposes /prometheus endpoint -->
<dependency>
<groupId>io.micronaut.micrometer</groupId>
<artifactId>micronaut-micrometer-core</artifactId>
</dependency>
<dependency>
<groupId>io.micronaut.micrometer</groupId>
<artifactId>micronaut-micrometer-registry-prometheus</artifactId>
</dependency>
<!-- OTel HTTP instrumentation: server/client HTTP + JDBC + MDC trace injection -->
<dependency>
<groupId>io.micronaut.tracing</groupId>
<artifactId>micronaut-tracing-opentelemetry-http</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
<scope>compile</scope>
</dependency>
<!-- Structured JSON logging (Logstash format for Alloy/Loki) -->
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>7.4</version>
</dependency>
micronaut-tracing-opentelemetry-http auto-injects traceId/spanId into SLF4J MDC — no extra code needed for log correlation.
Create src/main/resources/logback.xml.
Full file → references/code-templates.md.
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
LogstashEncoder writes every log line as a JSON object to stdout. Alloy's loki.source.kubernetes picks it up and the stage.json pipeline extracts level, mdc.traceId, mdc.spanId.
Full file → references/code-templates.md.
Key sections to add: