Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/opentelemetry/util/_providers.py: 47%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

17 statements  

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 

15from logging import getLogger 

16from os import environ 

17from typing import TYPE_CHECKING, TypeVar, cast 

18 

19from opentelemetry.util._importlib_metadata import entry_points 

20 

21if TYPE_CHECKING: 

22 from opentelemetry.metrics import MeterProvider 

23 from opentelemetry.trace import TracerProvider 

24 

25Provider = TypeVar("Provider", "TracerProvider", "MeterProvider") 

26 

27logger = getLogger(__name__) 

28 

29 

30def _load_provider( 

31 provider_environment_variable: str, provider: str 

32) -> Provider: # type: ignore[type-var] 

33 try: 

34 provider_name = cast( 

35 str, 

36 environ.get(provider_environment_variable, f"default_{provider}"), 

37 ) 

38 

39 return cast( 

40 Provider, 

41 next( # type: ignore 

42 iter( # type: ignore 

43 entry_points( # type: ignore 

44 group=f"opentelemetry_{provider}", 

45 name=provider_name, 

46 ) 

47 ) 

48 ).load()(), 

49 ) 

50 except Exception: # pylint: disable=broad-exception-caught 

51 logger.exception("Failed to load configured provider %s", provider) 

52 raise