diff --git a/config/otle.php b/config/otle.php index 3e19c43..24f01fe 100644 --- a/config/otle.php +++ b/config/otle.php @@ -20,6 +20,31 @@ */ 'automatically_trace_requests' => env('OTLE_AUTO_TRACE_REQUESTS', true), + /** + * Allow to trace requests with specific headers. You can use `*` as wildcard. + */ + 'allowed_headers' => [ + 'referer', + 'x-*', + 'accept', + 'request-id', + ], + + /** + * Sensitive headers will be marked as *** from the span attributes. You can use `*` as wildcard. + */ + 'sensitive_headers' => [ + // 'cookie', + // 'authorization', + // ... + ], + + /** + * The name of the header that will be used to pass the trace id in the response. + * if set to `null`, the header will not be added to the response. + */ + 'response_trace_header_name' => env('OTLE_RESPONSE_TRACE_HEADER_NAME', 'X-Trace-Id'), + /** * Will be applied to all channels. you can override it in the channel config. */ diff --git a/src/Middlewares/MeasureRequest.php b/src/Middlewares/MeasureRequest.php index a06bf68..ab491cf 100644 --- a/src/Middlewares/MeasureRequest.php +++ b/src/Middlewares/MeasureRequest.php @@ -49,11 +49,18 @@ public function handle(Request $request, Closure $next, ?string $name = null) try { $response = $next($request); + $this->recordHeaders($span, $request); + if ($response instanceof Response) { $this->recordHttpResponseToSpan($span, $response); $this->propagateHeaderToResponse($context, $response); } + // Add trace id to response header if configured. + if ($traceIdHeaderName = config('otle.response_trace_header_name')) { + $response->headers->set($traceIdHeaderName, Measure::traceId()); + } + return $response; } catch (Throwable $exception) { $span->recordException($exception) @@ -135,7 +142,7 @@ protected static function httpHostName(Request $request): string return ''; } - public function getRequestSpanAttributes(Request $request) + public function getRequestSpanAttributes(Request $request): array { return [ TraceAttributes::URL_FULL => $request->fullUrl(), diff --git a/src/Traits/InteractWithHttpHeaders.php b/src/Traits/InteractWithHttpHeaders.php index 6ce33ce..a997c66 100644 --- a/src/Traits/InteractWithHttpHeaders.php +++ b/src/Traits/InteractWithHttpHeaders.php @@ -3,6 +3,7 @@ namespace Overtrue\LaravelOpenTelemetry\Traits; use Illuminate\Support\Arr; +use Illuminate\Support\Str; trait InteractWithHttpHeaders { @@ -35,7 +36,7 @@ public static function getAllowedHeaders(): array public static function headerIsAllowed(string $header): bool { - return in_array($header, static::getAllowedHeaders()); + return Str::is(static::getAllowedHeaders(), $header); } /** @@ -48,7 +49,7 @@ public static function getSensitiveHeaders(): array public static function headerIsSensitive(string $header): bool { - return in_array($header, static::getSensitiveHeaders()); + return Str::is(static::getSensitiveHeaders(), $header); } protected function normalizeHeaders(array $headers): array