1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Telemetry |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Slick\Telemetry\TelemetryClient; |
13
|
|
|
|
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use Slick\Event\Event; |
16
|
|
|
use Slick\Telemetry\Model\Dependency; |
17
|
|
|
use Slick\Telemetry\Model\Event as TrackableEvent; |
18
|
|
|
use Slick\Telemetry\Model\ExceptionTrackable; |
19
|
|
|
use Slick\Telemetry\Model\Metric; |
20
|
|
|
use Slick\Telemetry\Model\PageView; |
21
|
|
|
use Slick\Telemetry\Model\Request; |
22
|
|
|
use Slick\Telemetry\TelemetryClient; |
23
|
|
|
use Slick\Telemetry\Trackable; |
24
|
|
|
use Throwable; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* TelemetryClientTrait trait |
28
|
|
|
* |
29
|
|
|
* @package Slick\Telemetry\TelemetryClient |
30
|
|
|
*/ |
31
|
|
|
trait TelemetryClientTrait |
32
|
|
|
{ |
33
|
|
|
protected iterable $context = []; |
34
|
|
|
protected LoggerInterface $logger; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritDoc |
38
|
|
|
*/ |
39
|
|
|
public function context(): iterable |
40
|
|
|
{ |
41
|
|
|
return $this->context; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
|
|
public function withContextParam(string $param, $value): iterable |
48
|
|
|
{ |
49
|
|
|
$this->context[$param] = $value; |
50
|
|
|
return $this->context; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
|
|
public function withoutContextParam(string $param): iterable |
57
|
|
|
{ |
58
|
|
|
if (array_key_exists($param, $this->context)) { |
59
|
|
|
unset($this->context[$param]); |
60
|
|
|
} |
61
|
|
|
return $this->context; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
abstract public function track(Trackable $trackableData): TelemetryClient; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
|
|
public function trackPageView( |
70
|
|
|
string $message, |
71
|
|
|
string $path, |
72
|
|
|
float $duration = 0, |
73
|
|
|
iterable $context = [] |
74
|
|
|
): TelemetryClient { |
75
|
|
|
return $this->track(new PageView($message, $path, $duration, $context)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
|
|
public function trackMetric( |
82
|
|
|
string $message, |
83
|
|
|
float $value, |
84
|
|
|
?int $count = null, |
85
|
|
|
?float $min = null, |
86
|
|
|
?float $max = null, |
87
|
|
|
?float $stdDev = null, |
88
|
|
|
iterable $context = [] |
89
|
|
|
): TelemetryClient { |
90
|
|
|
$this->track( |
91
|
|
|
new Metric($message, $value, $count, $min, $max, $stdDev, $context) |
92
|
|
|
); |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritDoc |
98
|
|
|
*/ |
99
|
|
|
public function trackEvent(Event $event, iterable $context = []): TelemetryClient |
100
|
|
|
{ |
101
|
|
|
$this->track(new TrackableEvent($event, $context)); |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritDoc |
107
|
|
|
*/ |
108
|
|
|
public function trackRequest( |
109
|
|
|
string $message, |
110
|
|
|
string $path, |
111
|
|
|
int $startTime, |
112
|
|
|
int $statusCode = 200, |
113
|
|
|
float $duration = 0, |
114
|
|
|
iterable $context = [] |
115
|
|
|
): TelemetryClient { |
116
|
|
|
$this->track( |
117
|
|
|
new Request($message, $path, $startTime, $statusCode, $duration, $context) |
118
|
|
|
); |
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritDoc |
124
|
|
|
*/ |
125
|
|
|
public function trackDependency( |
126
|
|
|
string $message, |
127
|
|
|
string $type, |
128
|
|
|
?string $command = null, |
129
|
|
|
?int $startTime = null, |
130
|
|
|
float $duration = 0, |
131
|
|
|
bool $successful = true, |
132
|
|
|
iterable $context = [] |
133
|
|
|
): TelemetryClient { |
134
|
|
|
$this->track(new Dependency($message, $type, $command, $startTime, $duration, $successful, $context)); |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @inheritDoc |
140
|
|
|
*/ |
141
|
|
|
public function trackException(Throwable $exception, iterable $context = []): TelemetryClient |
142
|
|
|
{ |
143
|
|
|
$this->track(new ExceptionTrackable($exception, $context)); |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|