Skip to content

Commit

Permalink
Include schema_url in FriendlySpanConverter (#1292)
Browse files Browse the repository at this point in the history
  • Loading branch information
sudol authored Apr 29, 2024
1 parent cea2d7c commit 9fe6aa0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/SDK/Trace/SpanExporter/FriendlySpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -63,6 +64,7 @@ private function convertSpan(SpanDataInterface $span): array
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()),
];
}

Expand Down Expand Up @@ -144,4 +146,12 @@ private function convertLinks(array $links): array

return $result;
}

/**
* @param string|null $schemaUrl
*/
private function convertSchemaUrl(?string $schemaUrl): string
{
return $schemaUrl ?? '';
}
}
21 changes: 21 additions & 0 deletions tests/Unit/SDK/Trace/SpanExporter/FriendlySpanConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand Down Expand Up @@ -78,6 +79,7 @@ class FriendlySpanConverterTest extends TestCase
'foz' => 'baz',
], ],
],
'schema_url' => 'https://opentelemetry.io/schemas/1.25.0',
];

public function test_convert(): void
Expand Down Expand Up @@ -160,9 +162,23 @@ private function createSpanDataInterfaceMock(): SpanDataInterface
}
$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);
Expand Down Expand Up @@ -249,4 +265,9 @@ public function createLinkInterfaceMock(SpanContextInterface $context, Attribute

return $mock;
}

public function createSchemaUrlMock(): string
{
return self::TEST_DATA['schema_url'];
}
}

0 comments on commit 9fe6aa0

Please sign in to comment.