diff --git a/src/Metrics/MetricsAggregator.php b/src/Metrics/MetricsAggregator.php index 47a5897021..0b17391044 100644 --- a/src/Metrics/MetricsAggregator.php +++ b/src/Metrics/MetricsAggregator.php @@ -99,7 +99,7 @@ public function add( $traceContext = $this->getTraceContext($isolationScope); $traceId = new TraceId($traceContext['trace_id']); - $spanId = new SpanId($traceContext['span_id']); + $spanId = $traceContext['span_id'] !== null ? new SpanId($traceContext['span_id']) : null; $metricTypeClass = self::METRIC_TYPES[$type]; $metric = new $metricTypeClass($name, $value, $traceId, $spanId, $attributes, microtime(true), $unit); @@ -149,15 +149,21 @@ public function flush(?ClientInterface $client = null, ?IsolationScope $isolatio } /** - * @return array{trace_id: string, span_id: string} + * @return array{trace_id: string, span_id: string|null} */ private function getTraceContext(IsolationScope $scope): array { $traceContext = $scope->getTraceContext(); + $spanId = null; + + // Without an active span the scope falls back to the propagation context, whose span ID belongs to no span + if ($scope->getSpan() !== null || IsolationScope::getExternalPropagationContext() !== null) { + $spanId = $traceContext['span_id']; + } return [ 'trace_id' => $traceContext['trace_id'], - 'span_id' => $traceContext['span_id'], + 'span_id' => $spanId, ]; } diff --git a/src/Metrics/Types/CounterMetric.php b/src/Metrics/Types/CounterMetric.php index 42710c2b2d..5c96a7bffb 100644 --- a/src/Metrics/Types/CounterMetric.php +++ b/src/Metrics/Types/CounterMetric.php @@ -31,7 +31,7 @@ public function __construct( string $name, $value, TraceId $traceId, - SpanId $spanId, + ?SpanId $spanId, array $attributes, float $timestamp, ?Unit $unit diff --git a/src/Metrics/Types/DistributionMetric.php b/src/Metrics/Types/DistributionMetric.php index 631062001c..2b6c03234b 100644 --- a/src/Metrics/Types/DistributionMetric.php +++ b/src/Metrics/Types/DistributionMetric.php @@ -31,7 +31,7 @@ public function __construct( string $name, $value, TraceId $traceId, - SpanId $spanId, + ?SpanId $spanId, array $attributes, float $timestamp, ?Unit $unit diff --git a/src/Metrics/Types/GaugeMetric.php b/src/Metrics/Types/GaugeMetric.php index ae5909fc76..516718ea00 100644 --- a/src/Metrics/Types/GaugeMetric.php +++ b/src/Metrics/Types/GaugeMetric.php @@ -31,7 +31,7 @@ public function __construct( string $name, $value, TraceId $traceId, - SpanId $spanId, + ?SpanId $spanId, array $attributes, float $timestamp, ?Unit $unit diff --git a/src/Metrics/Types/Metric.php b/src/Metrics/Types/Metric.php index d74cf424a9..40c8ae4f30 100644 --- a/src/Metrics/Types/Metric.php +++ b/src/Metrics/Types/Metric.php @@ -22,7 +22,7 @@ abstract class Metric private $traceId; /** - * @var SpanId + * @var SpanId|null */ private $spanId; @@ -47,7 +47,7 @@ abstract class Metric public function __construct( string $name, TraceId $traceId, - SpanId $spanId, + ?SpanId $spanId, float $timestamp, array $attributes, ?Unit $unit @@ -91,7 +91,7 @@ public function getTraceId(): TraceId return $this->traceId; } - public function getSpanId(): SpanId + public function getSpanId(): ?SpanId { return $this->spanId; } diff --git a/src/Serializer/EnvelopItems/MetricsItem.php b/src/Serializer/EnvelopItems/MetricsItem.php index 3bea49eb6b..f77c703f3b 100644 --- a/src/Serializer/EnvelopItems/MetricsItem.php +++ b/src/Serializer/EnvelopItems/MetricsItem.php @@ -33,7 +33,6 @@ public static function toEnvelopeItem(Event $event): string $item = [ 'timestamp' => $metric->getTimestamp(), 'trace_id' => (string) $metric->getTraceId(), - 'span_id' => (string) $metric->getSpanId(), 'name' => $metric->getName(), 'value' => $metric->getValue(), 'type' => $metric->getType(), @@ -45,6 +44,10 @@ public static function toEnvelopeItem(Event $event): string }, $metric->getAttributes()->all()), ]; + if ($metric->getSpanId() !== null) { + $item['span_id'] = (string) $metric->getSpanId(); + } + if ($metric->getUnit() !== null) { $item['unit'] = (string) $metric->getUnit(); } diff --git a/tests/Metrics/MetricsTest.php b/tests/Metrics/MetricsTest.php index 785128fda9..7d0c78c11b 100644 --- a/tests/Metrics/MetricsTest.php +++ b/tests/Metrics/MetricsTest.php @@ -17,6 +17,7 @@ use Sentry\SentrySdk; use Sentry\State\Scope; use Sentry\Tests\ClientReport\ClientReportAssertions; +use Sentry\Tracing\Span; use Sentry\UserDataBag; use function Sentry\metrics; @@ -312,6 +313,33 @@ public function testInvalidTypeIsDiscarded(): void $this->assertEmpty(StubTransport::$events); } + public function testMetricsUseSpanIdOfActiveSpan(): void + { + $span = new Span(); + SentrySdk::getIsolationScope()->setSpan($span); + + metrics()->count('test-count', 2); + metrics()->flush(); + + $this->assertCount(1, StubTransport::$events); + $metric = StubTransport::$events[0]->getMetrics()[0]; + $this->assertSame((string) $span->getTraceId(), (string) $metric->getTraceId()); + $this->assertSame((string) $span->getSpanId(), (string) $metric->getSpanId()); + } + + public function testMetricsHaveNoSpanIdWhenNoSpanIsActive(): void + { + $traceId = (string) SentrySdk::getIsolationScope()->getPropagationContext()->getTraceId(); + + metrics()->count('test-count', 2); + metrics()->flush(); + + $this->assertCount(1, StubTransport::$events); + $metric = StubTransport::$events[0]->getMetrics()[0]; + $this->assertSame($traceId, (string) $metric->getTraceId()); + $this->assertNull($metric->getSpanId()); + } + public function testMetricsUseExternalPropagationContextWhenNoLocalSpanExists(): void { Scope::registerExternalPropagationContext(static function (): array { diff --git a/tests/Serializer/PayloadSerializerTest.php b/tests/Serializer/PayloadSerializerTest.php index c2ebb0f721..1b1720d4e9 100644 --- a/tests/Serializer/PayloadSerializerTest.php +++ b/tests/Serializer/PayloadSerializerTest.php @@ -572,7 +572,7 @@ public static function serializeAsEnvelopeDataProvider(): iterable <<setMetrics([ + new CounterMetric('test-counter', 5, new TraceId('21160e9b836d479f81611368b2aa3d2c'), null, ['foo' => 'bar'], 1597790835.0, Unit::bit()), + ]); + + yield [ + $event, + <<