Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit 0c810ba

Browse files
Copilotovertrue
andcommitted
Add documentation for OTEL_EXPORTER_OTLP_HEADERS
Co-authored-by: overtrue <[email protected]>
1 parent a6dee7e commit 0c810ba

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ OTEL_TRACES_EXPORTER=console
6666
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
6767
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
6868
69+
# Optional: Add custom headers to OTLP exporter (e.g., for authentication)
70+
# OTEL_EXPORTER_OTLP_HEADERS="x-api-key=your-api-key,authorization=Bearer your-token"
71+
6972
# Context propagation
7073
OTEL_PROPAGATORS=tracecontext,baggage
7174
```
@@ -317,11 +320,58 @@ protected $middlewareGroups = [
317320
| `OTEL_TRACES_EXPORTER` | Trace exporter type | `otlp` | `console`, `otlp` |
318321
| `OTEL_EXPORTER_OTLP_ENDPOINT` | OTLP endpoint URL | `http://localhost:4318` | `https://api.honeycomb.io` |
319322
| `OTEL_EXPORTER_OTLP_PROTOCOL` | OTLP protocol | `http/protobuf` | `http/protobuf`, `grpc` |
323+
| `OTEL_EXPORTER_OTLP_HEADERS` | Custom headers for OTLP exporter (comma-separated) | `null` | `x-api-key=key123,authorization=Bearer token` |
320324
| `OTEL_PROPAGATORS` | Context propagators | `tracecontext,baggage` | `tracecontext,baggage,b3` |
321325
| `OTEL_TRACES_SAMPLER` | Sampling strategy | `parentbased_always_on` | `always_on`, `traceidratio` |
322326
| `OTEL_TRACES_SAMPLER_ARG` | Sampler argument | `null` | `0.1` |
323327
| `OTEL_RESOURCE_ATTRIBUTES` | Resource attributes | `null` | `key1=value1,key2=value2` |
324328

329+
### Authentication with OTLP Exporters
330+
331+
Many OpenTelemetry collectors and backends require authentication. You can add custom headers (like API keys or bearer tokens) to your OTLP exporter using the `OTEL_EXPORTER_OTLP_HEADERS` environment variable.
332+
333+
#### Examples:
334+
335+
**API Key Authentication:**
336+
```env
337+
OTEL_EXPORTER_OTLP_HEADERS="x-api-key=your-api-key-here"
338+
```
339+
340+
**Bearer Token Authentication:**
341+
```env
342+
OTEL_EXPORTER_OTLP_HEADERS="authorization=Bearer your-token-here"
343+
```
344+
345+
**Multiple Headers:**
346+
```env
347+
OTEL_EXPORTER_OTLP_HEADERS="x-api-key=key123,x-tenant-id=tenant456"
348+
```
349+
350+
**Common SaaS Providers:**
351+
352+
For **Honeycomb**:
353+
```env
354+
OTEL_EXPORTER_OTLP_ENDPOINT=https://api.honeycomb.io
355+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
356+
OTEL_EXPORTER_OTLP_HEADERS="x-honeycomb-team=your-api-key"
357+
```
358+
359+
For **New Relic**:
360+
```env
361+
OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp.nr-data.net:4318
362+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
363+
OTEL_EXPORTER_OTLP_HEADERS="api-key=your-new-relic-license-key"
364+
```
365+
366+
For **Grafana Cloud**:
367+
```env
368+
OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp-gateway-prod-us-central-0.grafana.net/otlp
369+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
370+
OTEL_EXPORTER_OTLP_HEADERS="authorization=Basic your-base64-encoded-credentials"
371+
```
372+
373+
> **Note**: The `OTEL_EXPORTER_OTLP_HEADERS` variable accepts comma-separated `key=value` pairs. Make sure to properly escape values if they contain special characters.
374+
325375
## Testing
326376

