Skip to content

Commit b758044

Browse files
Fix IS_REMOTE flag (#1522)
* Fixed span's is_remote flag computation * Added testing for cases without parent and with local parent span
1 parent 243d965 commit b758044

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

SpanConverter.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private function convertSpan(SpanDataInterface $span): Span
149149
$pSpan = new Span();
150150
$pSpan->setTraceId($this->serializer->serializeTraceId($span->getContext()->getTraceIdBinary()));
151151
$pSpan->setSpanId($this->serializer->serializeSpanId($span->getContext()->getSpanIdBinary()));
152-
$pSpan->setFlags(self::traceFlags($span->getContext()));
152+
$pSpan->setFlags(self::buildFlagsForSpan($span->getContext(), parentSpanContext: $span->getParentContext()));
153153
$pSpan->setTraceState((string) $span->getContext()->getTraceState());
154154
if ($span->getParentContext()->isValid()) {
155155
$pSpan->setParentSpanId($this->serializer->serializeSpanId($span->getParentContext()->getSpanIdBinary()));
@@ -174,7 +174,7 @@ private function convertSpan(SpanDataInterface $span): Span
174174
$pSpan->getLinks()[] = $pLink = new Link();
175175
$pLink->setTraceId($this->serializer->serializeTraceId($link->getSpanContext()->getTraceIdBinary()));
176176
$pLink->setSpanId($this->serializer->serializeSpanId($link->getSpanContext()->getSpanIdBinary()));
177-
$pLink->setFlags(self::traceFlags($link->getSpanContext()));
177+
$pLink->setFlags(self::buildFlagsForLink($link->getSpanContext()));
178178
$pLink->setTraceState((string) $link->getSpanContext()->getTraceState());
179179
$this->setAttributes($pLink, $link->getAttributes());
180180
}
@@ -188,14 +188,46 @@ private function convertSpan(SpanDataInterface $span): Span
188188
return $pSpan;
189189
}
190190

191-
private static function traceFlags(SpanContextInterface $spanContext): int
191+
private static function addRemoteFlags(SpanContextInterface $spanContext, int $baseFlags): int
192192
{
193-
$flags = $spanContext->getTraceFlags();
193+
$flags = $baseFlags;
194194
$flags |= SpanFlags::SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK;
195195
if ($spanContext->isRemote()) {
196196
$flags |= SpanFlags::SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK;
197197
}
198198

199199
return $flags;
200200
}
201+
202+
private static function buildFlagsForSpan(SpanContextInterface $spanContext, SpanContextInterface $parentSpanContext): int
203+
{
204+
$flags = $spanContext->getTraceFlags();
205+
206+
/**
207+
* @see https://github.com/open-telemetry/opentelemetry-proto/blob/v1.5.0/opentelemetry/proto/trace/v1/trace.proto#L122
208+
*
209+
* Bits 8 and 9 represent the 3 states of whether a span's parent is remote.
210+
* ^^^^^^
211+
* That is why we pass parent span's context.
212+
*/
213+
$flags = self::addRemoteFlags($parentSpanContext, $flags);
214+
215+
return $flags;
216+
}
217+
218+
private static function buildFlagsForLink(SpanContextInterface $linkSpanContext): int
219+
{
220+
$flags = $linkSpanContext->getTraceFlags();
221+
222+
/**
223+
* @see https://github.com/open-telemetry/opentelemetry-proto/blob/v1.5.0/opentelemetry/proto/trace/v1/trace.proto#L279
224+
*
225+
* Bits 8 and 9 represent the 3 states of whether the link is remote.
226+
* ^^^^
227+
* That is why we pass link span's context.
228+
*/
229+
$flags = self::addRemoteFlags($linkSpanContext, $flags);
230+
231+
return $flags;
232+
}
201233
}

0 commit comments

Comments
 (0)