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

Remove SDK dependency from auto-instrumentation #1420

Merged
merged 16 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from 13 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
3 changes: 2 additions & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include_trailing_comma=True
force_grid_wrap=0
use_parentheses=True
line_length=79
profile=black

; 3 stands for Vertical Hanging Indent, e.g.
; from third_party import (
Expand All @@ -13,6 +14,6 @@ line_length=79
; docs: https://github.com/timothycrosley/isort#multi-line-output-modes
multi_line_output=3
skip=target
skip_glob=**/gen/*,.venv*/*,venv*/*,**/proto/*,opentelemetry-python-contrib/*
skip_glob=**/gen/*,.venv*/*,venv*/*,**/proto/*,opentelemetry-python-contrib/*,.tox/*
known_first_party=opentelemetry,opentelemetry_example_app
known_third_party=psutil,pytest,redis,redis_opentracing
6 changes: 5 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ disable=missing-docstring,
wrong-import-order, # Leave this up to isort
bad-continuation, # Leave this up to black
line-too-long, # Leave this up to black
exec-used
exec-used,
super-with-arguments, # temp-pylint-upgrade
isinstance-second-argument-not-valid-type, # temp-pylint-upgrade
raise-missing-from, # temp-pylint-upgrade
unused-argument, # temp-pylint-upgrade

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1449](https://github.com/open-telemetry/opentelemetry-python/pull/1449))
- Added support for Jaeger propagator
([#1219](https://github.com/open-telemetry/opentelemetry-python/pull/1219))
- Remove dependency on SDK from `opentelemetry-instrumentation` package. The
`opentelemetry-sdk` package now registers an entrypoint `opentelemetry_configurator`
to allow `opentelemetry-instrument` to load the configuration for the SDK
([#1420](https://github.com/open-telemetry/opentelemetry-python/pull/1420))

## [0.16b1](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v0.16b1) - 2020-11-26
### Added
Expand Down
5 changes: 3 additions & 2 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pylint==2.4.4
pylint==2.6.0
flake8~=3.7
isort~=4.3
isort~=5.6
black>=19.3b0,==19.*
httpretty~=1.0
mypy==0.790
sphinx~=2.1
sphinx-rtd-theme~=0.4
Expand Down
12 changes: 6 additions & 6 deletions docs-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ sphinx-autodoc-typehints~=1.10.2
# doesn't work for pkg_resources.
./opentelemetry-api
./opentelemetry-sdk
./opentelemetry-python-contrib/exporter/opentelemetry-exporter-datadog
./opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-grpc
-e "git+https://github.com/open-telemetry/opentelemetry-python-contrib.git#egg=opentelemetry-exporter-datadog&subdirectory=exporter/opentelemetry-exporter-datadog"
-e "git+https://github.com/open-telemetry/opentelemetry-python-contrib.git#egg=opentelemetry-instrumentation-grpc&subdirectory=instrumentation/opentelemetry-instrumentation-grpc"
./opentelemetry-instrumentation
./opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-requests
./opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-wsgi
./opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-django
./opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-flask
-e "git+https://github.com/open-telemetry/opentelemetry-python-contrib.git#egg=opentelemetry-instrumentation-requests&subdirectory=instrumentation/opentelemetry-instrumentation-requests"
-e "git+https://github.com/open-telemetry/opentelemetry-python-contrib.git#egg=opentelemetry-instrumentation-wsgi&subdirectory=instrumentation/opentelemetry-instrumentation-wsgi"
-e "git+https://github.com/open-telemetry/opentelemetry-python-contrib.git#egg=opentelemetry-instrumentation-django&subdirectory=instrumentation/opentelemetry-instrumentation-django"
-e "git+https://github.com/open-telemetry/opentelemetry-python-contrib.git#egg=opentelemetry-instrumentation-flask&subdirectory=instrumentation/opentelemetry-instrumentation-flask"

# Required by ext packages
asgiref~=3.0
Expand Down
1 change: 1 addition & 0 deletions docs/examples/django/pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from django.http import HttpResponse

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/opentracing/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env python

from rediscache import RedisCache

from opentelemetry import trace
from opentelemetry.exporter.jaeger import JaegerSpanExporter
from opentelemetry.instrumentation import opentracing_shim
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
from rediscache import RedisCache

# Configure the tracer using the default implementation
trace.set_tracer_provider(TracerProvider())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ def _get_resource_data(

def _load_credential_from_file(filepath) -> ChannelCredentials:
try:
with open(filepath, "rb") as f:
credential = f.read()
with open(filepath, "rb") as creds_file:
credential = creds_file.read()
return ssl_channel_credentials(credential)
except FileNotFoundError:
logger.exception("Failed to read credential file")
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-api/src/opentelemetry/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
from opentelemetry.configuration import Configuration

if TYPE_CHECKING:
from opentelemetry.trace import TracerProvider
from opentelemetry.metrics import MeterProvider
from opentelemetry.trace import TracerProvider

Provider = Union["TracerProvider", "MeterProvider"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

try:
import contextvars # pylint: disable=unused-import

from opentelemetry.context.contextvars_context import (
ContextVarsRuntimeContext,
)
Expand Down
1 change: 0 additions & 1 deletion opentelemetry-instrumentation/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ zip_safe = False
include_package_data = True
install_requires =
opentelemetry-api == 0.17.dev0
opentelemetry-sdk == 0.17.dev0
wrapt >= 1.0.0, < 2.0.0

[options.packages.find]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@

from pkg_resources import iter_entry_points

from opentelemetry.instrumentation.auto_instrumentation.components import (
initialize_components,
)

logger = getLogger(__file__)


def auto_instrument():
def _load_distros():
# will be implemented in a subsequent PR
pass


def _load_instrumentors():
for entry_point in iter_entry_points("opentelemetry_instrumentor"):
try:
entry_point.load()().instrument() # type: ignore
Expand All @@ -35,10 +36,29 @@ def auto_instrument():
raise exc


def _load_configurators():
configured = None
for entry_point in iter_entry_points("opentelemetry_configurator"):
if configured is not None:
logger.warning(
"Configuration of %s not loaded, %s already loaded",
entry_point.name,
configured,
)
continue
try:
entry_point.load()().configure() # type: ignore
configured = entry_point.name
except Exception as exc: # pylint: disable=broad-except
logger.exception("Configuration of %s failed", entry_point.name)
raise exc


def initialize():
try:
initialize_components()
auto_instrument()
_load_distros()
_load_configurators()
_load_instrumentors()
except Exception: # pylint: disable=broad-except
logger.exception("Failed to auto initialize opentelemetry")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
# type: ignore

"""
OpenTelemetry Base Configurator
"""

from abc import ABC, abstractmethod
from logging import getLogger

_LOG = getLogger(__name__)


class BaseConfigurator(ABC):
"""An ABC for configurators

Configurators are used to configure
SDKs (i.e. TracerProvider, MeterProvider, Processors...)
to reduce the amount of manual configuration required.
"""

_instance = None
_is_instrumented = False

def __new__(cls, *args, **kwargs):

if cls._instance is None:
cls._instance = object.__new__(cls, *args, **kwargs)

return cls._instance

@abstractmethod
def _configure(self, **kwargs):
"""Configure the SDK"""

def configure(self, **kwargs):
"""Configure the SDK"""
self._configure(**kwargs)


__all__ = ["BaseConfigurator"]
3 changes: 3 additions & 0 deletions opentelemetry-sdk/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ zip_safe = False
include_package_data = True
install_requires =
opentelemetry-api == 0.17.dev0
opentelemetry-instrumentation == 0.17.dev0

[options.packages.find]
where = src
Expand All @@ -57,6 +58,8 @@ opentelemetry_propagator =
opentelemetry_exporter =
console_span = opentelemetry.sdk.trace.export:ConsoleSpanExporter
console_metrics = opentelemetry.sdk.metrics.export:ConsoleMetricsExporter
opentelemetry_configurator =
sdk_configurator = opentelemetry.sdk.configuration:Configurator

[options.extras_require]
test =
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from opentelemetry import trace
from opentelemetry.configuration import Configuration
from opentelemetry.instrumentation.configurator import BaseConfigurator
from opentelemetry.sdk.metrics.export import MetricsExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
Expand All @@ -38,15 +39,15 @@
_DEFAULT_IDS_GENERATOR = RANDOM_IDS_GENERATOR


def get_ids_generator() -> str:
def _get_ids_generator() -> str:
return Configuration().IDS_GENERATOR or _DEFAULT_IDS_GENERATOR


def get_service_name() -> str:
def _get_service_name() -> str:
return Configuration().SERVICE_NAME or ""


def get_exporter_names() -> Sequence[str]:
def _get_exporter_names() -> Sequence[str]:
exporter = Configuration().EXPORTER or _DEFAULT_EXPORTER
if exporter.lower().strip() == "none":
return []
Expand All @@ -62,10 +63,10 @@ def get_exporter_names() -> Sequence[str]:
return names


def init_tracing(
def _init_tracing(
exporters: Sequence[SpanExporter], ids_generator: trace.IdsGenerator
):
service_name = get_service_name()
service_name = _get_service_name()
provider = TracerProvider(
resource=Resource.create({"service.name": service_name}),
ids_generator=ids_generator(),
Expand All @@ -85,12 +86,12 @@ def init_tracing(
)


def init_metrics(exporters: Sequence[MetricsExporter]):
def _init_metrics(exporters: Sequence[MetricsExporter]):
if exporters:
logger.warning("automatic metric initialization is not supported yet.")


def import_tracer_provider_config_components(
def _import_tracer_provider_config_components(
selected_components, entry_point_name
) -> Sequence[Tuple[str, object]]:
component_entry_points = {
Expand All @@ -112,15 +113,15 @@ def import_tracer_provider_config_components(
return component_impls


def import_exporters(
def _import_exporters(
exporter_names: Sequence[str],
) -> Tuple[Sequence[SpanExporter], Sequence[MetricsExporter]]:
trace_exporters, metric_exporters = {}, {}

for (
exporter_name,
exporter_impl,
) in import_tracer_provider_config_components(
) in _import_tracer_provider_config_components(
exporter_names, "opentelemetry_exporter"
):
if issubclass(exporter_impl, SpanExporter):
Expand All @@ -136,11 +137,11 @@ def import_exporters(
return trace_exporters, metric_exporters


def import_ids_generator(ids_generator_name: str) -> trace.IdsGenerator:
def _import_ids_generator(ids_generator_name: str) -> trace.IdsGenerator:
# pylint: disable=unbalanced-tuple-unpacking
[
(ids_generator_name, ids_generator_impl)
] = import_tracer_provider_config_components(
] = _import_tracer_provider_config_components(
[ids_generator_name.strip()], "opentelemetry_ids_generator"
)

Expand All @@ -150,14 +151,19 @@ def import_ids_generator(ids_generator_name: str) -> trace.IdsGenerator:
raise RuntimeError("{0} is not an IdsGenerator".format(ids_generator_name))


def initialize_components():
exporter_names = get_exporter_names()
trace_exporters, metric_exporters = import_exporters(exporter_names)
ids_generator_name = get_ids_generator()
ids_generator = import_ids_generator(ids_generator_name)
init_tracing(trace_exporters, ids_generator)
def _initialize_components():
exporter_names = _get_exporter_names()
trace_exporters, metric_exporters = _import_exporters(exporter_names)
ids_generator_name = _get_ids_generator()
ids_generator = _import_ids_generator(ids_generator_name)
_init_tracing(trace_exporters, ids_generator)

# We don't support automatic initialization for metric yet but have added
# some boilerplate in order to make sure current implementation does not
# lock us out of supporting metrics later without major surgery.
init_metrics(metric_exporters)
_init_metrics(metric_exporters)


class Configurator(BaseConfigurator):
def _configure(self, **kwargs):
_initialize_components()
6 changes: 2 additions & 4 deletions opentelemetry-sdk/src/opentelemetry/sdk/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@

try:
# pylint: disable=ungrouped-imports
from collections.abc import MutableMapping
from collections.abc import Sequence
from collections.abc import MutableMapping, Sequence
except ImportError:
# pylint: disable=no-name-in-module,ungrouped-imports
from collections import MutableMapping
from collections import Sequence
from collections import MutableMapping, Sequence


def ns_to_iso_str(nanoseconds):
Expand Down
Loading