Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.

Commit c375029

Browse files
committed
Tests and docs
1 parent 2dbdfd9 commit c375029

File tree

7 files changed

+172
-55
lines changed

7 files changed

+172
-55
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@ $client = new AppInsightsPHP\Client\Factory(
1616
$client->trackMessage(...);
1717
$client->trackRequest(...);
1818
// ... all methods available by source package
19-
```
19+
```
20+
21+
### Limitations
22+
23+
Size of the telemetry is limited to [64 kilobytes](https://docs.microsoft.com/en-us/azure/azure-monitor/service-limits#application-insights).
24+
Before trying to send anything to AppInsights a telemetry is validated against that requirement. If it exceeds the limit
25+
exception is thrown. To avoid that exception you should first check if the given data are not too big. You can
26+
use `TelemetryData::exceededMaximumSize()` do it for you.

src/AppInsightsPHP/Client/Client.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function flush(): ?ResponseInterface
5757
}
5858

5959
try {
60-
if (!$this->failureCache->empty()) {
60+
if ($this->failureCache->empty()) {
6161
return $response;
6262
}
6363

src/AppInsightsPHP/Client/ClientFactory.php

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use ApplicationInsights\Telemetry_Client;
88
use Psr\Log\LoggerInterface;
9+
use Psr\SimpleCache\CacheInterface;
910

1011
final class ClientFactory implements ClientFactoryInterface
1112
{

src/AppInsightsPHP/Client/FailureCache.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
final class FailureCache
1111
{
12-
private const CACHE_CHANNEL_KEY = 'app_insights_php.failure_cache';
13-
private const CACHE_CHANNEL_TTL_SEC = 86400;
12+
public const CACHE_CHANNEL_KEY = 'app_insights_php.failure_cache';
13+
public const CACHE_CHANNEL_TTL_SEC = 86400;
1414

1515
private $cache;
1616

tests/AppInsightsPHP/Client/Tests/ClientTest.php

+80-51
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,28 @@
66

77
use AppInsightsPHP\Client\Client;
88
use AppInsightsPHP\Client\Configuration;
9+
use AppInsightsPHP\Client\FailureCache;
10+
use ApplicationInsights\Channel\Contracts\Envelope;
911
use ApplicationInsights\Channel\Contracts\Request_Data;
1012
use ApplicationInsights\Channel\Telemetry_Channel;
1113
use ApplicationInsights\Telemetry_Client;
1214
use PHPUnit\Framework\MockObject\MockObject;
1315
use PHPUnit\Framework\TestCase;
1416
use Psr\Log\LoggerInterface;
17+
use Psr\Log\NullLogger;
1518
use Psr\SimpleCache\CacheInterface;
1619

1720
final class ClientTest extends TestCase
1821
{
1922
public function test_tracking_when_client_is_disabled()
2023
{
21-
$telemetryMock = $this->createMock(Telemetry_Client::class);
24+
$telemetryMock = $this->createTelemetryClientMock();
2225

2326
$client = new Client(
2427
$telemetryMock,
25-
Configuration::createDefault()
28+
Configuration::createDefault(),
29+
new FailureCache($this->createMock(CacheInterface::class)),
30+
new NullLogger()
2631
);
2732

2833
$client->configuration()->disable();
@@ -49,11 +54,13 @@ public function test_tracking_when_client_is_disabled()
4954

5055
public function test_tracking_request_when_option_is_enabled()
5156
{
52-
$telemetryMock = $this->createMock(Telemetry_Client::class);
57+
$telemetryMock = $this->createTelemetryClientMock();
5358

5459
$client = new Client(
5560
$telemetryMock,
56-
Configuration::createDefault()
61+
Configuration::createDefault(),
62+
new FailureCache($this->createMock(CacheInterface::class)),
63+
new NullLogger()
5764
);
5865

5966
$telemetryMock->expects($this->once())
@@ -65,11 +72,13 @@ public function test_tracking_request_when_option_is_enabled()
6572

6673
public function test_tracking_request_when_option_is_disabled()
6774
{
68-
$telemetryMock = $this->createMock(Telemetry_Client::class);
75+
$telemetryMock = $this->createTelemetryClientMock();
6976

7077
$client = new Client(
7178
$telemetryMock,
72-
Configuration::createDefault()
79+
Configuration::createDefault(),
80+
new FailureCache($this->createMock(CacheInterface::class)),
81+
new NullLogger()
7382
);
7483

7584
$client->configuration()->requests()->disable();
@@ -88,11 +97,13 @@ public function test_tracking_request_when_option_is_disabled()
8897

8998
public function test_tracking_dependencies_when_option_is_enabled()
9099
{
91-
$telemetryMock = $this->createMock(Telemetry_Client::class);
100+
$telemetryMock = $this->createTelemetryClientMock();
92101

93102
$client = new Client(
94103
$telemetryMock,
95-
Configuration::createDefault()
104+
Configuration::createDefault(),
105+
new FailureCache($this->createMock(CacheInterface::class)),
106+
new NullLogger()
96107
);
97108

98109
$telemetryMock->expects($this->once())
@@ -104,11 +115,13 @@ public function test_tracking_dependencies_when_option_is_enabled()
104115

105116
public function test_tracking_dependencies_when_option_is_disabled()
106117
{
107-
$telemetryMock = $this->createMock(Telemetry_Client::class);
118+
$telemetryMock = $this->createTelemetryClientMock();
108119

109120
$client = new Client(
110121
$telemetryMock,
111-
Configuration::createDefault()
122+
Configuration::createDefault(),
123+
new FailureCache($this->createMock(CacheInterface::class)),
124+
new NullLogger()
112125
);
113126

114127
$client->configuration()->dependencies()->disable();
@@ -121,11 +134,13 @@ public function test_tracking_dependencies_when_option_is_disabled()
121134

122135
public function test_tracking_dependencies_when_dependency_is_ignored()
123136
{
124-
$telemetryMock = $this->createMock(Telemetry_Client::class);
137+
$telemetryMock = $this->createTelemetryClientMock();
125138

126139
$client = new Client(
127140
$telemetryMock,
128-
Configuration::createDefault()
141+
Configuration::createDefault(),
142+
new FailureCache($this->createMock(CacheInterface::class)),
143+
new NullLogger()
129144
);
130145

131146
$client->configuration()->dependencies()->ignore('dependency_name');
@@ -138,11 +153,13 @@ public function test_tracking_dependencies_when_dependency_is_ignored()
138153

139154
public function test_tracking_exceptions_when_option_is_enabled()
140155
{
141-
$telemetryMock = $this->createMock(Telemetry_Client::class);
156+
$telemetryMock = $this->createTelemetryClientMock();
142157

143158
$client = new Client(
144159
$telemetryMock,
145-
Configuration::createDefault()
160+
Configuration::createDefault(),
161+
new FailureCache($this->createMock(CacheInterface::class)),
162+
new NullLogger()
146163
);
147164

148165
$exception = new \Exception();
@@ -156,11 +173,13 @@ public function test_tracking_exceptions_when_option_is_enabled()
156173

157174
public function test_tracking_exceptions_when_option_is_disabled()
158175
{
159-
$telemetryMock = $this->createMock(Telemetry_Client::class);
176+
$telemetryMock = $this->createTelemetryClientMock();
160177

161178
$client = new Client(
162179
$telemetryMock,
163-
Configuration::createDefault()
180+
Configuration::createDefault(),
181+
new FailureCache($this->createMock(CacheInterface::class)),
182+
new NullLogger()
164183
);
165184

166185
$client->configuration()->exceptions()->disable();
@@ -175,11 +194,13 @@ public function test_tracking_exceptions_when_option_is_disabled()
175194

176195
public function test_tracking_exceptions_that_suppose_to_be_ignored()
177196
{
178-
$telemetryMock = $this->createMock(Telemetry_Client::class);
197+
$telemetryMock = $this->createTelemetryClientMock();
179198

180199
$client = new Client(
181200
$telemetryMock,
182-
Configuration::createDefault()
201+
Configuration::createDefault(),
202+
new FailureCache($this->createMock(CacheInterface::class)),
203+
new NullLogger()
183204
);
184205

185206
$client->configuration()->exceptions()->ignore(\RuntimeException::class);
@@ -193,11 +214,13 @@ public function test_tracking_exceptions_that_suppose_to_be_ignored()
193214

194215
public function test_tracking_traces_when_option_is_enabled()
195216
{
196-
$telemetryMock = $this->createMock(Telemetry_Client::class);
217+
$telemetryMock = $this->createTelemetryClientMock();
197218

198219
$client = new Client(
199220
$telemetryMock,
200-
Configuration::createDefault()
221+
Configuration::createDefault(),
222+
new FailureCache($this->createMock(CacheInterface::class)),
223+
new NullLogger()
201224
);
202225

203226
$telemetryMock->expects($this->once())
@@ -209,11 +232,13 @@ public function test_tracking_traces_when_option_is_enabled()
209232

210233
public function test_tracking_traces_when_option_is_disabled()
211234
{
212-
$telemetryMock = $this->createMock(Telemetry_Client::class);
235+
$telemetryMock = $this->createTelemetryClientMock();
213236

214237
$client = new Client(
215238
$telemetryMock,
216-
Configuration::createDefault()
239+
Configuration::createDefault(),
240+
new FailureCache($this->createMock(CacheInterface::class)),
241+
new NullLogger()
217242
);
218243

219244
$client->configuration()->traces()->disable();
@@ -229,98 +254,102 @@ public function test_flushing_when_client_is_disabled()
229254
$configuration = Configuration::createDefault();
230255
$configuration->disable();
231256

232-
$telemetryMock = $this->createMock(Telemetry_Client::class);
233-
$this->givenTelemetryChannelIsNotEmpty($telemetryMock);
257+
$telemetryMock = $this->createTelemetryClientMock();
234258

235259
$telemetryMock->expects($this->never())->method('flush');
236260

237-
$client = new Client($telemetryMock, $configuration);
261+
$client = new Client(
262+
$telemetryMock,
263+
$configuration,
264+
new FailureCache($this->createMock(CacheInterface::class)),
265+
new NullLogger()
266+
);
238267
$client->flush();
239268
}
240269

241270
public function test_fallback_logger_during_flush_unexpected_exception()
242271
{
243-
$telemetryMock = $this->createMock(Telemetry_Client::class);
272+
$telemetryMock = $this->createTelemetryClientMock();
244273
$loggerMock = $this->createMock(LoggerInterface::class);
245274

246-
$this->givenTelemetryChannelIsNotEmpty($telemetryMock);
247275
$telemetryMock->method('flush')->willThrowException(new \RuntimeException('Unexpected API exception'));
248276

249277
$loggerMock->expects($this->once())
250278
->method('error')
251279
->with('Exception occurred while flushing App Insights Telemetry Client: Unexpected API exception');
252280

253-
$client = new Client($telemetryMock, Configuration::createDefault(), null, $loggerMock);
281+
$client = new Client(
282+
$telemetryMock,
283+
Configuration::createDefault(),
284+
new FailureCache($this->createMock(CacheInterface::class)),
285+
$loggerMock
286+
);
254287
$client->flush();
255288
}
256289

257290
public function test_adding_queue_to_failure_cache_on_unexpected_api_exception_and_cache_is_empty()
258291
{
259-
$telemetryMock = $this->createMock(Telemetry_Client::class);
292+
$telemetryMock = $this->createTelemetryClientMock();
260293
$loggerMock = $this->createMock(LoggerInterface::class);
261294
$cacheMock = $this->createMock(CacheInterface::class);
262295

263-
$this->givenTelemetryChannelIsNotEmpty($telemetryMock);
264296
$telemetryMock->method('flush')->willThrowException(new \RuntimeException('Unexpected API exception'));
265297
$cacheMock->method('has')->willReturn(false);
266298

267299
$cacheMock->expects($this->once())
268300
->method('set')
269-
->with(Client::CACHE_CHANNEL_KEY, serialize(['some_log_entry']), Client::CACHE_CHANNEL_TTL_SEC);
301+
->with(FailureCache::CACHE_CHANNEL_KEY, serialize([new Envelope()]), FailureCache::CACHE_CHANNEL_TTL_SEC);
270302

271-
$client = new Client($telemetryMock, Configuration::createDefault(), $cacheMock, $loggerMock);
303+
$client = new Client($telemetryMock, Configuration::createDefault(), new FailureCache($cacheMock), $loggerMock);
272304
$client->flush();
273305
}
274306

275307
public function test_adding_queue_to_failure_cache_on_unexpected_api_exception_and_cache_is_not_empty()
276308
{
277-
$telemetryMock = $this->createMock(Telemetry_Client::class);
309+
$telemetryMock = $this->createTelemetryClientMock();
278310
$loggerMock = $this->createMock(LoggerInterface::class);
279311
$cacheMock = $this->createMock(CacheInterface::class);
280312

281-
$this->givenTelemetryChannelIsNotEmpty($telemetryMock);
282313
$telemetryMock->method('flush')->willThrowException(new \RuntimeException('Unexpected API exception'));
283314
$cacheMock->method('has')->willReturn(true);
284-
$cacheMock->method('get')->willReturn(serialize(['some_older_entry']));
315+
$cacheMock->method('get')->willReturn(serialize([new Envelope()]));
285316

286317
$cacheMock->expects($this->once())
287318
->method('set')
288-
->with(Client::CACHE_CHANNEL_KEY, serialize(['some_older_entry', 'some_log_entry']), Client::CACHE_CHANNEL_TTL_SEC);
319+
->with(FailureCache::CACHE_CHANNEL_KEY, serialize([new Envelope(), new Envelope()]), FailureCache::CACHE_CHANNEL_TTL_SEC);
289320

290-
$client = new Client($telemetryMock, Configuration::createDefault(), $cacheMock, $loggerMock);
321+
$client = new Client($telemetryMock, Configuration::createDefault(), new FailureCache($cacheMock), $loggerMock);
291322
$client->flush();
292323
}
293324

294325
public function test_flush_when_cache_is_not_empty()
295326
{
296-
$telemetryMock = $this->createMock(Telemetry_Client::class);
327+
$telemetryMock = $this->createTelemetryClientMock();
297328
$loggerMock = $this->createMock(LoggerInterface::class);
298329
$cacheMock = $this->createMock(CacheInterface::class);
299330

300-
$telemetryChannelMock = $this->givenTelemetryChannelIsNotEmpty($telemetryMock);
301331
$cacheMock->method('has')->willReturn(true);
302-
$cacheMock->method('get')->willReturn(serialize(['some_older_entry']));
332+
$cacheMock->method('get')->willReturn(serialize([new Envelope('some_older_entry')]));
303333

304334
$cacheMock->expects($this->once())
305335
->method('delete')
306-
->with(Client::CACHE_CHANNEL_KEY);
307-
308-
$telemetryChannelMock->expects($this->once())
309-
->method('setQueue')
310-
->with(['some_older_entry', 'some_log_entry']);
336+
->with(FailureCache::CACHE_CHANNEL_KEY);
311337

312338
$telemetryMock->expects($this->once())->method('flush');
313339

314-
$client = new Client($telemetryMock, Configuration::createDefault(), $cacheMock, $loggerMock);
340+
$client = new Client($telemetryMock, Configuration::createDefault(), new FailureCache($cacheMock), $loggerMock);
315341
$client->flush();
316342
}
317343

318-
private function givenTelemetryChannelIsNotEmpty(MockObject $telemetryMock): MockObject
344+
private function createTelemetryClientMock(): MockObject
319345
{
320-
$telemetryMock->method('getChannel')->willReturn($telemetryChannelMock = $this->createMock(Telemetry_Channel::class));
321-
$telemetryChannelMock->method('getQueue')->willReturn(['some_log_entry']);
322-
$telemetryChannelMock->method('getSerializedQueue')->willReturn(json_encode(['some_log_entry']));
346+
$telemetryClientMock = $this->createMock(Telemetry_Client::class);
347+
$telemetryClientMock->method('getChannel')->willReturn(
348+
$telemetryChannelMock = $this->createMock(Telemetry_Channel::class)
349+
);
350+
$telemetryChannelMock->method('getQueue')->willReturn([new Envelope()]);
351+
$telemetryChannelMock->method('getSerializedQueue')->willReturn(json_encode([new Envelope()]));
323352

324-
return $telemetryChannelMock;
353+
return $telemetryClientMock;
325354
}
326355
}

0 commit comments

Comments
 (0)