Skip to content

Commit d5a2885

Browse files
committed
Handle api and web middleware differently
1 parent c5fd243 commit d5a2885

4 files changed

+60
-17
lines changed

src/AppInsightsHelpers.php

-10
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ public function __construct(AppInsightsServer $appInsights)
2929
*/
3030
public function trackPageViewDuration($request)
3131
{
32-
if($request->expectsJson())
33-
{
34-
return;
35-
}
36-
3732
if (!$this->telemetryEnabled())
3833
{
3934
return;
@@ -158,11 +153,6 @@ private function getRequestPropertiesFromException(Throwable $e)
158153
*/
159154
public function flashPageInfo($request)
160155
{
161-
if($request->expectsJson())
162-
{
163-
return;
164-
}
165-
166156
if (!$this->telemetryEnabled())
167157
{
168158
return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
namespace Larasahib\AppInsightsLaravel\Middleware;
3+
4+
use Closure;
5+
use Larasahib\AppInsightsLaravel\AppInsightsHelpers;
6+
7+
class AppInsightsApiMiddleware
8+
{
9+
10+
/**
11+
* @var AppInsightsHelpers
12+
*/
13+
private AppInsightsHelpers $appInsightsHelpers;
14+
15+
16+
/**
17+
* @param AppInsightsHelpers $appInsightssHelpers
18+
*/
19+
public function __construct(AppInsightsHelpers $appInsightsHelpers)
20+
{
21+
$this->appInsightsHelpers = $appInsightsHelpers;
22+
}
23+
24+
/**
25+
* Handle an incoming request.
26+
*
27+
* @param \Illuminate\Http\Request $request
28+
* @param \Closure $next
29+
* @return mixed
30+
*/
31+
public function handle($request, Closure $next)
32+
{
33+
return $next($request);
34+
}
35+
36+
/**
37+
* @param \Illuminate\Http\Request $request
38+
* @param \Illuminate\Http\Response $response
39+
* @return void
40+
*/
41+
public function terminate($request, $response)
42+
{
43+
$this->appInsightsHelpers->trackRequest($request, $response);
44+
}
45+
46+
}

src/Middleware/AppInsightsMiddleware.php src/Middleware/AppInsightsWebMiddleware.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Closure;
55
use Larasahib\AppInsightsLaravel\AppInsightsHelpers;
66

7-
class AppInsightsMiddleware
7+
class AppInsightsWebMiddleware
88
{
99

1010
/**
@@ -19,7 +19,7 @@ class AppInsightsMiddleware
1919
public function __construct(AppInsightsHelpers $appInsightsHelpers)
2020
{
2121
$this->appInsightsHelpers = $appInsightsHelpers;
22-
}
22+
}
2323

2424
/**
2525
* Handle an incoming request.

src/Providers/AppInsightsServiceProvider.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
use ApplicationInsights\Telemetry_Client;
66
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
77
use Larasahib\AppInsightsLaravel\Queue\AppInsightsTelemeteryQueue;
8-
use Larasahib\AppInsightsLaravel\Middleware\AppInsightsMiddleware;
8+
use Larasahib\AppInsightsLaravel\Middleware\AppInsightsWebMiddleware;
9+
use Larasahib\AppInsightsLaravel\Middleware\AppInsightsApiMiddleware;
910
use Larasahib\AppInsightsLaravel\AppInsightsClient;
1011
use Larasahib\AppInsightsLaravel\AppInsightsHelpers;
1112
use Larasahib\AppInsightsLaravel\AppInsightsServer;
@@ -40,9 +41,14 @@ public function register()
4041
return new AppInsightsServer($telemetryClient);
4142
});
4243

43-
$this->app->singleton('AppInsightsMiddleware', function ($app) {
44+
$this->app->singleton('AppInsightsWebMiddleware', function ($app) {
4445
$appInsightsHelpers = new AppInsightsHelpers($app['AppInsightsServer']);
45-
return new AppInsightsMiddleware($appInsightsHelpers);
46+
return new AppInsightsWebMiddleware($appInsightsHelpers);
47+
});
48+
49+
$this->app->singleton('AppInsightsApiMiddleware', function ($app) {
50+
$appInsightsHelpers = new AppInsightsHelpers($app['AppInsightsServer']);
51+
return new AppInsightsApiMiddleware($appInsightsHelpers);
4652
});
4753

4854
$this->app->singleton('AppInsightsClient', function ($app) {
@@ -59,8 +65,9 @@ public function provides() {
5965

6066
return [
6167
'AppInsightsServer',
62-
'AppInsightsMiddleware',
63-
'AppInsightsClient'
68+
'AppInsightsWebMiddleware',
69+
'AppInsightsClient',
70+
"AppInsightsApiMiddleware"
6471
];
6572
}
6673

0 commit comments

Comments
 (0)