Skip to content

Commit 05d6132

Browse files
committed
POC: support target dependencies for instrumentations
1 parent 7054b53 commit 05d6132

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import sys
16+
import logging
1617
from logging import getLogger
1718
from os import environ, path
1819
from os.path import abspath, dirname, pathsep
@@ -23,7 +24,9 @@
2324
from opentelemetry.environment_variables import (
2425
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS,
2526
)
27+
from opentelemetry.instrumentation.resources import get_target_dependency_conflict
2628

29+
logging.basicConfig(level=logging.DEBUG)
2730
logger = getLogger(__file__)
2831

2932

@@ -53,6 +56,13 @@ def _load_instrumentors():
5356
"Instrumentation skipped for library %s", entry_point.name
5457
)
5558
continue
59+
60+
# check if instrumentor has any missing or conflicting dependencies
61+
conflict = get_target_dependency_conflict(entry_point.dist)
62+
if conflict:
63+
logger.debug(conflict)
64+
continue
65+
5666
entry_point.load()().instrument() # type: ignore
5767
logger.debug("Instrumented %s", entry_point.name)
5868
except Exception as exc: # pylint: disable=broad-except
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import Optional
2+
from pkg_resources import get_distribution, VersionConflict, DistributionNotFound
3+
4+
5+
class DependencyConflict:
6+
required: str = None
7+
found: Optional[str] = None
8+
9+
def __init__(self, required, found=None):
10+
self.required = required
11+
self.found = found
12+
13+
def __str__(self):
14+
return 'DependencyConflict: requested: "{0}" but found: "{1}"'.format(self.required, self.found)
15+
16+
17+
def get_target_dependency_conflict(dist) -> Optional[DependencyConflict]:
18+
19+
# skip the check if the distribution has not specified any target dependencies
20+
if 'target' not in dist.extras:
21+
return
22+
23+
# figure out target dependencies
24+
target_dependencies = [dep for dep in dist.requires(('target',)) if dep not in dist.requires()]
25+
for dep in target_dependencies:
26+
try:
27+
get_distribution(str(dep))
28+
except VersionConflict as exc:
29+
return DependencyConflict(dep, exc.dist)
30+
except DistributionNotFound as exc:
31+
return DependencyConflict(dep)
32+
return

0 commit comments

Comments
 (0)