Section 7 · Add metrics and traces

The two ways to send OTLP data to Grafana Cloud: directly to the OTLP gateway, or through a local Grafana Alloy or OpenTelemetry Collector.

Estimated time: 2 min

Direct export or a collector

The hands-on exercise in this section sends OTLP data directly to the Grafana Cloud OTLP gateway. The other route, a local Grafana Alloy or OpenTelemetry Collector, is covered below so you recognize it later.

In direct export, your application holds the credentials and exports OTLP data to the Grafana Cloud OTLP gateway. With a collector, your application exports unauthenticated OTLP to http://localhost:4318, and Grafana Alloy or the OpenTelemetry Collector receives it and forwards it to Grafana Cloud with credentials.

Direct export

With direct export, no collector needed. The OTLP endpoint and optional auth headers are set through environment variables. In the hands-on exercise you set two:

  • OTEL_EXPORTER_OTLP_ENDPOINT: the endpoint URL from the OpenTelemetry card, including /otlp but excluding paths such as /v1/traces.
  • OTEL_EXPORTER_OTLP_HEADERS: Authorization=Basic <BASE64_CREDENTIALS>, with a plain space after Basic, where <BASE64_CREDENTIALS> is the Base64-encoded OTLP instance ID and access policy token.

Note

Python exporters older than opentelemetry-exporter-otlp-proto-http 1.27.0 reject the space and log Header format invalid. On one of those, upgrade the exporter or write Basic%20<BASE64_CREDENTIALS> instead.

The documented command prints <OTLP_INSTANCE_ID>:<ACCESS_POLICY_TOKEN>, pipes it through base64, then through tr -d '\n'.

Warning

The tr -d '\n' is not optional. base64 adds a trailing newline that invalidates the header.

Base64, which is a standard encoding that turns the instance-id:token pair into one string the header can carry, is not encryption. Keep the encoded value as secret as the token.

A collector

A local Grafana Alloy or OpenTelemetry Collector receives unauthenticated OTLP from your application and forwards it to Grafana Cloud with credentials. Your application then points OTEL_EXPORTER_OTLP_ENDPOINT at http://localhost:4318 instead of the gateway, and you can skip the OpenTelemetry card.

A collector gives you centralized token management, retries, relabeling, and metadata enrichment, at the cost of running another process. Relabeling, which is changing or dropping labels on your metrics and traces before they are stored, and metadata enrichment, which is adding fields such as the host or environment, both happen in the collector. For how Alloy does each one, refer to otelcol.processor.attributes for relabeling and otelcol.processor.resourcedetection for metadata enrichment. This journey does not set up a collector.

Note

With this SDK setup, Alloy or the Collector forwards metrics and traces only. Generation data goes directly to the Agent Observability API.