mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-28 04:34:01 +08:00
Refactor code using pyupgrade for Python 3.6 (#770)
* Refactor code using pyupgrade for Python 3.6 This diff is the result of applying the following command to the project: ```shell find . -type f -name "*.py" -exec pyupgrade --py36-plus '{}' + ``` * Simplify yield from
This commit is contained in:

committed by
GitHub

parent
3ff06da2fb
commit
07c52aad38
@ -410,7 +410,7 @@ class TestAsgiAttributes(unittest.TestCase):
|
||||
def test_request_attributes(self):
|
||||
self.scope["query_string"] = b"foo=bar"
|
||||
headers = []
|
||||
headers.append(("host".encode("utf8"), "test".encode("utf8")))
|
||||
headers.append((b"host", b"test"))
|
||||
self.scope["headers"] = headers
|
||||
|
||||
attrs = otel_asgi.collect_request_attributes(self.scope)
|
||||
|
@ -235,11 +235,11 @@ def add_span_arg_tags(span, aws_service, args, args_names, args_traced):
|
||||
# Do not trace `Key Management Service` or `Secure Token Service` API calls
|
||||
# over concerns of security leaks.
|
||||
if aws_service not in {"kms", "sts"}:
|
||||
tags = dict(
|
||||
(name, value)
|
||||
tags = {
|
||||
name: value
|
||||
for (name, value) in zip(args_names, args)
|
||||
if name in args_traced
|
||||
)
|
||||
}
|
||||
tags = flatten_dict(tags)
|
||||
|
||||
for param_key, value in tags.items():
|
||||
|
@ -115,7 +115,7 @@ class TestBotocoreInstrumentor(TestBase):
|
||||
expected[span_attributes_request_id] = request_id
|
||||
|
||||
self.assertSpanHasAttributes(span, expected)
|
||||
self.assertEqual("{}.{}".format(service, operation), span.name)
|
||||
self.assertEqual(f"{service}.{operation}", span.name)
|
||||
return span
|
||||
|
||||
@mock_ec2
|
||||
|
@ -334,7 +334,7 @@ class TestMiddlewareAsgi(SimpleTestCase, TestBase):
|
||||
)
|
||||
self.assertEqual(
|
||||
response.headers["traceresponse"],
|
||||
"00-{0}-{1}-01".format(
|
||||
"00-{}-{}-01".format(
|
||||
format_trace_id(span.get_span_context().trace_id),
|
||||
format_span_id(span.get_span_context().span_id),
|
||||
),
|
||||
|
@ -174,10 +174,7 @@ class OpenTelemetryClientInterceptor(
|
||||
rpc_info.request = request_or_iterator
|
||||
|
||||
try:
|
||||
result = invoker(request_or_iterator, metadata)
|
||||
|
||||
for response in result:
|
||||
yield response
|
||||
yield from invoker(request_or_iterator, metadata)
|
||||
except grpc.RpcError as err:
|
||||
span.set_status(Status(StatusCode.ERROR))
|
||||
span.set_attribute(
|
||||
|
@ -1,4 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
||||
# source: test_server.proto
|
||||
|
||||
|
@ -3,7 +3,7 @@ import grpc
|
||||
from tests.protobuf import test_server_pb2 as test__server__pb2
|
||||
|
||||
|
||||
class GRPCTestServerStub(object):
|
||||
class GRPCTestServerStub:
|
||||
"""Missing associated documentation comment in .proto file"""
|
||||
|
||||
def __init__(self, channel):
|
||||
@ -34,7 +34,7 @@ class GRPCTestServerStub(object):
|
||||
)
|
||||
|
||||
|
||||
class GRPCTestServerServicer(object):
|
||||
class GRPCTestServerServicer:
|
||||
"""Missing associated documentation comment in .proto file"""
|
||||
|
||||
def SimpleMethod(self, request, context):
|
||||
@ -92,7 +92,7 @@ def add_GRPCTestServerServicer_to_server(servicer, server):
|
||||
|
||||
|
||||
# This class is part of an EXPERIMENTAL API.
|
||||
class GRPCTestServer(object):
|
||||
class GRPCTestServer:
|
||||
"""Missing associated documentation comment in .proto file"""
|
||||
|
||||
@staticmethod
|
||||
|
@ -316,8 +316,7 @@ class OpenTelemetryMiddleware:
|
||||
def _end_span_after_iterating(iterable, span, tracer, token):
|
||||
try:
|
||||
with trace.use_span(span):
|
||||
for yielded in iterable:
|
||||
yield yielded
|
||||
yield from iterable
|
||||
finally:
|
||||
close = getattr(iterable, "close", None)
|
||||
if close:
|
||||
|
@ -83,7 +83,7 @@ def main():
|
||||
source = astor.to_source(tree)
|
||||
|
||||
with open(
|
||||
os.path.join(scripts_path, "license_header.txt"), "r", encoding="utf-8"
|
||||
os.path.join(scripts_path, "license_header.txt"), encoding="utf-8"
|
||||
) as header_file:
|
||||
header = header_file.read()
|
||||
source = _template.format(header=header, source=source)
|
||||
|
@ -37,7 +37,6 @@ def main():
|
||||
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
with open(
|
||||
os.path.join(root_path, _template_dir, _template_name),
|
||||
"r",
|
||||
encoding="utf-8",
|
||||
) as template:
|
||||
setuppy_tmpl = Template(template.read())
|
||||
|
@ -26,9 +26,9 @@ from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
|
||||
|
||||
REDIS_HOST = os.getenv("REDIS_HOST", "localhost")
|
||||
REDIS_PORT = int(os.getenv("REDIS_PORT ", "6379"))
|
||||
REDIS_URL = "redis://{host}:{port}".format(host=REDIS_HOST, port=REDIS_PORT)
|
||||
BROKER_URL = "{redis}/{db}".format(redis=REDIS_URL, db=0)
|
||||
BACKEND_URL = "{redis}/{db}".format(redis=REDIS_URL, db=1)
|
||||
REDIS_URL = f"redis://{REDIS_HOST}:{REDIS_PORT}"
|
||||
BROKER_URL = f"{REDIS_URL}/0"
|
||||
BACKEND_URL = f"{REDIS_URL}/1"
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
|
@ -51,16 +51,16 @@ def test_instrumentation_info(celery_app, memory_exporter):
|
||||
assert run_span.parent.span_id == async_span.context.span_id
|
||||
assert run_span.context.trace_id == async_span.context.trace_id
|
||||
|
||||
assert async_span.instrumentation_info.name == "apply_async/{0}".format(
|
||||
assert async_span.instrumentation_info.name == "apply_async/{}".format(
|
||||
opentelemetry.instrumentation.celery.__name__
|
||||
)
|
||||
assert async_span.instrumentation_info.version == "apply_async/{0}".format(
|
||||
assert async_span.instrumentation_info.version == "apply_async/{}".format(
|
||||
opentelemetry.instrumentation.celery.__version__
|
||||
)
|
||||
assert run_span.instrumentation_info.name == "run/{0}".format(
|
||||
assert run_span.instrumentation_info.name == "run/{}".format(
|
||||
opentelemetry.instrumentation.celery.__name__
|
||||
)
|
||||
assert run_span.instrumentation_info.version == "run/{0}".format(
|
||||
assert run_span.instrumentation_info.version == "run/{}".format(
|
||||
opentelemetry.instrumentation.celery.__version__
|
||||
)
|
||||
|
||||
@ -489,9 +489,7 @@ def test_apply_async_previous_style_tasks(
|
||||
|
||||
@classmethod
|
||||
def apply_async(cls, args=None, kwargs=None, **kwargs_):
|
||||
return super(CelerySuperClass, cls).apply_async(
|
||||
args=args, kwargs=kwargs, **kwargs_
|
||||
)
|
||||
return super().apply_async(args=args, kwargs=kwargs, **kwargs_)
|
||||
|
||||
def run(self, *args, **kwargs):
|
||||
if "stop" in kwargs:
|
||||
|
@ -64,7 +64,7 @@ def retryable(func):
|
||||
ex,
|
||||
)
|
||||
time.sleep(RETRY_INTERVAL)
|
||||
raise Exception("waiting for {} failed".format(func.__name__))
|
||||
raise Exception(f"waiting for {func.__name__} failed")
|
||||
|
||||
return wrapper
|
||||
|
||||
|
@ -113,7 +113,7 @@ class SQLAlchemyTestMixin(TestBase):
|
||||
|
||||
def _check_span(self, span, name):
|
||||
if self.SQL_DB:
|
||||
name = "{0} {1}".format(name, self.SQL_DB)
|
||||
name = f"{name} {self.SQL_DB}"
|
||||
self.assertEqual(span.name, name)
|
||||
self.assertEqual(
|
||||
span.attributes.get(SpanAttributes.DB_NAME), self.SQL_DB
|
||||
|
Reference in New Issue
Block a user