opentelemetry.util.loader module

The OpenTelemetry loader module is mainly used internally to load the implementation for global objects like opentelemetry.trace.tracer_source().

An instance of a global object of type T is always created with a factory function with the following signature:

def my_factory_for_t(api_type: typing.Type[T]) -> typing.Optional[T]:
    # ...

That function is called with e.g., the type of the global object it should create as an argument (e.g. the type object opentelemetry.trace.TracerSource) and should return an instance of that type (such that instanceof(my_factory_for_t(T), T) is true). Alternatively, it may return None to indicate that the no-op default should be used.

When loading an implementation, the following algorithm is used to find a factory function or other means to create the global object:

  1. If the environment variable OPENTELEMETRY_PYTHON_IMPLEMENTATION_getter-name (e.g., OPENTELEMETRY_PYTHON_IMPLEMENTATION_TRACERSOURCE) is set to an nonempty value, an attempt is made to import a module with that name and use a factory function named get_opentelemetry_implementation in it.

  2. Otherwise, the same is tried with the environment variable OPENTELEMETRY_PYTHON_IMPLEMENTATION_DEFAULT.

  3. Otherwise, if a set_preferred_<type>_implementation was called (e.g. opentelemetry.trace.set_preferred_tracer_source_implementation()), the callback set there is used (that is, the environment variables override the callback set in code).

  4. Otherwise, if set_preferred_default_implementation() was called, the callback set there is used.

  5. Otherwise, an attempt is made to import and use the OpenTelemetry SDK.

  6. Otherwise the default implementation that ships with the API distribution (a fast no-op implementation) is used.

If any of the above steps fails (e.g., a module is loaded but does not define the required function or a module name is set but the module fails to load), the search immediatelly skips to the last step.

Note that the first two steps (those that query environment variables) are skipped if sys.flags has ignore_environment set (which usually means that the Python interpreter was invoked with the -E or -I flag).

opentelemetry.util.loader.set_preferred_default_implementation(implementation_factory)[source]

Sets a factory function that may be called for any implementation object. See the module docs for more details.

Return type

None