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

Adding opentelemetry-distro package and entrypoint #1482

Merged
merged 19 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from 11 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: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1285](https://github.com/open-telemetry/opentelemetry-python/pull/1285))
- Added `__repr__` for `DefaultSpan`, added `trace_flags` to `__repr__` of
`SpanContext` ([#1485](https://github.com/open-telemetry/opentelemetry-python/pull/1485)])
- Adding `opentelemetry-distro` package to add default configuration for
span exporter to OTLP
([#1482](https://github.com/open-telemetry/opentelemetry-python/pull/1482))

### Changed
- `opentelemetry-exporter-zipkin` Updated zipkin exporter status code and error tag
Expand Down
1 change: 1 addition & 0 deletions eachdist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sortfirst=
opentelemetry-sdk
opentelemetry-instrumentation
opentelemetry-proto
opentelemetry-distro
tests/util
instrumentation/*
exporter/*
Expand Down
7 changes: 7 additions & 0 deletions opentelemetry-distro/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
prune tests
graft src
global-exclude *.pyc
global-exclude *.pyo
global-exclude __pycache__/*
include MANIFEST.in
include README.rst
22 changes: 22 additions & 0 deletions opentelemetry-distro/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
OpenTelemetry Distro
lzchen marked this conversation as resolved.
Show resolved Hide resolved
====================

|pypi|

.. |pypi| image:: https://badge.fury.io/py/opentelemetry-distro.svg
:target: https://pypi.org/project/opentelemetry-distro/

Installation
------------

::

pip install opentelemetry-distro


This package provides entrypoints to configure OpenTelemetry

References
----------

* `OpenTelemetry Project <https://opentelemetry.io/>`_
58 changes: 58 additions & 0 deletions opentelemetry-distro/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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.
#
[metadata]
name = opentelemetry-distro
description = OpenTelemetry Python Distro
long_description = file: README.rst
long_description_content_type = text/x-rst
author = OpenTelemetry Authors
author_email = cncf-opentelemetry-contributors@lists.cncf.io
url = https://github.com/open-telemetry/opentelemetry-python/tree/master/opentelemetry-distro
platforms = any
license = Apache-2.0
classifiers =
Development Status :: 4 - Beta
Intended Audience :: Developers
License :: OSI Approved :: Apache Software License
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3.5
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8

[options]
python_requires = >=3.5
package_dir=
=src
packages=find_namespace:
zip_safe = False
include_package_data = True
install_requires =
opentelemetry-api == 0.17.dev0
opentelemetry-sdk == 0.17.dev0
opentelemetry-exporter-otlp == 0.17.dev0
lzchen marked this conversation as resolved.
Show resolved Hide resolved

[options.packages.find]
where = src

[options.entry_points]
opentelemetry_distro =
otel_distro = opentelemetry.distro:OpenTelemetryDistro
codeboten marked this conversation as resolved.
Show resolved Hide resolved
opentelemetry_configurator =
otel_configurator = opentelemetry.distro:Configurator
codeboten marked this conversation as resolved.
Show resolved Hide resolved

[options.extras_require]
test =
27 changes: 27 additions & 0 deletions opentelemetry-distro/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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.

import os

import setuptools

BASE_DIR = os.path.dirname(__file__)
VERSION_FILENAME = os.path.join(
BASE_DIR, "src", "opentelemetry", "distro", "version.py"
)
PACKAGE_INFO = {}
with open(VERSION_FILENAME) as f:
exec(f.read(), PACKAGE_INFO)

setuptools.setup(version=PACKAGE_INFO["__version__"],)
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
# 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 logging import getLogger
from typing import Sequence, Tuple

Expand All @@ -20,6 +21,7 @@
from opentelemetry import trace
from opentelemetry.configuration import Configuration
from opentelemetry.instrumentation.configurator import BaseConfigurator
from opentelemetry.instrumentation.distro import BaseDistro
from opentelemetry.sdk.metrics.export import MetricsExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
Expand All @@ -31,10 +33,11 @@

logger = getLogger(__file__)


EXPORTER_OTLP = "otlp"
EXPORTER_OTLP_SPAN = "otlp_span"
EXPORTER_OTLP_METRIC = "otlp_metric"
_DEFAULT_EXPORTER = EXPORTER_OTLP
_DEFAULT_EXPORTER = "none"

RANDOM_IDS_GENERATOR = "random"
_DEFAULT_IDS_GENERATOR = RANDOM_IDS_GENERATOR
Expand Down Expand Up @@ -87,11 +90,6 @@ def _init_tracing(
)


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


def _import_tracer_provider_config_components(
selected_components, entry_point_name
) -> Sequence[Tuple[str, object]]:
Expand Down Expand Up @@ -158,13 +156,27 @@ def _initialize_components():
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)


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


class Configurator(BaseConfigurator):
def _configure(self, **kwargs):
_initialize_components()


class OpenTelemetryDistro(BaseDistro):
"""
The OpenTelemetry provided Distro configures a default set of
configuration out of the box.
"""

def _configure(self, **kwargs):
lzchen marked this conversation as resolved.
Show resolved Hide resolved
os.environ.setdefault("OTEL_EXPORTER", "otlp")
15 changes: 15 additions & 0 deletions opentelemetry-distro/src/opentelemetry/distro/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 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.

__version__ = "0.17.dev0"
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from unittest.mock import patch

from opentelemetry.configuration import Configuration
from opentelemetry.sdk.configuration import (
from opentelemetry.distro import (
_get_ids_generator,
_import_ids_generator,
_init_tracing,
Expand Down Expand Up @@ -78,11 +78,10 @@ class TestTraceInit(TestCase):
def setUp(self):
super()
self.get_provider_patcher = patch(
"opentelemetry.sdk.configuration.TracerProvider", Provider,
"opentelemetry.distro.TracerProvider", Provider
)
self.get_processor_patcher = patch(
"opentelemetry.sdk.configuration.BatchExportSpanProcessor",
Processor,
"opentelemetry.distro.BatchExportSpanProcessor", Processor
)
self.set_provider_patcher = patch(
"opentelemetry.trace.set_tracer_provider"
Expand Down Expand Up @@ -133,7 +132,8 @@ def test_trace_init_otlp(self):
del environ["OTEL_SERVICE_NAME"]

@patch.dict(environ, {"OTEL_IDS_GENERATOR": "custom_ids_generator"})
@patch("opentelemetry.sdk.configuration.iter_entry_points")
@patch("opentelemetry.distro.IdsGenerator", new=IdsGenerator)
@patch("opentelemetry.distro.iter_entry_points")
def test_trace_init_custom_ids_generator(self, mock_iter_entry_points):
mock_iter_entry_points.configure_mock(
return_value=[
Expand Down
35 changes: 35 additions & 0 deletions opentelemetry-distro/tests/test_distro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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

import os
from unittest import TestCase

from pkg_resources import DistributionNotFound, require

from opentelemetry.distro import OpenTelemetryDistro

codeboten marked this conversation as resolved.
Show resolved Hide resolved

class TestDistribution(TestCase):
def test_package_available(self):
try:
require(["opentelemetry-distro"])
except DistributionNotFound:
self.fail("opentelemetry-distro not installed")

def test_default_configuration(self):
distro = OpenTelemetryDistro()
self.assertIsNone(os.environ.get("OTEL_EXPORTER"))
distro.configure()
self.assertEqual("otlp", os.environ.get("OTEL_EXPORTER"))
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@


def _load_distros():
# will be implemented in a subsequent PR
pass
for entry_point in iter_entry_points("opentelemetry_distro"):
try:
entry_point.load()().configure() # type: ignore
logger.debug("Distribution %s configured", entry_point.name)
except Exception as exc: # pylint: disable=broad-except
logger.exception(
"Distribution %s configuration failed", entry_point.name
)
raise exc


def _load_instrumentors():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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 Distribution (Distro)
"""

from abc import ABC, abstractmethod
from logging import getLogger

_LOG = getLogger(__name__)


class BaseDistro(ABC):
"""An ABC for distro"""

_instance = None

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 distribution"""

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


__all__ = ["BaseDistro"]
3 changes: 0 additions & 3 deletions opentelemetry-sdk/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-instrumentation == 0.17.dev0

[options.packages.find]
where = src
Expand All @@ -56,8 +55,6 @@ opentelemetry_tracer_provider =
opentelemetry_exporter =
console_span = opentelemetry.sdk.trace.export:ConsoleSpanExporter
console_metrics = opentelemetry.sdk.metrics.export:ConsoleMetricsExporter
opentelemetry_configurator =
sdk_configurator = opentelemetry.sdk.configuration:Configurator
opentelemetry_ids_generator =
random = opentelemetry.sdk.trace.ids_generator:RandomIdsGenerator

Expand Down
2 changes: 1 addition & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ DISTDIR=dist
mkdir -p $DISTDIR
rm -rf $DISTDIR/*

for d in opentelemetry-api/ opentelemetry-sdk/ opentelemetry-instrumentation/ opentelemetry-proto/ exporter/*/ instrumentation/*/ propagator/*/; do
for d in opentelemetry-api/ opentelemetry-sdk/ opentelemetry-instrumentation/ opentelemetry-proto/ opentelemetry-distro/ exporter/*/ instrumentation/*/ propagator/*/; do
(
echo "building $d"
cd "$d"
Expand Down
Loading