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

Bugfix/check future cancelled #2461

Merged
merged 21 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2418](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2418))
- Use sqlalchemy version in sqlalchemy commenter instead of opentelemetry library version
([#2404](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2404))
- `opentelemetry-instrumentation-asyncio` Check for cancelledException in the future
([#2461](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2461))

## Version 1.24.0/0.45b0 (2024-03-28)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,6 @@ class AsyncioInstrumentor(BaseInstrumentor):
"run_coroutine_threadsafe",
]

def __init__(self):
super().__init__()
self.process_duration_histogram = None
self.process_created_counter = None

self._tracer = None
self._meter = None
self._coros_name_to_trace: set = set()
self._to_thread_name_to_trace: set = set()
self._future_active_enabled: bool = False
aabmass marked this conversation as resolved.
Show resolved Hide resolved

def instrumentation_dependencies(self) -> Collection[str]:
return _instruments

Expand Down Expand Up @@ -307,13 +296,17 @@ def trace_future(self, future):
)

def callback(f):
exception = f.exception()
attr = {
"type": "future",
"state": (
"cancelled"
if f.cancelled()
else determine_state(f.exception())
aabmass marked this conversation as resolved.
Show resolved Hide resolved
),
}
state = determine_state(exception)
attr["state"] = state
self.record_process(start, attr, span, exception)
self.record_process(
start, attr, span, None if f.cancelled() else f.exception()
)

future.add_done_callback(callback)
return future
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import asyncio
from unittest.mock import patch

import pytest

from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
from opentelemetry.instrumentation.asyncio.environment_variables import (
OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED,
)
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import get_tracer


class TestTraceFuture(TestBase):
@patch.dict(
"os.environ", {OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED: "true"}
)
def setUp(self):
super().setUp()
self._tracer = get_tracer(
__name__,
)
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
aabmass marked this conversation as resolved.
Show resolved Hide resolved
self.instrumentor = AsyncioInstrumentor()
self.instrumentor.instrument()

def tearDown(self):
super().tearDown()
self.instrumentor.uninstrument()
self.loop.close()

@pytest.mark.asyncio
aabmass marked this conversation as resolved.
Show resolved Hide resolved
def test_trace_future_cancelled(self):
aabmass marked this conversation as resolved.
Show resolved Hide resolved
with self._tracer.start_as_current_span("root"):
future = asyncio.Future()
future = self.instrumentor.trace_future(future)
future.cancel()
try:
self.loop.run_until_complete(future)
except asyncio.CancelledError as exc:
self.assertEqual(isinstance(exc, asyncio.CancelledError), True)
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 2)
self.assertEqual(spans[0].name, "root")
self.assertEqual(spans[1].name, "asyncio future")
for metric in (
self.memory_metrics_reader.get_metrics_data()
.resource_metrics[0]
.scope_metrics[0]
.metrics
):
if metric.name == "asyncio.process.duration":
for point in metric.data.data_points:
self.assertEqual(point.attributes["type"], "future")
if metric.name == "asyncio.process.created":
for point in metric.data.data_points:
self.assertEqual(point.attributes["type"], "future")
self.assertEqual(point.attributes["state"], "cancelled")
aabmass marked this conversation as resolved.
Show resolved Hide resolved
Loading