Skip to content

Commit 263ec71

Browse files
committed
Black reformatting
1 parent 7806f66 commit 263ec71

3 files changed

Lines changed: 101 additions & 95 deletions

File tree

ext/opentelemetry-ext-asgi/src/opentelemetry/ext/asgi/__init__.py

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,17 @@
3030
_HTTP_VERSION_PREFIX = "HTTP/"
3131

3232

33-
def get_header_from_scope(
34-
scope: dict, header_name: str
35-
) -> typing.List[str]:
33+
def get_header_from_scope(scope: dict, header_name: str) -> typing.List[str]:
3634
"""Retrieve a HTTP header value from the ASGI scope.
3735
3836
Returns:
3937
A list with a single string with the header value if it exists, else an empty list.
4038
"""
41-
headers = scope.get('headers')
39+
headers = scope.get("headers")
4240
return [
43-
value.decode('utf8') for (key,value) in headers
44-
if key.decode('utf8') == header_name
41+
value.decode("utf8")
42+
for (key, value) in headers
43+
if key.decode("utf8") == header_name
4544
]
4645

4746

@@ -81,8 +80,8 @@ def collect_request_attributes(scope):
8180
dictionary to be used as span creation attributes."""
8281

8382
port = scope.get("server")[1]
84-
server_host = (
85-
scope.get("server")[0] + (":" + str(port) if port != 80 else "")
83+
server_host = scope.get("server")[0] + (
84+
":" + str(port) if port != 80 else ""
8685
)
8786
http_url = scope.get("scheme") + "://" + server_host + scope.get("path")
8887
if scope.get("query_string"):
@@ -159,33 +158,48 @@ async def __call__(self, scope, receive, send):
159158
span_name = get_default_span_name(scope)
160159

161160
with self.tracer.start_as_current_span(
162-
span_name, parent_span, kind=trace.SpanKind.SERVER,
163-
attributes=collect_request_attributes(scope)) as connection_span:
161+
span_name,
162+
parent_span,
163+
kind=trace.SpanKind.SERVER,
164+
attributes=collect_request_attributes(scope),
165+
) as connection_span:
164166

165167
@wraps(receive)
166168
async def wrapped_receive():
167-
with self.tracer.start_as_current_span(span_name + " (unknown-receive)") as receive_span:
169+
with self.tracer.start_as_current_span(
170+
span_name + " (unknown-receive)"
171+
) as receive_span:
168172
payload = await receive()
169-
if payload['type'] == "websocket.receive":
173+
if payload["type"] == "websocket.receive":
170174
set_status_code(receive_span, 200)
171-
receive_span.set_attribute("http.status_text", payload['text'])
172-
173-
receive_span.update_name(span_name + " (" + payload['type'] + ")")
174-
receive_span.set_attribute('type', payload['type'])
175+
receive_span.set_attribute(
176+
"http.status_text", payload["text"]
177+
)
178+
179+
receive_span.update_name(
180+
span_name + " (" + payload["type"] + ")"
181+
)
182+
receive_span.set_attribute("type", payload["type"])
175183
return payload
176184

177185
@wraps(send)
178186
async def wrapped_send(payload):
179-
with self.tracer.start_as_current_span(span_name + " (unknown-send)") as send_span:
180-
if payload['type'] == "http.response.start":
181-
status_code = payload['status']
187+
with self.tracer.start_as_current_span(
188+
span_name + " (unknown-send)"
189+
) as send_span:
190+
if payload["type"] == "http.response.start":
191+
status_code = payload["status"]
182192
set_status_code(send_span, status_code)
183-
elif payload['type'] == "websocket.send":
193+
elif payload["type"] == "websocket.send":
184194
set_status_code(send_span, 200)
185-
send_span.set_attribute("http.status_text", payload['text'])
186-
187-
send_span.update_name(span_name + " (" + payload['type'] + ")")
188-
send_span.set_attribute('type', payload['type'])
195+
send_span.set_attribute(
196+
"http.status_text", payload["text"]
197+
)
198+
199+
send_span.update_name(
200+
span_name + " (" + payload["type"] + ")"
201+
)
202+
send_span.set_attribute("type", payload["type"])
189203
await send(payload)
190204

191205
await self.app(scope, wrapped_receive, wrapped_send)

ext/opentelemetry-ext-asgi/tests/test_asgi_middleware.py

Lines changed: 48 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,43 @@
2020
import opentelemetry.ext.asgi as otel_asgi
2121
from opentelemetry import trace as trace_api
2222
from opentelemetry.ext.testutil.asgitestutil import (
23-
AsgiTestBase, setup_testing_defaults
23+
AsgiTestBase,
24+
setup_testing_defaults,
2425
)
2526

2627

2728
async def simple_asgi(scope, receive, send):
2829
assert isinstance(scope, dict)
29-
assert scope.get('type') == "http"
30+
assert scope.get("type") == "http"
3031
payload = await receive()
31-
if payload.get('type') == "http.request":
32-
await send({
33-
'type': 'http.response.start',
34-
'status': 200,
35-
'headers': [
36-
[b'Content-Type', b'text/plain'],
37-
],
38-
})
39-
await send({
40-
'type': 'http.response.body',
41-
'body': b"*"
42-
})
32+
if payload.get("type") == "http.request":
33+
await send(
34+
{
35+
"type": "http.response.start",
36+
"status": 200,
37+
"headers": [[b"Content-Type", b"text/plain"]],
38+
}
39+
)
40+
await send({"type": "http.response.body", "body": b"*"})
4341

4442

4543
async def error_asgi(scope, receive, send):
4644
assert isinstance(scope, dict)
47-
assert scope.get('type') == "http"
45+
assert scope.get("type") == "http"
4846
payload = await receive()
49-
if payload.get('type') == "http.request":
47+
if payload.get("type") == "http.request":
5048
try:
5149
raise ValueError
5250
except ValueError:
53-
scope['hack_exc_info'] = sys.exc_info()
54-
await send({
55-
'type': 'http.response.start',
56-
'status': 200,
57-
'headers': [
58-
[b'Content-Type', b'text/plain'],
59-
],
60-
})
61-
await send({
62-
'type': 'http.response.body',
63-
'body': b"*"
64-
})
51+
scope["hack_exc_info"] = sys.exc_info()
52+
await send(
53+
{
54+
"type": "http.response.start",
55+
"status": 200,
56+
"headers": [[b"Content-Type", b"text/plain"]],
57+
}
58+
)
59+
await send({"type": "http.response.body", "body": b"*"})
6560

6661

6762
class TestAsgiApplication(AsgiTestBase):
@@ -70,17 +65,19 @@ def validate_outputs(self, outputs, error=None):
7065
self.assertEqual(len(outputs), 2)
7166
response_start = outputs[0]
7267
response_body = outputs[1]
73-
self.assertEqual(response_start['type'], 'http.response.start')
74-
self.assertEqual(response_body['type'], 'http.response.body')
68+
self.assertEqual(response_start["type"], "http.response.start")
69+
self.assertEqual(response_body["type"], "http.response.body")
7570

7671
# Check http response body
77-
self.assertEqual(response_body['body'], b"*")
72+
self.assertEqual(response_body["body"], b"*")
7873

7974
# Check http response start
80-
self.assertEqual(response_start['status'], 200)
81-
self.assertEqual(response_start['headers'], [[b'Content-Type', b'text/plain']])
75+
self.assertEqual(response_start["status"], 200)
76+
self.assertEqual(
77+
response_start["headers"], [[b"Content-Type", b"text/plain"]]
78+
)
8279

83-
exc_info = self.scope.get('hack_exc_info')
80+
exc_info = self.scope.get("hack_exc_info")
8481
if error:
8582
self.assertIs(exc_info[0], error)
8683
self.assertIsInstance(exc_info[1], error)
@@ -93,32 +90,27 @@ def validate_outputs(self, outputs, error=None):
9390
self.assertEqual(len(span_list), 4)
9491
expected = [
9592
{
96-
'name': "/ (http.request)",
97-
'kind': trace_api.SpanKind.INTERNAL,
98-
'attributes': {
99-
"type": "http.request",
100-
},
93+
"name": "/ (http.request)",
94+
"kind": trace_api.SpanKind.INTERNAL,
95+
"attributes": {"type": "http.request"},
10196
},
102-
10397
{
104-
'name': "/ (http.response.start)",
105-
'kind': trace_api.SpanKind.INTERNAL,
106-
'attributes': {
98+
"name": "/ (http.response.start)",
99+
"kind": trace_api.SpanKind.INTERNAL,
100+
"attributes": {
107101
"http.status_code": 200,
108102
"type": "http.response.start",
109103
},
110104
},
111105
{
112-
'name': "/ (http.response.body)",
113-
'kind': trace_api.SpanKind.INTERNAL,
114-
'attributes': {
115-
"type": "http.response.body",
116-
},
106+
"name": "/ (http.response.body)",
107+
"kind": trace_api.SpanKind.INTERNAL,
108+
"attributes": {"type": "http.response.body"},
117109
},
118110
{
119-
'name': "/",
120-
'kind': trace_api.SpanKind.SERVER,
121-
'attributes': {
111+
"name": "/",
112+
"kind": trace_api.SpanKind.SERVER,
113+
"attributes": {
122114
"component": "http",
123115
"http.method": "GET",
124116
"http.server_name": "127.0.0.1",
@@ -134,9 +126,9 @@ def validate_outputs(self, outputs, error=None):
134126
},
135127
]
136128
for span, expected in zip(span_list, expected):
137-
self.assertEqual(span.name, expected['name'])
138-
self.assertEqual(span.kind, expected['kind'])
139-
self.assertEqual(span.attributes, expected['attributes'])
129+
self.assertEqual(span.name, expected["name"])
130+
self.assertEqual(span.kind, expected["kind"])
131+
self.assertEqual(span.attributes, expected["attributes"])
140132

141133
def test_basic_asgi_call(self):
142134
app = otel_asgi.OpenTelemetryMiddleware(simple_asgi)
@@ -176,15 +168,13 @@ def test_request_attributes(self):
176168
"http.server_name": "127.0.0.1",
177169
"http.flavor": "1.0",
178170
"net.peer.ip": "127.0.0.1",
179-
"net.peer.port": 32767
171+
"net.peer.port": 32767,
180172
},
181173
)
182174

183175
def test_response_attributes(self):
184176
otel_asgi.set_status_code(self.span, 404)
185-
expected = (
186-
mock.call("http.status_code", 404),
187-
)
177+
expected = (mock.call("http.status_code", 404),)
188178
self.assertEqual(self.span.set_attribute.call_count, 1)
189179
self.assertEqual(self.span.set_attribute.call_count, 1)
190180
self.span.set_attribute.assert_has_calls(expected, any_order=True)

ext/opentelemetry-ext-testutil/src/opentelemetry/ext/testutil/asgitestutil.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44

55

66
def setup_testing_defaults(scope):
7-
scope.update({
8-
'client': ('127.0.0.1', 32767),
9-
'headers': [],
10-
'http_version': '1.0',
11-
'method': 'GET',
12-
'path': '/',
13-
'query_string': b'',
14-
'scheme': 'http',
15-
'server': ('127.0.0.1', 80),
16-
'type': 'http'
17-
})
7+
scope.update(
8+
{
9+
"client": ("127.0.0.1", 32767),
10+
"headers": [],
11+
"http_version": "1.0",
12+
"method": "GET",
13+
"path": "/",
14+
"query_string": b"",
15+
"scheme": "http",
16+
"server": ("127.0.0.1", 80),
17+
"type": "http",
18+
}
19+
)
1820

1921

2022
class AsgiTestBase(SpanTestBase):
@@ -38,9 +40,9 @@ def send_input(self, payload):
3840
asyncio.get_event_loop().run_until_complete(
3941
self.communicator.send_input(payload)
4042
)
41-
43+
4244
def send_default_request(self):
43-
self.send_input({'type': 'http.request', 'body': b''})
45+
self.send_input({"type": "http.request", "body": b""})
4446

4547
def get_output(self):
4648
output = asyncio.get_event_loop().run_until_complete(

0 commit comments

Comments
 (0)