Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 9 additions & 3 deletions src/Metrics/MetricsAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Metrics/Types/CounterMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
string $name,
$value,
TraceId $traceId,
SpanId $spanId,
?SpanId $spanId,
array $attributes,
float $timestamp,
?Unit $unit
Expand Down
2 changes: 1 addition & 1 deletion src/Metrics/Types/DistributionMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
string $name,
$value,
TraceId $traceId,
SpanId $spanId,
?SpanId $spanId,
array $attributes,
float $timestamp,
?Unit $unit
Expand Down
2 changes: 1 addition & 1 deletion src/Metrics/Types/GaugeMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
string $name,
$value,
TraceId $traceId,
SpanId $spanId,
?SpanId $spanId,
array $attributes,
float $timestamp,
?Unit $unit
Expand Down
6 changes: 3 additions & 3 deletions src/Metrics/Types/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class Metric
private $traceId;

/**
* @var SpanId
* @var SpanId|null
*/
private $spanId;

Expand All @@ -47,7 +47,7 @@ abstract class Metric
public function __construct(
string $name,
TraceId $traceId,
SpanId $spanId,
?SpanId $spanId,
float $timestamp,
array $attributes,
?Unit $unit
Expand Down Expand Up @@ -91,7 +91,7 @@ public function getTraceId(): TraceId
return $this->traceId;
}

public function getSpanId(): SpanId
public function getSpanId(): ?SpanId
{
return $this->spanId;
}
Expand Down
5 changes: 4 additions & 1 deletion src/Serializer/EnvelopItems/MetricsItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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();
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Metrics/MetricsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 18 additions & 4 deletions tests/Serializer/PayloadSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ public static function serializeAsEnvelopeDataProvider(): iterable
<<<TEXT
{"sent_at":"2020-08-18T22:47:15Z","dsn":"http:\/\/public@example.com\/sentry\/1","sdk":{"name":"sentry.php","version":"$sdkVersion","packages":[{"name":"composer:sentry\/sentry","version":"$sdkVersion"}]}}
{"type":"trace_metric","item_count":1,"content_type":"application\/vnd.sentry.items.trace-metric+json"}
{"items":[{"timestamp":1597790835,"trace_id":"21160e9b836d479f81611368b2aa3d2c","span_id":"d051f34163cd45fb","name":"test-counter","value":5,"type":"counter","attributes":{"foo":{"type":"string","value":"bar"}}}]}
{"items":[{"timestamp":1597790835,"trace_id":"21160e9b836d479f81611368b2aa3d2c","name":"test-counter","value":5,"type":"counter","attributes":{"foo":{"type":"string","value":"bar"}},"span_id":"d051f34163cd45fb"}]}
TEXT
];

Expand All @@ -586,7 +586,7 @@ public static function serializeAsEnvelopeDataProvider(): iterable
<<<TEXT
{"sent_at":"2020-08-18T22:47:15Z","dsn":"http:\/\/public@example.com\/sentry\/1","sdk":{"name":"sentry.php","version":"$sdkVersion","packages":[{"name":"composer:sentry\/sentry","version":"$sdkVersion"}]}}
{"type":"trace_metric","item_count":1,"content_type":"application\/vnd.sentry.items.trace-metric+json"}
{"items":[{"timestamp":1597790835,"trace_id":"21160e9b836d479f81611368b2aa3d2c","span_id":"d051f34163cd45fb","name":"test-counter","value":5,"type":"counter","attributes":{"foo":{"type":"string","value":"bar"}},"unit":"bit"}]}
{"items":[{"timestamp":1597790835,"trace_id":"21160e9b836d479f81611368b2aa3d2c","name":"test-counter","value":5,"type":"counter","attributes":{"foo":{"type":"string","value":"bar"}},"span_id":"d051f34163cd45fb","unit":"bit"}]}
TEXT
];

Expand All @@ -600,7 +600,7 @@ public static function serializeAsEnvelopeDataProvider(): iterable
<<<TEXT
{"sent_at":"2020-08-18T22:47:15Z","dsn":"http:\/\/public@example.com\/sentry\/1","sdk":{"name":"sentry.php","version":"$sdkVersion","packages":[{"name":"composer:sentry\/sentry","version":"$sdkVersion"}]}}
{"type":"trace_metric","item_count":1,"content_type":"application\/vnd.sentry.items.trace-metric+json"}
{"items":[{"timestamp":1597790835,"trace_id":"21160e9b836d479f81611368b2aa3d2c","span_id":"d051f34163cd45fb","name":"test-gauge","value":5,"type":"gauge","attributes":{"foo":{"type":"string","value":"bar"}},"unit":"second"}]}
{"items":[{"timestamp":1597790835,"trace_id":"21160e9b836d479f81611368b2aa3d2c","name":"test-gauge","value":5,"type":"gauge","attributes":{"foo":{"type":"string","value":"bar"}},"span_id":"d051f34163cd45fb","unit":"second"}]}
TEXT
];

Expand All @@ -614,7 +614,21 @@ public static function serializeAsEnvelopeDataProvider(): iterable
<<<TEXT
{"sent_at":"2020-08-18T22:47:15Z","dsn":"http:\/\/public@example.com\/sentry\/1","sdk":{"name":"sentry.php","version":"$sdkVersion","packages":[{"name":"composer:sentry\/sentry","version":"$sdkVersion"}]}}
{"type":"trace_metric","item_count":1,"content_type":"application\/vnd.sentry.items.trace-metric+json"}
{"items":[{"timestamp":1597790835,"trace_id":"21160e9b836d479f81611368b2aa3d2c","span_id":"d051f34163cd45fb","name":"test-distribution","value":5,"type":"distribution","attributes":{"foo":{"type":"string","value":"bar"}},"unit":"day"}]}
{"items":[{"timestamp":1597790835,"trace_id":"21160e9b836d479f81611368b2aa3d2c","name":"test-distribution","value":5,"type":"distribution","attributes":{"foo":{"type":"string","value":"bar"}},"span_id":"d051f34163cd45fb","unit":"day"}]}
TEXT
];

$event = Event::createMetrics(new EventId('fc9442f5aef34234bb22b9a615e30ccd'));
$event->setMetrics([
new CounterMetric('test-counter', 5, new TraceId('21160e9b836d479f81611368b2aa3d2c'), null, ['foo' => 'bar'], 1597790835.0, Unit::bit()),
]);

yield [
$event,
<<<TEXT
{"sent_at":"2020-08-18T22:47:15Z","dsn":"http:\/\/public@example.com\/sentry\/1","sdk":{"name":"sentry.php","version":"$sdkVersion","packages":[{"name":"composer:sentry\/sentry","version":"$sdkVersion"}]}}
{"type":"trace_metric","item_count":1,"content_type":"application\/vnd.sentry.items.trace-metric+json"}
{"items":[{"timestamp":1597790835,"trace_id":"21160e9b836d479f81611368b2aa3d2c","name":"test-counter","value":5,"type":"counter","attributes":{"foo":{"type":"string","value":"bar"}},"unit":"bit"}]}
TEXT
];

Expand Down