Skip to content

Commit

Permalink
Update names of decisions in SamplingResult (#1115)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffe4 authored Sep 16, 2020
1 parent 553f6e8 commit 1abdc02
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
2 changes: 2 additions & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
([#1091](https://github.com/open-telemetry/opentelemetry-python/pull/1091))
- Drop support for Python 3.4
([#1099](https://github.com/open-telemetry/opentelemetry-python/pull/1099))
- Rename members of `trace.sampling.Decision` enum
([#1115](https://github.com/open-telemetry/opentelemetry-python/pull/1115))

## Version 0.12b0

Expand Down
28 changes: 14 additions & 14 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@

class Decision(enum.Enum):
# IsRecording() == false, span will not be recorded and all events and attributes will be dropped.
NOT_RECORD = 0
IGNORE = 0
# IsRecording() == true, but Sampled flag MUST NOT be set.
RECORD = 1
RECORD_ONLY = 1
# IsRecording() == true AND Sampled flag` MUST be set.
RECORD_AND_SAMPLED = 2
RECORD_AND_SAMPLE = 2

def is_recording(self):
return self in (Decision.RECORD, Decision.RECORD_AND_SAMPLED)
return self in (Decision.RECORD_ONLY, Decision.RECORD_AND_SAMPLE)

def is_sampled(self):
return self is Decision.RECORD_AND_SAMPLED
return self is Decision.RECORD_AND_SAMPLE


class SamplingResult:
Expand Down Expand Up @@ -140,12 +140,12 @@ def should_sample(
attributes: Attributes = None,
links: Sequence["Link"] = (),
) -> "SamplingResult":
if self._decision is Decision.NOT_RECORD:
if self._decision is Decision.IGNORE:
return SamplingResult(self._decision)
return SamplingResult(self._decision, attributes)

def get_description(self) -> str:
if self._decision is Decision.NOT_RECORD:
if self._decision is Decision.IGNORE:
return "AlwaysOffSampler"
return "AlwaysOnSampler"

Expand Down Expand Up @@ -194,10 +194,10 @@ def should_sample(
attributes: Attributes = None, # TODO
links: Sequence["Link"] = (),
) -> "SamplingResult":
decision = Decision.NOT_RECORD
decision = Decision.IGNORE
if trace_id & self.TRACE_ID_LIMIT < self.bound:
decision = Decision.RECORD_AND_SAMPLED
if decision is Decision.NOT_RECORD:
decision = Decision.RECORD_AND_SAMPLE
if decision is Decision.IGNORE:
return SamplingResult(decision)
return SamplingResult(decision, attributes)

Expand Down Expand Up @@ -231,8 +231,8 @@ def should_sample(
not parent_context.is_valid
or not parent_context.trace_flags.sampled
):
return SamplingResult(Decision.NOT_RECORD)
return SamplingResult(Decision.RECORD_AND_SAMPLED, attributes)
return SamplingResult(Decision.IGNORE)
return SamplingResult(Decision.RECORD_AND_SAMPLE, attributes)

return self._delegate.should_sample(
parent_context=parent_context,
Expand All @@ -246,10 +246,10 @@ def get_description(self):
return "ParentBased{{{}}}".format(self._delegate.get_description())


ALWAYS_OFF = StaticSampler(Decision.NOT_RECORD)
ALWAYS_OFF = StaticSampler(Decision.IGNORE)
"""Sampler that never samples spans, regardless of the parent span's sampling decision."""

ALWAYS_ON = StaticSampler(Decision.RECORD_AND_SAMPLED)
ALWAYS_ON = StaticSampler(Decision.RECORD_AND_SAMPLE)
"""Sampler that always samples spans, regardless of the parent span's sampling decision."""

DEFAULT_OFF = ParentBased(ALWAYS_OFF)
Expand Down
20 changes: 10 additions & 10 deletions opentelemetry-sdk/tests/trace/test_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,34 @@
class TestDecision(unittest.TestCase):
def test_is_recording(self):
self.assertTrue(
sampling.Decision.is_recording(sampling.Decision.RECORD)
sampling.Decision.is_recording(sampling.Decision.RECORD_ONLY)
)
self.assertTrue(
sampling.Decision.is_recording(
sampling.Decision.RECORD_AND_SAMPLED
)
sampling.Decision.is_recording(sampling.Decision.RECORD_AND_SAMPLE)
)
self.assertFalse(
sampling.Decision.is_recording(sampling.Decision.NOT_RECORD)
sampling.Decision.is_recording(sampling.Decision.IGNORE)
)

def test_is_sampled(self):
self.assertFalse(
sampling.Decision.is_sampled(sampling.Decision.RECORD)
sampling.Decision.is_sampled(sampling.Decision.RECORD_ONLY)
)
self.assertTrue(
sampling.Decision.is_sampled(sampling.Decision.RECORD_AND_SAMPLED)
sampling.Decision.is_sampled(sampling.Decision.RECORD_AND_SAMPLE)
)
self.assertFalse(
sampling.Decision.is_sampled(sampling.Decision.NOT_RECORD)
sampling.Decision.is_sampled(sampling.Decision.IGNORE)
)


class TestSamplingResult(unittest.TestCase):
def test_ctr(self):
attributes = {"asd": "test"}
result = sampling.SamplingResult(sampling.Decision.RECORD, attributes)
self.assertIs(result.decision, sampling.Decision.RECORD)
result = sampling.SamplingResult(
sampling.Decision.RECORD_ONLY, attributes
)
self.assertIs(result.decision, sampling.Decision.RECORD_ONLY)
with self.assertRaises(TypeError):
result.attributes["test"] = "mess-this-up"
self.assertTrue(len(result.attributes), 1)
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def test_sampling_attributes(self):
"attr-in-both": "decision-attr",
}
tracer_provider = trace.TracerProvider(
sampling.StaticSampler(sampling.Decision.RECORD_AND_SAMPLED,)
sampling.StaticSampler(sampling.Decision.RECORD_AND_SAMPLE,)
)

self.tracer = tracer_provider.get_tracer(__name__)
Expand Down

0 comments on commit 1abdc02

Please sign in to comment.