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

Commit d83260c

Browse files
Copilotovertrue
andcommitted
Add authentication example with verification script
Co-authored-by: overtrue <[email protected]>
1 parent 0c810ba commit d83260c

File tree

1 file changed

+248
-0
lines changed

1 file changed

+248
-0
lines changed
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
<?php
2+
3+
/**
4+
* OTLP Exporter Authentication Examples
5+
*
6+
* This file demonstrates how to configure authentication headers for various
7+
* OpenTelemetry backends and collectors using the OTEL_EXPORTER_OTLP_HEADERS
8+
* environment variable.
9+
*
10+
* The OTEL_EXPORTER_OTLP_HEADERS variable is a standard OpenTelemetry SDK
11+
* configuration option and is automatically recognized without any additional
12+
* configuration in this Laravel package.
13+
*/
14+
15+
// ============================================================================
16+
// Example 1: Simple API Key Authentication
17+
// ============================================================================
18+
/*
19+
Many custom collectors and some SaaS providers use a simple API key header:
20+
21+
OTEL_SERVICE_NAME=my-laravel-app
22+
OTEL_TRACES_EXPORTER=otlp
23+
OTEL_EXPORTER_OTLP_ENDPOINT=https://collector.example.com:4318
24+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
25+
OTEL_EXPORTER_OTLP_HEADERS="x-api-key=your-secret-api-key-here"
26+
*/
27+
28+
// ============================================================================
29+
// Example 2: Bearer Token Authentication
30+
// ============================================================================
31+
/*
32+
OAuth2 or JWT-based authentication using Bearer tokens:
33+
34+
OTEL_SERVICE_NAME=my-laravel-app
35+
OTEL_TRACES_EXPORTER=otlp
36+
OTEL_EXPORTER_OTLP_ENDPOINT=https://collector.example.com:4318
37+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
38+
OTEL_EXPORTER_OTLP_HEADERS="authorization=Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
39+
*/
40+
41+
// ============================================================================
42+
// Example 3: Basic Authentication
43+
// ============================================================================
44+
/*
45+
HTTP Basic authentication with base64-encoded credentials:
46+
47+
OTEL_SERVICE_NAME=my-laravel-app
48+
OTEL_TRACES_EXPORTER=otlp
49+
OTEL_EXPORTER_OTLP_ENDPOINT=https://collector.example.com:4318
50+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
51+
OTEL_EXPORTER_OTLP_HEADERS="authorization=Basic dXNlcm5hbWU6cGFzc3dvcmQ="
52+
53+
# To generate the base64 credentials in bash:
54+
# echo -n "username:password" | base64
55+
*/
56+
57+
// ============================================================================
58+
// Example 4: Multiple Headers
59+
// ============================================================================
60+
/*
61+
Some backends require multiple headers for authentication and routing:
62+
63+
OTEL_SERVICE_NAME=my-laravel-app
64+
OTEL_TRACES_EXPORTER=otlp
65+
OTEL_EXPORTER_OTLP_ENDPOINT=https://collector.example.com:4318
66+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
67+
OTEL_EXPORTER_OTLP_HEADERS="x-api-key=key123,x-tenant-id=tenant-abc,x-environment=production"
68+
*/
69+
70+
// ============================================================================
71+
// Example 5: Honeycomb (Popular Observability Platform)
72+
// ============================================================================
73+
/*
74+
Honeycomb uses a team API key for authentication:
75+
76+
OTEL_PHP_AUTOLOAD_ENABLED=true
77+
OTEL_SERVICE_NAME=my-laravel-app
78+
OTEL_TRACES_EXPORTER=otlp
79+
OTEL_EXPORTER_OTLP_ENDPOINT=https://api.honeycomb.io
80+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
81+
OTEL_EXPORTER_OTLP_HEADERS="x-honeycomb-team=hcxik_01hqk4kxxxxxxxxxxxxxxxxxxxxxxx"
82+
83+
# Optional: Specify dataset
84+
# OTEL_EXPORTER_OTLP_HEADERS="x-honeycomb-team=your-key,x-honeycomb-dataset=my-dataset"
85+
*/
86+
87+
// ============================================================================
88+
// Example 6: New Relic
89+
// ============================================================================
90+
/*
91+
New Relic requires a license key:
92+
93+
OTEL_PHP_AUTOLOAD_ENABLED=true
94+
OTEL_SERVICE_NAME=my-laravel-app
95+
OTEL_TRACES_EXPORTER=otlp
96+
OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp.nr-data.net:4318
97+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
98+
OTEL_EXPORTER_OTLP_HEADERS="api-key=your-new-relic-license-key-here"
99+
100+
# For EU region, use:
101+
# OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp.eu01.nr-data.net:4318
102+
*/
103+
104+
// ============================================================================
105+
// Example 7: Grafana Cloud
106+
// ============================================================================
107+
/*
108+
Grafana Cloud uses Basic authentication with instance ID and API token:
109+
110+
OTEL_PHP_AUTOLOAD_ENABLED=true
111+
OTEL_SERVICE_NAME=my-laravel-app
112+
OTEL_TRACES_EXPORTER=otlp
113+
OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp-gateway-prod-us-central-0.grafana.net/otlp
114+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
115+
OTEL_EXPORTER_OTLP_HEADERS="authorization=Basic <base64-encoded-instanceId:token>"
116+
117+
# Generate the base64 credentials:
118+
# echo -n "instanceId:glc_token" | base64
119+
*/
120+
121+
// ============================================================================
122+
// Example 8: Datadog
123+
// ============================================================================
124+
/*
125+
Datadog OTLP endpoint with API key:
126+
127+
OTEL_PHP_AUTOLOAD_ENABLED=true
128+
OTEL_SERVICE_NAME=my-laravel-app
129+
OTEL_TRACES_EXPORTER=otlp
130+
OTEL_EXPORTER_OTLP_ENDPOINT=https://api.datadoghq.com
131+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
132+
OTEL_EXPORTER_OTLP_HEADERS="dd-api-key=your-datadog-api-key"
133+
134+
# For EU region, use:
135+
# OTEL_EXPORTER_OTLP_ENDPOINT=https://api.datadoghq.eu
136+
*/
137+
138+
// ============================================================================
139+
// Example 9: AWS X-Ray (via OpenTelemetry Collector)
140+
// ============================================================================
141+
/*
142+
When using AWS X-Ray with OpenTelemetry Collector:
143+
144+
OTEL_PHP_AUTOLOAD_ENABLED=true
145+
OTEL_SERVICE_NAME=my-laravel-app
146+
OTEL_TRACES_EXPORTER=otlp
147+
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
148+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
149+
150+
# Note: AWS X-Ray typically doesn't require headers when using the collector
151+
# The collector itself handles AWS credentials via IAM roles or environment variables
152+
*/
153+
154+
// ============================================================================
155+
// Example 10: Self-Hosted OpenTelemetry Collector with Auth
156+
// ============================================================================
157+
/*
158+
Custom authentication for a self-hosted collector:
159+
160+
OTEL_PHP_AUTOLOAD_ENABLED=true
161+
OTEL_SERVICE_NAME=my-laravel-app
162+
OTEL_TRACES_EXPORTER=otlp
163+
OTEL_EXPORTER_OTLP_ENDPOINT=https://otel-collector.mycompany.com:4318
164+
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
165+
OTEL_EXPORTER_OTLP_HEADERS="authorization=Bearer company-secret-token,x-service-id=laravel-api"
166+
*/
167+
168+
// ============================================================================
169+
// Important Notes
170+
// ============================================================================
171+
/*
172+
1. Security Best Practices:
173+
- Never commit API keys or tokens to source control
174+
- Use environment variables or secure secret management systems
175+
- Rotate credentials regularly
176+
- Use separate credentials for different environments
177+
178+
2. Header Format:
179+
- Format: comma-separated key=value pairs
180+
- No spaces around the equals sign
181+
- Header names are case-insensitive but commonly lowercase
182+
- Values with special characters may need URL encoding
183+
184+
3. Testing Your Configuration:
185+
- Use OTEL_TRACES_EXPORTER=console first to verify telemetry generation
186+
- Check collector/backend documentation for specific header requirements
187+
- Monitor application logs for connection errors
188+
- Use the package's test command: php artisan otel:test
189+
190+
4. Debugging:
191+
- Enable debug logging to see outgoing requests
192+
- Check for typos in header names
193+
- Verify endpoint URL and protocol match your backend
194+
- Ensure network connectivity to the endpoint
195+
196+
5. This package requires NO code changes:
197+
- The OTEL_EXPORTER_OTLP_HEADERS variable is automatically recognized
198+
- The OpenTelemetry PHP SDK handles header injection
199+
- Just set the environment variable and restart your application
200+
*/
201+
202+
// ============================================================================
203+
// Verification Script (Optional)
204+
// ============================================================================
205+
/*
206+
You can verify your configuration using a simple PHP script:
207+
*/
208+
209+
if (php_sapi_name() === 'cli') {
210+
echo "OpenTelemetry OTLP Headers Configuration\n";
211+
echo str_repeat('=', 50) . "\n\n";
212+
213+
// Check if headers are configured
214+
$headers = getenv('OTEL_EXPORTER_OTLP_HEADERS');
215+
216+
if ($headers) {
217+
echo "✓ OTEL_EXPORTER_OTLP_HEADERS is set\n";
218+
echo " Value: {$headers}\n\n";
219+
220+
// Parse and display headers (for verification only - be careful with sensitive data!)
221+
$headerPairs = explode(',', $headers);
222+
echo " Parsed headers:\n";
223+
foreach ($headerPairs as $pair) {
224+
$parts = explode('=', $pair, 2);
225+
if (count($parts) === 2) {
226+
$headerName = trim($parts[0]);
227+
$headerValue = trim($parts[1]);
228+
// Mask sensitive values
229+
$maskedValue = (stripos($headerName, 'key') !== false ||
230+
stripos($headerName, 'token') !== false ||
231+
stripos($headerName, 'authorization') !== false)
232+
? str_repeat('*', min(strlen($headerValue), 20))
233+
: $headerValue;
234+
echo " - {$headerName}: {$maskedValue}\n";
235+
}
236+
}
237+
} else {
238+
echo "✗ OTEL_EXPORTER_OTLP_HEADERS is not set\n";
239+
echo " No authentication headers will be sent to the OTLP endpoint.\n";
240+
}
241+
242+
echo "\n";
243+
echo "Other relevant configuration:\n";
244+
echo " OTEL_EXPORTER_OTLP_ENDPOINT: " . (getenv('OTEL_EXPORTER_OTLP_ENDPOINT') ?: 'not set') . "\n";
245+
echo " OTEL_EXPORTER_OTLP_PROTOCOL: " . (getenv('OTEL_EXPORTER_OTLP_PROTOCOL') ?: 'not set') . "\n";
246+
echo " OTEL_SERVICE_NAME: " . (getenv('OTEL_SERVICE_NAME') ?: 'not set') . "\n";
247+
echo "\n";
248+
}

0 commit comments

Comments
 (0)