Skip to content

Commit 692fbea

Browse files
authored
feat: Implement OpenTelemetry Basic Instrumentation (#320)
* feat: install keepsuit/laravel-opentelemetry library * feat: add otlp driver to Monolog * feat: add basic instrumentation middleware * fix: final fixes in middleware * fix: MR comments, generating dynamic span id * feat: Add Open Telemetry environment vars * feat: default for env variable
1 parent 33574ca commit 692fbea

File tree

7 files changed

+1390
-45
lines changed

7 files changed

+1390
-45
lines changed

.env.example

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,8 @@ EMAIL_TEMPLATE_GENERIC_SPEAKER_BANNER=https://spaces.fnvirtual.app/FNTECH/Generi
203203
EMAIL_TEMPLATE_TICKET_TOP_GRAPHIC=https://spaces.fnvirtual.app/emails/REGISTRATION_ATTENDEE_TICKET/header-top.jpg
204204
EMAIL_TEMPLATE_TICKET_BOTTOM_GRAPHIC=https://spaces.fnvirtual.app/emails/REGISTRATION_ATTENDEE_TICKET/header-bottom.jpg
205205
EMAIL_TEMPLATE_PRIMARY_COLOR="#000000"
206-
EMAIL_TEMPLATE_SECONDARY_COLOR="#808080"
206+
EMAIL_TEMPLATE_SECONDARY_COLOR="#808080"
207+
208+
#Open Telemetry vars
209+
OTEL_EXPORTER_OTLP_ENDPOINT=
210+
TRACE_SPAN_PREFIX=SPAN

app/Http/Kernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Kernel extends HttpKernel
2828
protected $middleware = [
2929
//\App\Http\Middleware\TrustProxies::class,
3030
\App\Http\Middleware\CheckForMaintenanceMode::class,
31+
\App\Http\Middleware\TrackRequestMiddleware::class,
3132
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
3233
\App\Http\Middleware\TrimStrings::class,
3334
//\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Illuminate\Http\Request;
6+
use Closure;
7+
use Illuminate\Support\Str;
8+
use \OpenTelemetry\API\Trace\SpanInterface;
9+
use Symfony\Component\HttpFoundation\Response;
10+
use Illuminate\Log\LogManager;
11+
use Keepsuit\LaravelOpenTelemetry\Facades\Tracer;
12+
13+
14+
class TrackRequestMiddleware
15+
{
16+
/**
17+
* @var LogManager
18+
*/
19+
protected LogManager $logger;
20+
21+
/**
22+
* @var float
23+
*/
24+
protected float $startTime = 0;
25+
26+
/**
27+
* @var SpanInterface
28+
*/
29+
protected SpanInterface $span;
30+
31+
/**
32+
* Constructor del middleware.
33+
* Laravel inyectará el LogManager aquí.
34+
*
35+
* @param LogManager $logger
36+
*/
37+
public function __construct(LogManager $logger)
38+
{
39+
$this->logger = $logger;
40+
}
41+
42+
/**
43+
* @param \Illuminate\Http\Request $request
44+
* @param \Closure $next
45+
* @return mixed
46+
*/
47+
public function handle(Request $request, Closure $next)
48+
{
49+
try {
50+
// generating dynamic id for span with configurable prefix
51+
$spanId = env('TRACE_SPAN_PREFIX', 'SPAN') . '_' . Str::uuid();
52+
$this->startTime = microtime(true);
53+
$this->span = Tracer::newSpan($spanId)->start();
54+
55+
$this->logger->channel('otlp')->info('Request started.', [
56+
'endpoint' => $request->url(),
57+
'method' => $request->method(),
58+
'timestamp_utc' => now()->toIso8601String(),
59+
]);
60+
} catch (\Throwable $e) {
61+
// forcing 'single' channel in case otlp log fails
62+
$this->logger->channel('single')->error("Error on request tracking" . $e->getMessage());
63+
}
64+
65+
$response = $next($request);
66+
return $response;
67+
}
68+
69+
/**
70+
* @param Request $request
71+
* @param Response $response
72+
* @return void
73+
*/
74+
public function terminate(Request $request, Response $response): void
75+
{
76+
try {
77+
$endTime = microtime(true);
78+
$responseTime = intval(($endTime - $this->startTime) * 1000);
79+
$this->logger->channel('otlp')->info('Request finished.', [
80+
'response_time' => $responseTime,
81+
]);
82+
83+
if (isset($this->span)) {
84+
$this->span->end();
85+
}
86+
87+
} catch (\Throwable $e) {
88+
// forcing 'single' channel in case otlp log fails
89+
$this->logger->channel('single')->error("Error on request tracking: " . $e->getMessage());
90+
}
91+
}
92+
}

composer.json

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,8 @@
1515
"ext-openssl": "*",
1616
"ext-pdo": "*",
1717
"ext-zlib": "*",
18-
"laravel/framework": "12.1.1",
19-
"laravel/helpers": "^1.7.0",
20-
"laravel/tinker": "2.10.1",
21-
"laravel-doctrine/orm": "3.1.1",
22-
"laravel-doctrine/extensions": "2.0.1",
23-
"laravel-doctrine/migrations": "3.4.0",
24-
"beberlei/doctrineextensions": "1.5.0",
2518
"affinipay/chargeio-php": "dev-master",
19+
"beberlei/doctrineextensions": "1.5.0",
2620
"behat/transliterator": "^1.2",
2721
"caseyamcl/guzzle_retry_middleware": "^2.6",
2822
"cocur/slugify": "^2.3",
@@ -33,6 +27,14 @@
3327
"google/apiclient": "^2.2",
3428
"guzzlehttp/guzzle": "7.8.2",
3529
"guzzlehttp/uri-template": "^1.0",
30+
"keepsuit/laravel-opentelemetry": "^1.6",
31+
"laminas/laminas-math": "^3.7",
32+
"laravel-doctrine/extensions": "2.0.1",
33+
"laravel-doctrine/migrations": "3.4.0",
34+
"laravel-doctrine/orm": "3.1.1",
35+
"laravel/framework": "12.1.1",
36+
"laravel/helpers": "^1.7.0",
37+
"laravel/tinker": "2.10.1",
3638
"league/csv": "^9.6",
3739
"league/flysystem-aws-s3-v3": "3.8.0",
3840
"league/oauth2-client": "^2.4",
@@ -48,8 +50,7 @@
4850
"stripe/stripe-php": "^7.95.0",
4951
"symfony/yaml": "^7.1.1",
5052
"tecnickcom/tcpdf": "^6.2",
51-
"vladimir-yuldashev/laravel-queue-rabbitmq": "v14.2.0",
52-
"laminas/laminas-math": "^3.7"
53+
"vladimir-yuldashev/laravel-queue-rabbitmq": "v14.2.0"
5354
},
5455
"require-dev": {
5556
"fakerphp/faker": "^1.23",
@@ -114,7 +115,9 @@
114115
"sort-packages": true,
115116
"optimize-autoloader": true,
116117
"allow-plugins": {
117-
"composer/package-versions-deprecated": true
118+
"composer/package-versions-deprecated": true,
119+
"php-http/discovery": true,
120+
"tbachert/spi": true
118121
}
119122
},
120123
"minimum-stability": "dev",

0 commit comments

Comments
 (0)