Skip to content

Commit

Permalink
fixed lint with autopep8, isort and black
Browse files Browse the repository at this point in the history
  • Loading branch information
pridhi-arora committed Feb 25, 2023
1 parent 3b5afc0 commit f9301ed
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
OpenTelemetry Threading Instrumentation
=======================================
== == == == == == == == == == == == == == == == == == == =

|pypi|

.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-threading.svg
:target: https://pypi.org/project/opentelemetry-instrumentation-threading/
.. | pypi | image:: https: // badge.fury.io/py/opentelemetry-instrumentation-threading.svg
: target: https: // pypi.org / project / opentelemetry - instrumentation - threading/


Installation
------------

::

pip install opentelemetry-instrumentation-threading
pip install opentelemetry - instrumentation - threading

References
----------

* `OpenTelemetry Threading Instrumentation <https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/threading/threading.html>`_
* `OpenTelemetry Project <https://opentelemetry.io/>`_
* `OpenTelemetry Python Examples <https://github.com/open-telemetry/opentelemetry-python/tree/main/docs/examples>`_
* `OpenTelemetry Threading Instrumentation < https: // opentelemetry - python - contrib.readthedocs.io / en / latest / instrumentation / threading / threading.html >`_
* `OpenTelemetry Project < https: // opentelemetry.io / >`_
* `OpenTelemetry Python Examples < https: // github.com / open - telemetry / opentelemetry - python / tree / main / docs / examples >`_
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
[build-system]
[build - system]
requires = ["hatchling"]
build-backend = "hatchling.build"
build - backend = "hatchling.build"

[project]
name = "opentelemetry-instrumentation-threading"
dynamic = ["version"]
description = "Threading instrumentation for OpenTelemetry"
readme = "README.rst"
license = "Apache-2.0"
requires-python = ">=3.7"
requires - python = ">=3.7"
authors = [
{ name = "OpenTelemetry Authors", email = "cncf-opentelemetry-contributors@lists.cncf.io" },
{name= "OpenTelemetry Authors", email = "cncf-opentelemetry-contributors@lists.cncf.io"},
]
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
]
dependencies = [
"opentelemetry-api ~= 1.12",
"opentelemetry-instrumentation == 0.38b0.dev",
"opentelemetry-api ~= 1.12",
"opentelemetry-instrumentation == 0.38b0.dev",
]

[project.optional-dependencies]
[project.optional - dependencies]
instruments = []
test = [
"opentelemetry-instrumentation-threading[instruments]",
"opentelemetry-test-utils == 0.38b0.dev",
"opentelemetry-instrumentation-threading[instruments]",
"opentelemetry-test-utils == 0.38b0.dev",
]

[project.entry-points.opentelemetry_instrumentor]
[project.entry - points.opentelemetry_instrumentor]
threading = "opentelemetry.instrumentation.threading:ThreadingInstrumentor"

[project.urls]
Expand All @@ -47,8 +47,8 @@ path = "src/opentelemetry/instrumentation/threading/version.py"

[tool.hatch.build.targets.sdist]
include = [
"/src",
"/tests",
"/src",
"/tests",
]

[tool.hatch.build.targets.wheel]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,24 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# pylint: disable=empty-docstring,no-value-for-parameter,no-member,no-name-in-module
# pylint:
# disable=empty-docstring,no-value-for-parameter,no-member,no-name-in-module

import threading # pylint: disable=import-self
import threading # pylint: disable=import-self
from os import environ
from typing import Collection
from opentelemetry import context

from opentelemetry import context, trace
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor

from opentelemetry.instrumentation.threading.package import _instruments
from opentelemetry.instrumentation.threading.version import __version__

from opentelemetry import trace
from opentelemetry.trace import (
get_current_span,
get_tracer,
get_tracer_provider,
get_tracer
)


class _InstrumentedThread(threading.Thread):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -47,21 +46,23 @@ def run(self):
context.attach(ctx)
super().run()

class ThreadingInstrumentor(BaseInstrumentor): # pylint: disable=empty-docstring


class ThreadingInstrumentor(
BaseInstrumentor
): # pylint: disable=empty-docstring
original_threadcls = threading.Thread

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


