Skip to content

Commit

Permalink
Merge branch 'main' into issue_3167_1
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl authored Mar 15, 2023
2 parents 2c5f653 + a70e4a0 commit 624b7cb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
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
18 changes: 15 additions & 3 deletions opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py
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

0 comments on commit 624b7cb

Please sign in to comment.