Skip to content

Commit c970d7f

Browse files
committed
type updates
1 parent 01ce611 commit c970d7f

File tree

17 files changed

+103
-225
lines changed

17 files changed

+103
-225
lines changed

src/Apm.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
class Apm
1212
{
13-
/** @var Transaction|null */
14-
protected static $t;
13+
protected static ?Transaction $t = null;
1514

1615
/**
1716
* Remove the current transaction
@@ -107,7 +106,7 @@ public static function setStartTime(float $time)
107106
/**
108107
* Set end time for transaction. If a transaction has not started yet a new one will be created.
109108
*
110-
* @param float $time Unix timestamp in milliseconds
109+
* @param float|null $time Unix timestamp in milliseconds
111110
*/
112111
public static function setEndTime(?float $time)
113112
{
@@ -169,10 +168,6 @@ public static function tags(): Tags
169168
public static function finish()
170169
{
171170
$t = static::transaction();
172-
if (!$t) {
173-
return;
174-
}
175-
176171
static::mergeTransactionMetadata($t);
177172

178173
if ($t->endTime() === null) {

src/Apm/Transaction.php

Lines changed: 38 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,44 @@ class Transaction
1616
public const TYPE_REQUEST = 'request';
1717
public const TYPE_JOB = 'job';
1818

19-
/** @var string Unique ID for transaction */
20-
protected $id;
19+
/** Unique ID for transaction */
20+
protected string $id;
2121

22-
/** @var string Type of transaction */
23-
protected $type;
22+
/** Type of transaction */
23+
protected string $type;
2424

25-
/** @var string|null Name to identify transaction */
26-
protected $name;
25+
/** Name to identify transaction */
26+
protected ?string $name = null;
2727

28-
/** @var int Start time as milliseconds since epoch */
29-
protected $startTime;
28+
/** Start time as milliseconds since epoch */
29+
protected int $startTime;
3030

31-
/** @var int End time as milliseconds since epoch */
32-
protected $endTime;
31+
/** End time as milliseconds since epoch */
32+
protected ?int $endTime = null;
3333

34-
/** @var int Duration as milliseconds */
35-
protected $duration;
34+
/** Duration as milliseconds */
35+
protected ?int $duration = null;
3636

37-
/** @var Agent Meta information for transaction agent */
38-
protected $agent;
37+
/** Meta information for transaction agent */
38+
protected Agent $agent;
3939

40-
/** @var Http Meta information for request transaction */
41-
protected $http;
40+
/** Meta information for request transaction */
41+
protected ?Http $http = null;
4242

43-
/** @var Service Meta information for transaction service */
44-
protected $service;
43+
/** Meta information for transaction service */
44+
protected ?Service $service = null;
4545

46-
/** @var System Meta information for transaction system */
47-
protected $system;
46+
/** Meta information for transaction system */
47+
protected System $system;
4848

49-
/** @var Tags Custom meta tags */
50-
protected $tags;
49+
/** Custom meta tags */
50+
protected ?Tags $tags = null;
5151

52-
/** @var User Meta information for transaction user */
53-
protected $user;
52+
/** Meta information for transaction user */
53+
protected ?User $user = null;
5454

5555
/** @var Span[] Spans that occur during transaction */
56-
protected $spans;
56+
protected array $spans;
5757

5858
/**
5959
* Deserialize formatted properties array into Transaction object
@@ -66,13 +66,13 @@ public static function createFromArray(array $properties): Transaction
6666
$type = $trace['type'];
6767
$name = $trace['name'];
6868
$startTime = $trace['start_time'];
69-
$endTime = isset($trace['end_time']) ? $trace['end_time'] : null;
70-
$duration = isset($trace['duration']) ? $trace['duration'] : null;
69+
$endTime = $trace['end_time'] ?? null;
70+
$duration = $trace['duration'] ?? null;
7171

7272
# service properties
7373
$service = $properties['service'];
74-
$serviceName = isset($service['name']) ? $service['name'] : null;
75-
$environment = isset($service['environment']) ? $service['environment'] : null;
74+
$serviceName = $service['name'] ?? null;
75+
$environment = $service['environment'] ?? null;
7676

7777
# create transaction
7878
$transaction = new Transaction($id, $type, $name);
@@ -83,37 +83,37 @@ public static function createFromArray(array $properties): Transaction
8383
$transaction->service()->setEnvironment($environment);
8484

8585
# agent properties
86-
$agent = isset($properties['agent']) ? $properties['agent'] : [];
86+
$agent = $properties['agent'] ?? [];
8787
if ($agent !== []) {
8888
$transaction->agent()->fillFromArray($agent);
8989
}
9090

9191
# http properties
92-
$http = isset($properties['http']) ? $properties['http'] : [];
92+
$http = $properties['http'] ?? [];
9393
if ($http !== []) {
9494
$transaction->http()->fillFromArray($http);
9595
}
9696

9797
# system properties
98-
$system = isset($properties['system']) ? $properties['system'] : [];
98+
$system = $properties['system'] ?? [];
9999
if ($system !== []) {
100100
$transaction->system()->fillFromArray($system);
101101
}
102102

103103
# tags
104-
$tags = isset($properties['tags']) ? $properties['tags'] : [];
104+
$tags = $properties['tags'] ?? [];
105105
if ($tags !== []) {
106106
$transaction->tags()->replaceAll($tags);
107107
}
108108

109109
# user properties
110-
$user = isset($properties['user']) ? $properties['user'] : [];
110+
$user = $properties['user'] ?? [];
111111
if ($user !== []) {
112112
$transaction->user()->fillFromArray($user);
113113
}
114114

115115
# spans
116-
$spans = isset($properties['spans']) ? $properties['spans'] : [];
116+
$spans = $properties['spans'] ?? [];
117117
foreach ($spans as $spanProperties) {
118118
$span = Span::createFromArray($transaction, $spanProperties);
119119
$transaction->pushSpan($span);
@@ -255,21 +255,9 @@ public function finish(?int $at = null): Transaction
255255
*/
256256
public function agent(): Agent
257257
{
258-
if ($this->agent === null) {
259-
$this->agent = new Agent();
260-
}
261-
262258
return $this->agent;
263259
}
264260

265-
/**
266-
* Determine if agent information is present
267-
*/
268-
public function hasAgent(): bool
269-
{
270-
return $this->agent !== null;
271-
}
272-
273261
/**
274262
* Service meta information
275263
*/
@@ -315,21 +303,9 @@ public function hasHttp(): bool
315303
*/
316304
public function system(): System
317305
{
318-
if ($this->system === null) {
319-
$this->system = new System();
320-
}
321-
322306
return $this->system;
323307
}
324308

325-
/**
326-
* Determine if system information is present
327-
*/
328-
public function hasSystem(): bool
329-
{
330-
return $this->system !== null;
331-
}
332-
333309
/**
334310
* Custom meta information
335311
*/
@@ -423,7 +399,7 @@ public function newFilesystemSpan(string $name, ?string $parentSpanId = null): S
423399
return $this->newSpan($name, Span::TYPE_FILESYSTEM, $parentSpanId);
424400
}
425401

426-
public function serialize()
402+
public function serialize(): array
427403
{
428404
$spanData = [];
429405
foreach ($this->spans() as $span) {
@@ -442,18 +418,14 @@ public function serialize()
442418
'spans' => $spanData,
443419
];
444420

445-
if ($this->hasAgent()) {
446-
$data['agent'] = $this->agent()->serialize();
447-
}
421+
$data['agent'] = $this->agent()->serialize();
422+
$data['system'] = $this->system()->serialize();
448423
if ($this->hasHttp()) {
449424
$data['http'] = $this->http()->serialize();
450425
}
451426
if ($this->hasService()) {
452427
$data['service'] = $this->service()->serialize();
453428
}
454-
if ($this->hasSystem()) {
455-
$data['system'] = $this->system()->serialize();
456-
}
457429
if ($this->hasTags()) {
458430
$data['tags'] = $this->tags()->serialize();
459431
}

src/Client.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ class Client
77
public const LOGS_ENDPOINT = 'https://ingest.tail.dev/logs';
88
public const APM_ENDPOINT = 'https://ingest.tail.dev/traces';
99

10-
protected $token;
10+
protected ?string $token = null;
1111

12-
protected $logSendHandlers = [];
12+
protected array $logSendHandlers = [];
1313

14-
protected $apmSendHandlers = [];
14+
protected array $apmSendHandlers = [];
1515

16-
public function __construct($token)
16+
public function __construct(?string $token)
1717
{
1818
$this->token = $token;
1919
$this->registerDefaultLogSendHandler();
@@ -50,7 +50,7 @@ public function sendApm(array $transaction)
5050
}
5151
}
5252

53-
public function token()
53+
public function token(): ?string
5454
{
5555
return $this->token;
5656
}

src/Laravel/Trackers/ArtisanTracker.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77

88
class ArtisanTracker implements Tracker
99
{
10-
/**
11-
*
12-
* @param Application $app
13-
*/
14-
public function register($app)
10+
public function register(Application $app)
1511
{
1612
if ($app->runningInConsole()) {
1713
$this->registerArtisan($app);

src/Laravel/Trackers/FrameworkBootTracker.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@
88

99
class FrameworkBootTracker implements Tracker
1010
{
11-
/** @var Span */
12-
protected $span;
11+
protected ?Span $span = null;
1312

14-
/**
15-
* @var Application $app
16-
*/
17-
public function register($app)
13+
public function register(Application $app)
1814
{
1915
$app->booting(function ($app) {
2016
$this->onStartBoot($app);
@@ -25,18 +21,12 @@ public function register($app)
2521
});
2622
}
2723

28-
/**
29-
* @var Application $app
30-
*/
31-
public function onStartBoot($app)
24+
public function onStartBoot(Application $app)
3225
{
3326
$this->span = Apm::newSpan('Framework boot', 'framework');
3427
}
3528

36-
/**
37-
* @var Application $app
38-
*/
39-
public function onFinishBoot($app)
29+
public function onFinishBoot(Application $app)
4030
{
4131
if (!$this->span) {
4232
return;
@@ -45,10 +35,7 @@ public function onFinishBoot($app)
4535
$this->span->finish();
4636
}
4737

48-
/**
49-
* @return Span|null
50-
*/
51-
public function getSpan()
38+
public function getSpan(): ?Span
5239
{
5340
return $this->span;
5441
}

src/Laravel/Trackers/FrameworkStartupTracker.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@
88

99
class FrameworkStartupTracker implements Tracker
1010
{
11-
/** @var Span */
12-
protected $span;
11+
protected ?Span $span = null;
1312

14-
/**
15-
* @var Application $app
16-
*/
17-
public function register($app)
13+
public function register(Application $app)
1814
{
1915
$start = defined('LARAVEL_START') ? LARAVEL_START * 1000 : microtime(true) * 1000;
2016
$this->span = Apm::newSpan('Framework startup', 'framework')->setStartTime($start);
@@ -24,10 +20,7 @@ public function register($app)
2420
});
2521
}
2622

27-
/**
28-
* @var Application $app
29-
*/
30-
public function onStartBoot($app)
23+
public function onStartBoot(Application $app)
3124
{
3225
if (!$this->span) {
3326
return;
@@ -36,10 +29,7 @@ public function onStartBoot($app)
3629
$this->span->finish();
3730
}
3831

39-
/**
40-
* @return Span|null
41-
*/
42-
public function getSpan()
32+
public function getSpan(): ?Span
4333
{
4434
return $this->span;
4535
}

src/Laravel/Trackers/HttpTracker.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212

1313
class HttpTracker implements Tracker
1414
{
15-
/**
16-
* @param Application $app
17-
*/
18-
public function register($app)
15+
public function register(Application $app)
1916
{
2017
$app['events']->listen(RequestHandled::class, [$this, 'requestHandled']);
2118
}

0 commit comments

Comments
 (0)