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

Commit 5bd912f

Browse files
committed
ignored routes
1 parent 5bf6058 commit 5bd912f

15 files changed

+75
-42
lines changed

.idea/laravel-open-telemetry.iml

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/php.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/otel.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
// ...
3333
],
3434

35+
/**
36+
* Ignore routes will not be traced. You can use `*` as wildcard.
37+
*/
38+
'ignore_routes' => [
39+
// 'api/*',
40+
// 'webhook/*',
41+
],
42+
3543
/**
3644
* The name of the header that will be used to pass the trace id in the response.
3745
* if set to `null`, the header will not be added to the response.

src/Middlewares/MeasureRequest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Closure;
66
use Illuminate\Http\Request;
7-
use OpenTelemetry\API\Trace\SpanInterface;
7+
use Illuminate\Support\Str;
88
use OpenTelemetry\API\Trace\StatusCode;
99
use OpenTelemetry\SemConv\TraceAttributes;
1010
use Overtrue\LaravelOpenTelemetry\Facades\Measure;
@@ -22,6 +22,12 @@ class MeasureRequest
2222
*/
2323
public function handle(Request $request, Closure $next, ?string $name = null)
2424
{
25+
// skip if ignored by config `otel.ignore_routes`
26+
$ignoredRoutes = config('otel.ignore_routes', []);
27+
if (Str::is($ignoredRoutes, $request->path())) {
28+
return $next($request);
29+
}
30+
2531
static::$allowedHeaders = $this->normalizeHeaders(config('otel.allowed_headers', []));
2632

2733
static::$sensitiveHeaders = array_merge(
@@ -50,8 +56,6 @@ public function handle(Request $request, Closure $next, ?string $name = null)
5056
}
5157
}
5258

53-
54-
5559
protected static function httpHostName(Request $request): string
5660
{
5761
if (method_exists($request, 'host')) {

src/OpenTelemetryServiceProvider.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
use Illuminate\Contracts\Http\Kernel;
88
use Illuminate\Http\Client\PendingRequest;
99
use Illuminate\Support\ServiceProvider;
10-
use OpenTelemetry\API\Common\Time\Clock;
1110
use Overtrue\LaravelOpenTelemetry\Middlewares\MeasureRequest;
12-
use Overtrue\LaravelOpenTelemetry\Support\CarbonClock;
1311
use Overtrue\LaravelOpenTelemetry\Support\GuzzleTraceMiddleware;
1412
use Overtrue\LaravelOpenTelemetry\Support\Measure;
1513

src/Support/GuzzleTraceMiddleware.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Closure;
66
use GuzzleHttp\Promise\PromiseInterface;
7-
use OpenTelemetry\API\Globals;
87
use OpenTelemetry\API\Trace\SpanInterface;
98
use OpenTelemetry\API\Trace\SpanKind;
109
use OpenTelemetry\API\Trace\StatusCode;
@@ -22,7 +21,12 @@ public static function make(): Closure
2221
{
2322
return static function (callable $handler): callable {
2423
return static function (RequestInterface $request, array $options) use ($handler) {
25-
$span = \Overtrue\LaravelOpenTelemetry\Facades\Measure::span(sprintf('HTTP %s', $request->getMethod()))
24+
$name = sprintf(
25+
'[HTTP] %s %s',
26+
$request->getMethod(),
27+
$request->getUri()->__toString()
28+
);
29+
$span = \Overtrue\LaravelOpenTelemetry\Facades\Measure::span($name)
2630
->setSpanKind(SpanKind::KIND_CLIENT)
2731
->setAttribute(TraceAttributes::URL_FULL, sprintf('%s://%s%s', $request->getUri()->getScheme(), $request->getUri()->getHost(), $request->getUri()->getPath()))
2832
->setAttribute(TraceAttributes::URL_PATH, $request->getUri()->getPath())
@@ -91,4 +95,4 @@ protected static function recordHeaders(SpanInterface $span, RequestInterface|Re
9195

9296
return $span;
9397
}
94-
}
98+
}

src/Support/Measure.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ public function __construct(protected Application $app)
2626
$app->terminating($this->flush(...));
2727
}
2828

29-
public function span(string $name): SpanBuilder
29+
public function span(string $name, ?string $prefix = null): SpanBuilder
3030
{
31+
if ($prefix) {
32+
$name = sprintf('[%s] %s', $prefix, $name);
33+
}
34+
3135
return new SpanBuilder($this->tracer()->spanBuilder($name));
3236
}
3337

src/Support/ResponsePropagationSetter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static function instance(): self
1818
{
1919
static $instance;
2020

21-
return $instance ??= new self();
21+
return $instance ??= new self;
2222
}
2323

2424
public function set(&$carrier, string $key, string $value): void

src/Support/SpanBuilder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ class SpanBuilder
1313
{
1414
public function __construct(
1515
protected SpanBuilderInterface $spanBuilder
16-
) {
17-
}
16+
) {}
1817

1918
public function setParent(?ContextInterface $context): SpanBuilder
2019
{
@@ -73,6 +72,9 @@ public function start(): SpanInterface
7372
return $this->spanBuilder->startSpan();
7473
}
7574

75+
/**
76+
* @throws \Throwable
77+
*/
7678
public function measure(\Closure $callback): mixed
7779
{
7880
$span = $this->spanBuilder->startSpan();

src/Support/StartedSpan.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,5 @@
77

88
class StartedSpan
99
{
10-
public function __construct(public SpanInterface $span, public ScopeInterface $scope)
11-
{
12-
}
10+
public function __construct(public SpanInterface $span, public ScopeInterface $scope) {}
1311
}

0 commit comments

Comments
 (0)