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

Make auto instrumentation use the same dependency resolver as manual instrumentation does #3202

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

rjduffner
Copy link
Contributor

@rjduffner rjduffner commented Jan 22, 2025

Description

In order for the auto instrumentation to use the same dependency checker as the instrumentation itself, I introduce an exception that allows for an early exit from instrument function through the distro and back to the _load.

Fixes #3201

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • Updated existing tests to handle the case of the new dependency check
  • Added a new test to make sure that when a dependency conflict gets returned to the instrument function, it raises an exception.

Does This PR Require a Core Repo Change?

  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added

@rjduffner rjduffner force-pushed the rjduffner-autoinstrumentation-2 branch 3 times, most recently from 9cf2a02 to 49762d1 Compare January 27, 2025 22:22
@rjduffner rjduffner changed the title Add a DependencyConflictError Exception To Allow For Instrumentation To Stop When Dependcies Are Not Installed Make auto instrumentation use the same dependency resolver as manual instrumentation does Jan 27, 2025
@rjduffner rjduffner marked this pull request as ready for review January 27, 2025 22:34
@rjduffner rjduffner requested a review from a team as a code owner January 27, 2025 22:34
@rjduffner rjduffner force-pushed the rjduffner-autoinstrumentation-2 branch 3 times, most recently from 00362cf to 149f84f Compare January 28, 2025 00:02
@rjduffner
Copy link
Contributor Author

@xrmx, Here is an option for making the auto instrumentation use the same dependency checker as the manual instrumentation.

There are a few rough edges but I think ironing those out gets us down a path of a major rewrite pretty quickly so maybe this little PR could work.


_logger = getLogger(__name__)


class _EntryPointDistFinder:
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason why you are removing this cache? I'm not sure this cache really helps with anything :)

Copy link
Contributor Author

@rjduffner rjduffner Jan 28, 2025

Choose a reason for hiding this comment

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

When I removed line 103 below, I couldn't find any other usages of this class so I removed this class just to keep things tidy.

If I missed a usage or a use case somewhere I am happy to re-add it. I just pulled it since my code search told me it was dead code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually think get_dist_dependency_conflicts is now unused too

> ag get_dist_dependency_conflicts
opentelemetry-instrumentation/tests/test_dependencies.py
23:    get_dist_dependency_conflicts,
68:    def test_get_dist_dependency_conflicts(self):
82:        conflict = get_dist_dependency_conflicts(dist)
90:    def test_get_dist_dependency_conflicts_requires_none(self):
103:        conflict = get_dist_dependency_conflicts(dist)

opentelemetry-instrumentation/src/opentelemetry/instrumentation/dependencies.py
51:def get_dist_dependency_conflicts(

CHANGELOG.md
64:- `opentelemetry-instrumentation` Fix `get_dist_dependency_conflicts` if no distribution requires

@rjduffner rjduffner force-pushed the rjduffner-autoinstrumentation-2 branch from 3533e55 to aad1679 Compare January 28, 2025 22:00
@rjduffner
Copy link
Contributor Author

@xrmx just checking in, have you had a chance to look at this and my responses to your comment?

@rjduffner rjduffner force-pushed the rjduffner-autoinstrumentation-2 branch 2 times, most recently from 28e57e5 to 1b6d07e Compare February 6, 2025 04:47
@xrmx
Copy link
Contributor

xrmx commented Feb 6, 2025

@xrmx just checking in, have you had a chance to look at this and my responses to your comment?

Not yet, sorry

@rjduffner rjduffner force-pushed the rjduffner-autoinstrumentation-2 branch 2 times, most recently from d8af135 to a5c86fa Compare February 13, 2025 04:21
@rjduffner rjduffner force-pushed the rjduffner-autoinstrumentation-2 branch 2 times, most recently from 87a167d to eecb02f Compare February 19, 2025 22:40
lzchen
lzchen previously approved these changes Feb 21, 2025
@lzchen lzchen self-requested a review February 21, 2025 21:54
@lzchen lzchen dismissed their stale review February 21, 2025 21:55

Accident

@rjduffner rjduffner force-pushed the rjduffner-autoinstrumentation-2 branch from eecb02f to e0d489f Compare February 24, 2025 19:35
@rjduffner
Copy link
Contributor Author

@xrmx changelog fixed up

@@ -64,41 +62,3 @@ def test_get_dependency_conflicts_mismatched_version(self):
str(conflict),
f'DependencyConflict: requested: "pytest == 5000" but found: "pytest {pytest.__version__}"',
)

