feat: OpenTelemetry tracing + JSON logging for Grafana Loki#127
Merged
Conversation
Add the version-catalog entries (settings.gradle.kts) and wire them into
the backend: micronaut-tracing-opentelemetry http/jdbc, the OTLP exporter,
logstash-logback-encoder, the OpenTelemetry logback MDC appender and janino.
Also disable the AOT replaceLogbackXml optimization so logback.xml is parsed
at runtime, which the env-driven JSON/plain switch and ${...} substitution
in the optimized (Docker/prod) jar depend on.
Switch logback to emit structured JSON on stdout when LOG_JSON=true (set in the Docker/prod container) so Loki can index the fields; local dev keeps the human-readable pattern. Wrap the appender with the OpenTelemetry MDC appender so each line carries trace_id/span_id for log <-> trace correlation.
Add the otel config block: tracing is disabled by default (exporter "none") and activates when OTEL_TRACES_EXPORTER=otlp is set, exporting spans over OTLP gRPC to the configured endpoint. Metrics stay on Prometheus/Micrometer, and health/prometheus/swagger paths are excluded from tracing.
Contributor
Test results 6 files 6 suites 0s ⏱️ Results for commit e35b7d8. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
For prod/Docker we want structured JSON logs that Grafana Loki can index cleanly, plus OpenTelemetry tracing with log↔trace correlation. Metrics stay on the existing Prometheus/Micrometer path.
What
logback.xml): emits structured JSON on stdout whenLOG_JSON=true; local dev keeps the human-readable pattern. Every line carriestrace_id/span_idvia the OpenTelemetry MDC appender.application.yml): disabled by default (otel.traces.exporter: none), activates whenOTEL_TRACES_EXPORTER=otlpis set, exporting spans over OTLP gRPC./health,/prometheus,/swagger*are excluded.libsversion catalog insettings.gradle.kts(OTLP exporter version managed by the Micronaut platform BOM).replaceLogbackXmldisabled so the env-driven log switch works at runtime in the optimized jar.Container env vars (prod/Docker)
Without these (local dev) behavior is unchanged: readable logs, no export.
Grafana
level/logger_name/serviceetc.trace_id→ Tempo for log-to-trace jumps.Verification
./gradlew :backend:test✅:backend:jar :backend:optimizedBuildLayers :backend:optimizedDockerfile✅ (logback parses both branches; janino conditional resolves)Not done: runtime smoke test of an actual JSON line / live span (needs a running DB + collector).