Skip to content

Commit d994e75

Browse files
author
alrex
authored
Adding opentelemetry-distro package and entrypoint (open-telemetry#1482)
1 parent 9a1f594 commit d994e75

File tree

16 files changed

+258
-20
lines changed

16 files changed

+258
-20
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3636
`SpanContext` ([#1485](https://github.com/open-telemetry/opentelemetry-python/pull/1485)])
3737
- `opentelemetry-sdk` Add support for OTEL_TRACE_SAMPLER and OTEL_TRACE_SAMPLER_ARG env variables
3838
([#1496](https://github.com/open-telemetry/opentelemetry-python/pull/1496))
39+
- Adding `opentelemetry-distro` package to add default configuration for
40+
span exporter to OTLP
41+
([#1482](https://github.com/open-telemetry/opentelemetry-python/pull/1482))
3942

4043
### Changed
4144
- `opentelemetry-exporter-zipkin` Updated zipkin exporter status code and error tag

eachdist.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ sortfirst=
99
opentelemetry-sdk
1010
opentelemetry-instrumentation
1111
opentelemetry-proto
12+
opentelemetry-distro
1213
tests/util
1314
instrumentation/*
1415
exporter/*

opentelemetry-distro/MANIFEST.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
prune tests
2+
graft src
3+
global-exclude *.pyc
4+
global-exclude *.pyo
5+
global-exclude __pycache__/*
6+
include MANIFEST.in
7+
include README.rst

opentelemetry-distro/README.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
OpenTelemetry Distro
2+
====================
3+
4+
|pypi|
5+
6+
.. |pypi| image:: https://badge.fury.io/py/opentelemetry-distro.svg
7+
:target: https://pypi.org/project/opentelemetry-distro/
8+
9+
Installation
10+
------------
11+
12+
::
13+
14+
pip install opentelemetry-distro
15+
16+
17+
This package provides entrypoints to configure OpenTelemetry
18+
19+
References
20+
----------
21+
22+
* `OpenTelemetry Project <https://opentelemetry.io/>`_

opentelemetry-distro/setup.cfg

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
[metadata]
16+
name = opentelemetry-distro
17+
description = OpenTelemetry Python Distro
18+
long_description = file: README.rst
19+
long_description_content_type = text/x-rst
20+
author = OpenTelemetry Authors
21+
author_email = cncf-opentelemetry-contributors@lists.cncf.io
22+
url = https://github.com/open-telemetry/opentelemetry-python/tree/master/opentelemetry-distro
23+
platforms = any
24+
license = Apache-2.0
25+
classifiers =
26+
Development Status :: 4 - Beta
27+
Intended Audience :: Developers
28+
License :: OSI Approved :: Apache Software License
29+
Programming Language :: Python
30+
Programming Language :: Python :: 3
31+
Programming Language :: Python :: 3.5
32+
Programming Language :: Python :: 3.6
33+
Programming Language :: Python :: 3.7
34+
Programming Language :: Python :: 3.8
35+
36+
[options]
37+
python_requires = >=3.5
38+
package_dir=
39+
=src
40+
packages=find_namespace:
41+
zip_safe = False
42+
include_package_data = True
43+
install_requires =
44+
opentelemetry-api == 0.17.dev0
45+
opentelemetry-sdk == 0.17.dev0
46+
47+
[options.packages.find]
48+
where = src
49+
50+
[options.entry_points]
51+
opentelemetry_distro =
52+
distro = opentelemetry.distro:OpenTelemetryDistro
53+
opentelemetry_configurator =
54+
configurator = opentelemetry.distro:Configurator
55+
56+
[options.extras_require]
57+
test =
58+
otlp =
59+
opentelemetry-exporter-otlp == 0.17.dev0

opentelemetry-distro/setup.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import setuptools
18+
19+
BASE_DIR = os.path.dirname(__file__)
20+
VERSION_FILENAME = os.path.join(
21+
BASE_DIR, "src", "opentelemetry", "distro", "version.py"
22+
)
23+
PACKAGE_INFO = {}
24+
with open(VERSION_FILENAME) as f:
25+
exec(f.read(), PACKAGE_INFO)
26+
27+
setuptools.setup(version=PACKAGE_INFO["__version__"],)

opentelemetry-sdk/src/opentelemetry/sdk/configuration/__init__.py renamed to opentelemetry-distro/src/opentelemetry/distro/__init__.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
14+
#
15+
import os
1516
from logging import getLogger
1617
from typing import Sequence, Tuple
1718

@@ -20,6 +21,7 @@
2021
from opentelemetry import trace
2122
from opentelemetry.configuration import Configuration
2223
from opentelemetry.instrumentation.configurator import BaseConfigurator
24+
from opentelemetry.instrumentation.distro import BaseDistro
2325
from opentelemetry.sdk.metrics.export import MetricsExporter
2426
from opentelemetry.sdk.resources import Resource
2527
from opentelemetry.sdk.trace import TracerProvider
@@ -31,10 +33,10 @@
3133

3234
logger = getLogger(__file__)
3335

36+
3437
EXPORTER_OTLP = "otlp"
3538
EXPORTER_OTLP_SPAN = "otlp_span"
3639
EXPORTER_OTLP_METRIC = "otlp_metric"
37-
_DEFAULT_EXPORTER = EXPORTER_OTLP
3840

3941
RANDOM_IDS_GENERATOR = "random"
4042
_DEFAULT_IDS_GENERATOR = RANDOM_IDS_GENERATOR
@@ -49,7 +51,7 @@ def _get_service_name() -> str:
4951

5052

5153
def _get_exporter_names() -> Sequence[str]:
52-
exporter = Configuration().EXPORTER or _DEFAULT_EXPORTER
54+
exporter = Configuration().EXPORTER or EXPORTER_OTLP
5355
if exporter.lower().strip() == "none":
5456
return []
5557

@@ -87,11 +89,6 @@ def _init_tracing(
8789
)
8890

8991

90-
def _init_metrics(exporters: Sequence[MetricsExporter]):
91-
if exporters:
92-
logger.warning("automatic metric initialization is not supported yet.")
93-
94-
9592
def _import_tracer_provider_config_components(
9693
selected_components, entry_point_name
9794
) -> Sequence[Tuple[str, object]]:
@@ -158,13 +155,27 @@ def _initialize_components():
158155
ids_generator_name = _get_ids_generator()
159156
ids_generator = _import_ids_generator(ids_generator_name)
160157
_init_tracing(trace_exporters, ids_generator)
161-
162158
# We don't support automatic initialization for metric yet but have added
163159
# some boilerplate in order to make sure current implementation does not
164160
# lock us out of supporting metrics later without major surgery.
165161
_init_metrics(metric_exporters)
166162

167163

164+
def _init_metrics(exporters: Sequence[MetricsExporter]):
165+
if exporters:
166+
logger.warning("automatic metric initialization is not supported yet.")
167+
168+
168169
class Configurator(BaseConfigurator):
169170
def _configure(self, **kwargs):
170171
_initialize_components()
172+
173+
174+
class OpenTelemetryDistro(BaseDistro):
175+
"""
176+
The OpenTelemetry provided Distro configures a default set of
177+
configuration out of the box.
178+
"""
179+
180+
def _configure(self, **kwargs):
181+
os.environ.setdefault("OTEL_EXPORTER", "otlp")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
__version__ = "0.17.dev0"

opentelemetry-distro/tests/__init__.py

Whitespace-only changes.

opentelemetry-sdk/tests/configuration/test_configurator.py renamed to opentelemetry-distro/tests/test_configurator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from unittest.mock import patch
1919

2020
from opentelemetry.configuration import Configuration
21-
from opentelemetry.sdk.configuration import (
21+
from opentelemetry.distro import (
2222
_get_ids_generator,
2323
_import_ids_generator,
2424
_init_tracing,
@@ -78,11 +78,10 @@ class TestTraceInit(TestCase):
7878
def setUp(self):
7979
super()
8080
self.get_provider_patcher = patch(
81-
"opentelemetry.sdk.configuration.TracerProvider", Provider,
81+
"opentelemetry.distro.TracerProvider", Provider
8282
)
8383
self.get_processor_patcher = patch(
84-
"opentelemetry.sdk.configuration.BatchExportSpanProcessor",
85-
Processor,
84+
"opentelemetry.distro.BatchExportSpanProcessor", Processor
8685
)
8786
self.set_provider_patcher = patch(
8887
"opentelemetry.trace.set_tracer_provider"
@@ -133,7 +132,8 @@ def test_trace_init_otlp(self):
133132
del environ["OTEL_SERVICE_NAME"]
134133

135134
@patch.dict(environ, {"OTEL_IDS_GENERATOR": "custom_ids_generator"})
136-
@patch("opentelemetry.sdk.configuration.iter_entry_points")
135+
@patch("opentelemetry.distro.IdsGenerator", new=IdsGenerator)
136+
@patch("opentelemetry.distro.iter_entry_points")
137137
def test_trace_init_custom_ids_generator(self, mock_iter_entry_points):
138138
mock_iter_entry_points.configure_mock(
139139
return_value=[
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# type: ignore
15+
16+
import os
17+
from unittest import TestCase
18+
19+
from pkg_resources import DistributionNotFound, require
20+
21+
from opentelemetry.distro import OpenTelemetryDistro
22+
23+
24+
class TestDistribution(TestCase):
25+
def test_package_available(self):
26+
try:
27+
require(["opentelemetry-distro"])
28+
except DistributionNotFound:
29+
self.fail("opentelemetry-distro not installed")
30+
31+
def test_default_configuration(self):
32+
distro = OpenTelemetryDistro()
33+
self.assertIsNone(os.environ.get("OTEL_EXPORTER"))
34+
distro.configure()
35+
self.assertEqual("otlp", os.environ.get("OTEL_EXPORTER"))

opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,15 @@
2424

2525

2626
def _load_distros():
27-
# will be implemented in a subsequent PR
28-
pass
27+
for entry_point in iter_entry_points("opentelemetry_distro"):
28+
try:
29+
entry_point.load()().configure() # type: ignore
30+
logger.debug("Distribution %s configured", entry_point.name)
31+
except Exception as exc: # pylint: disable=broad-except
32+
logger.exception(
33+
"Distribution %s configuration failed", entry_point.name
34+
)
35+
raise exc
2936

3037

3138
def _load_instrumentors():
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# type: ignore
15+
16+
"""
17+
OpenTelemetry Base Distribution (Distro)
18+
"""
19+
20+
from abc import ABC, abstractmethod
21+
from logging import getLogger
22+
23+
_LOG = getLogger(__name__)
24+
25+
26+
class BaseDistro(ABC):
27+
"""An ABC for distro"""
28+
29+
_instance = None
30+
31+
def __new__(cls, *args, **kwargs):
32+
33+
if cls._instance is None:
34+
cls._instance = object.__new__(cls, *args, **kwargs)
35+
36+
return cls._instance
37+
38+
@abstractmethod
39+
def _configure(self, **kwargs):
40+
"""Configure the distribution"""
41+
42+
def configure(self, **kwargs):
43+
"""Configure the distribution"""
44+
self._configure(**kwargs)
45+
46+
47+
__all__ = ["BaseDistro"]

opentelemetry-sdk/setup.cfg

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ zip_safe = False
4343
include_package_data = True
4444
install_requires =
4545
opentelemetry-api == 0.17.dev0
46-
opentelemetry-instrumentation == 0.17.dev0
4746

4847
[options.packages.find]
4948
where = src
@@ -56,8 +55,6 @@ opentelemetry_tracer_provider =
5655
opentelemetry_exporter =
5756
console_span = opentelemetry.sdk.trace.export:ConsoleSpanExporter
5857
console_metrics = opentelemetry.sdk.metrics.export:ConsoleMetricsExporter
59-
opentelemetry_configurator =
60-
sdk_configurator = opentelemetry.sdk.configuration:Configurator
6158
opentelemetry_ids_generator =
6259
random = opentelemetry.sdk.trace.ids_generator:RandomIdsGenerator
6360

scripts/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ DISTDIR=dist
1616
mkdir -p $DISTDIR
1717
rm -rf $DISTDIR/*
1818

19-
for d in opentelemetry-api/ opentelemetry-sdk/ opentelemetry-instrumentation/ opentelemetry-proto/ exporter/*/ instrumentation/*/ propagator/*/; do
19+
for d in opentelemetry-api/ opentelemetry-sdk/ opentelemetry-instrumentation/ opentelemetry-proto/ opentelemetry-distro/ exporter/*/ instrumentation/*/ propagator/*/; do
2020
(
2121
echo "building $d"
2222
cd "$d"

0 commit comments

Comments
 (0)