327377
```bash

config/otel.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@
1414
*/
1515
'tracer_name' => env('OTEL_TRACER_NAME', 'overtrue.laravel-open-telemetry'),
1616

17+
/**
18+
* OpenTelemetry SDK Configuration
19+
*
20+
* This package uses standard OpenTelemetry environment variables for configuration.
21+
* These are automatically picked up by the OpenTelemetry PHP SDK:
22+
*
23+
* Core Variables:
24+
* - OTEL_SERVICE_NAME: Name of your service (e.g., 'my-laravel-app')
25+
* - OTEL_SERVICE_VERSION: Version of your service (e.g., '1.0.0')
26+
* - OTEL_TRACES_EXPORTER: Trace exporter type ('console', 'otlp', etc.)
27+
* - OTEL_EXPORTER_OTLP_ENDPOINT: OTLP endpoint URL (e.g., 'http://localhost:4318')
28+
* - OTEL_EXPORTER_OTLP_PROTOCOL: OTLP protocol ('http/protobuf' or 'grpc')
29+
* - OTEL_EXPORTER_OTLP_HEADERS: Custom headers for authentication (e.g., 'x-api-key=key123')
30+
* Format: comma-separated key=value pairs
31+
* Example: "x-api-key=abc123,authorization=Bearer token456"
32+
* - OTEL_PROPAGATORS: Context propagators (e.g., 'tracecontext,baggage')
33+
* - OTEL_TRACES_SAMPLER: Sampling strategy ('always_on', 'traceidratio', etc.)
34+
* - OTEL_TRACES_SAMPLER_ARG: Sampler argument (e.g., '0.1' for 10% sampling)
35+
* - OTEL_RESOURCE_ATTRIBUTES: Additional resource attributes (e.g., 'key1=value1,key2=value2')
36+
*
37+
* For more information, see: https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/
38+
*/
39+
1740
/**
1841
* Middleware Configuration
1942
*/

examples/middleware_example.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
2222
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
2323
24+
# Optional: Add authentication headers to OTLP exporter
25+
# OTEL_EXPORTER_OTLP_HEADERS="x-api-key=your-api-key"
26+
2427
# Context propagation
2528
OTEL_PROPAGATORS=tracecontext,baggage
2629
@@ -175,6 +178,10 @@ public function handle($request, Closure $next)
175178
OTEL_TRACES_EXPORTER=otlp
176179
OTEL_EXPORTER_OTLP_ENDPOINT=https://otel-collector.company.com:4318
177180
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
181+
182+
# Add authentication headers for production collectors
183+
OTEL_EXPORTER_OTLP_HEADERS="authorization=Bearer production-token-here"
184+
178185
OTEL_PROPAGATORS=tracecontext,baggage
179186
180187
# Sampling configuration
@@ -239,3 +246,43 @@ public function createUser(array $userData)
239246
],
240247
],
241248
*/
249+
250+
// 11. Authenticating with OTLP Collectors
251+
/*
252+
Many OpenTelemetry backends require authentication headers. You can configure these
253+
using the OTEL_EXPORTER_OTLP_HEADERS environment variable:
254+
255+
Format: comma-separated key=value pairs
256+
Example: OTEL_EXPORTER_OTLP_HEADERS="x-api-key=abc123,authorization=Bearer token456"
257+
258+
Common SaaS Provider Examples:
259+
260+
# Honeycomb
261+
OTEL_EXPORTER_OTLP_ENDPOINT=https://api.honeycomb.io
262+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
263+
OTEL_EXPORTER_OTLP_HEADERS="x-honeycomb-team=your-api-key"
264+
265+
# New Relic
266+
OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp.nr-data.net:4318
267+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
268+
OTEL_EXPORTER_OTLP_HEADERS="api-key=your-new-relic-license-key"
269+
270+
# Grafana Cloud
271+
OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp-gateway-prod-us-central-0.grafana.net/otlp
272+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
273+
OTEL_EXPORTER_OTLP_HEADERS="authorization=Basic base64-encoded-credentials"
274+
275+
# Custom Collector with Bearer Token
276+
OTEL_EXPORTER_OTLP_ENDPOINT=https://your-collector.example.com:4318
277+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
278+
OTEL_EXPORTER_OTLP_HEADERS="authorization=Bearer your-secret-token"
279+
280+
# Multiple Headers
281+
OTEL_EXPORTER_OTLP_ENDPOINT=https://your-collector.example.com:4318
282+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
283+
OTEL_EXPORTER_OTLP_HEADERS="x-api-key=key123,x-tenant-id=tenant456,authorization=Bearer token789"
284+
285+
Note: The OTEL_EXPORTER_OTLP_HEADERS variable is automatically recognized by the
286+
OpenTelemetry PHP SDK and requires no additional configuration in this package.
287+
*/
288+

0 commit comments

Comments
 (0)