-
-
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
Open
Litarnus
wants to merge
12
commits into
master
Choose a base branch
from
metrics
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 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 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
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,109 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sentry\Metrics; | ||
|
|
||
| use Sentry\Client; | ||
| use Sentry\Event; | ||
| use Sentry\EventId; | ||
| use Sentry\Metrics\Types\AbstractType; | ||
| use Sentry\Metrics\Types\CounterType; | ||
| use Sentry\Metrics\Types\DistributionType; | ||
| use Sentry\Metrics\Types\GaugeType; | ||
| use Sentry\SentrySdk; | ||
| use Sentry\State\Scope; | ||
| use Sentry\Util\RingBuffer; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| final class MetricsAggregator | ||
| { | ||
| /** | ||
| * @var int | ||
| */ | ||
| public const METRICS_BUFFER_SIZE = 1000; | ||
|
|
||
| /** | ||
| * @var RingBuffer<AbstractType> | ||
| */ | ||
| private $metrics; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->metrics = new RingBuffer(self::METRICS_BUFFER_SIZE); | ||
| } | ||
|
|
||
| private const METRIC_TYPES = [ | ||
| CounterType::TYPE => CounterType::class, | ||
| DistributionType::TYPE => DistributionType::class, | ||
| GaugeType::TYPE => GaugeType::class, | ||
| ]; | ||
|
|
||
| /** | ||
| * @param int|float|string $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(); | ||
|
|
||
| $defaultAttributes = [ | ||
| 'sentry.sdk.name' => $client->getSdkIdentifier(), | ||
| 'sentry.sdk.version' => $client->getSdkVersion(), | ||
| 'sentry.environment' => $options->getEnvironment() ?? Event::DEFAULT_ENVIRONMENT, | ||
| ]; | ||
|
|
||
| $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 AbstractType $metric */ | ||
| /** @phpstan-ignore-next-line SetType accepts int|float|string, others only int|float */ | ||
| $metric = new $metricTypeClass($name, $value, $traceId, $spanId, $attributes, microtime(true), $unit); | ||
|
|
||
| $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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.