Skip to content

Commit

Permalink
Flush metrics when events are being flushed and after each request (#841
Browse files Browse the repository at this point in the history
)

* Also flush metrics when events are being flushed

* Add middleware to flush events after request
  • Loading branch information
stayallive authored Jan 29, 2024
1 parent 994af1f commit 7b19858
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/Sentry/Laravel/Http/FlushEventsMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Sentry\Laravel\Http;

use Closure;
use Illuminate\Http\Request;
use Sentry\Laravel\Integration;

class FlushEventsMiddleware
{
public function handle(Request $request, Closure $next)
{
return $next($request);
}

public function terminate(Request $request, $response): void
{
Integration::flushEvents();
}
}
5 changes: 4 additions & 1 deletion src/Sentry/Laravel/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use function Sentry\getBaggage;
use function Sentry\getTraceparent;
use function Sentry\getW3CTraceparent;
use function Sentry\metrics;

class Integration implements IntegrationInterface
{
Expand Down Expand Up @@ -100,7 +101,7 @@ public static function setTransaction(?string $transaction): void
}

/**
* Block until all async events are processed for the HTTP transport.
* Block until all events are processed by the PHP SDK client. Also flushes metrics.
*
* @internal This is not part of the public API and is here temporarily until
* the underlying issue can be resolved, this method will be removed.
Expand All @@ -112,6 +113,8 @@ public static function flushEvents(): void
if ($client !== null) {
$client->flush();
}

metrics()->flush();
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Sentry/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sentry\Laravel\Console\PublishCommand;
use Sentry\Laravel\Console\TestCommand;
use Sentry\Laravel\Features\Feature;
use Sentry\Laravel\Http\FlushEventsMiddleware;
use Sentry\Laravel\Http\LaravelRequestFetcher;
use Sentry\Laravel\Http\SetRequestIpMiddleware;
use Sentry\Laravel\Http\SetRequestMiddleware;
Expand Down Expand Up @@ -78,12 +79,14 @@ public function boot(): void
if ($this->app instanceof Lumen) {
$this->app->middleware(SetRequestMiddleware::class);
$this->app->middleware(SetRequestIpMiddleware::class);
$this->app->middleware(FlushEventsMiddleware::class);
} elseif ($this->app->bound(HttpKernelInterface::class)) {
$httpKernel = $this->app->make(HttpKernelInterface::class);

if ($httpKernel instanceof HttpKernel) {
$httpKernel->pushMiddleware(SetRequestMiddleware::class);
$httpKernel->pushMiddleware(SetRequestIpMiddleware::class);
$httpKernel->pushMiddleware(FlushEventsMiddleware::class);
}
}
}
Expand Down

0 comments on commit 7b19858

Please sign in to comment.