Skip to content

Commit 9fe6aa0

Browse files
authored
Include schema_url in FriendlySpanConverter (#1292)
1 parent cea2d7c commit 9fe6aa0

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/SDK/Trace/SpanExporter/FriendlySpanConverter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class FriendlySpanConverter implements SpanConverterInterface
3535
private const EVENTS_ATTR = 'events';
3636
private const TIMESTAMP_ATTR = 'timestamp';
3737
private const LINKS_ATTR = 'links';
38+
private const SCHEMA_URL_ATTR = 'schema_url';
3839

3940
public function convert(iterable $spans): array
4041
{
@@ -63,6 +64,7 @@ private function convertSpan(SpanDataInterface $span): array
6364
self::STATUS_ATTR => $this->covertStatus($span->getStatus()),
6465
self::EVENTS_ATTR => $this->convertEvents($span->getEvents()),
6566
self::LINKS_ATTR => $this->convertLinks($span->getLinks()),
67+
self::SCHEMA_URL_ATTR => $this->convertSchemaUrl($span->getInstrumentationScope()->getSchemaUrl()),
6668
];
6769
}
6870

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

145147
return $result;
146148
}
149+
150+
/**
151+
* @param string|null $schemaUrl
152+
*/
153+
private function convertSchemaUrl(?string $schemaUrl): string
154+
{
155+
return $schemaUrl ?? '';
156+
}
147157
}

tests/Unit/SDK/Trace/SpanExporter/FriendlySpanConverterTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use OpenTelemetry\API\Trace\SpanKind;
99
use OpenTelemetry\API\Trace\TraceStateInterface;
1010
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
11+
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface;
1112
use OpenTelemetry\SDK\Resource\ResourceInfo;
1213
use OpenTelemetry\SDK\Trace\EventInterface;
1314
use OpenTelemetry\SDK\Trace\LinkInterface;
@@ -78,6 +79,7 @@ class FriendlySpanConverterTest extends TestCase
7879
'foz' => 'baz',
7980
], ],
8081
],
82+
'schema_url' => 'https://opentelemetry.io/schemas/1.25.0',
8183
];
8284

8385
public function test_convert(): void
@@ -160,9 +162,23 @@ private function createSpanDataInterfaceMock(): SpanDataInterface
160162
}
161163
$mock->method('getLinks')->willReturn($links);
162164

165+
$mock->method('getInstrumentationScope')
166+
->willReturn(
167+
$this->createInstrumentationScopeMock()
168+
);
169+
163170
return $mock;
164171
}
165172

173+
private function createInstrumentationScopeMock(): InstrumentationScopeInterface
174+
{
175+
$mock = $this->createMock(InstrumentationScopeInterface::class);
176+
177+
$mock->method('getSchemaUrl')
178+
->willReturn($this->createSchemaUrlMock());
179+
180+
return $mock;
181+
}
166182
private function createSpanContextMock(string $spanId, string $traceId = '0', string $traceState = null): SpanContextInterface
167183
{
168184
$mock = $this->createMock(SpanContextInterface::class);
@@ -249,4 +265,9 @@ public function createLinkInterfaceMock(SpanContextInterface $context, Attribute
249265

250266
return $mock;
251267
}
268+
269+
public function createSchemaUrlMock(): string
270+
{
271+
return self::TEST_DATA['schema_url'];
272+
}
252273
}

0 commit comments

Comments
 (0)