-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add tracing_attributes to tracing decorator #9297
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
802c246
Add tracing_attributes to tracing decorator
lmazuel ff76264
Add tests
lmazuel c7e6e89
Fix request_id tests
lmazuel 68f9d83
Add attribute tests for tracing decorator
lmazuel 56f105d
Blackened
lmazuel 0e486a7
Improve tests
lmazuel 339d678
Change tracing_attributes to kwargs
lmazuel 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
182 changes: 182 additions & 0 deletions
182
sdk/core/azure-core/tests/azure_core_asynctests/test_tracing_decorator_async.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,182 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| """The tests for decorators_async.py""" | ||
|
|
||
| try: | ||
| from unittest import mock | ||
| except ImportError: | ||
| import mock | ||
|
|
||
| import sys | ||
| import time | ||
|
|
||
| import pytest | ||
| from azure.core.pipeline import Pipeline, PipelineResponse | ||
| from azure.core.pipeline.policies import HTTPPolicy | ||
| from azure.core.pipeline.transport import HttpTransport, HttpRequest | ||
| from azure.core.settings import settings | ||
| from azure.core.tracing.decorator import distributed_trace | ||
| from azure.core.tracing.decorator_async import distributed_trace_async | ||
| from tracing_common import FakeSpan | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def fake_span(): | ||
| settings.tracing_implementation.set_value(FakeSpan) | ||
|
|
||
|
|
||
| class MockClient: | ||
| @distributed_trace | ||
| def __init__(self, policies=None, assert_current_span=False): | ||
| time.sleep(0.001) | ||
| self.request = HttpRequest("GET", "https://bing.com") | ||
| if policies is None: | ||
| policies = [] | ||
| policies.append(mock.Mock(spec=HTTPPolicy, send=self.verify_request)) | ||
| self.policies = policies | ||
| self.transport = mock.Mock(spec=HttpTransport) | ||
| self.pipeline = Pipeline(self.transport, policies=policies) | ||
|
|
||
| self.expected_response = mock.Mock(spec=PipelineResponse) | ||
| self.assert_current_span = assert_current_span | ||
|
|
||
| def verify_request(self, request): | ||
| if self.assert_current_span: | ||
| assert execution_context.get_current_span() is not None | ||
| return self.expected_response | ||
|
|
||
| @distributed_trace_async | ||
| async def make_request(self, numb_times, **kwargs): | ||
| time.sleep(0.001) | ||
| if numb_times < 1: | ||
| return None | ||
| response = self.pipeline.run(self.request, **kwargs) | ||
| await self.get_foo(merge_span=True) | ||
| kwargs['merge_span'] = True | ||
| await self.make_request(numb_times - 1, **kwargs) | ||
| return response | ||
|
|
||
| @distributed_trace_async | ||
| async def merge_span_method(self): | ||
| return await self.get_foo(merge_span=True) | ||
|
|
||
| @distributed_trace_async | ||
| async def no_merge_span_method(self): | ||
| return await self.get_foo() | ||
|
|
||
| @distributed_trace_async | ||
| async def get_foo(self): | ||
| time.sleep(0.001) | ||
| return 5 | ||
|
|
||
| @distributed_trace_async(name_of_span="different name") | ||
| async def check_name_is_different(self): | ||
| time.sleep(0.001) | ||
|
|
||
| @distributed_trace_async(tracing_attributes={'foo': 'bar'}) | ||
| async def tracing_attr(self): | ||
| time.sleep(0.001) | ||
|
|
||
| @distributed_trace_async | ||
| async def raising_exception(self): | ||
| raise ValueError("Something went horribly wrong here") | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("fake_span") | ||
| class TestAsyncDecorator(object): | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_decorator_tracing_attr(self): | ||
| with FakeSpan(name="parent") as parent: | ||
| client = MockClient() | ||
| await client.tracing_attr() | ||
|
|
||
| assert len(parent.children) == 2 | ||
| assert parent.children[0].name == "MockClient.__init__" | ||
| assert parent.children[1].name == "MockClient.tracing_attr" | ||
| assert parent.children[1].attributes == {'foo': 'bar'} | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_decorator_has_different_name(self): | ||
| with FakeSpan(name="parent") as parent: | ||
| client = MockClient() | ||
| await client.check_name_is_different() | ||
| assert len(parent.children) == 2 | ||
| assert parent.children[0].name == "MockClient.__init__" | ||
| assert parent.children[1].name == "different name" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_used(self): | ||
| with FakeSpan(name="parent") as parent: | ||
| client = MockClient(policies=[]) | ||
| await client.get_foo(parent_span=parent) | ||
| await client.get_foo() | ||
|
|
||
| assert len(parent.children) == 3 | ||
| assert parent.children[0].name == "MockClient.__init__" | ||
| assert not parent.children[0].children | ||
| assert parent.children[1].name == "MockClient.get_foo" | ||
| assert not parent.children[1].children | ||
| assert parent.children[2].name == "MockClient.get_foo" | ||
| assert not parent.children[2].children | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_span_merge_span(self): | ||
| with FakeSpan(name="parent") as parent: | ||
| client = MockClient() | ||
| await client.merge_span_method() | ||
| await client.no_merge_span_method() | ||
|
|
||
| assert len(parent.children) == 3 | ||
| assert parent.children[0].name == "MockClient.__init__" | ||
| assert not parent.children[0].children | ||
| assert parent.children[1].name == "MockClient.merge_span_method" | ||
| assert not parent.children[1].children | ||
| assert parent.children[2].name == "MockClient.no_merge_span_method" | ||
| assert parent.children[2].children[0].name == "MockClient.get_foo" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_span_complicated(self): | ||
| with FakeSpan(name="parent") as parent: | ||
| client = MockClient() | ||
| await client.make_request(2) | ||
| with parent.span("child") as child: | ||
| time.sleep(0.001) | ||
| await client.make_request(2, parent_span=parent) | ||
| assert FakeSpan.get_current_span() == child | ||
| await client.make_request(2) | ||
|
|
||
| assert len(parent.children) == 4 | ||
| assert parent.children[0].name == "MockClient.__init__" | ||
| assert not parent.children[0].children | ||
| assert parent.children[1].name == "MockClient.make_request" | ||
| assert not parent.children[1].children | ||
| assert parent.children[2].name == "child" | ||
| assert parent.children[2].children[0].name == "MockClient.make_request" | ||
| assert parent.children[3].name == "MockClient.make_request" | ||
| assert not parent.children[3].children | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_span_with_exception(self): | ||
| """Assert that if an exception is raised, the next sibling method is actually a sibling span. | ||
| """ | ||
| with FakeSpan(name="parent") as parent: | ||
| client = MockClient() | ||
| try: | ||
| await client.raising_exception() | ||
| except: | ||
| pass | ||
| await client.get_foo() | ||
|
|
||
| assert len(parent.children) == 3 | ||
| assert parent.children[0].name == "MockClient.__init__" | ||
| assert parent.children[1].name == "MockClient.raising_exception" | ||
| # Exception should propagate status for Opencensus | ||
| assert parent.children[1].status == 'Something went horribly wrong here' | ||
| assert parent.children[2].name == "MockClient.get_foo" | ||
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
Oops, something went wrong.
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.