Skip to content

Commit c2ba065

Browse files
authored
API: Renaming TraceOptions to TraceFlags (#450)
Renaming TraceOptions to TraceFlags, which is the term used to describe the flags associated with the trace in the OpenTelemetry specification. Closes #434
1 parent a756492 commit c2ba065

File tree

13 files changed

+46
-66
lines changed

13 files changed

+46
-66
lines changed

examples/opentelemetry-example-app/tests/test_flask_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_full_path(self):
5959
"traceparent": "00-{:032x}-{:016x}-{:02x}".format(
6060
trace_id,
6161
trace_sdk.generate_span_id(),
62-
trace.TraceOptions.SAMPLED,
62+
trace.TraceFlags.SAMPLED,
6363
)
6464
},
6565
)

ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def _translate_to_jaeger(spans: Span):
171171
refs = _extract_refs_from_span(span)
172172
logs = _extract_logs_from_span(span)
173173

174-
flags = int(ctx.trace_options)
174+
flags = int(ctx.trace_flags)
175175

176176
jaeger_span = jaeger.Span(
177177
traceIdHigh=_get_trace_id_high(trace_id),

ext/opentelemetry-ext-otcollector/tests/test_otcollector_exporter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
)
2828
from opentelemetry.sdk import trace
2929
from opentelemetry.sdk.trace.export import SpanExportResult
30-
from opentelemetry.trace import TraceOptions
30+
from opentelemetry.trace import TraceFlags
3131

3232

3333
# pylint: disable=no-member
@@ -92,7 +92,7 @@ def test_translate_to_collector(self):
9292
span_context = trace_api.SpanContext(
9393
trace_id,
9494
span_id,
95-
trace_options=TraceOptions(TraceOptions.SAMPLED),
95+
trace_flags=TraceFlags(TraceFlags.SAMPLED),
9696
trace_state=trace_api.TraceState({"testKey": "testValue"}),
9797
)
9898
parent_context = trace_api.SpanContext(trace_id, parent_id)
@@ -279,7 +279,7 @@ def test_export(self):
279279
trace_id = 0x6E0C63257DE34C926F9EFCD03927272E
280280
span_id = 0x34BF92DEEFC58C92
281281
span_context = trace_api.SpanContext(
282-
trace_id, span_id, trace_options=TraceOptions(TraceOptions.SAMPLED)
282+
trace_id, span_id, trace_flags=TraceFlags(TraceFlags.SAMPLED)
283283
)
284284
otel_spans = [
285285
trace.Span(

ext/opentelemetry-ext-zipkin/src/opentelemetry/ext/zipkin/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def _translate_to_zipkin(self, spans: Sequence[Span]):
132132
"annotations": _extract_annotations_from_events(span.events),
133133
}
134134

135-
if context.trace_options.sampled:
135+
if context.trace_flags.sampled:
136136
zipkin_span["debug"] = 1
137137

138138
if isinstance(span.parent, Span):

ext/opentelemetry-ext-zipkin/tests/test_zipkin_exporter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from opentelemetry.ext.zipkin import ZipkinSpanExporter
2121
from opentelemetry.sdk import trace
2222
from opentelemetry.sdk.trace.export import SpanExportResult
23-
from opentelemetry.trace import TraceOptions
23+
from opentelemetry.trace import TraceFlags
2424

2525

2626
class MockResponse:
@@ -114,7 +114,7 @@ def test_export(self):
114114
)
115115

116116
span_context = trace_api.SpanContext(
117-
trace_id, span_id, trace_options=TraceOptions(TraceOptions.SAMPLED)
117+
trace_id, span_id, trace_flags=TraceFlags(TraceFlags.SAMPLED)
118118
)
119119
parent_context = trace_api.SpanContext(trace_id, parent_id)
120120
other_context = trace_api.SpanContext(trace_id, other_id)

