Skip to content

Commit

Permalink
Set otlp exporter proto 1.2.0 remote flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevay committed Apr 29, 2024
1 parent 3a1ce3d commit b280eff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
17 changes: 15 additions & 2 deletions src/Contrib/Otlp/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace OpenTelemetry\Contrib\Otlp;

use OpenTelemetry\API\Trace as API;
use OpenTelemetry\API\Trace\SpanContextInterface;
use Opentelemetry\Proto\Collector\Trace\V1\ExportTraceServiceRequest;
use Opentelemetry\Proto\Common\V1\InstrumentationScope;
use Opentelemetry\Proto\Common\V1\KeyValue;
Expand All @@ -15,6 +16,7 @@
use Opentelemetry\Proto\Trace\V1\Span\Event;
use Opentelemetry\Proto\Trace\V1\Span\Link;
use Opentelemetry\Proto\Trace\V1\Span\SpanKind;
use Opentelemetry\Proto\Trace\V1\SpanFlags;
use Opentelemetry\Proto\Trace\V1\Status;
use Opentelemetry\Proto\Trace\V1\Status\StatusCode;
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
Expand Down Expand Up @@ -147,7 +149,7 @@ private function convertSpan(SpanDataInterface $span): Span
$pSpan = new Span();
$pSpan->setTraceId($this->serializer->serializeTraceId($span->getContext()->getTraceIdBinary()));
$pSpan->setSpanId($this->serializer->serializeSpanId($span->getContext()->getSpanIdBinary()));
$pSpan->setFlags($span->getContext()->getTraceFlags());
$pSpan->setFlags(self::traceFlags($span->getContext()));
$pSpan->setTraceState((string) $span->getContext()->getTraceState());
if ($span->getParentContext()->isValid()) {
$pSpan->setParentSpanId($this->serializer->serializeSpanId($span->getParentContext()->getSpanIdBinary()));
Expand All @@ -172,7 +174,7 @@ private function convertSpan(SpanDataInterface $span): Span
$pSpan->getLinks()[] = $pLink = new Link();
$pLink->setTraceId($this->serializer->serializeTraceId($link->getSpanContext()->getTraceIdBinary()));
$pLink->setSpanId($this->serializer->serializeSpanId($link->getSpanContext()->getSpanIdBinary()));
$pLink->setFlags($link->getSpanContext()->getTraceFlags());
$pLink->setFlags(self::traceFlags($link->getSpanContext()));
$pLink->setTraceState((string) $link->getSpanContext()->getTraceState());
$this->setAttributes($pLink, $link->getAttributes());
}
Expand All @@ -185,4 +187,15 @@ private function convertSpan(SpanDataInterface $span): Span

return $pSpan;
}

private static function traceFlags(SpanContextInterface $spanContext): int
{
$flags = $spanContext->getTraceFlags();
$flags |= SpanFlags::SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK;
if ($spanContext->isRemote()) {
$flags |= SpanFlags::SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK;
}

return $flags;
}
}
2 changes: 1 addition & 1 deletion src/Contrib/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"ext-json": "*",
"open-telemetry/api": "^1.0",
"open-telemetry/context": "^1.0",
"open-telemetry/gen-otlp-protobuf": "^1.1",
"open-telemetry/gen-otlp-protobuf": "^1.2",
"open-telemetry/sdk": "^1.0",
"open-telemetry/sem-conv": "^1.0",
"php-http/async-client-implementation": "^1.0",
Expand Down
21 changes: 19 additions & 2 deletions tests/Unit/Contrib/Otlp/SpanConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use function bin2hex;
use OpenTelemetry\API\Trace\SpanContext;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\TraceFlags;
use OpenTelemetry\Contrib\Otlp\SpanConverter;
use Opentelemetry\Proto\Common\V1\AnyValue;
use Opentelemetry\Proto\Common\V1\ArrayValue;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function test_convert_span_to_payload(): void

$this->assertSame($span->getContext()->getSpanId(), bin2hex($row->getSpanId()));
$this->assertSame($span->getContext()->getTraceId(), bin2hex($row->getTraceId()));
$this->assertSame($span->getContext()->getTraceFlags(), $row->getFlags());
$this->assertSame(V1\SpanFlags::SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK, $row->getFlags());
$this->assertSame($span->getName(), $row->getName());

$this->assertCount(2, $row->getAttributes());
Expand All @@ -57,10 +58,26 @@ public function test_convert_span_to_payload(): void

$this->assertSame($context->getTraceId(), bin2hex($link->getTraceId()));
$this->assertSame($context->getSpanId(), bin2hex($link->getSpanId()));
$this->assertSame($context->getTraceFlags(), $link->getFlags());
$this->assertSame(V1\SpanFlags::SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK, $link->getFlags());
$this->assertCount(1, $link->getAttributes());
}

public function test_span_context_is_remote_flags(): void
{
$span = (new SpanData())
->setContext(SpanContext::createFromRemoteParent('0000000000000001', '00000001'))
->addLink(SpanContext::createFromRemoteParent('0000000000000001', '00000002'), Attributes::create([]))
->addLink(SpanContext::createFromRemoteParent('0000000000000001', '00000003', TraceFlags::SAMPLED), Attributes::create([]));

$converter = new SpanConverter();
/** @psalm-suppress InvalidArgument */
$row = $converter->convert([$span])->getResourceSpans()[0]->getScopeSpans()[0]->getSpans()[0];

$this->assertSame(V1\SpanFlags::SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK | V1\SpanFlags::SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK, $row->getFlags());
$this->assertSame(V1\SpanFlags::SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK | V1\SpanFlags::SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK, $row->getLinks()[0]->getFlags());
$this->assertSame(V1\SpanFlags::SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK | V1\SpanFlags::SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK | TraceFlags::SAMPLED, $row->getLinks()[1]->getFlags());
}

/**
* @dataProvider attributeAreCoercedCorrectlyDataProvider
*/
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Contrib/Otlp/SpanExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function test_json_span_and_trace_id_hex_format(): void
"traceId": "0af7651916cd43dd8448eb211c80319c",
"spanId": "b7ad6b7169203331",
"name": "test-span-data",
"flags": 256,
"kind": 1,
"startTimeUnixNano": "1505855794194009601",
"endTimeUnixNano": "1505855799465726528",
Expand Down Expand Up @@ -131,6 +132,7 @@ public function test_json_invalid_utf8_sequence_is_encoded_as_bytes_value(): voi
"traceId": "0af7651916cd43dd8448eb211c80319c",
"spanId": "b7ad6b7169203331",
"name": "test-span-data",
"flags": 256,
"kind": 1,
"startTimeUnixNano": "1505855794194009601",
"endTimeUnixNano": "1505855799465726528",
Expand Down

0 comments on commit b280eff

Please sign in to comment.