| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\Sentry\Metrics; |
| 5 |
|
| 6 |
use WCPOS\Vendor\Sentry\Client; |
| 7 |
use WCPOS\Vendor\Sentry\Event; |
| 8 |
use WCPOS\Vendor\Sentry\EventId; |
| 9 |
use WCPOS\Vendor\Sentry\Metrics\Types\CounterMetric; |
| 10 |
use WCPOS\Vendor\Sentry\Metrics\Types\DistributionMetric; |
| 11 |
use WCPOS\Vendor\Sentry\Metrics\Types\GaugeMetric; |
| 12 |
use WCPOS\Vendor\Sentry\Metrics\Types\Metric; |
| 13 |
use WCPOS\Vendor\Sentry\SentrySdk; |
| 14 |
use WCPOS\Vendor\Sentry\State\HubInterface; |
| 15 |
use WCPOS\Vendor\Sentry\State\Scope; |
| 16 |
use WCPOS\Vendor\Sentry\Tracing\SpanId; |
| 17 |
use WCPOS\Vendor\Sentry\Tracing\TraceId; |
| 18 |
use WCPOS\Vendor\Sentry\Unit; |
| 19 |
use WCPOS\Vendor\Sentry\Util\TelemetryStorage; |
| 20 |
/** |
| 21 |
* @internal |
| 22 |
*/ |
| 23 |
final class MetricsAggregator |
| 24 |
{ |
| 25 |
/** |
| 26 |
* @var int |
| 27 |
*/ |
| 28 |
public const METRICS_BUFFER_SIZE = 1000; |
| 29 |
private const METRIC_TYPES = [CounterMetric::TYPE => CounterMetric::class, DistributionMetric::TYPE => DistributionMetric::class, GaugeMetric::TYPE => GaugeMetric::class]; |
| 30 |
/** |
| 31 |
* @var TelemetryStorage<Metric>|null |
| 32 |
*/ |
| 33 |
private $metrics; |
| 34 |
/** |
| 35 |
* @param int|float $value |
| 36 |
* @param array<string, int|float|string|bool|null> $attributes |
| 37 |
*/ |
| 38 |
public function add(string $type, string $name, $value, array $attributes, ?Unit $unit) : void |
| 39 |
{ |
| 40 |
$hub = SentrySdk::getCurrentHub(); |
| 41 |
$client = $hub->getClient(); |
| 42 |
$metricFlushThreshold = null; |
| 43 |
if (!\is_int($value) && !\is_float($value)) { |
| 44 |
if ($client !== null) { |
| 45 |
$client->getOptions()->getLoggerOrNullLogger()->debug('Metrics value is neither int nor float. Metric will be discarded'); |
| 46 |
} |
| 47 |
return; |
| 48 |
} |
| 49 |
if ($client !== null) { |
| 50 |
$options = $client->getOptions(); |
| 51 |
$metricFlushThreshold = $options->getMetricFlushThreshold(); |
| 52 |
$defaultAttributes = ['sentry.environment' => $options->getEnvironment() ?? Event::DEFAULT_ENVIRONMENT, 'server.address' => $options->getServerName()]; |
| 53 |
if ($client instanceof Client) { |
| 54 |
$defaultAttributes['sentry.sdk.name'] = $client->getSdkIdentifier(); |
| 55 |
$defaultAttributes['sentry.sdk.version'] = $client->getSdkVersion(); |
| 56 |
} |
| 57 |
$hub->configureScope(static function (Scope $scope) use(&$defaultAttributes) { |
| 58 |
$user = $scope->getUser(); |
| 59 |
if ($user !== null) { |
| 60 |
if ($user->getId() !== null) { |
| 61 |
$defaultAttributes['user.id'] = $user->getId(); |
| 62 |
} |
| 63 |
if ($user->getEmail() !== null) { |
| 64 |
$defaultAttributes['user.email'] = $user->getEmail(); |
| 65 |
} |
| 66 |
if ($user->getUsername() !== null) { |
| 67 |
$defaultAttributes['user.name'] = $user->getUsername(); |
| 68 |
} |
| 69 |
} |
| 70 |
}); |
| 71 |
$release = $options->getRelease(); |
| 72 |
if ($release !== null) { |
| 73 |
$defaultAttributes['sentry.release'] = $release; |
| 74 |
} |
| 75 |
$attributes += $defaultAttributes; |
| 76 |
} |
| 77 |
$traceContext = $this->getTraceContext($hub); |
| 78 |
$traceId = new TraceId($traceContext['trace_id']); |
| 79 |
$spanId = new SpanId($traceContext['span_id']); |
| 80 |
$metricTypeClass = self::METRIC_TYPES[$type]; |
| 81 |
/** @var Metric $metric */ |
| 82 |
$metric = new $metricTypeClass($name, $value, $traceId, $spanId, $attributes, \microtime(\true), $unit); |
| 83 |
if ($client !== null) { |
| 84 |
try { |
| 85 |
$beforeSendMetric = $client->getOptions()->getBeforeSendMetricCallback(); |
| 86 |
$metric = $beforeSendMetric($metric); |
| 87 |
if ($metric === null) { |
| 88 |
return; |
| 89 |
} |
| 90 |
} catch (\Throwable $exception) { |
| 91 |
$client->getOptions()->getLoggerOrNullLogger()->error(\sprintf('The "before_send_metric" callback failed with exception: "%s".', $exception->getMessage())); |
| 92 |
return; |
| 93 |
} |
| 94 |
} |
| 95 |
$metrics = $this->getStorage($metricFlushThreshold); |
| 96 |
$metrics->push($metric); |
| 97 |
if ($metricFlushThreshold !== null && \count($metrics) >= $metricFlushThreshold) { |
| 98 |
$this->flush($hub); |
| 99 |
} |
| 100 |
} |
| 101 |
public function flush(?HubInterface $hub = null) : ?EventId |
| 102 |
{ |
| 103 |
if ($this->metrics === null || $this->metrics->isEmpty()) { |
| 104 |
return null; |
| 105 |
} |
| 106 |
$hub = $hub ?? SentrySdk::getCurrentHub(); |
| 107 |
$event = Event::createMetrics()->setMetrics($this->metrics->drain()); |
| 108 |
return $hub->captureEvent($event); |
| 109 |
} |
| 110 |
/** |
| 111 |
* @return array{trace_id: string, span_id: string} |
| 112 |
*/ |
| 113 |
private function getTraceContext(HubInterface $hub) : array |
| 114 |
{ |
| 115 |
$traceContext = null; |
| 116 |
$hub->configureScope(static function (Scope $scope) use(&$traceContext) : void { |
| 117 |
$traceContext = $scope->getTraceContext(); |
| 118 |
}); |
| 119 |
/** @var array{trace_id: string, span_id: string} $traceContext */ |
| 120 |
return $traceContext; |
| 121 |
} |
| 122 |
/** |
| 123 |
* @return TelemetryStorage<Metric> |
| 124 |
*/ |
| 125 |
private function getStorage(?int $metricFlushThreshold = null) : TelemetryStorage |
| 126 |
{ |
| 127 |
if ($this->metrics === null) { |
| 128 |
/** @var TelemetryStorage<Metric> $metrics */ |
| 129 |
$metrics = $metricFlushThreshold !== null ? TelemetryStorage::unbounded() : TelemetryStorage::bounded(self::METRICS_BUFFER_SIZE); |
| 130 |
$this->metrics = $metrics; |
| 131 |
} |
| 132 |
return $this->metrics; |
| 133 |
} |
| 134 |
} |
| 135 |
|