def test_get_dist_dependency_conflicts(self):
Copy link
Member

Choose a reason for hiding this comment

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

I understand this is deleted because we don't need get_dist_dependency_conflicts anymore. or is there other any reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ya the function, get_dist_dependency_conflicts has been fully removed and we no longer pull dependencies from the dist.

https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3202/files#diff-fa513a8f13e9ef85e44311858ced6f35bb55b07f87f19ca27094ff82be08ae34L43-L59

def test_instruments_without_fastapi_installed(self, mock_version): # pylint: disable=no-self-use
mock_version.side_effect = mock_version_without_fastapi
@patch("opentelemetry.instrumentation.auto_instrumentation._load._logger")
def test_instruments_without_fastapi_installed(self, mock_logger): # pylint: disable=no-self-use
Copy link
Member

Choose a reason for hiding this comment

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

Would you open to write similar test to kafka since it's the instrumentation lib stated in your issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ya I can work on that. I am at a company conference this week but will try to get to it next week.

@rjduffner rjduffner force-pushed the rjduffner-autoinstrumentation-2 branch from c4b965b to 7796556 Compare March 3, 2025 21:27
@rjduffner
Copy link
Contributor Author

rjduffner commented Mar 3, 2025

@emdneto,

I added some testing but stopped short of the full auto instrumentation test. I stopped short because the maintainer of kafka-python has come back and is now releasing updates again. The maintainer of kafka-python-ng is reccomending switching back to kafka-python and will be archiving kafka-python-ng soon (kafka-python-ng/kafka-python-ng#210 (comment)).

These two packages were the reason this whole bug came about. But in a future PR (very soon) I'll be back with an update to the kafka-python instrumentation so it will support py312 and removal of the kafka-python-ng part of this instrumentation.

In that vein, I don't think we need to add a test for a use case that will no longer happen here shortly.

However, i still believe this PR should be merged and pushed out as I think using the same dependency resolver everywhere is the better option long term as there is one other instrumentation that also has this same behavior.

cc @xrmx

@rjduffner rjduffner force-pushed the rjduffner-autoinstrumentation-2 branch from 7fa0e85 to 97db979 Compare March 4, 2025 00:07
@xrmx
Copy link
Contributor

xrmx commented Mar 6, 2025

@emdneto,

I added some testing but stopped short of the full auto instrumentation test. I stopped short because the maintainer of kafka-python has come back and is now releasing updates again. The maintainer of kafka-python-ng is reccomending switching back to kafka-python and will be archiving kafka-python-ng soon (kafka-python-ng/kafka-python-ng#210 (comment)).

These two packages were the reason this whole bug came about. But in a future PR (very soon) I'll be back with an update to the kafka-python instrumentation so it will support py312 and removal of the kafka-python-ng part of this instrumentation.

In that vein, I don't think we need to add a test for a use case that will no longer happen here shortly.

However, i still believe this PR should be merged and pushed out as I think using the same dependency resolver everywhere is the better option long term as there is one other instrumentation that also has this same behavior.

cc @xrmx

psycopg2 / psycopg2-binary won't go away so we can write a test case for that

@rjduffner
Copy link
Contributor Author

Ok ill take a look at those tomorrow... hopefully. :)

@rjduffner rjduffner force-pushed the rjduffner-autoinstrumentation-2 branch from 97db979 to 379a9f2 Compare March 11, 2025 20:54



class TestPsycopg2InstrumentationDependencies(TestCase):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@emdneto, I think i got it here.

I added some tests first to make sure that the instrumentation_dependencies functions handle both, one or none installed.

The last test, test_instruments_with_psycopg2_installed, tests higher up in the auto instrumentation flow to make sure that the individual error around one package working but not the other doesn't happen again.

@rjduffner rjduffner force-pushed the rjduffner-autoinstrumentation-2 branch from 214f9fe to 92d697a Compare March 12, 2025 00:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants