Skip to content
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

Add timing span when emiting a timing metric #1717

Merged
merged 4 commits into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions src/Metrics/Metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Sentry\Metrics\Types\DistributionType;
use Sentry\Metrics\Types\GaugeType;
use Sentry\Metrics\Types\SetType;
use Sentry\Tracing\SpanContext;

use function Sentry\trace;

class Metrics
{
Expand Down Expand Up @@ -142,21 +145,32 @@ public function timing(
array $tags = [],
int $stackLevel = 0
) {
$startTimestamp = microtime(true);

$result = $callback();

$this->aggregator->add(
DistributionType::TYPE,
$key,
microtime(true) - $startTimestamp,
MetricsUnit::second(),
$tags,
(int) $startTimestamp,
$stackLevel
return trace(
function () use ($callback, $key, $tags, $stackLevel) {
$startTimestamp = microtime(true);

$result = $callback();

/**
* Emitting the metric here, will attach it to the
* "metric.timing" span.
*/
$this->aggregator->add(
DistributionType::TYPE,
$key,
microtime(true) - $startTimestamp,
MetricsUnit::second(),
$tags,
(int) $startTimestamp,
$stackLevel + 4 // the `trace` helper adds 4 additional stack frames
);

return $result;
},
SpanContext::make()
->setOp('metric.timing')
->setDescription($key)
);

return $result;
}

public function flush(): ?EventId
Expand Down
Loading