Skip to content

Commit 9259d72

Browse files
committed
modified lint results
1 parent 8b077af commit 9259d72

17 files changed

+218
-120
lines changed

instrumentation/opentelemetry-instrumentation-asyncio/pyproject.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ classifiers = [
2626
]
2727
dependencies = [
2828
"opentelemetry-api ~= 1.14",
29+
"opentelemetry-instrumentation == 0.44b0.dev",
30+
"opentelemetry-semantic-conventions == 0.44b0.dev",
31+
"opentelemetry-test-utils == 0.44b0.dev",
2932
"wrapt >= 1.0.0, < 2.0.0",
3033
]
3134

3235
[project.optional-dependencies]
3336
instruments = []
3437
test = [
3538
"opentelemetry-instrumentation-asyncio[instruments]",
36-
"opentelemetry-test-utils == 0.43b0",
3739
"pytest",
3840
"wrapt >= 1.0.0, < 2.0.0",
3941
"pytest-asyncio",

instrumentation/opentelemetry-instrumentation-asyncio/src/opentelemetry/instrumentation/asyncio/__init__.py

+61-21
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ async def main():
5252
-------------
5353
.. code:: python
5454
55-
# export OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE=func
56-
5755
import asyncio
5856
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
5957
@@ -103,18 +101,43 @@ def func():
103101
from timeit import default_timer
104102
from typing import Collection
105103

106-
from opentelemetry.metrics import get_meter
107-
from opentelemetry.trace import get_tracer
108-
from opentelemetry.trace.status import Status, StatusCode
109104
from wrapt import wrap_function_wrapper as _wrap
110105

111-
from opentelemetry.instrumentation.asyncio.metrics import *
106+
from opentelemetry.instrumentation.asyncio.metrics import (
107+
ASYNCIO_COROUTINE_ACTIVE,
108+
ASYNCIO_COROUTINE_CANCELLED,
109+
ASYNCIO_COROUTINE_CREATED,
110+
ASYNCIO_COROUTINE_DURATION,
111+
ASYNCIO_COROUTINE_EXCEPTIONS,
112+
ASYNCIO_COROUTINE_FINISHED,
113+
ASYNCIO_COROUTINE_NAME,
114+
ASYNCIO_COROUTINE_TIMEOUTS,
115+
ASYNCIO_EXCEPTIONS_NAME,
116+
ASYNCIO_FUTURES_ACTIVE,
117+
ASYNCIO_FUTURES_CANCELLED,
118+
ASYNCIO_FUTURES_CREATED,
119+
ASYNCIO_FUTURES_DURATION,
120+
ASYNCIO_FUTURES_EXCEPTIONS,
121+
ASYNCIO_FUTURES_FINISHED,
122+
ASYNCIO_FUTURES_TIMEOUTS,
123+
ASYNCIO_TO_THREAD_ACTIVE,
124+
ASYNCIO_TO_THREAD_CREATED,
125+
ASYNCIO_TO_THREAD_DURATION,
126+
ASYNCIO_TO_THREAD_EXCEPTIONS,
127+
ASYNCIO_TO_THREAD_FINISHED,
128+
)
112129
from opentelemetry.instrumentation.asyncio.package import _instruments
113-
from opentelemetry.instrumentation.asyncio.utils import get_coros_to_trace, get_future_trace_enabled, \
114-
get_to_thread_to_trace
130+
from opentelemetry.instrumentation.asyncio.utils import (
131+
get_coros_to_trace,
132+
get_future_trace_enabled,
133+
get_to_thread_to_trace,
134+
)
115135
from opentelemetry.instrumentation.asyncio.version import __version__
116136
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
117137
from opentelemetry.instrumentation.utils import unwrap
138+
from opentelemetry.metrics import get_meter
139+
from opentelemetry.trace import get_tracer
140+
from opentelemetry.trace.status import Status, StatusCode
118141

119142
ASYNCIO_PREFIX = "asyncio."
120143

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

171194
def _instrument(self, **kwargs):
172195
tracer_provider = kwargs.get("tracer_provider")
173-
self._tracer = get_tracer(
174-
__name__, __version__, tracer_provider
175-
)
196+
self._tracer = get_tracer(__name__, __version__, tracer_provider)
176197
self._meter = get_meter(
177198
__name__, __version__, kwargs.get("meter_provider")
178199
)
@@ -211,11 +232,15 @@ def wrap_coro_or_future(method, instance, args, kwargs):
211232
if args and len(args) > 0:
212233
first_arg = args[0]
213234
# Check if it's a coroutine or future and wrap it
214-
if asyncio.iscoroutine(first_arg) or futures.isfuture(first_arg):
235+
if asyncio.iscoroutine(first_arg) or futures.isfuture(
236+
first_arg
237+
):
215238
args = (self.trace_item(first_arg),) + args[1:]
216239
# Check if it's a list and wrap each item
217240
elif isinstance(first_arg, list):
218-
args = ([self.trace_item(item) for item in first_arg],) + args[1:]
241+
args = (
242+
[self.trace_item(item) for item in first_arg],
243+
) + args[1:]
219244
return method(*args, **kwargs)
220245

221246
_wrap(asyncio, method_name, wrap_coro_or_future)
@@ -227,7 +252,6 @@ def uninstrument_method_with_coroutine(self, method_name):
227252
unwrap(asyncio, method_name)
228253

229254
def instrument_gather(self):
230-
231255
def wrap_coros_or_futures(method, instance, args, kwargs):
232256
if args and len(args) > 0:
233257
# Check if it's a coroutine or future and wrap it
@@ -286,8 +310,13 @@ def trace_to_thread(self, func):
286310
start = default_timer()
287311
self.to_thread_created_metric.add(1)
288312
self.to_thread_active_metric.add(1)
289-
span = self._tracer.start_span(
290-
f"{ASYNCIO_PREFIX}to_thread_func-" + func.__name__) if func.__name__ in self._to_thread_name_to_trace else None
313+
span = (
314+
self._tracer.start_span(
315+
f"{ASYNCIO_PREFIX}to_thread_func-" + func.__name__
316+
)
317+
if func.__name__ in self._to_thread_name_to_trace
318+
else None
319+
)
291320
exception = None
292321
try:
293322
return func
@@ -326,8 +355,11 @@ async def trace_coroutine(self, coro):
326355
self.coro_created_metric.add(1, coro_attr)
327356
self.coro_active_metric.add(1, coro_attr)
328357

329-
span = self._tracer.start_span(
330-
f"{ASYNCIO_PREFIX}coro-" + coro.__name__) if coro.__name__ in self._coros_name_to_trace else None
358+
span = (
359+
self._tracer.start_span(f"{ASYNCIO_PREFIX}coro-" + coro.__name__)
360+
if coro.__name__ in self._coros_name_to_trace
361+
else None
362+
)
331363

332364
exception = None
333365
try:
@@ -343,7 +375,9 @@ async def trace_coroutine(self, coro):
343375
except Exception as exc:
344376
exception = exc
345377
coro_exception_attr = coro_attr.copy()
346-
coro_exception_attr[ASYNCIO_EXCEPTIONS_NAME] = exc.__class__.__name__
378+
coro_exception_attr[
379+
ASYNCIO_EXCEPTIONS_NAME
380+
] = exc.__class__.__name__
347381
self.coro_exception_metric.add(1, coro_exception_attr)
348382
raise
349383
finally:
@@ -362,7 +396,11 @@ def trace_future(self, future):
362396
start = default_timer()
363397
self.future_created_metric.add(1)
364398
self.future_active_metric.add(1)
365-
span = self._tracer.start_span(f"{ASYNCIO_PREFIX}future") if self._future_active_enabled else None
399+
span = (
400+
self._tracer.start_span(f"{ASYNCIO_PREFIX}future")
401+
if self._future_active_enabled
402+
else None
403+
)
366404

367405
def callback(f):
368406
exception = f.exception()
@@ -371,7 +409,9 @@ def callback(f):
371409
elif isinstance(exception, asyncio.TimeoutError):
372410
self.future_timeout_metric.add(1)
373411
elif exception:
374-
exception_attr = {ASYNCIO_EXCEPTIONS_NAME: exception.__class__.__name__}
412+
exception_attr = {
413+
ASYNCIO_EXCEPTIONS_NAME: exception.__class__.__name__
414+
}
375415
self.future_exception_metric.add(1, exception_attr)
376416

377417
duration = max(round((default_timer() - start) * 1000), 0)

instrumentation/opentelemetry-instrumentation-asyncio/src/opentelemetry/instrumentation/asyncio/environment_variables.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@
1515
"""
1616
Enter the names of the coroutines to be traced through the environment variable below, separated by commas.
1717
"""
18-
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE = "OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE"
18+
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE = (
19+
"OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE"
20+
)
1921

2022
"""
2123
To determines whether the tracing feature for Future of Asyncio in Python is enabled or not.
2224
"""
23-
OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED = "OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED"
25+
OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED = (
26+
"OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED"
27+
)
2428

2529
"""
2630
Enter the names of the functions to be traced through the environment variable below, separated by commas.
2731
"""
28-
OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE = "OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE"
32+
OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE = (
33+
"OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE"
34+
)

instrumentation/opentelemetry-instrumentation-asyncio/src/opentelemetry/instrumentation/asyncio/metrics.py

-3
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,15 @@
4545
"ASYNCIO_COROUTINE_CREATED",
4646
"ASYNCIO_COROUTINE_FINISHED",
4747
"ASYNCIO_COROUTINE_TIMEOUTS",
48-
4948
"ASYNCIO_COROUTINE_NAME",
5049
"ASYNCIO_EXCEPTIONS_NAME",
51-
5250
"ASYNCIO_FUTURES_DURATION",
5351
"ASYNCIO_FUTURES_EXCEPTIONS",
5452
"ASYNCIO_FUTURES_CANCELLED",
5553
"ASYNCIO_FUTURES_CREATED",
5654
"ASYNCIO_FUTURES_ACTIVE",
5755
"ASYNCIO_FUTURES_FINISHED",
5856
"ASYNCIO_FUTURES_TIMEOUTS",
59-
6057
"ASYNCIO_TO_THREAD_DURATION",
6158
"ASYNCIO_TO_THREAD_EXCEPTIONS",
6259
"ASYNCIO_TO_THREAD_CREATED",

instrumentation/opentelemetry-instrumentation-asyncio/src/opentelemetry/instrumentation/asyncio/utils.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
# limitations under the License.
1414
import os
1515

16-
from opentelemetry.instrumentation.asyncio.environment_variables import OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE, \
17-
OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED, OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE
16+
from opentelemetry.instrumentation.asyncio.environment_variables import (
17+
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE,
18+
OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED,
19+
OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE,
20+
)
1821

1922

2023
def separate_coro_names_by_comma(coro_names) -> set:
@@ -39,15 +42,24 @@ def get_future_trace_enabled() -> bool:
3942
Function to get the future active enabled flag from the environment variable
4043
default value is False
4144
"""
42-
return os.getenv(OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED, "False").lower() == "true"
45+
return (
46+
os.getenv(OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED, "False").lower()
47+
== "true"
48+
)
4349

4450

4551
def get_to_thread_to_trace() -> set:
4652
"""
4753
Function to get the functions to be traced from the environment variable
4854
"""
49-
func_names = os.getenv(OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE)
55+
func_names = os.getenv(
56+
OTEL_PYTHON_ASYNCIO_TO_THREAD_FUNCTION_NAMES_TO_TRACE
57+
)
5058
return separate_coro_names_by_comma(func_names)
5159

5260

53-
__all__ = ["get_coros_to_trace", "get_future_trace_enabled", "get_to_thread_to_trace"]
61+
__all__ = [
62+
"get_coros_to_trace",
63+
"get_future_trace_enabled",
64+
"get_to_thread_to_trace",
65+
]

instrumentation/opentelemetry-instrumentation-asyncio/tests/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
# distributed under the License is distributed on an "AS IS" BASIS,
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
13-
# limitations under the License.
13+
# limitations under the License.

instrumentation/opentelemetry-instrumentation-asyncio/tests/common_test_func.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ async def async_func():
1919
await asyncio.sleep(0.1)
2020

2121

22-
async def factorial(name, number):
23-
f = 1
24-
for i in range(2, number + 1):
25-
print(f"Task {name}: Compute factorial({number}), currently i={i}...")
22+
async def factorial(number):
23+
factorial_value = 1
24+
for value in range(2, number + 1):
2625
await asyncio.sleep(0)
27-
f *= i
28-
print(f"Task {name}: factorial({number}) = {f}")
29-
return f
26+
factorial_value *= value
27+
return factorial_value
3028

3129

3230
async def cancellable_coroutine():

instrumentation/opentelemetry-instrumentation-asyncio/tests/test_asyncio_cancellation.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,29 @@
1414
import asyncio
1515
from unittest.mock import patch
1616

17+
from opentelemetry.instrumentation.asyncio import (
18+
ASYNCIO_COROUTINE_ACTIVE,
19+
ASYNCIO_COROUTINE_CANCELLED,
20+
ASYNCIO_COROUTINE_CREATED,
21+
ASYNCIO_COROUTINE_DURATION,
22+
ASYNCIO_COROUTINE_FINISHED,
23+
AsyncioInstrumentor,
24+
)
25+
from opentelemetry.instrumentation.asyncio.environment_variables import (
26+
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE,
27+
)
1728
from opentelemetry.test.test_base import TestBase
18-
from opentelemetry.trace import get_tracer, SpanKind
29+
from opentelemetry.trace import SpanKind, get_tracer
1930

20-
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor, ASYNCIO_COROUTINE_CANCELLED, \
21-
ASYNCIO_COROUTINE_DURATION, ASYNCIO_COROUTINE_ACTIVE, ASYNCIO_COROUTINE_CREATED, ASYNCIO_COROUTINE_FINISHED
22-
from opentelemetry.instrumentation.asyncio.environment_variables import OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE
2331
from .common_test_func import cancellation_create_task
2432

2533

2634
class TestAsyncioCancel(TestBase):
2735
@patch.dict(
28-
"os.environ", {
36+
"os.environ",
37+
{
2938
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE: "cancellation_coro, cancellable_coroutine"
30-
}
39+
},
3140
)
3241
def setUp(self):
3342
super().setUp()
@@ -48,13 +57,18 @@ def test_cancel(self):
4857
self.assertEqual(spans[0].context.trace_id, spans[1].context.trace_id)
4958
self.assertEqual(spans[2].context.trace_id, spans[1].context.trace_id)
5059

51-
self.assertEqual(spans[0].name, 'asyncio.coro-cancellable_coroutine')
52-
self.assertEqual(spans[1].name, 'asyncio.coro-cancellation_coro')
53-
for metric in self.memory_metrics_reader.get_metrics_data().resource_metrics[0].scope_metrics[0].metrics:
60+
self.assertEqual(spans[0].name, "asyncio.coro-cancellable_coroutine")
61+
self.assertEqual(spans[1].name, "asyncio.coro-cancellation_coro")
62+
for metric in (
63+
self.memory_metrics_reader.get_metrics_data()
64+
.resource_metrics[0]
65+
.scope_metrics[0]
66+
.metrics
67+
):
5468
if metric.name == ASYNCIO_COROUTINE_CANCELLED:
5569
self.assertEqual(metric.data.data_points[0].value, 1)
5670
elif metric.name == ASYNCIO_COROUTINE_DURATION:
57-
self.assertNotEquals(metric.data.data_points[0].min, 0)
71+
self.assertEqual(metric.data.data_points[0].min != 0, True)
5872
elif metric.name == ASYNCIO_COROUTINE_ACTIVE:
5973
self.assertEqual(metric.data.data_points[0].value, 0)
6074
elif metric.name == ASYNCIO_COROUTINE_CREATED:

instrumentation/opentelemetry-instrumentation-asyncio/tests/test_asyncio_create_task.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@
1414
import asyncio
1515
from unittest.mock import patch
1616

17+
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
18+
from opentelemetry.instrumentation.asyncio.environment_variables import (
19+
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE,
20+
)
1721
from opentelemetry.test.test_base import TestBase
1822
from opentelemetry.trace import get_tracer
1923

2024
from .common_test_func import factorial
21-
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
22-
from opentelemetry.instrumentation.asyncio.environment_variables import OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE
2325

2426

2527
class TestAsyncioCreateTask(TestBase):
2628
@patch.dict(
27-
"os.environ", {
28-
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE: "sleep"
29-
}
29+
"os.environ", {OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE: "sleep"}
3030
)
3131
def setUp(self):
3232
super().setUp()
@@ -42,7 +42,7 @@ def tearDown(self):
4242
def test_asyncio_create_task(self):
4343
async def async_func():
4444
await asyncio.create_task(asyncio.sleep(0))
45-
await asyncio.create_task(factorial("A", 3))
45+
await asyncio.create_task(factorial(3))
4646

4747
asyncio.run(async_func())
4848
spans = self.memory_exporter.get_finished_spans()

0 commit comments

Comments
 (0)