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 8b077af commit 9259d72
Show file tree
Hide file tree
Showing 17 changed files with 218 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ classifiers = [
]
dependencies = [
"opentelemetry-api ~= 1.14",
"opentelemetry-instrumentation == 0.44b0.dev",
"opentelemetry-semantic-conventions == 0.44b0.dev",
"opentelemetry-test-utils == 0.44b0.dev",
"wrapt >= 1.0.0, < 2.0.0",
]

[project.optional-dependencies]
instruments = []
test = [
"opentelemetry-instrumentation-asyncio[instruments]",
"opentelemetry-test-utils == 0.43b0",
"pytest",
"wrapt >= 1.0.0, < 2.0.0",
"pytest-asyncio",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ async def main():
-------------
.. code:: python
# export OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE=func
import asyncio
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
Expand Down Expand Up @@ -103,18 +101,43 @@ def func():
from timeit import default_timer
from typing import Collection

from opentelemetry.metrics import get_meter
from opentelemetry.trace import get_tracer
from opentelemetry.trace.status import Status, StatusCode
from wrapt import wrap_function_wrapper as _wrap

from opentelemetry.instrumentation.asyncio.metrics import *
from opentelemetry.instrumentation.asyncio.metrics import (
ASYNCIO_COROUTINE_ACTIVE,
ASYNCIO_COROUTINE_CANCELLED,
ASYNCIO_COROUTINE_CREATED,
ASYNCIO_COROUTINE_DURATION,
ASYNCIO_COROUTINE_EXCEPTIONS,
ASYNCIO_COROUTINE_FINISHED,
ASYNCIO_COROUTINE_NAME,
ASYNCIO_COROUTINE_TIMEOUTS,
ASYNCIO_EXCEPTIONS_NAME,
ASYNCIO_FUTURES_ACTIVE,
ASYNCIO_FUTURES_CANCELLED,
ASYNCIO_FUTURES_CREATED,
ASYNCIO_FUTURES_DURATION,
ASYNCIO_FUTURES_EXCEPTIONS,
ASYNCIO_FUTURES_FINISHED,
ASYNCIO_FUTURES_TIMEOUTS,
ASYNCIO_TO_THREAD_ACTIVE,
ASYNCIO_TO_THREAD_CREATED,
ASYNCIO_TO_THREAD_DURATION,
ASYNCIO_TO_THREAD_EXCEPTIONS,
ASYNCIO_TO_THREAD_FINISHED,
)
from opentelemetry.instrumentation.asyncio.package import _instruments
from opentelemetry.instrumentation.asyncio.utils import get_coros_to_trace, get_future_trace_enabled, \
get_to_thread_to_trace
from opentelemetry.instrumentation.asyncio.utils import (
get_coros_to_trace,
get_future_trace_enabled,
get_to_thread_to_trace,
)
from opentelemetry.instrumentation.asyncio.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.utils import unwrap
from opentelemetry.metrics import get_meter
from opentelemetry.trace import get_tracer
from opentelemetry.trace.status import Status, StatusCode

ASYNCIO_PREFIX = "asyncio."

Expand Down Expand Up @@ -170,9 +193,7 @@ def instrumentation_dependencies(self) -> Collection[str]:

def _instrument(self, **kwargs):
tracer_provider = kwargs.get("tracer_provider")
self._tracer = get_tracer(
__name__, __version__, tracer_provider
)
self._tracer = get_tracer(__name__, __version__, tracer_provider)
self._meter = get_meter(
__name__, __version__, kwargs.get("meter_provider")
)
Expand Down Expand Up @@ -211,11 +232,15 @@ def wrap_coro_or_future(method, instance, args, kwargs):
if args and len(args) > 0:
first_arg = args[0]
# Check if it's a coroutine or future and wrap it
if asyncio.iscoroutine(first_arg) or futures.isfuture(first_arg):
if asyncio.iscoroutine(first_arg) or futures.isfuture(
first_arg
):
args = (self.trace_item(first_arg),) + args[1:]
# Check if it's a list and wrap each item
elif isinstance(first_arg, list):
args = ([self.trace_item(item) for item in first_arg],) + args[1:]
args = (
[self.trace_item(item) for item in first_arg],
) + args[1:]
return method(*args, **kwargs)

_wrap(asyncio, method_name, wrap_coro_or_future)
Expand All @@ -227,7 +252,6 @@ def uninstrument_method_with_coroutine(self, method_name):
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
Expand Down Expand Up @@ -286,8 +310,13 @@ def trace_to_thread(self, func):
start = default_timer()
self.to_thread_created_metric.add(1)
self.to_thread_active_metric.add(1)
span = self._tracer.start_span(
f"{ASYNCIO_PREFIX}to_thread_func-" + func.__name__) if func.__name__ in self._to_thread_name_to_trace else None
span = (
self._tracer.start_span(
f"{ASYNCIO_PREFIX}to_thread_func-" + func.__name__
)
if func.__name__ in self._to_thread_name_to_trace
else None
)
exception = None
try:
return func
Expand Down Expand Up @@ -326,8 +355,11 @@ async def trace_coroutine(self, coro):
self.coro_created_metric.add(1, coro_attr)
self.coro_active_metric.add(1, coro_attr)

span = self._tracer.start_span(
f"{ASYNCIO_PREFIX}coro-" + coro.__name__) if coro.__name__ in self._coros_name_to_trace else None
span = (
self._tracer.start_span(f"{ASYNCIO_PREFIX}coro-" + coro.__name__)
if coro.__name__ in self._coros_name_to_trace
else None
)

exception = None
try:
Expand All @@ -343,7 +375,9 @@ async def trace_coroutine(self, coro):
except Exception as exc:
exception = exc
coro_exception_attr = coro_attr.copy()
coro_exception_attr[ASYNCIO_EXCEPTIONS_NAME] = exc.__class__.__name__
coro_exception_attr[
ASYNCIO_EXCEPTIONS_NAME
] = exc.__class__.__name__
self.coro_exception_metric.add(1, coro_exception_attr)
raise
finally:
Expand All @@ -362,7 +396,11 @@ def trace_future(self, future):
start = default_timer()
self.future_created_metric.add(1)
self.future_active_metric.add(1)
span = self._tracer.start_span(f"{ASYNCIO_PREFIX}future") if self._future_active_enabled else None
span = (
self._tracer.start_span(f"{ASYNCIO_PREFIX}future")
if self._future_active_enabled
else None
)

def callback(f):
exception = f.exception()
Expand All @@ -371,7 +409,9 @@ def callback(f):
elif isinstance(exception, asyncio.TimeoutError):
self.future_timeout_metric.add(1)
elif exception:
exception_attr = {ASYNCIO_EXCEPTIONS_NAME: exception.__class__.__name__}
exception_attr = {
ASYNCIO_EXCEPTIONS_NAME: exception.__class__.__name__
}
self.future_exception_metric.add(1, exception_attr)

duration = max(round((default_timer() - start) * 1000), 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@
"""
Enter the names of the coroutines to be traced through the environment variable below, separated by commas.
"""
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE = "OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE"
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE = (
"OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE"
)

"""
To determines whether the tracing feature for Future of Asyncio in Python is enabled or not.
"""
OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED = "OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED"
OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED = (
"OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED"
)

"""
Enter the names of the functions to be traced through the environment variable below, separated by commas.
"""
OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE = "OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE"
OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE = (
"OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE"
)
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,15 @@
"ASYNCIO_COROUTINE_CREATED",
"ASYNCIO_COROUTINE_FINISHED",
"ASYNCIO_COROUTINE_TIMEOUTS",

"ASYNCIO_COROUTINE_NAME",
"ASYNCIO_EXCEPTIONS_NAME",

"ASYNCIO_FUTURES_DURATION",
"ASYNCIO_FUTURES_EXCEPTIONS",
"ASYNCIO_FUTURES_CANCELLED",
"ASYNCIO_FUTURES_CREATED",
"ASYNCIO_FUTURES_ACTIVE",
"ASYNCIO_FUTURES_FINISHED",
"ASYNCIO_FUTURES_TIMEOUTS",

"ASYNCIO_TO_THREAD_DURATION",
"ASYNCIO_TO_THREAD_EXCEPTIONS",
"ASYNCIO_TO_THREAD_CREATED",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
# limitations under the License.
import os

from opentelemetry.instrumentation.asyncio.environment_variables import OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE, \
OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED, OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE
from opentelemetry.instrumentation.asyncio.environment_variables import (
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE,
OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED,
OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE,
)


def separate_coro_names_by_comma(coro_names) -> set:
Expand All @@ -39,15 +42,24 @@ def get_future_trace_enabled() -> bool:
Function to get the future active enabled flag from the environment variable
default value is False
"""
return os.getenv(OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED, "False").lower() == "true"
return (
os.getenv(OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED, "False").lower()
== "true"
)


def get_to_thread_to_trace() -> set:
"""
Function to get the functions to be traced from the environment variable
"""
func_names = os.getenv(OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE)
func_names = os.getenv(
OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE
)
return separate_coro_names_by_comma(func_names)


__all__ = ["get_coros_to_trace", "get_future_trace_enabled", "get_to_thread_to_trace"]
__all__ = [
"get_coros_to_trace",
"get_future_trace_enabled",
"get_to_thread_to_trace",
]
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
# 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.
# limitations under the License.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ async def async_func():
await asyncio.sleep(0.1)


async def factorial(name, number):
f = 1
for i in range(2, number + 1):
print(f"Task {name}: Compute factorial({number}), currently i={i}...")
async def factorial(number):
factorial_value = 1
for value in range(2, number + 1):
await asyncio.sleep(0)
f *= i
print(f"Task {name}: factorial({number}) = {f}")
return f
factorial_value *= value
return factorial_value


async def cancellable_coroutine():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,29 @@
import asyncio
from unittest.mock import patch

from opentelemetry.instrumentation.asyncio import (
ASYNCIO_COROUTINE_ACTIVE,
ASYNCIO_COROUTINE_CANCELLED,
ASYNCIO_COROUTINE_CREATED,
ASYNCIO_COROUTINE_DURATION,
ASYNCIO_COROUTINE_FINISHED,
AsyncioInstrumentor,
)
from opentelemetry.instrumentation.asyncio.environment_variables import (
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE,
)
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import get_tracer, SpanKind
from opentelemetry.trace import SpanKind, get_tracer

from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor, ASYNCIO_COROUTINE_CANCELLED, \
ASYNCIO_COROUTINE_DURATION, ASYNCIO_COROUTINE_ACTIVE, ASYNCIO_COROUTINE_CREATED, ASYNCIO_COROUTINE_FINISHED
from opentelemetry.instrumentation.asyncio.environment_variables import OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE
from .common_test_func import cancellation_create_task


class TestAsyncioCancel(TestBase):
@patch.dict(
"os.environ", {
"os.environ",
{
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE: "cancellation_coro, cancellable_coroutine"
}
},
)
def setUp(self):
super().setUp()
Expand All @@ -48,13 +57,18 @@ def test_cancel(self):
self.assertEqual(spans[0].context.trace_id, spans[1].context.trace_id)
self.assertEqual(spans[2].context.trace_id, spans[1].context.trace_id)

self.assertEqual(spans[0].name, 'asyncio.coro-cancellable_coroutine')
self.assertEqual(spans[1].name, 'asyncio.coro-cancellation_coro')
for metric in self.memory_metrics_reader.get_metrics_data().resource_metrics[0].scope_metrics[0].metrics:
self.assertEqual(spans[0].name, "asyncio.coro-cancellable_coroutine")
self.assertEqual(spans[1].name, "asyncio.coro-cancellation_coro")
for metric in (
self.memory_metrics_reader.get_metrics_data()
.resource_metrics[0]
.scope_metrics[0]
.metrics
):
if metric.name == ASYNCIO_COROUTINE_CANCELLED:
self.assertEqual(metric.data.data_points[0].value, 1)
elif metric.name == ASYNCIO_COROUTINE_DURATION:
self.assertNotEquals(metric.data.data_points[0].min, 0)
self.assertEqual(metric.data.data_points[0].min != 0, True)
elif metric.name == ASYNCIO_COROUTINE_ACTIVE:
self.assertEqual(metric.data.data_points[0].value, 0)
elif metric.name == ASYNCIO_COROUTINE_CREATED:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
import asyncio
from unittest.mock import patch

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

from .common_test_func import factorial
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
from opentelemetry.instrumentation.asyncio.environment_variables import OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE


class TestAsyncioCreateTask(TestBase):
@patch.dict(
"os.environ", {
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE: "sleep"
}
"os.environ", {OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE: "sleep"}
)
def setUp(self):
super().setUp()
Expand All @@ -42,7 +42,7 @@ def tearDown(self):
def test_asyncio_create_task(self):
async def async_func():
await asyncio.create_task(asyncio.sleep(0))
await asyncio.create_task(factorial("A", 3))
await asyncio.create_task(factorial(3))

asyncio.run(async_func())
spans = self.memory_exporter.get_finished_spans()
Expand Down
Loading

0 comments on commit 9259d72

Please sign in to comment.