Skip to content

Commit 1aea0df

Browse files
committed
wip
1 parent 0a0c98e commit 1aea0df

21 files changed

+109
-70
lines changed

_register.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// Check if OpenTelemetry extension is loaded
1313
if (extension_loaded('opentelemetry') === false) {
1414
trigger_error('The opentelemetry extension must be loaded in order to autoload the OpenTelemetry Laravel auto-instrumentation', E_USER_WARNING);
15+
1516
return;
1617
}
1718

@@ -21,6 +22,6 @@
2122
}
2223

2324
// Register Laravel enhancements
24-
require_once __DIR__ . '/src/LaravelInstrumentation.php';
25+
require_once __DIR__.'/src/LaravelInstrumentation.php';
2526

2627
\Overtrue\LaravelOpenTelemetry\LaravelInstrumentation::register();

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
"laravel/pint": "^1.15",
3232
"spatie/test-time": "^1.3"
3333
},
34-
"suggest": {},
34+
"suggest": {
35+
"open-telemetry/opentelemetry-auto-guzzle": "If you want to automatically instrument Guzzle HTTP client requests."
36+
},
3537
"autoload-dev": {
3638
"psr-4": {
3739
"Overtrue\\LaravelOpenTelemetry\\Tests\\": "tests/"

src/Facades/Measure.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
namespace Overtrue\LaravelOpenTelemetry\Facades;
66

77
use Illuminate\Support\Facades\Facade;
8-
use OpenTelemetry\API\Trace\SpanBuilderInterface;
98
use OpenTelemetry\API\Trace\SpanInterface;
109
use OpenTelemetry\API\Trace\TracerInterface;
1110
use OpenTelemetry\Context\Context;
12-
use OpenTelemetry\Context\Propagation\PropagationGetterInterface;
1311
use OpenTelemetry\Context\ScopeInterface;
1412

1513
/**
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overtrue\LaravelOpenTelemetry\Hooks\Illuminate\Contracts\Http;
6+
7+
use Illuminate\Contracts\Http\Kernel as KernelContract;
8+
use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHook;
9+
use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHookTrait;
10+
use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\PostHookTrait;
11+
use Overtrue\LaravelOpenTelemetry\Facades\Measure;
12+
use Symfony\Component\HttpFoundation\Response;
13+
14+
use function OpenTelemetry\Instrumentation\hook;
15+
16+
class Kernel implements LaravelHook
17+
{
18+
use LaravelHookTrait;
19+
use PostHookTrait;
20+
21+
public function instrument(): void
22+
{
23+
// Hook into the handle method to add trace ID to response
24+
hook(
25+
class: KernelContract::class,
26+
function: 'handle',
27+
post: function (KernelContract $kernel, array $params, ?Response $response, ?\Throwable $exception) {
28+
if ($response) {
29+
$this->addTraceIdToResponse($response);
30+
}
31+
});
32+
}
33+
34+
/**
35+
* Add trace ID to response headers
36+
*/
37+
private function addTraceIdToResponse(Response $response): void
38+
{
39+
$headerName = config('otel.response_trace_header_name');
40+
41+
// Skip if header name is not configured or empty
42+
if (empty($headerName)) {
43+
return;
44+
}
45+
46+
try {
47+
// Get current trace ID
48+
$traceId = Measure::traceId();
49+
50+
// Add trace ID to response header if it's valid (not empty and not all zeros)
51+
if (! empty($traceId) && $traceId !== '00000000000000000000000000000000') {
52+
$response->headers->set($headerName, $traceId);
53+
}
54+
} catch (\Throwable $e) {
55+
// Silently ignore errors when getting trace ID
56+
// This prevents failures when there's no trace context
57+
}
58+
}
59+
}

src/Hooks/Illuminate/Foundation/Application.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Foundation\Application as FoundationApplication;
88
use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHook;
99
use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHookTrait;
10+
1011
use function OpenTelemetry\Instrumentation\hook;
1112

1213
class Application implements LaravelHook
@@ -15,6 +16,7 @@ class Application implements LaravelHook
1516

1617
public function instrument(): void
1718
{
19+
file_put_contents('/tmp/telemetry.log', 111);
1820
hook(
1921
class: FoundationApplication::class,
2022
function: 'boot',

src/Hooks/Illuminate/Http/Kernel.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,26 @@
55
namespace Overtrue\LaravelOpenTelemetry\Hooks\Illuminate\Http;
66

77
use Illuminate\Http\Kernel as HttpKernel;
8-
use Illuminate\Http\Request;
98
use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHook;
109
use OpenTelemetry\Contrib\Instrumentation\Laravel\Hooks\LaravelHookTrait;
1110
use Overtrue\LaravelOpenTelemetry\Facades\Measure;
1211
use Symfony\Component\HttpFoundation\Response;
12+
1313
use function OpenTelemetry\Instrumentation\hook;
1414

1515
class Kernel implements LaravelHook
1616
{
1717
use LaravelHookTrait;
1818

19-
public function instrument(): void
19+
public function instrument(): void
2020
{
2121
// Hook into the handle method to add trace ID to response
2222
hook(
2323
class: HttpKernel::class,
2424
function: 'handle',
2525
post: function (HttpKernel $kernel, array $params, Response $response) {
2626
$this->addTraceIdToResponse($response);
27+
2728
return $response;
2829
}
2930
);
@@ -46,7 +47,7 @@ private function addTraceIdToResponse(Response $response): void
4647
$traceId = Measure::traceId();
4748

4849
// Add trace ID to response header if it's valid (not empty and not all zeros)
49-
if (!empty($traceId) && $traceId !== '00000000000000000000000000000000') {
50+
if (! empty($traceId) && $traceId !== '00000000000000000000000000000000') {
5051
$response->headers->set($headerName, $traceId);
5152
}
5253
} catch (\Throwable $e) {

src/LaravelInstrumentation.php

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

55
namespace Overtrue\LaravelOpenTelemetry;
66

7-
use Overtrue\LaravelOpenTelemetry\Hooks;
87
use OpenTelemetry\API\Instrumentation\CachedInstrumentation;
98

109
/**
@@ -23,18 +22,8 @@ class LaravelInstrumentation
2322
public static function register(): void
2423
{
2524
// Register ApplicationHook for Watchers
26-
$applicationHookClass = Hooks\Illuminate\Foundation\Application::class;
27-
if (class_exists($applicationHookClass)) {
28-
$hook = $applicationHookClass::hook(self::instrumentation());
29-
$hook->instrument();
30-
}
31-
32-
// Register KernelHook for Response Trace ID
33-
$kernelHookClass = Hooks\Illuminate\Http\Kernel::class;
34-
if (class_exists($kernelHookClass)) {
35-
$hook = $kernelHookClass::hook(self::instrumentation());
36-
$hook->instrument();
37-
}
25+
Hooks\Illuminate\Foundation\Application::hook(self::instrumentation());
26+
Hooks\Illuminate\Contracts\Http\Kernel::hook(self::instrumentation());
3827
}
3928

4029
/**

src/OpenTelemetryServiceProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public function register(): void
4141
Log::debug('[laravel-open-telemetry] registered.');
4242
}
4343

44-
4544
protected function registerCommands()
4645
{
4746
if ($this->app->runningInConsole()) {

src/Support/Measure.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ class Measure
1616
{
1717
protected static ?StartedSpan $currentSpan = null;
1818

19-
public function __construct(protected Application $app)
20-
{
21-
}
19+
public function __construct(protected Application $app) {}
2220

2321
public function span(string $spanName): SpanBuilder
2422
{
@@ -45,7 +43,7 @@ public function end(): void
4543

4644
public function tracer(): TracerInterface
4745
{
48-
return $this->app->make(TracerInterface::class);
46+
return Globals::tracerProvider()->getTracer('io.opentelemetry.contrib.php.laravel');
4947
}
5048

5149
public function activeSpan(): SpanInterface
@@ -68,7 +66,7 @@ public function propagator()
6866
return Globals::propagator();
6967
}
7068

71-
public function propagationHeaders(Context $context = null): array
69+
public function propagationHeaders(?Context $context = null): array
7270
{
7371
$headers = [];
7472
$this->propagator()->inject($headers, null, $context);

src/Support/SpanBuilder.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@
66

77
use Carbon\CarbonInterface;
88
use OpenTelemetry\API\Trace\SpanBuilderInterface;
9-
use OpenTelemetry\API\Trace\SpanContextInterface;
109
use OpenTelemetry\API\Trace\SpanInterface;
11-
use OpenTelemetry\Context\ContextInterface;
12-
use OpenTelemetry\API\Trace\SpanKind;
1310
use OpenTelemetry\Context\Context;
1411

1512
// this file is copied from https://github.com/keepsuit/laravel-opentelemetry/blob/main/src/Support/SpanBuilder.php
1613
class SpanBuilder
1714
{
18-
public function __construct(protected SpanBuilderInterface $builder)
19-
{
20-
}
15+
public function __construct(protected SpanBuilderInterface $builder) {}
2116

2217
public function setParent(?Context $context = null): self
2318
{

0 commit comments

Comments
 (0)