Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydvoss committed Oct 13, 2022
1 parent 3c4a626 commit 1668c29
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
13 changes: 10 additions & 3 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,9 @@ def _get_from_env_or_default() -> Sampler:
try:
rate = float(os.getenv(OTEL_TRACES_SAMPLER_ARG))
except ValueError:
_logger.warning("Could not convert TRACES_SAMPLER_ARG to float.")
_logger.warning(
"Could not convert TRACES_SAMPLER_ARG to float."
)
rate = 1.0
return _KNOWN_SAMPLERS[trace_sampler_name](rate)
return _KNOWN_SAMPLERS[trace_sampler_name]
Expand All @@ -443,13 +445,18 @@ def _get_from_env_or_default() -> Sampler:
trace_sampler = trace_sampler_factory(sampler_arg)
_logger.warning("JEREVOSS: trace_sampler: %s" % trace_sampler)
if not issubclass(type(trace_sampler), Sampler):
message = "Output of traces sampler factory, %s, was not a Sampler object." % trace_sampler_factory
message = (
"Output of traces sampler factory, %s, was not a Sampler object."
% trace_sampler_factory
)
_logger.warning(message)
raise ValueError(message)
return trace_sampler
except Exception as exc:
_logger.warning(
"Failed to initialize custom sampler, %s: %s", trace_sampler_name, exc
"Failed to initialize custom sampler, %s: %s",
trace_sampler_name,
exc,
)
return _KNOWN_SAMPLERS["parentbased_always_on"]

Expand Down
71 changes: 47 additions & 24 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,28 +304,34 @@ def test_sampler_with_env_non_existent_entry_point(self):
self.verify_default_sampler(tracer_provider)

@mock.patch("opentelemetry.sdk.trace.util.iter_entry_points")
@mock.patch.dict("os.environ", {OTEL_TRACES_SAMPLER: "custom_sampler_factory"})
def test_custom_sampler_with_env(
self, mock_iter_entry_points
):
@mock.patch.dict(
"os.environ", {OTEL_TRACES_SAMPLER: "custom_sampler_factory"}
)
def test_custom_sampler_with_env(self, mock_iter_entry_points):
# mock_iter_entry_points.return_value = [
# ("custom_sampler_factory", CustomSamplerFactory.get_custom_sampler)
# ]
mock_iter_entry_points.return_value=[
IterEntryPoint("custom_sampler_factory", CustomSamplerFactory.get_custom_sampler)
mock_iter_entry_points.return_value = [
IterEntryPoint(
"custom_sampler_factory",
CustomSamplerFactory.get_custom_sampler,
)
]
# pylint: disable=protected-access
reload(trace)
tracer_provider = trace.TracerProvider()
self.assertIsInstance(tracer_provider.sampler, CustomSampler)

@mock.patch("opentelemetry.sdk.trace.util.iter_entry_points")
@mock.patch.dict("os.environ", {OTEL_TRACES_SAMPLER: "custom_sampler_factory"})
def test_custom_sampler_with_env_bad_factory(
self, mock_iter_entry_points
):
@mock.patch.dict(
"os.environ", {OTEL_TRACES_SAMPLER: "custom_sampler_factory"}
)
def test_custom_sampler_with_env_bad_factory(self, mock_iter_entry_points):
mock_iter_entry_points.return_value = [
IterEntryPoint("custom_sampler_factory", CustomSamplerFactory.empty_get_custom_sampler)
IterEntryPoint(
"custom_sampler_factory",
CustomSamplerFactory.empty_get_custom_sampler,
)
]
# pylint: disable=protected-access
reload(trace)
Expand All @@ -340,11 +346,12 @@ def test_custom_sampler_with_env_bad_factory(
OTEL_TRACES_SAMPLER_ARG: "0.5",
},
)
def test_custom_sampler_with_env_unused_arg(
self, mock_iter_entry_points
):
def test_custom_sampler_with_env_unused_arg(self, mock_iter_entry_points):
mock_iter_entry_points.return_value = [
IterEntryPoint("custom_sampler_factory", CustomSamplerFactory.get_custom_sampler)
IterEntryPoint(
"custom_sampler_factory",
CustomSamplerFactory.get_custom_sampler,
)
]
# pylint: disable=protected-access
reload(trace)
Expand All @@ -359,11 +366,12 @@ def test_custom_sampler_with_env_unused_arg(
OTEL_TRACES_SAMPLER_ARG: "0.5",
},
)
def test_custom_ratio_sampler_with_env(
self, mock_iter_entry_points
):
def test_custom_ratio_sampler_with_env(self, mock_iter_entry_points):
mock_iter_entry_points.return_value = [
IterEntryPoint("custom_ratio_sampler_factory", CustomSamplerFactory.get_custom_ratio_sampler)
IterEntryPoint(
"custom_ratio_sampler_factory",
CustomSamplerFactory.get_custom_ratio_sampler,
)
]
# pylint: disable=protected-access
reload(trace)
Expand All @@ -383,7 +391,10 @@ def test_custom_ratio_sampler_with_env_bad_arg(
self, mock_iter_entry_points
):
mock_iter_entry_points.return_value = [
IterEntryPoint("custom_ratio_sampler_factory", CustomSamplerFactory.get_custom_ratio_sampler)
IterEntryPoint(
"custom_ratio_sampler_factory",
CustomSamplerFactory.get_custom_ratio_sampler,
)
]
# pylint: disable=protected-access
reload(trace)
Expand All @@ -401,7 +412,10 @@ def test_custom_ratio_sampler_with_env_no_arg(
self, mock_iter_entry_points
):
mock_iter_entry_points.return_value = [
IterEntryPoint("custom_ratio_sampler_factory", CustomSamplerFactory.get_custom_ratio_sampler)
IterEntryPoint(
"custom_ratio_sampler_factory",
CustomSamplerFactory.get_custom_ratio_sampler,
)
]
# pylint: disable=protected-access
reload(trace)
Expand All @@ -420,9 +434,18 @@ def test_custom_ratio_sampler_with_env_multiple_entry_points(
self, mock_iter_entry_points
):
mock_iter_entry_points.return_value = [
IterEntryPoint("custom_ratio_sampler_factory", CustomSamplerFactory.get_custom_ratio_sampler),
IterEntryPoint("custom_sampler_factory", CustomSamplerFactory.get_custom_sampler),
IterEntryPoint("custom_z_sampler_factory", CustomSamplerFactory.empty_get_custom_sampler)
IterEntryPoint(
"custom_ratio_sampler_factory",
CustomSamplerFactory.get_custom_ratio_sampler,
),
IterEntryPoint(
"custom_sampler_factory",
CustomSamplerFactory.get_custom_sampler,
),
IterEntryPoint(
"custom_z_sampler_factory",
CustomSamplerFactory.empty_get_custom_sampler,
),
]
# pylint: disable=protected-access
reload(trace)
Expand Down

0 comments on commit 1668c29

Please sign in to comment.