-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathMetricsAggregator.php
More file actions
149 lines (125 loc) · 4.39 KB
/
MetricsAggregator.php
File metadata and controls
149 lines (125 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
declare(strict_types=1);
namespace Sentry\Metrics;
use Sentry\Client;
use Sentry\Event;
use Sentry\EventId;
use Sentry\Metrics\Types\CounterMetric;
use Sentry\Metrics\Types\DistributionMetric;
use Sentry\Metrics\Types\GaugeMetric;
use Sentry\Metrics\Types\Metric;
use Sentry\SentrySdk;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Sentry\Unit;
use Sentry\Util\RingBuffer;
/**
* @internal
*/
final class MetricsAggregator
{
/**
* @var int
*/
public const METRICS_BUFFER_SIZE = 1000;
/**
* @var RingBuffer<Metric>
*/
private $metrics;
public function __construct()
{
$this->metrics = new RingBuffer(self::METRICS_BUFFER_SIZE);
}
private const METRIC_TYPES = [
CounterMetric::TYPE => CounterMetric::class,
DistributionMetric::TYPE => DistributionMetric::class,
GaugeMetric::TYPE => GaugeMetric::class,
];
/**
* @param int|float $value
* @param array<string, int|float|string|bool> $attributes
*/
public function add(
string $type,
string $name,
$value,
array $attributes,
?Unit $unit
): void {
$hub = SentrySdk::getCurrentHub();
$client = $hub->getClient();
if (!\is_int($value) && !\is_float($value)) {
if ($client !== null) {
$client->getOptions()->getLoggerOrNullLogger()->debug('Metrics value is neither int nor float. Metric will be discarded');
}
return;
}
if ($client instanceof Client) {
$options = $client->getOptions();
if ($options->getEnableMetrics() === false) {
return;
}
$defaultAttributes = [
'sentry.sdk.name' => $client->getSdkIdentifier(),
'sentry.sdk.version' => $client->getSdkVersion(),
'sentry.environment' => $options->getEnvironment() ?? Event::DEFAULT_ENVIRONMENT,
'server.address' => $options->getServerName(),
];
if ($options->shouldSendDefaultPii()) {
$hub->configureScope(static function (Scope $scope) use (&$defaultAttributes) {
$user = $scope->getUser();
if ($user !== null) {
if ($user->getId() !== null) {
$defaultAttributes['user.id'] = $user->getId();
}
if ($user->getEmail() !== null) {
$defaultAttributes['user.email'] = $user->getEmail();
}
if ($user->getUsername() !== null) {
$defaultAttributes['user.name'] = $user->getUsername();
}
}
});
}
$release = $options->getRelease();
if ($release !== null) {
$defaultAttributes['sentry.release'] = $release;
}
$attributes += $defaultAttributes;
}
$spanId = null;
$traceId = null;
$span = $hub->getSpan();
if ($span !== null) {
$spanId = $span->getSpanId();
$traceId = $span->getTraceId();
} else {
$hub->configureScope(static function (Scope $scope) use (&$traceId, &$spanId) {
$propagationContext = $scope->getPropagationContext();
$traceId = $propagationContext->getTraceId();
$spanId = $propagationContext->getSpanId();
});
}
$metricTypeClass = self::METRIC_TYPES[$type];
/** @var Metric $metric */
/** @phpstan-ignore-next-line */
$metric = new $metricTypeClass($name, $value, $traceId, $spanId, $attributes, microtime(true), $unit);
if ($client !== null) {
$beforeSendMetric = $client->getOptions()->getBeforeSendMetricCallback();
$metric = $beforeSendMetric($metric);
if ($metric === null) {
return;
}
}
$this->metrics->push($metric);
}
public function flush(?HubInterface $hub = null): ?EventId
{
if ($this->metrics->isEmpty()) {
return null;
}
$hub = $hub ?? SentrySdk::getCurrentHub();
$event = Event::createMetrics()->setMetrics($this->metrics->drain());
return $hub->captureEvent($event);
}
}