Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix use of built-in samplers in SDK configuration #3176

Merged
merged 7 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#3100](https://github.com/open-telemetry/opentelemetry-python/pull/3100))
- Fix formatting of ConsoleMetricExporter.
([#3197](https://github.com/open-telemetry/opentelemetry-python/pull/3197))
- Fix use of built-in samplers in SDK configuration
([#3176](https://github.com/open-telemetry/opentelemetry-python/pull/3176))
- Implement shutdown procedure forOTLP grpc exporters
([#3138](https://github.com/open-telemetry/opentelemetry-python/pull/3138))
- Add exponential histogram
Expand Down
8 changes: 8 additions & 0 deletions opentelemetry-sdk/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ sdk = "opentelemetry.sdk.environment_variables"
[project.entry-points.opentelemetry_id_generator]
random = "opentelemetry.sdk.trace.id_generator:RandomIdGenerator"

[project.entry-points.opentelemetry_traces_sampler]
always_on = "opentelemetry.sdk.trace.sampling:_AlwaysOn"
always_off = "opentelemetry.sdk.trace.sampling:_AlwaysOff"
parentbased_always_on = "opentelemetry.sdk.trace.sampling:_ParentBasedAlwaysOn"
parentbased_always_off = "opentelemetry.sdk.trace.sampling:_ParentBasedAlwaysOff"
traceidratio = "opentelemetry.sdk.trace.sampling:TraceIdRatioBased"
parentbased_traceidratio = "opentelemetry.sdk.trace.sampling:ParentBasedTraceIdRatio"

[project.entry-points.opentelemetry_logger_provider]
sdk_logger_provider = "opentelemetry.sdk._logs:LoggerProvider"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,28 @@ def _import_sampler(sampler_name: str) -> Optional[Sampler]:
return None
try:
sampler_factory = _import_sampler_factory(sampler_name)
sampler_arg = os.getenv(OTEL_TRACES_SAMPLER_ARG, "")
sampler = sampler_factory(sampler_arg)
arg = None
if sampler_name in ("traceidratio", "parentbased_traceidratio"):
try:
rate = float(os.getenv(OTEL_TRACES_SAMPLER_ARG))
except (ValueError, TypeError):
_logger.warning(
"Could not convert TRACES_SAMPLER_ARG to float. Using default value 1.0."
)
rate = 1.0
arg = rate
else:
arg = os.getenv(OTEL_TRACES_SAMPLER_ARG)

sampler = sampler_factory(arg)
if not isinstance(sampler, Sampler):
message = f"Sampler factory, {sampler_factory}, produced output, {sampler}, which is not a Sampler."
_logger.warning(message)
raise ValueError(message)
return sampler
except Exception as exc: # pylint: disable=broad-except
_logger.warning(
"Using default sampler. Failed to initialize custom sampler, %s: %s",
"Using default sampler. Failed to initialize sampler, %s: %s",
sampler_name,
exc,
)
Expand Down
22 changes: 21 additions & 1 deletion opentelemetry-sdk/src/opentelemetry/sdk/trace/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,26 @@ def __init__(self, rate: float):
super().__init__(root=root)


class _AlwaysOff(StaticSampler):
def __init__(self, _):
super().__init__(Decision.DROP)


class _AlwaysOn(StaticSampler):
def __init__(self, _):
super().__init__(Decision.RECORD_AND_SAMPLE)


class _ParentBasedAlwaysOff(ParentBased):
def __init__(self, _):
super().__init__(ALWAYS_OFF)


class _ParentBasedAlwaysOn(ParentBased):
def __init__(self, _):
super().__init__(ALWAYS_ON)


_KNOWN_SAMPLERS = {
"always_on": ALWAYS_ON,
"always_off": ALWAYS_OFF,
Expand All @@ -415,7 +435,7 @@ def _get_from_env_or_default() -> Sampler:
if trace_sampler in ("traceidratio", "parentbased_traceidratio"):
try:
rate = float(os.getenv(OTEL_TRACES_SAMPLER_ARG))
except ValueError:
except (ValueError, TypeError):
_logger.warning("Could not convert TRACES_SAMPLER_ARG to float.")
rate = 1.0
return _KNOWN_SAMPLERS[trace_sampler](rate)
Expand Down