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

Enable lint on CI and update deps #2067

Merged
merged 13 commits into from
Nov 21, 2023
Next Next commit
Fix black an isort
  • Loading branch information
srikanthccv committed Nov 18, 2023
commit 45a2679a4e8a2fd371f2bb5d6fd3b17b2556f871
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@
# limitations under the License.

import urllib
from timeit import default_timer
from typing import Dict, List, Tuple, Union

from aiohttp import web
from multidict import CIMultiDictProxy
from timeit import default_timer
from typing import Tuple, Dict, List, Union

from opentelemetry import context, trace, metrics
from opentelemetry import context, metrics, trace
from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY
from opentelemetry.instrumentation.aiohttp_server.package import _instruments
from opentelemetry.instrumentation.aiohttp_server.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.utils import http_status_to_status_code
from opentelemetry.propagators.textmap import Getter
from opentelemetry.propagate import extract
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.propagators.textmap import Getter
from opentelemetry.semconv.metrics import MetricInstruments
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace.status import Status, StatusCode
from opentelemetry.util.http import get_excluded_urls
from opentelemetry.util.http import remove_url_credentials
from opentelemetry.util.http import get_excluded_urls, remove_url_credentials

_duration_attrs = [
SpanAttributes.HTTP_METHOD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from http import HTTPStatus

import aiohttp
import pytest
import pytest_asyncio
import aiohttp
from http import HTTPStatus
from .utils import HTTPMethod

from opentelemetry import trace as trace_api
from opentelemetry.test.test_base import TestBase
from opentelemetry.instrumentation.aiohttp_server import AioHttpServerInstrumentor
from opentelemetry.instrumentation.aiohttp_server import (
AioHttpServerInstrumentor,
)
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.test.globals_test import reset_trace_globals
from opentelemetry.test.test_base import TestBase
from opentelemetry.util._importlib_metadata import entry_points

from opentelemetry.test.globals_test import (
reset_trace_globals,
)
from .utils import HTTPMethod


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -54,8 +55,7 @@ async def server_fixture(tracer, aiohttp_server):
AioHttpServerInstrumentor().instrument()

app = aiohttp.web.Application()
app.add_routes(
[aiohttp.web.get("/test-path", default_handler)])
app.add_routes([aiohttp.web.get("/test-path", default_handler)])

server = await aiohttp_server(app)
yield server, app
Expand All @@ -67,23 +67,28 @@ async def server_fixture(tracer, aiohttp_server):

def test_checking_instrumentor_pkg_installed():

(instrumentor_entrypoint,) = entry_points(group="opentelemetry_instrumentor", name="aiohttp-server")
(instrumentor_entrypoint,) = entry_points(
group="opentelemetry_instrumentor", name="aiohttp-server"
)
instrumentor = instrumentor_entrypoint.load()()
assert (isinstance(instrumentor, AioHttpServerInstrumentor))
assert isinstance(instrumentor, AioHttpServerInstrumentor)


@pytest.mark.asyncio
@pytest.mark.parametrize("url, expected_method, expected_status_code", [
("/test-path", HTTPMethod.GET, HTTPStatus.OK),
("/not-found", HTTPMethod.GET, HTTPStatus.NOT_FOUND)
])
@pytest.mark.parametrize(
"url, expected_method, expected_status_code",
[
("/test-path", HTTPMethod.GET, HTTPStatus.OK),
("/not-found", HTTPMethod.GET, HTTPStatus.NOT_FOUND),
],
)
async def test_status_code_instrumentation(
tracer,
server_fixture,
aiohttp_client,
url,
expected_method,
expected_status_code
expected_status_code,
):
_, memory_exporter = tracer
server, app = server_fixture
Expand All @@ -98,8 +103,12 @@ async def test_status_code_instrumentation(
[span] = memory_exporter.get_finished_spans()

assert expected_method.value == span.attributes[SpanAttributes.HTTP_METHOD]
assert expected_status_code == span.attributes[SpanAttributes.HTTP_STATUS_CODE]

assert f"http://{server.host}:{server.port}{url}" == span.attributes[
SpanAttributes.HTTP_URL
]
assert (
expected_status_code
== span.attributes[SpanAttributes.HTTP_STATUS_CODE]
)

assert (
f"http://{server.host}:{server.port}{url}"
== span.attributes[SpanAttributes.HTTP_URL]
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class HTTPMethod(Enum):
def __repr__(self):
return f"{self.value}"

CONNECT = 'CONNECT'
DELETE = 'DELETE'
GET = 'GET'
HEAD = 'HEAD'
OPTIONS = 'OPTIONS'
PATCH = 'PATCH'
POST = 'POST'
PUT = 'PUT'
TRACE = 'TRACE'
CONNECT = "CONNECT"
DELETE = "DELETE"
GET = "GET"
HEAD = "HEAD"
OPTIONS = "OPTIONS"
PATCH = "PATCH"
POST = "POST"
PUT = "PUT"
TRACE = "TRACE"
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ def extract_attributes(
attributes[SpanAttributes.MESSAGING_DESTINATION] = destination_name

# TODO: Use SpanAttributes.MESSAGING_DESTINATION_NAME when opentelemetry-semantic-conventions 0.42b0 is released
attributes["messaging.destination.name"] = cls._extract_input_arn(call_context)
attributes["messaging.destination.name"] = cls._extract_input_arn(
call_context
)
call_context.span_name = (
f"{'phone_number' if is_phone_number else destination_name} send"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def _test_publish_to_arn(self, arg_name: str):
target_arn,
# TODO: Use SpanAttributes.MESSAGING_DESTINATION_NAME when
# opentelemetry-semantic-conventions 0.42b0 is released
span.attributes["messaging.destination.name"]
span.attributes["messaging.destination.name"],
)

@mock_sns
Expand Down Expand Up @@ -194,7 +194,7 @@ def test_publish_batch_to_topic(self):
topic_arn,
# TODO: Use SpanAttributes.MESSAGING_DESTINATION_NAME when
# opentelemetry-semantic-conventions 0.42b0 is released
span.attributes["messaging.destination.name"]
span.attributes["messaging.destination.name"],
)

self.assert_injected_span(message1_attrs, span)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
from wrapt import wrap_function_wrapper

from opentelemetry import trace
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.cassandra.package import _instruments
from opentelemetry.instrumentation.cassandra.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.utils import unwrap
from opentelemetry.semconv.trace import SpanAttributes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def add(x, y):
from timeit import default_timer
from typing import Collection, Iterable

from billiard import VERSION
from billiard.einfo import ExceptionInfo
from celery import signals # pylint: disable=no-name-in-module

Expand All @@ -76,8 +77,6 @@ def add(x, y):
from opentelemetry.propagators.textmap import Getter
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace.status import Status, StatusCode
from billiard import VERSION


if VERSION >= (4, 0, 1):
from billiard.einfo import ExceptionWithTraceback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import logging

from celery import registry # pylint: disable=no-name-in-module
from billiard import VERSION
from celery import registry # pylint: disable=no-name-in-module

from opentelemetry.semconv.trace import SpanAttributes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ def instrument_consumer(consumer: Consumer, tracer_provider=None)
from .package import _instruments
from .utils import (
KafkaPropertiesExtractor,
_end_current_consume_span,
_create_new_consume_span,
_end_current_consume_span,
_enrich_span,
_get_span_name,
_kafka_getter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from typing import List, Optional

from opentelemetry import context, propagate
from opentelemetry.trace import SpanKind, Link
from opentelemetry.propagators import textmap
from opentelemetry.semconv.trace import (
MessagingDestinationKindValues,
MessagingOperationValues,
SpanAttributes,
)
from opentelemetry.trace import Link, SpanKind

_LOG = getLogger(__name__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@

# pylint: disable=no-name-in-module

from opentelemetry.semconv.trace import (
SpanAttributes,
MessagingDestinationKindValues,
)
from opentelemetry.test.test_base import TestBase
from .utils import MockConsumer, MockedMessage

from confluent_kafka import Consumer, Producer

from opentelemetry.instrumentation.confluent_kafka import (
Expand All @@ -32,6 +25,13 @@
KafkaContextGetter,
KafkaContextSetter,
)
from opentelemetry.semconv.trace import (
MessagingDestinationKindValues,
SpanAttributes,
)
from opentelemetry.test.test_base import TestBase

from .utils import MockConsumer, MockedMessage


class TestConfluentKafka(TestBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS,
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST,
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE,
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE,
OTEL_PYTHON_INSTRUMENTATION_HTTP_CAPTURE_ALL_METHODS,
get_excluded_urls,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@
"""

import gc
import os
import logging
import os
import threading
from platform import python_implementation
from typing import Collection, Dict, Iterable, List, Optional
Expand Down Expand Up @@ -358,7 +358,7 @@ def _instrument(self, **kwargs):
if "process.runtime.gc_count" in self._config:
if self._python_implementation == "pypy":
_logger.warning(
"The process.runtime.gc_count metric won't be collected because the interpreter is PyPy"
"The process.runtime.gc_count metric won't be collected because the interpreter is PyPy"
)
else:
self._meter.create_observable_counter(
Expand All @@ -367,7 +367,6 @@ def _instrument(self, **kwargs):
description=f"Runtime {self._python_implementation} GC count",
unit="bytes",
)


if "process.runtime.thread_count" in self._config:
self._meter.create_observable_up_down_counter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
from platform import python_implementation
from unittest import mock, skipIf

from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
from opentelemetry.test.test_base import TestBase

from opentelemetry.instrumentation.system_metrics import (
SystemMetricsInstrumentor,
)
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
from opentelemetry.test.test_base import TestBase


def _mock_netconnection():
Expand Down Expand Up @@ -120,12 +119,14 @@ def test_system_metrics_instrument(self):
f"process.runtime.{self.implementation}.context_switches",
f"process.runtime.{self.implementation}.cpu.utilization",
]

if self.implementation == "pypy":
self.assertEqual(len(metric_names), 20)
else:
self.assertEqual(len(metric_names), 21)
observer_names.append(f"process.runtime.{self.implementation}.gc_count",)
observer_names.append(
f"process.runtime.{self.implementation}.gc_count",
)

for observer in metric_names:
self.assertIn(observer, observer_names)
Expand All @@ -139,7 +140,7 @@ def test_runtime_metrics_instrument(self):
"process.runtime.cpu.utilization": None,
"process.runtime.context_switches": ["involuntary", "voluntary"],
}

if self.implementation != "pypy":
runtime_config["process.runtime.gc_count"] = None

Expand All @@ -166,7 +167,9 @@ def test_runtime_metrics_instrument(self):
self.assertEqual(len(metric_names), 5)
else:
self.assertEqual(len(metric_names), 6)
observer_names.append(f"process.runtime.{self.implementation}.gc_count")
observer_names.append(
f"process.runtime.{self.implementation}.gc_count"
)

for observer in metric_names:
self.assertIn(observer, observer_names)
Expand All @@ -181,9 +184,9 @@ def _assert_metrics(self, observer_name, reader, expected):
for data_point in metric.data.data_points:
for expect in expected:
if (
dict(data_point.attributes)
== expect.attributes
and metric.name == observer_name
dict(data_point.attributes)
== expect.attributes
and metric.name == observer_name
):
self.assertEqual(
data_point.value,
Expand Down Expand Up @@ -791,7 +794,9 @@ def test_runtime_cpu_time(self, mock_process_cpu_times):
)

@mock.patch("gc.get_count")
@skipIf(python_implementation().lower() == "pypy", "not supported for pypy")
@skipIf(
python_implementation().lower() == "pypy", "not supported for pypy"
)
def test_runtime_get_count(self, mock_gc_get_count):
mock_gc_get_count.configure_mock(**{"return_value": (1, 2, 3)})

Expand Down
Loading