Skip to content

Commit 71e3a7a

Browse files
authored
Change should_sample parameters to be spec compliant (#1764)
1 parent b73d800 commit 71e3a7a

File tree

4 files changed

+106
-51
lines changed

4 files changed

+106
-51
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Added `py.typed` file to every package. This should resolve a bunch of mypy
1111
errors for users.
1212
([#1720](https://github.com/open-telemetry/opentelemetry-python/pull/1720))
13+
- Added `SpanKind` to `should_sample` parameters, suggest using parent span context's tracestate
14+
instead of manually passed in tracestate in `should_sample`
15+
([#1764](https://github.com/open-telemetry/opentelemetry-python/pull/1764))
1316

1417
### Changed
1518
- Adjust `B3Format` propagator to be spec compliant by not modifying context

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -905,20 +905,17 @@ def start_span( # pylint: disable=too-many-locals
905905
if parent_span_context is None or not parent_span_context.is_valid:
906906
parent_span_context = None
907907
trace_id = self.id_generator.generate_trace_id()
908-
trace_flags = None
909-
trace_state = None
910908
else:
911909
trace_id = parent_span_context.trace_id
912-
trace_flags = parent_span_context.trace_flags
913-
trace_state = parent_span_context.trace_state
914910

915911
# The sampler decides whether to create a real or no-op span at the
916912
# time of span creation. No-op spans do not record events, and are not
917913
# exported.
918914
# The sampler may also add attributes to the newly-created span, e.g.
919915
# to include information about the sampling result.
916+
# The sampler may also modify the parent span context's tracestate
920917
sampling_result = self.sampler.should_sample(
921-
context, trace_id, name, attributes, links, trace_state
918+
context, trace_id, name, kind, attributes, links
922919
)
923920

924921
trace_flags = (

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
3434
Custom samplers can be created by subclassing `Sampler` and implementing `Sampler.should_sample` as well as `Sampler.get_description`.
3535
36+
Samplers are able to modify the `opentelemetry.trace.span.TraceState` of the parent of the span being created. For custom samplers, it is suggested to implement `Sampler.should_sample` to utilize the
37+
parent span context's `opentelemetry.trace.span.TraceState` and pass into the `SamplingResult` instead of the explicit trace_state field passed into the parameter of `Sampler.should_sample`.
38+
3639
To use a sampler, pass it into the tracer provider constructor. For example:
3740
3841
.. code:: python
@@ -108,7 +111,7 @@
108111
OTEL_TRACES_SAMPLER,
109112
OTEL_TRACES_SAMPLER_ARG,
110113
)
111-
from opentelemetry.trace import Link, get_current_span
114+
from opentelemetry.trace import Link, SpanKind, get_current_span
112115
from opentelemetry.trace.span import TraceState
113116
from opentelemetry.util.types import Attributes
114117

@@ -167,6 +170,7 @@ def should_sample(
167170
parent_context: Optional["Context"],
168171
trace_id: int,
169172
name: str,
173+
kind: SpanKind = None,
170174
attributes: Attributes = None,
171175
links: Sequence["Link"] = None,
172176
trace_state: "TraceState" = None,
@@ -189,13 +193,18 @@ def should_sample(
189193
parent_context: Optional["Context"],
190194
trace_id: int,
191195
name: str,
196+
kind: SpanKind = None,
192197
attributes: Attributes = None,
193198
links: Sequence["Link"] = None,
194199
trace_state: "TraceState" = None,
195200
) -> "SamplingResult":
196201
if self._decision is Decision.DROP:
197202
attributes = None
198-
return SamplingResult(self._decision, attributes, trace_state)
203+
return SamplingResult(
204+
self._decision,
205+
attributes,
206+
_get_parent_trace_state(parent_context),
207+
)
199208

200209
def get_description(self) -> str:
201210
if self._decision is Decision.DROP:
@@ -246,6 +255,7 @@ def should_sample(
246255
parent_context: Optional["Context"],
247256
trace_id: int,
248257
name: str,
258+
kind: SpanKind = None,
249259
attributes: Attributes = None,
250260
links: Sequence["Link"] = None,
251261
trace_state: "TraceState" = None,
@@ -255,7 +265,9 @@ def should_sample(
255265
decision = Decision.RECORD_AND_SAMPLE
256266
if decision is Decision.DROP:
257267
attributes = None
258-
return SamplingResult(decision, attributes, trace_state)
268+
return SamplingResult(
269+
decision, attributes, _get_parent_trace_state(parent_context),
270+
)
259271

260272
def get_description(self) -> str:
261273
return "TraceIdRatioBased{{{}}}".format(self._rate)
@@ -296,6 +308,7 @@ def should_sample(
296308
parent_context: Optional["Context"],
297309
trace_id: int,
298310
name: str,
311+
kind: SpanKind = None,
299312
attributes: Attributes = None,
300313
links: Sequence["Link"] = None,
301314
trace_state: "TraceState" = None,
@@ -322,9 +335,9 @@ def should_sample(
322335
parent_context=parent_context,
323336
trace_id=trace_id,
324337
name=name,
338+
kind=kind,
325339
attributes=attributes,
326340
links=links,
327-
trace_state=trace_state,
328341
)
329342

330343
def get_description(self):
@@ -385,3 +398,10 @@ def _get_from_env_or_default() -> Sampler:
385398
return _KNOWN_SAMPLERS[trace_sampler](rate)
386399

387400
return _KNOWN_SAMPLERS[trace_sampler]
401+
402+
403+
def _get_parent_trace_state(parent_context) -> Optional["TraceState"]:
404+
parent_span_context = get_current_span(parent_context).get_span_context()
405+
if parent_span_context is None or not parent_span_context.is_valid:
406+
return None
407+
return parent_span_context.trace_state

0 commit comments

Comments
 (0)