Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include schema_url in FriendlySpanConverter #1292

Merged
merged 2 commits into from
Apr 29, 2024
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
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'];
}
}