Skip to content

Commit

Permalink
modified lint results
Browse files Browse the repository at this point in the history
  • Loading branch information
bourbonkk committed Dec 23, 2023
1 parent 05e6a74 commit 39d0a53
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def func():

from wrapt import wrap_function_wrapper as _wrap

# pylint: disable=no-name-in-module
from opentelemetry.instrumentation.asyncio.metrics import (
ASYNCIO_COROUTINE_ACTIVE,
ASYNCIO_COROUTINE_CANCELLED,
Expand Down Expand Up @@ -215,10 +216,10 @@ def _instrument(self, **kwargs):

def _uninstrument(self, **kwargs):
for method in self.methods_with_coroutine:
self.uninstrument_method_with_coroutine(method)
self.uninstrument_gather()
self.uninstrument_to_thread()
self.uninstrument_taskgroup_create_task()
uninstrument_method_with_coroutine(method)
uninstrument_gather()
uninstrument_to_thread()
uninstrument_taskgroup_create_task()

def instrument_method_with_coroutine(self, method_name):
"""
Expand All @@ -245,24 +246,16 @@ def wrap_coro_or_future(method, instance, args, kwargs):

_wrap(asyncio, method_name, wrap_coro_or_future)

def uninstrument_method_with_coroutine(self, method_name):
"""
Uninstrument specified asyncio method.
"""
unwrap(asyncio, method_name)

def instrument_gather(self):
def wrap_coros_or_futures(method, instance, args, kwargs):
if args and len(args) > 0:
# Check if it's a coroutine or future and wrap it
wrapped_args = tuple(self.trace_item(item) for item in args)
return method(*wrapped_args, **kwargs)
return method(*args, **kwargs)

_wrap(asyncio, "gather", wrap_coros_or_futures)

def uninstrument_gather(self):
unwrap(asyncio, "gather")

def instrument_to_thread(self):
# to_thread was added in Python 3.9
if sys.version_info < (3, 9):
Expand All @@ -276,15 +269,10 @@ def wrap_to_thread(method, instance, args, kwargs):
wrapped_args = (wrapped_first_arg,) + args[1:]

return method(*wrapped_args, **kwargs)
return method(*args, **kwargs)

_wrap(asyncio, "to_thread", wrap_to_thread)

def uninstrument_to_thread(self):
# to_thread was added in Python 3.9
if sys.version_info < (3, 9):
return
unwrap(asyncio, "to_thread")

def instrument_taskgroup_create_task(self):
# TaskGroup.create_task was added in Python 3.11
if sys.version_info < (3, 11):
Expand All @@ -296,14 +284,11 @@ def wrap_taskgroup_create_task(method, instance, args, kwargs):
wrapped_coro = self.trace_coroutine(coro)
wrapped_args = (wrapped_coro,) + args[1:]
return method(*wrapped_args, **kwargs)
return method(*args, **kwargs)

_wrap(asyncio.TaskGroup, "create_task", wrap_taskgroup_create_task)

def uninstrument_taskgroup_create_task(self):
# TaskGroup.create_task was added in Python 3.11
if sys.version_info < (3, 11):
return
unwrap(asyncio.TaskGroup, "create_task")
_wrap(
asyncio.TaskGroup, "create_task", wrap_taskgroup_create_task # pylint: disable=no-member
)

def trace_to_thread(self, func):
"""Trace a function."""
Expand Down Expand Up @@ -343,7 +328,7 @@ def trace_item(self, coro_or_future):
return coro_or_future
if asyncio.iscoroutine(coro_or_future):
return self.trace_coroutine(coro_or_future)
elif futures.isfuture(coro_or_future):
if futures.isfuture(coro_or_future):
return self.trace_future(coro_or_future)
return coro_or_future

Expand Down Expand Up @@ -527,3 +512,28 @@ def create_to_thread_metric(self):
description="Number of asyncio function finished",
unit="1",
)


def uninstrument_taskgroup_create_task():
# TaskGroup.create_task was added in Python 3.11
if sys.version_info < (3, 11):
return
unwrap(asyncio.TaskGroup, "create_task") # pylint: disable=no-member


def uninstrument_to_thread():
# to_thread was added in Python 3.9
if sys.version_info < (3, 9):
return
unwrap(asyncio, "to_thread")


def uninstrument_gather():
unwrap(asyncio, "gather")


def uninstrument_method_with_coroutine(method_name):
"""
Uninstrument specified asyncio method.
"""
unwrap(asyncio, method_name)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
import os

# pylint: disable=no-name-in-module
from opentelemetry.instrumentation.asyncio.environment_variables import (
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE,
OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import asyncio
from unittest.mock import patch

# pylint: disable=no-name-in-module
from opentelemetry.instrumentation.asyncio import (
ASYNCIO_COROUTINE_ACTIVE,
ASYNCIO_COROUTINE_CANCELLED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import asyncio
from unittest.mock import patch

# pylint: disable=no-name-in-module
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
from opentelemetry.instrumentation.asyncio.environment_variables import (
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE,
Expand Down Expand Up @@ -46,8 +47,6 @@ async def async_func():

asyncio.run(async_func())
spans = self.memory_exporter.get_finished_spans()
"""
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE: "sleep"
"""

self.assertEqual(len(spans), 1)
self.assertEqual(spans[0].name, "asyncio.coro-sleep")
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import pytest

# pylint: disable=no-name-in-module
from opentelemetry.instrumentation.asyncio import (
ASYNCIO_FUTURES_ACTIVE,
ASYNCIO_FUTURES_CANCELLED,
Expand Down Expand Up @@ -92,7 +93,7 @@ async def test():
if span.name == "root":
self.assertEqual(span.parent, None)
if span.name == "asyncio.future":
self.assertNotEquals(span.parent.trace_id, 0)
self.assertNotEqual(span.parent.trace_id, 0)

for metric in (
self.memory_metrics_reader.get_metrics_data()
Expand All @@ -101,7 +102,7 @@ async def test():
.metrics
):
if metric.name == ASYNCIO_FUTURES_DURATION:
self.assertEquals(metric.data.data_points[0].count, 1)
self.assertEqual(metric.data.data_points[0].count, 1)
elif metric.name == ASYNCIO_FUTURES_ACTIVE:
self.assertEqual(metric.data.data_points[0].value, 0)
elif metric.name == ASYNCIO_FUTURES_CREATED:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import asyncio
from unittest.mock import patch

# pylint: disable=no-name-in-module
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
from opentelemetry.instrumentation.asyncio.environment_variables import (
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import asyncio
from unittest.mock import patch

# pylint: disable=no-name-in-module
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
from opentelemetry.instrumentation.asyncio.environment_variables import (
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE,
Expand All @@ -31,9 +32,6 @@ def setUp(self):
__name__,
)

def tearDown(self):
super().tearDown()

@patch.dict(
"os.environ", {OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE: "sleep"}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from concurrent.futures import ThreadPoolExecutor
from unittest.mock import patch

# pylint: disable=no-name-in-module
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
from opentelemetry.instrumentation.asyncio.environment_variables import (
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import sys
from unittest.mock import patch

# pylint: disable=no-name-in-module
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
from opentelemetry.instrumentation.asyncio.environment_variables import (
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE,
Expand Down Expand Up @@ -51,7 +52,7 @@ def test_task_group_create_task(self):
return

async def main():
async with asyncio.TaskGroup() as tg:
async with asyncio.TaskGroup() as tg: # pylint: disable=no-member
for _ in range(10):
tg.create_task(async_func())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import sys
from unittest.mock import patch

# pylint: disable=no-name-in-module
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
from opentelemetry.instrumentation.asyncio.environment_variables import (
OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE,
Expand Down Expand Up @@ -63,7 +64,7 @@ async def to_thread():
.metrics
):
if metric.name == "asyncio.to_thread.duration":
self.assertEquals(metric.data.data_points[0].count, 1)
self.assertEqual(metric.data.data_points[0].count, 1)
elif metric.name == "asyncio.to_thread.active":
self.assertEqual(metric.data.data_points[0].value, 0)
elif metric.name == "asyncio.to_thread.created":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@
from unittest import TestCase
from unittest.mock import patch

# pylint: disable=no-name-in-module
from opentelemetry.instrumentation.asyncio.environment_variables import (
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE,
OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED,
)
from opentelemetry.instrumentation.asyncio.utils import (
get_coros_to_trace,
get_future_trace_enabled,
)


class TestAsyncioToThread(TestCase):
Expand All @@ -28,10 +33,6 @@ class TestAsyncioToThread(TestCase):
},
)
def test_separator(self):
from opentelemetry.instrumentation.asyncio.utils import (
get_coros_to_trace,
)

self.assertEqual(
get_coros_to_trace(), {"test1", "test2", "test3", "test4"}
)
Expand All @@ -40,8 +41,4 @@ def test_separator(self):
"os.environ", {OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED: "true"}
)
def test_future_trace_enabled(self):
from opentelemetry.instrumentation.asyncio.utils import (
get_future_trace_enabled,
)

self.assertEqual(get_future_trace_enabled(), True)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import sys
from unittest.mock import patch

# pylint: disable=no-name-in-module
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
from opentelemetry.instrumentation.asyncio.environment_variables import (
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE,
Expand Down
Loading

0 comments on commit 39d0a53

Please sign in to comment.