def _instrument(self, *args, **kwargs):

tracer_provider = kwargs.get("tracer_provider", None) or get_tracer_provider()
def _instrument(self, *args, **kwargs):
tracer_provider = (
kwargs.get("tracer_provider", None) or get_tracer_provider()
)

tracer = get_tracer(__name__, __version__, tracer_provider)
threading.Thread = _InstrumentedThread
_InstrumentedThread._tracer = tracer

def _uninstrument(self, **kwargs):
threading.Thread = self.original_threadcls
threading.Thread = self.original_threadcls
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from unittest import mock

from packaging import version

from opentelemetry import trace as trace_api
from opentelemetry.instrumentation.threading import ThreadingInstrumentor
from opentelemetry.test.test_base import TestBase
Expand All @@ -33,32 +34,34 @@ def setUp(self):
def tearDown(self):
super().tearDown()
ThreadingInstrumentor().uninstrument()

def print_square(self, num):
with self.tracer.start_as_current_span("square"):
print("Square: {}" .format(num * num))
print("Square: {}".format(num * num))

def print_cube(self, num):
with self.tracer.start_as_current_span("cube"):
print("Cube: {}" .format(num * num * num))
print("Cube: {}".format(num * num * num))

def print_square_with_thread(self, num):
with self.tracer.start_as_current_span("square"):
cube_thread = threading.Thread(target=self.print_cube, args=(10,))
print("Square: {}" .format(num * num))
print("Square: {}".format(num * num))
cube_thread.start()
cube_thread.join()

def calculate(self, num):
with self.tracer.start_as_current_span("calculate"):
square_thread = threading.Thread(target=self.print_square, args=(num,))
square_thread = threading.Thread(
target=self.print_square, args=(num,)
)
cube_thread = threading.Thread(target=self.print_cube, args=(num,))
square_thread.start()
square_thread.join()

cube_thread.start()
cube_thread.join()


def test_without_thread_nesting(self):
square_thread = threading.Thread(target=self.print_square, args=(10,))

Expand All @@ -70,18 +73,19 @@ def test_without_thread_nesting(self):
self.assertEqual(len(spans), 2)

target, root = spans[:2]

self.assertIs(target.parent, root.get_span_context())
self.assertIsNone(root.parent)

def test_with_thread_nesting(self):
#
# Following scenario is tested.
# threadA -> methodA -> threadB -> methodB
#

square_thread = threading.Thread(target=self.print_square_with_thread, args=(10,))
#
# Following scenario is tested.
# threadA -> methodA -> threadB -> methodB
#

square_thread = threading.Thread(
target=self.print_square_with_thread, args=(10,)
)

with self.tracer.start_as_current_span("root"):
square_thread.start()
Expand All @@ -92,30 +96,30 @@ def test_with_thread_nesting(self):
self.assertEqual(len(spans), 3)

cube, square, root = spans[:3]

self.assertIs(cube.parent, square.get_span_context())
self.assertIs(square.parent, root.get_span_context())
self.assertIsNone(root.parent)

def test_with_thread_multi_nesting(self):
#
# Following scenario is tested.
# / threadB -> methodB
# threadA -> methodA ->
# \ threadC -> methodC
#
#
# Following scenario is tested.
# / threadB -> methodB
# threadA -> methodA ->
# \ threadC -> methodC
#
calculate_thread = threading.Thread(target=self.calculate, args=(10,))

with self.tracer.start_as_current_span("root"):
calculate_thread.start()
calculate_thread.join()

spans = self.memory_exporter.get_finished_spans()

self.assertEqual(len(spans), 4)

cube, square, calculate, root = spans[:4]

self.assertIs(cube.parent, calculate.get_span_context())
self.assertIs(square.parent, calculate.get_span_context())
self.assertIs(calculate.parent, root.get_span_context())
Expand All @@ -130,4 +134,4 @@ def test_uninstrumented(self):
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)

ThreadingInstrumentor().instrument()
ThreadingInstrumentor().instrument()

0 comments on commit f9301ed

Please sign in to comment.