From ad90a4c49b718d66597d1871478979adf6904dd6 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Fri, 8 Mar 2024 13:00:45 +0100 Subject: [PATCH 1/3] Add timing span when emiting a timing metric --- src/Metrics/Metrics.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Metrics/Metrics.php b/src/Metrics/Metrics.php index b218b3749..e18ffc296 100644 --- a/src/Metrics/Metrics.php +++ b/src/Metrics/Metrics.php @@ -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 { @@ -144,7 +147,12 @@ public function timing( ) { $startTimestamp = microtime(true); - $result = $callback(); + $result = trace(function () use ($callback) { + return $callback(); + }, SpanContext::make() + ->setOp('metric.timing') + ->setDescription($key) + ); $this->aggregator->add( DistributionType::TYPE, From a3c44b60fab436f3cf7aa774926f2260d1caa8ab Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Wed, 13 Mar 2024 02:55:37 +0100 Subject: [PATCH 2/3] Emit the timing metric at the right place --- src/Metrics/Metrics.php | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/Metrics/Metrics.php b/src/Metrics/Metrics.php index e18ffc296..ee11eda5b 100644 --- a/src/Metrics/Metrics.php +++ b/src/Metrics/Metrics.php @@ -145,25 +145,31 @@ public function timing( array $tags = [], int $stackLevel = 0 ) { - $startTimestamp = microtime(true); - - $result = trace(function () use ($callback) { - return $callback(); + $result = 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 + ); + + return $result; }, SpanContext::make() ->setOp('metric.timing') ->setDescription($key) ); - $this->aggregator->add( - DistributionType::TYPE, - $key, - microtime(true) - $startTimestamp, - MetricsUnit::second(), - $tags, - (int) $startTimestamp, - $stackLevel - ); - return $result; } From 41d5c5e8c95c5102152a7dfa9e7d8be271629560 Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Fri, 24 May 2024 12:23:51 +0200 Subject: [PATCH 3/3] Fix tests --- src/Metrics/Metrics.php | 50 ++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/Metrics/Metrics.php b/src/Metrics/Metrics.php index ee11eda5b..e8c6a6374 100644 --- a/src/Metrics/Metrics.php +++ b/src/Metrics/Metrics.php @@ -145,32 +145,32 @@ public function timing( array $tags = [], int $stackLevel = 0 ) { - $result = 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 - ); - - return $result; - }, SpanContext::make() - ->setOp('metric.timing') - ->setDescription($key) + 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