-
Notifications
You must be signed in to change notification settings - Fork 798
opentelemetry-instrumentation: expose a way to init autoinstrumentation programmatically #3273
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
Merged
+128
−28
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
87befcb
opentelemetry-instrumentation: expose a way to init autoinstrumentation
xrmx 70e3f14
Please pylint
xrmx 7b330fb
Add changelog
xrmx 9447b83
Fix example
xrmx ef518ab
Fix whitespace in README
xrmx 2dfdff2
Add a note aboout ordering of initialization vs imports
xrmx 39f81ce
Don't touch PYTHONPATH if not set
xrmx e421f34
Update opentelemetry-instrumentation/README.rst
xrmx c6e67dd
Update CHANGELOG.md
xrmx 8a84828
Merge branch 'main' into manual-autoinstrumentation
xrmx 760eb49
Update opentelemetry-instrumentation/README.rst
xrmx 6baf449
Merge branch 'main' into manual-autoinstrumentation
xrmx ffdea38
Merge branch 'main' into manual-autoinstrumentation
xrmx 6fd61a7
Merge branch 'main' into manual-autoinstrumentation
xrmx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
opentelemetry-instrumentation/tests/auto_instrumentation/test_initialize.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# type: ignore | ||
|
||
from os import environ | ||
from os.path import abspath, dirname, pathsep | ||
from unittest import TestCase | ||
from unittest.mock import patch | ||
|
||
from opentelemetry.instrumentation import auto_instrumentation | ||
|
||
# TODO: convert to assertNoLogs instead of mocking logger when 3.10 is baseline | ||
|
||
|
||
class TestInitialize(TestCase): | ||
auto_instrumentation_path = dirname(abspath(auto_instrumentation.__file__)) | ||
|
||
@patch.dict("os.environ", {}, clear=True) | ||
@patch("opentelemetry.instrumentation.auto_instrumentation._logger") | ||
def test_handles_pythonpath_not_set(self, logger_mock): | ||
auto_instrumentation.initialize() | ||
self.assertNotIn("PYTHONPATH", environ) | ||
logger_mock.exception.assert_not_called() | ||
|
||
@patch.dict("os.environ", {"PYTHONPATH": "."}) | ||
@patch("opentelemetry.instrumentation.auto_instrumentation._logger") | ||
def test_handles_pythonpath_set(self, logger_mock): | ||
auto_instrumentation.initialize() | ||
self.assertEqual(environ["PYTHONPATH"], ".") | ||
logger_mock.exception.assert_not_called() | ||
|
||
@patch.dict( | ||
"os.environ", | ||
{"PYTHONPATH": auto_instrumentation_path + pathsep + "foo"}, | ||
) | ||
@patch("opentelemetry.instrumentation.auto_instrumentation._logger") | ||
def test_clears_auto_instrumentation_path(self, logger_mock): | ||
auto_instrumentation.initialize() | ||
self.assertEqual(environ["PYTHONPATH"], "foo") | ||
logger_mock.exception.assert_not_called() | ||
|
||
@patch("opentelemetry.instrumentation.auto_instrumentation._logger") | ||
@patch("opentelemetry.instrumentation.auto_instrumentation._load_distro") | ||
def test_handles_exceptions(self, load_distro_mock, logger_mock): | ||
# pylint:disable=no-self-use | ||
load_distro_mock.side_effect = ValueError | ||
auto_instrumentation.initialize() | ||
logger_mock.exception.assert_called_once_with( | ||
"Failed to auto initialize OpenTelemetry" | ||
) |
28 changes: 28 additions & 0 deletions
28
opentelemetry-instrumentation/tests/auto_instrumentation/test_sitecustomize.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# type: ignore | ||
|
||
from unittest import TestCase | ||
from unittest.mock import patch | ||
|
||
|
||
class TestSiteCustomize(TestCase): | ||
# pylint:disable=import-outside-toplevel,unused-import,no-self-use | ||
@patch("opentelemetry.instrumentation.auto_instrumentation.initialize") | ||
def test_sitecustomize_side_effects(self, initialize_mock): | ||
initialize_mock.assert_not_called() | ||
|
||
import opentelemetry.instrumentation.auto_instrumentation.sitecustomize # NOQA | ||
|
||
initialize_mock.assert_called_once() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.