Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auto-instrumentation dependency conflict detection #530

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.3.0-0.22b0...HEAD)

- `opentelemetry-instrumentation` Fixed cases where trying to use an instrumentation package without the
target library was crashing auto instrumentation agent.
([#530](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/530))

## [0.22b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.3.0-0.22b0) - 2021-06-01

### Changed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from logging import getLogger
from typing import Collection, Optional

from pkg_resources import (
Distribution,
DistributionNotFound,
RequirementParseError,
VersionConflict,
get_distribution,
)

logger = getLogger(__file__)


class DependencyConflict:
required: str = None
Expand All @@ -25,22 +29,36 @@ def __str__(self):
def get_dist_dependency_conflicts(
dist: Distribution,
) -> Optional[DependencyConflict]:
deps = [
dep
for dep in dist.requires(("instruments",))
if dep not in dist.requires()
]
return get_dependency_conflicts(deps)
main_deps = dist.requires()
instrumentation_deps = []
for dep in dist.requires(("instruments",)):
Copy link
Contributor

@lzchen lzchen Jun 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify, this would generate the Requirements for whatever is in extra_requires["instruments"] IN ADDITION TO the regular install_requires correct? In this case, the logic underneath will only parse through the ones that are not in the regualr install_requires?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. pkg_resource does not have an API to fetch only a specific extra require group. We could have used something like dist.requires(only_extra=('instruments',)) but it doesn't exist :)

if dep not in main_deps:
# we set marker to none so string representation of the dependency looks like
# requests ~= 1.0
# instead of
# requests ~= 1.0; extra = "instruments"
# which does not work with `get_distribution()`
dep.marker = None
instrumentation_deps.append(str(dep))

return get_dependency_conflicts(instrumentation_deps)


def get_dependency_conflicts(
deps: Collection[str],
) -> Optional[DependencyConflict]:
for dep in deps:
try:
get_distribution(str(dep))
get_distribution(dep)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth adding a catch all here to prevent any other exceptions from causing the script to exit unexpectedly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could but I don't think there is anything too special about this and I expect it to work always unless in truly exceptional cases like the filesystem misbehaving or something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an additional RequirementParseError that should catch most issues with invalid requirement strings being passed to this func.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the original issue behind this? get_distribution behavior if the dep was not installed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It cannot parse requirement strings that might also contain markers (extra requires section name).

get_distribution("lib == 1.0") works but get_distribution('lib == 1.0; extra = "instruments"') does not.

except VersionConflict as exc:
return DependencyConflict(dep, exc.dist)
except DistributionNotFound:
return DependencyConflict(dep)
except RequirementParseError as exc:
logger.warning(
'error parsing dependency, reporting as a conflict: "%s" - %s',
dep,
exc,
)
return DependencyConflict(dep)
return None
27 changes: 25 additions & 2 deletions opentelemetry-instrumentation/tests/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

# pylint: disable=protected-access

import pkg_resources
import pytest

from opentelemetry.instrumentation.dependencies import (
DependencyConflict,
get_dependency_conflicts,
get_dist_dependency_conflicts,
)
from opentelemetry.test.test_base import TestBase

Expand All @@ -37,7 +39,6 @@ def test_get_dependency_conflicts_not_installed(self):
conflict = get_dependency_conflicts(["this-package-does-not-exist"])
self.assertTrue(conflict is not None)
self.assertTrue(isinstance(conflict, DependencyConflict))
print(conflict)
self.assertEqual(
str(conflict),
'DependencyConflict: requested: "this-package-does-not-exist" but found: "None"',
Expand All @@ -47,10 +48,32 @@ def test_get_dependency_conflicts_mismatched_version(self):
conflict = get_dependency_conflicts(["pytest == 5000"])
self.assertTrue(conflict is not None)
self.assertTrue(isinstance(conflict, DependencyConflict))
print(conflict)
self.assertEqual(
str(conflict),
'DependencyConflict: requested: "pytest == 5000" but found: "pytest {0}"'.format(
pytest.__version__
),
)

def test_get_dist_dependency_conflicts(self):
def mock_requires(extras=()):
if "instruments" in extras:
return [
pkg_resources.Requirement(
'test-pkg ~= 1.0; extra == "instruments"'
)
]
return []

dist = pkg_resources.Distribution(
project_name="test-instrumentation", version="1.0"
)
dist.requires = mock_requires

conflict = get_dist_dependency_conflicts(dist)
self.assertTrue(conflict is not None)
self.assertTrue(isinstance(conflict, DependencyConflict))
self.assertEqual(
str(conflict),
'DependencyConflict: requested: "test-pkg~=1.0" but found: "None"',
)