Skip to content

Commit

Permalink
feat: Response X-Trace-Id
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Apr 25, 2024
1 parent c2a5b3c commit 749df87
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
25 changes: 25 additions & 0 deletions config/otle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
9 changes: 8 additions & 1 deletion src/Middlewares/MeasureRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(),
Expand Down
5 changes: 3 additions & 2 deletions src/Traits/InteractWithHttpHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Overtrue\LaravelOpenTelemetry\Traits;

use Illuminate\Support\Arr;
use Illuminate\Support\Str;

trait InteractWithHttpHeaders
{
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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
Expand Down

0 comments on commit 749df87

Please sign in to comment.