I would like to know how to make certain OpenTelemetry resource attributes show up as labels in Grafana Cloud when sending logs via OTLP and configuring the OpenTelemetry Java Agent using the OTEL_*
environment variables. I researched how to set the loki.resource.labels
resource label to achieve this, but was unsuccessful in implementing it.
Our Java service starts with the OpenTelemetry Java Agent version 1.26.0 and (among others) following environment variables:
OTEL_LOGS_EXPORTER=otlp
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp-gateway-prod-eu-west-0.grafana.net/otlp
-
OTEL_EXPORTER_OTLP_HEADERS
set to contain the credentials -
OTEL_RESOURCE_ATTRIBUTES
set toservice.name=foo-service,service.namespace=staging,service.version=commit-sha,service.instance.id=something-random
- and a few
container.*
andk8s.*
attributes according to the spec
- and a few
The logs show up fine in our Grafana Cloud instance and have the default exporter
, instance
, job
and level
labels set as expected.
I want to add more labels and follow the documentation in:
- OpenTelemetry Protocol (OTLP) format considerations | Grafana Cloud documentation
-
opentelemetry-collector-contrib/README.md at 45b9b987d434f4ac034d670fba9057de80dcdbfe · open-telemetry/opentelemetry-collector-contrib · GitHub
- assuming the Grafana Cloud uses the same
lokiexporter
OpenTelemetry Collector, which judging from Grafana docs seems likely
- assuming the Grafana Cloud uses the same
- Resource SDK | OpenTelemetry
- Propagation format for distributed context: Baggage
I set OTEL_RESOURCE_ATTRIBUTES
to service.name=foo-service,[...],loki.resource.labels=service.name%2Cservice.namespace
(%2C
is comma / ,
in percent encoding; the middle part is shortened to [...]
for brevity in this post) and restart the service, but now Grafana Cloud stops showing any logs. I get these messages in my service’s console logs:
[otel.javaagent 2023-05-13 06:41:17:069 +0000] [OkHttp https://otlp-gateway-prod-eu-west-0.grafana.net/...] WARN io.opentelemetry.exporter.internal.okhttp.OkHttpExporter - Failed to export logs. Server responded with HTTP status code 400. Error message: Unable to parse response body, HTTP status message:
Setting loki.resource.labels=service.name
(single attribute, no special encoding) has the same effect.
Changing loki.resource.labels
to a non-existent resource attribute, for example loki.resource.labels=service_name
, allows for the logs to flow to Grafana Cloud again. No additional labels are set, which is expected, since the resource attribute does not actually exist, but I found it noteworthy that a non-existent label does not trigger the HTTP status code 400 error.
Turning OpenTelemetry Java Agent debug logging on (cf. GitHub - open-telemetry/opentelemetry-java-instrumentation: OpenTelemetry auto-instrumentation and instrumentation libraries for Java) does not reveal further information. In particular it does not show the body of the response that the OkHttp / OTLP HTTP exporter was unable to parse.
Any help on how to achieve my goal or how to further debug this (e.g. how to see the HTTP response body cannot be parsed) are appreciated.