Include schema_url in FriendlySpanConverter (#1292)

This commit is contained in:
Paul 2024-04-28 19:22:58 -05:00 committed by GitHub
parent cea2d7c4cb
commit 9fe6aa0cd7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 0 deletions

View File

@ -35,6 +35,7 @@ class FriendlySpanConverter implements SpanConverterInterface
private const EVENTS_ATTR = 'events';
private const TIMESTAMP_ATTR = 'timestamp';
private const LINKS_ATTR = 'links';
private const SCHEMA_URL_ATTR = 'schema_url';
public function convert(iterable $spans): array
{
@ -63,6 +64,7 @@ class FriendlySpanConverter implements SpanConverterInterface
self::STATUS_ATTR => $this->covertStatus($span->getStatus()),
self::EVENTS_ATTR => $this->convertEvents($span->getEvents()),
self::LINKS_ATTR => $this->convertLinks($span->getLinks()),
self::SCHEMA_URL_ATTR => $this->convertSchemaUrl($span->getInstrumentationScope()->getSchemaUrl()),
];
}
@ -144,4 +146,12 @@ class FriendlySpanConverter implements SpanConverterInterface
return $result;
}
/**
* @param string|null $schemaUrl
*/
private function convertSchemaUrl(?string $schemaUrl): string
{
return $schemaUrl ?? '';
}
}

View File

@ -8,6 +8,7 @@ use OpenTelemetry\API\Trace\SpanContextInterface;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\TraceStateInterface;
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Trace\EventInterface;
use OpenTelemetry\SDK\Trace\LinkInterface;
@ -78,6 +79,7 @@ class FriendlySpanConverterTest extends TestCase
'foz' => 'baz',
], ],
],
'schema_url' => 'https://opentelemetry.io/schemas/1.25.0',
];
public function test_convert(): void
@ -160,9 +162,23 @@ class FriendlySpanConverterTest extends TestCase
}
$mock->method('getLinks')->willReturn($links);
$mock->method('getInstrumentationScope')
->willReturn(
$this->createInstrumentationScopeMock()
);
return $mock;
}
private function createInstrumentationScopeMock(): InstrumentationScopeInterface
{
$mock = $this->createMock(InstrumentationScopeInterface::class);
$mock->method('getSchemaUrl')
->willReturn($this->createSchemaUrlMock());
return $mock;
}
private function createSpanContextMock(string $spanId, string $traceId = '0', string $traceState = null): SpanContextInterface
{
$mock = $this->createMock(SpanContextInterface::class);
@ -249,4 +265,9 @@ class FriendlySpanConverterTest extends TestCase
return $mock;
}
public function createSchemaUrlMock(): string
{
return self::TEST_DATA['schema_url'];
}
}