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

NH-63282 Remove pkg_resources usage #451

Merged
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
11 changes: 7 additions & 4 deletions solarwinds_apm/apm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
OTEL_TRACES_EXPORTER,
)
from opentelemetry.sdk.resources import Resource
from pkg_resources import iter_entry_points
from opentelemetry.util._importlib_metadata import entry_points

import solarwinds_apm.apm_noop as noop_extension
from solarwinds_apm import apm_logging
Expand Down Expand Up @@ -391,9 +391,12 @@ def _calculate_agent_enabled_config(self) -> bool:
!= INTL_SWO_DEFAULT_TRACES_EXPORTER
):
next(
iter_entry_points(
"opentelemetry_traces_exporter",
environ_exporter_name,
iter(
# pylint: disable=too-many-function-args
entry_points(
group="opentelemetry_traces_exporter",
name=environ_exporter_name,
)
)
)
except StopIteration:
Expand Down
66 changes: 42 additions & 24 deletions solarwinds_apm/configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
BatchSpanProcessor,
SimpleSpanProcessor,
)
from pkg_resources import get_distribution, iter_entry_points, load_entry_point
from opentelemetry.util._importlib_metadata import entry_points, version

from solarwinds_apm import apm_logging
from solarwinds_apm.apm_config import SolarWindsApmConfig
Expand Down Expand Up @@ -189,11 +189,14 @@ def _configure_sampler(
trace.set_tracer_provider(trace.NoOpTracerProvider())
return
try:
sampler = load_entry_point(
"solarwinds_apm",
"opentelemetry_traces_sampler",
self._DEFAULT_SW_TRACES_SAMPLER,
)(apm_config, reporter, oboe_api)
sampler = next(
iter(
entry_points(
group="opentelemetry_traces_sampler",
name=self._DEFAULT_SW_TRACES_SAMPLER,
)
)
).load()(apm_config, reporter, oboe_api)
except Exception as ex:
logger.exception("A exception was raised: %s", ex)
logger.exception(
Expand Down Expand Up @@ -339,20 +342,27 @@ def _configure_traces_exporter(
exporter = None
try:
if exporter_name == INTL_SWO_DEFAULT_TRACES_EXPORTER:
exporter = load_entry_point(
"solarwinds_apm",
"opentelemetry_traces_exporter",
exporter_name,
)(
exporter = next(
iter(
entry_points(
group="opentelemetry_traces_exporter",
name=exporter_name,
)
)
).load()(
reporter,
apm_txname_manager,
apm_fwkv_manager,
apm_config,
)
else:
exporter = next(
iter_entry_points(
"opentelemetry_traces_exporter", exporter_name
iter(
# pylint: disable=too-many-function-args
entry_points(
group="opentelemetry_traces_exporter",
name=exporter_name,
)
)
).load()()
except Exception as ex:
Expand Down Expand Up @@ -419,9 +429,11 @@ def _configure_metrics_exporter(
exporter = None
try:
exporter = next(
iter_entry_points(
"opentelemetry_metrics_exporter",
exporter_name,
iter(
entry_points(
group="opentelemetry_metrics_exporter",
name=exporter_name,
)
)
).load()(preferred_temporality=temporality_delta)
except Exception as ex:
Expand Down Expand Up @@ -534,8 +546,11 @@ def _configure_logs_exporter(
exporter = None
try:
exporter = next(
iter_entry_points(
"opentelemetry_logs_exporter", exporter_name
iter(
entry_points(
group="opentelemetry_logs_exporter",
name=exporter_name,
)
)
).load()()
except Exception as ex:
Expand Down Expand Up @@ -577,8 +592,11 @@ def _configure_propagator(self) -> None:
try:
propagators.append(
next(
iter_entry_points(
"opentelemetry_propagator", propagator_name
iter(
entry_points(
group="opentelemetry_propagator",
name=propagator_name,
)
)
).load()()
)
Expand Down Expand Up @@ -659,7 +677,9 @@ def _add_all_instrumented_python_framework_versions(
package_to_exclude = package_to_exclude.split(",")
package_to_exclude = [x.strip() for x in package_to_exclude]

for entry_point in iter_entry_points("opentelemetry_instrumentor"):
for entry_point in iter(
entry_points(group="opentelemetry_instrumentor")
):
if entry_point.name in package_to_exclude:
logger.debug(
"Skipping version lookup for library %s because excluded",
Expand Down Expand Up @@ -750,9 +770,7 @@ def _add_all_instrumented_python_framework_versions(
f"{entry_point_name}.connector"
].__version__
elif entry_point_name == "pyramid":
version_keys[instr_key] = get_distribution(
entry_point_name
).version
version_keys[instr_key] = version(entry_point_name)
elif entry_point_name == "sqlite3":
version_keys[instr_key] = sys.modules[
entry_point_name
Expand Down
2 changes: 1 addition & 1 deletion solarwinds_apm/distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
OTEL_EXPORTER_OTLP_TRACES_PROTOCOL,
)
from opentelemetry.sdk.version import __version__ as sdk_version
from pkg_resources import EntryPoint
from opentelemetry.util._importlib_metadata import EntryPoint

from solarwinds_apm.apm_config import SolarWindsApmConfig
from solarwinds_apm.apm_constants import (
Expand Down
4 changes: 2 additions & 2 deletions solarwinds_apm/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from opentelemetry.sdk.trace.export import SpanExporter
from opentelemetry.trace import SpanKind
from pkg_resources import get_distribution
from opentelemetry.util._importlib_metadata import version

from solarwinds_apm.apm_constants import (
INTL_SWO_LIBOBOE_TXN_NAME_KEY_PREFIX,
Expand Down Expand Up @@ -251,7 +251,7 @@ def _add_info_instrumented_framework(self, span, evt) -> None:
f"{framework}.connector"
].__version__
elif framework == "pyramid":
version_str = get_distribution(framework).version
version_str = version(framework)
elif framework == "sqlite3":
version_str = sys.modules[framework].sqlite_version
elif framework == "tornado":
Expand Down
20 changes: 9 additions & 11 deletions tests/integration/test_base_sw_headers_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

import os
from pkg_resources import (
iter_entry_points,
load_entry_point
)
import re

import flask
Expand All @@ -26,6 +22,7 @@
)
from opentelemetry.test.globals_test import reset_trace_globals
from opentelemetry.test.test_base import TestBase
from opentelemetry.util._importlib_metadata import entry_points

from solarwinds_apm.apm_config import SolarWindsApmConfig
from solarwinds_apm.configurator import SolarWindsConfigurator
Expand Down Expand Up @@ -76,8 +73,10 @@ def setUp(self):
# Based on auto_instrumentation run() and sitecustomize.py
# Load OTel env vars entry points
argument_otel_environment_variable = {}
for entry_point in iter_entry_points(
"opentelemetry_environment_variables"
for entry_point in iter(
entry_points(
group="opentelemetry_environment_variables"
)
):
environment_variable_module = entry_point.load()
for attribute in dir(environment_variable_module):
Expand All @@ -102,11 +101,10 @@ def setUp(self):
# This is done because set_tracer_provider cannot override the
# current tracer provider. Has to be done here.
reset_trace_globals()
sampler = load_entry_point(
"solarwinds_apm",
"opentelemetry_traces_sampler",
configurator._DEFAULT_SW_TRACES_SAMPLER
)(apm_config, reporter, OboeAPI())
sampler = next(iter(entry_points(
group="opentelemetry_traces_sampler",
name=configurator._DEFAULT_SW_TRACES_SAMPLER,
))).load()(apm_config, reporter, OboeAPI())
self.tracer_provider = TracerProvider(sampler=sampler)
# Set InMemorySpanExporter for testing
# We do NOT use SolarWindsSpanExporter
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/test_apm_config/test_apm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ def _mock_service_key(self, mocker, service_key):
mocker.patch.dict(os.environ, {
"SW_APM_SERVICE_KEY": service_key,
})
mock_iter_entry_points = mocker.patch(
"solarwinds_apm.apm_config.iter_entry_points"
mock_entry_points = mocker.patch(
"solarwinds_apm.apm_config.entry_points"
)
mock_points = mocker.MagicMock()
mock_points.__iter__.return_value = ["foo"]
mock_iter_entry_points.configure_mock(
mock_entry_points.configure_mock(
return_value=mock_points
)

Expand Down Expand Up @@ -601,12 +601,12 @@ def test_calculate_metrics_enabled_ao_prod_with_port(self, mocker):
assert apm_config.SolarWindsApmConfig()._calculate_metrics_enabled() == False

def test_mask_service_key_no_key_empty_default(self, mocker):
mock_iter_entry_points = mocker.patch(
"solarwinds_apm.apm_config.iter_entry_points"
mock_entry_points = mocker.patch(
"solarwinds_apm.apm_config.entry_points"
)
mock_points = mocker.MagicMock()
mock_points.__iter__.return_value = ["foo"]
mock_iter_entry_points.configure_mock(
mock_entry_points.configure_mock(
return_value=mock_points
)
assert apm_config.SolarWindsApmConfig().mask_service_key() == ""
Expand Down
Loading