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