|
| 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 | + |
| 16 | +from abc import ABC, abstractmethod |
| 17 | +from os import environ |
| 18 | + |
| 19 | +from pkg_resources import iter_entry_points |
| 20 | + |
| 21 | +from opentelemetry.configurator.environment_variables import ( |
| 22 | + OTEL_PYTHON_CONFIGURATORS, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +def _configure(): |
| 27 | + |
| 28 | + installed_configurators = { |
| 29 | + entry_point.name: entry_point |
| 30 | + for entry_point in iter_entry_points("opentelemetry-configurator") |
| 31 | + } |
| 32 | + |
| 33 | + configured_configurator_names = environ.get( |
| 34 | + OTEL_PYTHON_CONFIGURATORS, "" |
| 35 | + ).split() |
| 36 | + |
| 37 | + if len(installed_configurators) > 1 and not ( |
| 38 | + configured_configurator_names |
| 39 | + ): |
| 40 | + |
| 41 | + raise Exception( |
| 42 | + "More than 1 configurator is available:\n\n\t{}\n" |
| 43 | + "Select the ones that are to be used by setting " |
| 44 | + "{}.".format( |
| 45 | + "\n\t".join(list(installed_configurators.keys())), |
| 46 | + OTEL_PYTHON_CONFIGURATORS, |
| 47 | + ) |
| 48 | + ) |
| 49 | + |
| 50 | + for configured_configurator_name in environ.get( |
| 51 | + OTEL_PYTHON_CONFIGURATORS, "" |
| 52 | + ).split(): |
| 53 | + |
| 54 | + if configured_configurator_name in installed_configurators.keys(): |
| 55 | + |
| 56 | + installed_configurators[ |
| 57 | + configured_configurator_name |
| 58 | + ].load()().configure() |
| 59 | + |
| 60 | + |
| 61 | +class BaseConfigurator(ABC): |
| 62 | + @abstractmethod |
| 63 | + def configure(self): |
| 64 | + pass |
0 commit comments