-
Notifications
You must be signed in to change notification settings - Fork 0
Feature | Add middleware for OpenTelemetry #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,751
−122
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
49cfd87
feat: initial opentelemetry configuration and docker initialization
matiasperrone c9266fd
feat: Add middleware
matiasperrone 1b7cf6b
chore: include middleware in every request
matiasperrone a86857f
chore: add check config for enabled otel
matiasperrone ee130fb
chore: add changes requested
matiasperrone 156098f
chore: Add PR's changes requested
matiasperrone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| <?php | ||
|
|
||
| namespace App\Http\Middleware; | ||
|
|
||
| use Closure; | ||
| use Illuminate\Http\Request; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Keepsuit\LaravelOpenTelemetry\Facades\Tracer; | ||
| use Illuminate\Log\LogManager; | ||
| use OpenTelemetry\API\Baggage\Baggage; | ||
| use OpenTelemetry\Context\ScopeInterface; | ||
|
|
||
| class TrackRequestMiddleware | ||
| { | ||
| private const ATTRIBUTE_START_TIME = '_start_time'; | ||
| private const EVENT_REQUEST_STARTED = 'request.started'; | ||
| private const EVENT_REQUEST_FINISHED = 'request.finished'; | ||
|
|
||
| protected LogManager $logger; | ||
| private ?ScopeInterface $baggageScope = null; | ||
| private bool $shouldTrack; | ||
|
|
||
| public function __construct(LogManager $logger) | ||
| { | ||
| $this->logger = $logger; | ||
| $this->shouldTrack = env('OTEL_SERVICE_ENABLED', false); | ||
| } | ||
|
|
||
| public function handle(Request $request, Closure $next) | ||
| { | ||
| if (!$this->shouldTrack) { | ||
| return $next($request); | ||
| } | ||
|
|
||
| try { | ||
| $request->attributes->set(self::ATTRIBUTE_START_TIME, microtime(true)); | ||
| if ($span = Tracer::activeSpan()) { | ||
| if ($ray = $request->header('cf-ray')) { | ||
| $span->setAttribute('cloudflare.cf-ray', $ray); | ||
|
|
||
| $baggage = Baggage::getCurrent() | ||
| ->toBuilder() | ||
| ->set('cf-ray', $ray) | ||
| ->set('user_agent', substr($request->userAgent() ?? 'unknown', 0, 100)) | ||
| ->build(); | ||
|
|
||
| $this->baggageScope = $baggage->activate(); | ||
| } | ||
|
|
||
| $span->addEvent(self::EVENT_REQUEST_STARTED, [ | ||
| 'method' => $request->method(), | ||
| 'url' => $request->fullUrl(), | ||
| ]); | ||
| } | ||
| } catch (\Throwable $e) { | ||
| $this->logger->channel('daily')->error("Error on request tracking: " . $e->getMessage()); | ||
| } | ||
|
|
||
| return $next($request); | ||
| } | ||
|
|
||
| public function terminate(Request $request, Response $response): void | ||
| { | ||
| if (!$this->shouldTrack) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $start = (float) $request->attributes->get(self::ATTRIBUTE_START_TIME, microtime(true)); | ||
| $ms = (int) ((microtime(true) - $start) * 1000); | ||
|
|
||
| if ($span = Tracer::activeSpan()) { | ||
| $span->setAttribute('app.response_ms', $ms); | ||
| $span->setAttribute('http.status_code', $response->getStatusCode()); | ||
| $span->addEvent(self::EVENT_REQUEST_FINISHED, ['response_ms' => $ms]); | ||
| } | ||
| } catch (\Throwable $e) { | ||
| $this->logger->channel('daily')->error("Error on request tracking: " . $e->getMessage()); | ||
| } finally { | ||
| if ($this->baggageScope) { | ||
| $this->baggageScope->detach(); | ||
| $this->baggageScope = null; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.