Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ def _encode_resource_spans(
return json_resource_spans


def _span_flags(parent_span_context: SpanContext | None) -> int:
def _span_flags(span_context: SpanContext | None) -> int:
Comment thread
gavalur-sfdc marked this conversation as resolved.
Outdated
flags = JSONSpanFlags.SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
if parent_span_context and parent_span_context.is_remote:
flags |= JSONSpanFlags.SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK
if span_context:
flags |= int(span_context.trace_flags) & JSONSpanFlags.SPAN_FLAGS_TRACE_FLAGS_MASK
if span_context.is_remote:
flags |= JSONSpanFlags.SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK
return int(flags)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import Event, SpanContext
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
from opentelemetry.trace import Link, SpanKind
from opentelemetry.trace import Link, SpanKind, TraceFlags
from opentelemetry.trace.span import TraceState
from opentelemetry.trace.status import Status, StatusCode
from tests import (
Expand Down Expand Up @@ -246,24 +246,44 @@ def test_span_kind_map(self):
self.assertEqual(_SPAN_KIND_MAP[sdk_kind], json_kind)

def test_span_flags(self):
has_remote = int(JSONSpanFlags.SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK)
is_remote = int(JSONSpanFlags.SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK)
sampled = int(TraceFlags.SAMPLED)
Comment thread
gavalur-sfdc marked this conversation as resolved.
Outdated
cases = [
(
"remote_parent",
"remote_parent_unsampled",
SpanContext(TRACE_ID, 0x1111, is_remote=True),
int(
JSONSpanFlags.SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
| JSONSpanFlags.SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK
has_remote | is_remote,
),
(
"remote_parent_sampled",
SpanContext(
TRACE_ID,
0x1111,
is_remote=True,
trace_flags=TraceFlags(TraceFlags.SAMPLED),
),
has_remote | is_remote | sampled,
),
(
"local_parent",
"local_parent_unsampled",
SpanContext(TRACE_ID, 0x2222, is_remote=False),
int(JSONSpanFlags.SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK),
has_remote,
),
(
"local_parent_sampled",
SpanContext(
TRACE_ID,
0x2222,
is_remote=False,
trace_flags=TraceFlags(TraceFlags.SAMPLED),
),
has_remote | sampled,
),
(
"no_parent",
None,
int(JSONSpanFlags.SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK),
has_remote,
),
]
for name, context, expected_flags in cases:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ def _encode_resource_spans(
return pb2_resource_spans


def _span_flags(parent_span_context: SpanContext | None) -> int:
def _span_flags(span_context: SpanContext | None) -> int:
Comment thread
gavalur-sfdc marked this conversation as resolved.
Outdated
flags = PB2SpanFlags.SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
if parent_span_context and parent_span_context.is_remote:
flags |= PB2SpanFlags.SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK
if span_context:
flags |= int(span_context.trace_flags) & PB2SpanFlags.SPAN_FLAGS_TRACE_FLAGS_MASK
if span_context.is_remote:
flags |= PB2SpanFlags.SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK
return flags


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from opentelemetry.exporter.otlp.proto.common._internal.trace_encoder import (
_SPAN_KIND_MAP,
_encode_status,
_span_flags,
)
from opentelemetry.exporter.otlp.proto.common.trace_encoder import encode_spans
from opentelemetry.proto.collector.trace.v1.trace_service_pb2 import (
Expand All @@ -30,6 +31,7 @@
)
from opentelemetry.proto.trace.v1.trace_pb2 import ScopeSpans as PB2ScopeSpans
from opentelemetry.proto.trace.v1.trace_pb2 import Span as PB2SPan
from opentelemetry.proto.trace.v1.trace_pb2 import SpanFlags as PB2SpanFlags
from opentelemetry.proto.trace.v1.trace_pb2 import Status as PB2Status
from opentelemetry.sdk.trace import Event as SDKEvent
from opentelemetry.sdk.trace import Resource as SDKResource
Expand Down Expand Up @@ -423,3 +425,49 @@ def test_encode_status_code_translations(self):
code=SDKStatusCode.ERROR.value,
),
)

def test_span_flags(self):
trace_id = 0x3E0C63257DE34C926F9EFCD03927272E
has_remote = PB2SpanFlags.SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
is_remote = PB2SpanFlags.SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK
sampled = SDKTraceFlags.SAMPLED
cases = [
(
"remote_parent_unsampled",
SDKSpanContext(trace_id, 0x1111, is_remote=True),
has_remote | is_remote,
),
(
"remote_parent_sampled",
SDKSpanContext(
trace_id,
0x1111,
is_remote=True,
trace_flags=SDKTraceFlags(SDKTraceFlags.SAMPLED),
),
has_remote | is_remote | sampled,
),
(
"local_parent_unsampled",
SDKSpanContext(trace_id, 0x2222, is_remote=False),
has_remote,
),
(
"local_parent_sampled",
SDKSpanContext(
trace_id,
0x2222,
is_remote=False,
trace_flags=SDKTraceFlags(SDKTraceFlags.SAMPLED),
),
has_remote | sampled,
),
(
"no_parent",
None,
has_remote,
),
]
for name, context, expected_flags in cases:
with self.subTest(name=name):
self.assertEqual(_span_flags(context), expected_flags)