-
-
Notifications
You must be signed in to change notification settings - Fork 462
feat(metrics): introduce metrics #1968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6b7e6ac
feat: Add Trace Metrics
cleptric 98a45f8
Add trace and span ID
cleptric 3ceb8e5
Merge branch 'master' into metrics
cleptric 8aec319
wip
cleptric 7ec9ec7
introduce ring buffer, add tests
Litarnus 35babbb
cs
Litarnus 1aa4f55
rename MetricsUnit -> Unit
Litarnus a3ac803
cs
Litarnus b7c6f9e
lints
Litarnus 82e101f
fix
Litarnus 080e9b1
psalm
Litarnus 30db6b2
Apply suggestion from @Litarnus
Litarnus 42a7fae
add TraceMetrics, revert Metrics to old state to remain BC
Litarnus 2889ed3
lint
Litarnus 0355f04
add enable_metrics and before_send_metric
Litarnus bba5667
deprecate units
Litarnus ab49ab6
PR feedback
Litarnus 4ada622
CS + lint
Litarnus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| <?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\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 ($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(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(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(): ?EventId | ||
| { | ||
| if ($this->metrics->isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| $hub = SentrySdk::getCurrentHub(); | ||
| $event = Event::createMetrics()->setMetrics($this->metrics->drain()); | ||
|
|
||
| return $hub->captureEvent($event); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.