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

Commit 749df87

Browse files
committed
feat: Response X-Trace-Id
1 parent c2a5b3c commit 749df87

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

config/otle.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,31 @@
2020
*/
2121
'automatically_trace_requests' => env('OTLE_AUTO_TRACE_REQUESTS', true),
2222

23+
/**
24+
* Allow to trace requests with specific headers. You can use `*` as wildcard.
25+
*/
26+
'allowed_headers' => [
27+
'referer',
28+
'x-*',
29+
'accept',
30+
'request-id',
31+
],
32+
33+
/**
34+
* Sensitive headers will be marked as *** from the span attributes. You can use `*` as wildcard.
35+
*/
36+
'sensitive_headers' => [
37+
// 'cookie',
38+
// 'authorization',
39+
// ...
40+
],
41+
42+
/**
43+
* The name of the header that will be used to pass the trace id in the response.
44+
* if set to `null`, the header will not be added to the response.
45+
*/
46+
'response_trace_header_name' => env('OTLE_RESPONSE_TRACE_HEADER_NAME', 'X-Trace-Id'),
47+
2348
/**
2449
* Will be applied to all channels. you can override it in the channel config.
2550
*/

src/Middlewares/MeasureRequest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,18 @@ public function handle(Request $request, Closure $next, ?string $name = null)
4949
try {
5050
$response = $next($request);
5151

52+
$this->recordHeaders($span, $request);
53+
5254
if ($response instanceof Response) {
5355
$this->recordHttpResponseToSpan($span, $response);
5456
$this->propagateHeaderToResponse($context, $response);
5557
}
5658

59+
// Add trace id to response header if configured.
60+
if ($traceIdHeaderName = config('otle.response_trace_header_name')) {
61+
$response->headers->set($traceIdHeaderName, Measure::traceId());
62+
}
63+
5764
return $response;
5865
} catch (Throwable $exception) {
5966
$span->recordException($exception)
@@ -135,7 +142,7 @@ protected static function httpHostName(Request $request): string
135142
return '';
136143
}
137144

138-
public function getRequestSpanAttributes(Request $request)
145+
public function getRequestSpanAttributes(Request $request): array
139146
{
140147
return [
141148
TraceAttributes::URL_FULL => $request->fullUrl(),

src/Traits/InteractWithHttpHeaders.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Overtrue\LaravelOpenTelemetry\Traits;
44

55
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Str;
67

78
trait InteractWithHttpHeaders
89
{
@@ -35,7 +36,7 @@ public static function getAllowedHeaders(): array
3536

3637
public static function headerIsAllowed(string $header): bool
3738
{
38-
return in_array($header, static::getAllowedHeaders());
39+
return Str::is(static::getAllowedHeaders(), $header);
3940
}
4041

4142
/**
@@ -48,7 +49,7 @@ public static function getSensitiveHeaders(): array
4849

4950
public static function headerIsSensitive(string $header): bool
5051
{
51-
return in_array($header, static::getSensitiveHeaders());
52+
return Str::is(static::getSensitiveHeaders(), $header);
5253
}
5354

5455
protected function normalizeHeaders(array $headers): array

0 commit comments

Comments
 (0)