opentelemetry-api/src/opentelemetry/context/propagation/tracecontexthttptextformat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def extract(
7777
version = match.group(1)
7878
trace_id = match.group(2)
7979
span_id = match.group(3)
80-
trace_options = match.group(4)
80+
trace_flags = match.group(4)
8181

8282
if trace_id == "0" * 32 or span_id == "0" * 16:
8383
return trace.INVALID_SPAN_CONTEXT
@@ -96,7 +96,7 @@ def extract(
9696
span_context = trace.SpanContext(
9797
trace_id=int(trace_id, 16),
9898
span_id=int(span_id, 16),
99-
trace_options=trace.TraceOptions(trace_options),
99+
trace_flags=trace.TraceFlags(trace_flags),
100100
trace_state=tracestate,
101101
)
102102

@@ -115,7 +115,7 @@ def inject(
115115
if context == trace.INVALID_SPAN_CONTEXT:
116116
return
117117
traceparent_string = "00-{:032x}-{:016x}-{:02x}".format(
118-
context.trace_id, context.span_id, context.trace_options
118+
context.trace_id, context.span_id, context.trace_flags
119119
)
120120
set_in_carrier(
121121
carrier, cls._TRACEPARENT_HEADER_NAME, traceparent_string

opentelemetry-api/src/opentelemetry/trace/__init__.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def __exit__(
249249
self.end()
250250

251251

252-
class TraceOptions(int):
252+
class TraceFlags(int):
253253
"""A bitmask that represents options specific to the trace.
254254
255255
The only supported option is the "sampled" flag (``0x01``). If set, this
@@ -265,15 +265,15 @@ class TraceOptions(int):
265265
SAMPLED = 0x01
266266

267267
@classmethod
268-
def get_default(cls) -> "TraceOptions":
268+
def get_default(cls) -> "TraceFlags":
269269
return cls(cls.DEFAULT)
270270

271271
@property
272272
def sampled(self) -> bool:
273-
return bool(self & TraceOptions.SAMPLED)
273+
return bool(self & TraceFlags.SAMPLED)
274274

275275

276-
DEFAULT_TRACE_OPTIONS = TraceOptions.get_default()
276+
DEFAULT_TRACE_OPTIONS = TraceFlags.get_default()
277277

278278

279279
class TraceState(typing.Dict[str, str]):
@@ -312,24 +312,24 @@ class SpanContext:
312312
Args:
313313
trace_id: The ID of the trace that this span belongs to.
314314
span_id: This span's ID.
315-
trace_options: Trace options to propagate.
315+
trace_flags: Trace options to propagate.
316316
trace_state: Tracing-system-specific info to propagate.
317317
"""
318318

319319
def __init__(
320320
self,
321321
trace_id: int,
322322
span_id: int,
323-
trace_options: "TraceOptions" = DEFAULT_TRACE_OPTIONS,
323+
trace_flags: "TraceFlags" = DEFAULT_TRACE_OPTIONS,
324324
trace_state: "TraceState" = DEFAULT_TRACE_STATE,
325325
) -> None:
326-
if trace_options is None:
327-
trace_options = DEFAULT_TRACE_OPTIONS
326+
if trace_flags is None:
327+
trace_flags = DEFAULT_TRACE_OPTIONS
328328
if trace_state is None:
329329
trace_state = DEFAULT_TRACE_STATE
330330
self.trace_id = trace_id
331331
self.span_id = span_id
332-
self.trace_options = trace_options
332+
self.trace_flags = trace_flags
333333
self.trace_state = trace_state
334334

335335
def __repr__(self) -> str:

opentelemetry-api/src/opentelemetry/trace/sampling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def should_sample(
113113
links: Sequence["Link"] = (),
114114
) -> "Decision":
115115
if parent_context is not None:
116-
return Decision(parent_context.trace_options.sampled)
116+
return Decision(parent_context.trace_flags.sampled)
117117

118118
return Decision(trace_id & self.TRACE_ID_LIMIT < self.bound)
119119

opentelemetry-api/tests/trace/test_sampling.py

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818
from opentelemetry import trace
1919
from opentelemetry.trace import sampling
2020

21-
TO_DEFAULT = trace.TraceOptions(trace.TraceOptions.DEFAULT)
22-
TO_SAMPLED = trace.TraceOptions(trace.TraceOptions.SAMPLED)
21+
TO_DEFAULT = trace.TraceFlags(trace.TraceFlags.DEFAULT)
22+
TO_SAMPLED = trace.TraceFlags(trace.TraceFlags.SAMPLED)
2323

2424

2525
class TestSampler(unittest.TestCase):
2626
def test_always_on(self):
2727
no_record_always_on = sampling.ALWAYS_ON.should_sample(
28-
trace.SpanContext(
29-
0xDEADBEEF, 0xDEADBEF0, trace_options=TO_DEFAULT
30-
),
28+
trace.SpanContext(0xDEADBEEF, 0xDEADBEF0, trace_flags=TO_DEFAULT),
3129
0xDEADBEF1,
3230
0xDEADBEF2,
3331
"unsampled parent, sampling on",
@@ -36,9 +34,7 @@ def test_always_on(self):
3634
self.assertEqual(no_record_always_on.attributes, {})
3735

3836
sampled_always_on = sampling.ALWAYS_ON.should_sample(
39-
trace.SpanContext(
40-
0xDEADBEEF, 0xDEADBEF0, trace_options=TO_SAMPLED
41-
),
37+
trace.SpanContext(0xDEADBEEF, 0xDEADBEF0, trace_flags=TO_SAMPLED),
4238
0xDEADBEF1,
4339
0xDEADBEF2,
4440
"sampled parent, sampling on",
@@ -48,9 +44,7 @@ def test_always_on(self):
4844

4945
def test_always_off(self):
5046
no_record_always_off = sampling.ALWAYS_OFF.should_sample(
51-
trace.SpanContext(
52-
0xDEADBEEF, 0xDEADBEF0, trace_options=TO_DEFAULT
53-
),
47+
trace.SpanContext(0xDEADBEEF, 0xDEADBEF0, trace_flags=TO_DEFAULT),
5448
0xDEADBEF1,
5549
0xDEADBEF2,
5650
"unsampled parent, sampling off",
@@ -59,9 +53,7 @@ def test_always_off(self):
5953
self.assertEqual(no_record_always_off.attributes, {})
6054

6155
sampled_always_on = sampling.ALWAYS_OFF.should_sample(
62-
trace.SpanContext(
63-
0xDEADBEEF, 0xDEADBEF0, trace_options=TO_SAMPLED
64-
),
56+
trace.SpanContext(0xDEADBEEF, 0xDEADBEF0, trace_flags=TO_SAMPLED),
6557
0xDEADBEF1,
6658
0xDEADBEF2,
6759
"sampled parent, sampling off",
@@ -71,9 +63,7 @@ def test_always_off(self):
7163

7264
def test_default_on(self):
7365
no_record_default_on = sampling.DEFAULT_ON.should_sample(
74-
trace.SpanContext(
75-
0xDEADBEEF, 0xDEADBEF0, trace_options=TO_DEFAULT
76-
),
66+
trace.SpanContext(0xDEADBEEF, 0xDEADBEF0, trace_flags=TO_DEFAULT),
7767
0xDEADBEF1,
7868
0xDEADBEF2,
7969
"unsampled parent, sampling on",
@@ -82,9 +72,7 @@ def test_default_on(self):
8272
self.assertEqual(no_record_default_on.attributes, {})
8373

8474
sampled_default_on = sampling.DEFAULT_ON.should_sample(
85-
trace.SpanContext(
86-
0xDEADBEEF, 0xDEADBEF0, trace_options=TO_SAMPLED
87-
),
75+
trace.SpanContext(0xDEADBEEF, 0xDEADBEF0, trace_flags=TO_SAMPLED),
8876
0xDEADBEF1,
8977
0xDEADBEF2,
9078
"sampled parent, sampling on",
@@ -94,9 +82,7 @@ def test_default_on(self):
9482

9583
def test_default_off(self):
9684
no_record_default_off = sampling.DEFAULT_OFF.should_sample(
97-
trace.SpanContext(
98-
0xDEADBEEF, 0xDEADBEF0, trace_options=TO_DEFAULT
99-
),
85+
trace.SpanContext(0xDEADBEEF, 0xDEADBEF0, trace_flags=TO_DEFAULT),
10086
0xDEADBEF1,
10187
0xDEADBEF2,
10288
"unsampled parent, sampling off",
@@ -105,9 +91,7 @@ def test_default_off(self):
10591
self.assertEqual(no_record_default_off.attributes, {})
10692

10793
sampled_default_off = sampling.DEFAULT_OFF.should_sample(
108-
trace.SpanContext(
109-
0xDEADBEEF, 0xDEADBEF0, trace_options=TO_SAMPLED
110-
),
94+
trace.SpanContext(0xDEADBEEF, 0xDEADBEF0, trace_flags=TO_SAMPLED),
11195
0xDEADBEF1,
11296
0xDEADBEF2,
11397
"sampled parent, sampling off",
@@ -136,7 +120,7 @@ def test_probability_sampler(self):
136120
self.assertFalse(
137121
sampler.should_sample(
138122
trace.SpanContext(
139-
0xDEADBEF0, 0xDEADBEF1, trace_options=TO_DEFAULT
123+
0xDEADBEF0, 0xDEADBEF1, trace_flags=TO_DEFAULT
140124
),
141125
0x7FFFFFFFFFFFFFFF,
142126
0xDEADBEEF,
@@ -146,7 +130,7 @@ def test_probability_sampler(self):
146130
self.assertTrue(
147131
sampler.should_sample(
148132
trace.SpanContext(
149-
0xDEADBEF0, 0xDEADBEF1, trace_options=TO_SAMPLED
133+
0xDEADBEF0, 0xDEADBEF1, trace_flags=TO_SAMPLED
150134
),
151135
0x8000000000000000,
152136
0xDEADBEEF,

opentelemetry-sdk/src/opentelemetry/sdk/context/propagation/b3_format.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,18 @@ def extract(cls, get_from_carrier, carrier):
9191
# the desire for some form of sampling, propagate if either
9292
# header is set to allow.
9393
if sampled in cls._SAMPLE_PROPAGATE_VALUES or flags == "1":
94-
options |= trace.TraceOptions.SAMPLED
94+
options |= trace.TraceFlags.SAMPLED
9595
return trace.SpanContext(
9696
# trace an span ids are encoded in hex, so must be converted
9797
trace_id=int(trace_id, 16),
9898
span_id=int(span_id, 16),
99-
trace_options=trace.TraceOptions(options),
99+
trace_flags=trace.TraceFlags(options),
100100
trace_state=trace.TraceState(),
101101
)
102102

103103
@classmethod
104104
def inject(cls, span, set_in_carrier, carrier):
105-
sampled = (
106-
trace.TraceOptions.SAMPLED & span.context.trace_options
107-
) != 0
105+
sampled = (trace.TraceFlags.SAMPLED & span.context.trace_flags) != 0
108106
set_in_carrier(
109107
carrier, cls.TRACE_ID_KEY, format_trace_id(span.context.trace_id)
110108
)

0 commit comments

Comments
 (0)