Skip to content

Commit c5fd243

Browse files
committed
Adding queue config option for flushing telemetry queue immediately/later
1 parent 9a05166 commit c5fd243

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

Diff for: config/AppInsightsLaravel.php

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
*/
1717

1818
'instrumentationKey' => env('MS_INSTRUMENTATION_KEY', null),
19+
'flushQueueAfterSeconds' => env('MS_AI_FLUSH_QUEUE_AFTER_SECONDS', 0),
1920

2021
];

Diff for: src/AppInsightsHelpers.php

+32-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public function trackPageViewDuration($request)
4646

4747
$properties = $this->getPageViewProperties($request);
4848
\AIServer::trackMessage('browse_duration', $properties);
49-
\AIQueue::dispatch(\AIServer::getChannel()->getQueue())->delay(now()->addSeconds(3));
49+
$this->flush();
50+
5051
}
5152

5253

@@ -77,7 +78,7 @@ public function trackRequest($request, $response)
7778
$this->getRequestProperties($request),
7879
$this->getRequestMeasurements($request, $response)
7980
);
80-
\AIQueue::dispatch(\AIServer::getChannel()->getQueue())->delay(now()->addSeconds(3));
81+
$this->flush();
8182
}
8283

8384
/**
@@ -92,9 +93,37 @@ public function trackException(Throwable $e)
9293
return;
9394
}
9495
\AIServer::trackException($e, $this->getRequestPropertiesFromException($e));
95-
\AIQueue::dispatch(\AIServer::getChannel()->getQueue())->delay(now()->addSeconds(3));
96+
$this->flush();
9697
}
9798

99+
/**
100+
* flushes the telemery queue, will wait for the time provided in config
101+
* if time was not set in config then it wil flush immediately
102+
*/
103+
private function flush()
104+
{
105+
$queue_seconds = $this->appInsights->getFlushQueueAfterSeconds();
106+
if($queue_seconds)
107+
{
108+
\AIQueue::dispatch(\AIServer::getChannel()->getQueue())
109+
->delay(now()->addSeconds($queue_seconds));
110+
}
111+
else
112+
{
113+
try
114+
{
115+
\AIServer::flush();
116+
}
117+
catch (RequestException $e)
118+
{
119+
Log::debug('RequestException: Could not flush AIServer server. Error:'.$e->getMessage());
120+
}
121+
catch(Exception $e)
122+
{
123+
Log::debug('Exception: Could not flush AIServer server. Error:'.$e->getMessage());
124+
}
125+
}
126+
}
98127

99128
/**
100129
* Get request properties from the exception trace, if available

Diff for: src/Handlers/AppInsightsExceptionHandler.php

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
<?php
22
namespace Larasahib\AppInsightsLaravel\Handlers;
3-
3+
use Larasahib\AppInsightsLaravel\AppInsightsHelpers;
44
use Throwable;
55
use Illuminate\Contracts\Container\Container;
66
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
77

88
class AppInsightsExceptionHandler extends ExceptionHandler
99
{
10+
/**
11+
* @var appInsightsHelpers
12+
*/
13+
private AppInsightsHelpers $appInsightsHelpers;
14+
15+
16+
public function __construct(AppInsightsHelpers $appInsightsHelpers, Container $container)
17+
{
18+
parent::__construct($container);
19+
$this->appInsightsHelpers = $appInsightsHelpers;
20+
}
1021
/**
1122
* Report or log an exception.
1223
*
@@ -17,17 +28,7 @@ class AppInsightsExceptionHandler extends ExceptionHandler
1728
*/
1829
public function report(Throwable $e)
1930
{
20-
if($this->telemetryEnabled())
21-
{
22-
\AIServer::trackException($e);
23-
\AIQueue::dispatch(\AIServer::getChannel()->getQueue())
24-
->delay(now()->addSeconds(3));
25-
}
31+
$this->appInsightsHelpers->trackException($e);
2632
return parent::report($e);
2733
}
28-
29-
private function telemetryEnabled()
30-
{
31-
return \AIServer::getChannel() != null;
32-
}
3334
}

Diff for: src/InstrumentationKey.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class InstrumentationKey
77
{
88
protected $instrumentationKey;
9+
protected $flushQueueAfterSeconds;
910

1011
public function __construct()
1112
{
@@ -15,7 +16,7 @@ public function __construct()
1516
protected function setInstrumentationKey()
1617
{
1718
$instrumentationKey = config('AppInsightsLaravel.instrumentationKey');
18-
19+
$this->flushQueueAfterSeconds = config('AppInsightsLaravel.flushQueueAfterSeconds');
1920
if ( ! empty($instrumentationKey)
2021
&& $this->checkInstrumentationKeyValidity($instrumentationKey))
2122
{
@@ -36,4 +37,9 @@ protected function checkInstrumentationKeyValidity($instrumentationKey)
3637

3738
throw new InvalidInstrumentationKeyException("'{$instrumentationKey}' is not a valid Microsoft Application Insights instrumentation key.");
3839
}
40+
41+
public function getFlushQueueAfterSeconds()
42+
{
43+
return $this->flushQueueAfterSeconds ?? 0;
44+
}
3945
}

0 commit comments

Comments
 (0)