Skip to content

Commit eac6bc0

Browse files
committed
Allow distro and configurator specification
1 parent cae6ce4 commit eac6bc0

File tree

4 files changed

+52
-11
lines changed

4 files changed

+52
-11
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Add optional distro and configurator specification for auto-instrumentation
11+
([#1823](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1823))
12+
1013
## Version 1.18.0/0.39b0 (2023-05-10)
1114

1215
- `opentelemetry-instrumentation-system-metrics` Add `process.` prefix to `runtime.memory`, `runtime.cpu.time`, and `runtime.gc_count`. Change `runtime.memory` from count to UpDownCounter. ([#1735](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1735))

opentelemetry-instrumentation/README.rst

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ This package provides a couple of commands that help automatically instruments a
1818

1919
.. note::
2020
You need to install a distro package to get auto instrumentation working. The ``opentelemetry-distro``
21-
package contains the default distro and automatically configures some of the common options for users.
21+
package contains the default distro and configurator and automatically configures some of the common options for users.
2222
For more info about ``opentelemetry-distro`` check `here <https://opentelemetry-python.readthedocs.io/en/latest/examples/distro/README.html>`__
2323
::
2424

2525
pip install opentelemetry-distro[otlp]
2626

27+
When creating a custom distro and/or configurator, be sure to add entry points for each under opentelemetry_distro and opentelemetry_configurator respectfully.
28+
If you have entry points for multiple distros or configurators present in your environment, you should specify the entry point name of the distro and configurator you want to be used via the OTEL_PYTHON_DISTRO and OTEL_PYTHON_CONFIGURATOR environment variables.
29+
2730

2831
opentelemetry-bootstrap
2932
-----------------------
@@ -58,6 +61,8 @@ The command supports the following configuration options as CLI arguments and en
5861

5962
* ``--traces_exporter`` or ``OTEL_TRACES_EXPORTER``
6063
* ``--metrics_exporter`` or ``OTEL_METRICS_EXPORTER``
64+
* ``--distro`` or ``OTEL_PYTHON_DISTRO``
65+
* ``--configurator`` or ``OTEL_PYTHON_CONFIGURATOR``
6166

6267
Used to specify which trace exporter to use. Can be set to one or more of the well-known exporter
6368
names (see below).

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

+33-10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
from opentelemetry.instrumentation.distro import BaseDistro, DefaultDistro
2525
from opentelemetry.instrumentation.environment_variables import (
2626
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS,
27+
OTEL_PYTHON_DISTRO,
28+
OTEL_PYTHON_CONFIGURATOR,
2729
)
2830
from opentelemetry.instrumentation.utils import _python_path_without_directory
2931
from opentelemetry.instrumentation.version import __version__
@@ -32,19 +34,28 @@
3234

3335

3436
def _load_distros() -> BaseDistro:
37+
distro_name = environ.get(OTEL_PYTHON_DISTRO, None)
3538
for entry_point in iter_entry_points("opentelemetry_distro"):
3639
try:
37-
distro = entry_point.load()()
38-
if not isinstance(distro, BaseDistro):
40+
if distro_name is None or distro_name == entry_point.name:
41+
distro = entry_point.load()()
42+
if not isinstance(distro, BaseDistro):
43+
logger.debug(
44+
"%s is not an OpenTelemetry Distro. Skipping",
45+
entry_point.name,
46+
)
47+
continue
3948
logger.debug(
40-
"%s is not an OpenTelemetry Distro. Skipping",
49+
"Distribution %s will be configured", entry_point.name
50+
)
51+
return distro
52+
else:
53+
logger.warning(
54+
"%s distro not loaded because %s is set by %s",
4155
entry_point.name,
56+
distro_name,
57+
OTEL_PYTHON_DISTRO,
4258
)
43-
continue
44-
logger.debug(
45-
"Distribution %s will be configured", entry_point.name
46-
)
47-
return distro
4859
except Exception as exc: # pylint: disable=broad-except
4960
logger.exception(
5061
"Distribution %s configuration failed", entry_point.name
@@ -92,6 +103,7 @@ def _load_instrumentors(distro):
92103

93104

94105
def _load_configurators():
106+
configurator_name = environ.get(OTEL_PYTHON_CONFIGURATOR, None)
95107
configured = None
96108
for entry_point in iter_entry_points("opentelemetry_configurator"):
97109
if configured is not None:
@@ -102,8 +114,19 @@ def _load_configurators():
102114
)
103115
continue
104116
try:
105-
entry_point.load()().configure(auto_instrumentation_version=__version__) # type: ignore
106-
configured = entry_point.name
117+
if (
118+
configurator_name is None
119+
or configurator_name == entry_point.name
120+
):
121+
entry_point.load()().configure(auto_instrumentation_version=__version__) # type: ignore
122+
configured = entry_point.name
123+
else:
124+
logger.warning(
125+
"Configuration of %s not loaded because %s is set by %s",
126+
entry_point.name,
127+
configurator_name,
128+
OTEL_PYTHON_CONFIGURATOR,
129+
)
107130
except Exception as exc: # pylint: disable=broad-except
108131
logger.exception("Configuration of %s failed", entry_point.name)
109132
raise exc

opentelemetry-instrumentation/src/opentelemetry/instrumentation/environment_variables.py

+10
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,13 @@
1616
"""
1717
.. envvar:: OTEL_PYTHON_DISABLED_INSTRUMENTATIONS
1818
"""
19+
20+
OTEL_PYTHON_DISTRO = "OTEL_PYTHON_DISTRO"
21+
"""
22+
.. envvar:: OTEL_PYTHON_DISTRO
23+
"""
24+
25+
OTEL_PYTHON_CONFIGURATOR = "OTEL_PYTHON_CONFIGURATOR"
26+
"""
27+
.. envvar:: OTEL_PYTHON_CONFIGURATOR
28+
"""

0 commit comments

Comments
 (0)