Skip to content

Commit c104651

Browse files
committed
exporter: add is_remote_parent span flags to OTLP exported spans and links
Set SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK when the span parent is remote. Fixes #3876
1 parent 397e357 commit c104651

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3434
([#3586](https://github.com/open-telemetry/opentelemetry-python/pull/3586))
3535
- Rename test objects to avoid pytest warnings
3636
([#3823] (https://github.com/open-telemetry/opentelemetry-python/pull/3823))
37+
- Add span flags to OLTP spans and links
38+
([#3881](https://github.com/open-telemetry/opentelemetry-python/pull/3881))
3739

3840
## Version 1.24.0/0.45b0 (2024-03-28)
3941

exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/trace_encoder/__init__.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,25 @@
1717
from typing import List, Optional, Sequence
1818

1919
from opentelemetry.exporter.otlp.proto.common._internal import (
20-
_encode_trace_id,
21-
_encode_span_id,
22-
_encode_instrumentation_scope,
2320
_encode_attributes,
21+
_encode_instrumentation_scope,
2422
_encode_resource,
23+
_encode_span_id,
24+
_encode_trace_id,
2525
)
2626
from opentelemetry.proto.collector.trace.v1.trace_service_pb2 import (
2727
ExportTraceServiceRequest as PB2ExportTraceServiceRequest,
2828
)
29-
from opentelemetry.proto.trace.v1.trace_pb2 import (
30-
ScopeSpans as PB2ScopeSpans,
31-
)
3229
from opentelemetry.proto.trace.v1.trace_pb2 import (
3330
ResourceSpans as PB2ResourceSpans,
3431
)
32+
from opentelemetry.proto.trace.v1.trace_pb2 import ScopeSpans as PB2ScopeSpans
3533
from opentelemetry.proto.trace.v1.trace_pb2 import Span as PB2SPan
34+
from opentelemetry.proto.trace.v1.trace_pb2 import SpanFlags as PB2SpanFlags
3635
from opentelemetry.proto.trace.v1.trace_pb2 import Status as PB2Status
3736
from opentelemetry.sdk.trace import Event, ReadableSpan
38-
from opentelemetry.trace import Link
39-
from opentelemetry.trace import SpanKind
40-
from opentelemetry.trace.span import SpanContext, TraceState, Status
37+
from opentelemetry.trace import Link, SpanKind
38+
from opentelemetry.trace.span import SpanContext, Status, TraceState
4139

4240
# pylint: disable=E1101
4341
_SPAN_KIND_MAP = {
@@ -104,6 +102,13 @@ def _encode_resource_spans(
104102
return pb2_resource_spans
105103

106104

105+
def _span_flags(parent_span_context: Optional[SpanContext]) -> int:
106+
flags = PB2SpanFlags.SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
107+
if parent_span_context and parent_span_context.is_remote:
108+
flags |= PB2SpanFlags.SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK
109+
return flags
110+
111+
107112
def _encode_span(sdk_span: ReadableSpan) -> PB2SPan:
108113
span_context = sdk_span.get_span_context()
109114
return PB2SPan(
@@ -122,6 +127,7 @@ def _encode_span(sdk_span: ReadableSpan) -> PB2SPan:
122127
dropped_attributes_count=sdk_span.dropped_attributes,
123128
dropped_events_count=sdk_span.dropped_events,
124129
dropped_links_count=sdk_span.dropped_links,
130+
flags=_span_flags(sdk_span.parent),
125131
)
126132

127133

@@ -152,6 +158,7 @@ def _encode_links(links: Sequence[Link]) -> Sequence[PB2SPan.Link]:
152158
span_id=_encode_span_id(link.context.span_id),
153159
attributes=_encode_attributes(link.attributes),
154160
dropped_attributes_count=link.attributes.dropped,
161+
flags=_span_flags(link.context),
155162
)
156163
pb2_links.append(encoded_link)
157164
return pb2_links

exporter/opentelemetry-exporter-otlp-proto-common/tests/test_trace_encoder.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def get_exhaustive_otel_span_list() -> List[SDKSpan]:
8181
)
8282

8383
parent_span_context = SDKSpanContext(
84-
trace_id, 0x1111111111111111, is_remote=False
84+
trace_id, 0x1111111111111111, is_remote=True
8585
)
8686

8787
other_context = SDKSpanContext(
@@ -252,12 +252,14 @@ def get_exhaustive_test_spans(
252252
),
253253
),
254254
],
255+
flags=0x100,
255256
)
256257
],
257258
status=PB2Status(
258259
code=SDKStatusCode.ERROR.value,
259260
message="Example description",
260261
),
262+
flags=0x300,
261263
)
262264
],
263265
),
@@ -284,6 +286,7 @@ def get_exhaustive_test_spans(
284286
events=None,
285287
links=None,
286288
status={},
289+
flags=0x100,
287290
)
288291
],
289292
),
@@ -321,6 +324,7 @@ def get_exhaustive_test_spans(
321324
events=None,
322325
links=None,
323326
status={},
327+
flags=0x100,
324328
),
325329
PB2SPan(
326330
trace_id=trace_id,
@@ -346,6 +350,7 @@ def get_exhaustive_test_spans(
346350
events=None,
347351
links=None,
348352
status={},
353+
flags=0x100,
349354
),
350355
],
351356
)

exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_trace_exporter.py

+6
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,10 @@ def test_translate_spans(self):
597597
),
598598
),
599599
],
600+
flags=0x300,
600601
)
601602
],
603+
flags=0x300,
602604
)
603605
],
604606
)
@@ -699,8 +701,10 @@ def test_translate_spans_multi(self):
699701
),
700702
),
701703
],
704+
flags=0x300,
702705
)
703706
],
707+
flags=0x300,
704708
)
705709
],
706710
),
@@ -730,6 +734,7 @@ def test_translate_spans_multi(self):
730734
OTLPSpan.SpanKind.SPAN_KIND_INTERNAL
731735
),
732736
status=Status(code=0, message=""),
737+
flags=0x300,
733738
)
734739
],
735740
),
@@ -771,6 +776,7 @@ def test_translate_spans_multi(self):
771776
OTLPSpan.SpanKind.SPAN_KIND_INTERNAL
772777
),
773778
status=Status(code=0, message=""),
779+
flags=0x300,
774780
)
775781
],
776782
)

0 commit comments

Comments
 (0)