Skip to content

Commit fbb677a

Browse files
alrexocelotl
alrex
andauthored
use f-strings instead of format (#693)
Co-authored-by: Diego Hurtado <[email protected]>
1 parent 2710e25 commit fbb677a

File tree

39 files changed

+90
-128
lines changed

39 files changed

+90
-128
lines changed

exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/exporter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def agent_writer(self):
102102
self._agent_writer = AgentWriter(uds_path=url_parsed.path)
103103
else:
104104
raise ValueError(
105-
"Unknown scheme `%s` for agent URL" % url_parsed.scheme
105+
f"Unknown scheme `{url_parsed.scheme}` for agent URL"
106106
)
107107
return self._agent_writer
108108

@@ -225,7 +225,7 @@ def _get_span_name(span):
225225
)
226226
span_kind_name = span.kind.name if span.kind else None
227227
name = (
228-
"{}.{}".format(instrumentation_name, span_kind_name)
228+
f"{instrumentation_name}.{span_kind_name}"
229229
if instrumentation_name and span_kind_name
230230
else span.name
231231
)

instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ async def on_request_start(
175175
return
176176

177177
http_method = params.method.upper()
178-
request_span_name = "HTTP {}".format(http_method)
178+
request_span_name = f"HTTP {http_method}"
179179

180180
trace_config_ctx.span = trace_config_ctx.tracer.start_span(
181181
request_span_name, kind=SpanKind.CLIENT,

instrumentation/opentelemetry-instrumentation-aiohttp-client/tests/test_aiohttp_client_integration.py

+10-18
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def _http_request(
9292
method: str = "GET",
9393
status_code: int = HTTPStatus.OK,
9494
request_handler: typing.Callable = None,
95-
**kwargs
95+
**kwargs,
9696
) -> typing.Tuple[str, int]:
9797
"""Helper to start an aiohttp test server and send an actual HTTP request to it."""
9898

@@ -132,9 +132,7 @@ def test_status_codes(self):
132132
(span_status, None),
133133
{
134134
SpanAttributes.HTTP_METHOD: "GET",
135-
SpanAttributes.HTTP_URL: "http://{}:{}/test-path?query=param#foobar".format(
136-
host, port
137-
),
135+
SpanAttributes.HTTP_URL: f"http://{host}:{port}/test-path?query=param#foobar",
138136
SpanAttributes.HTTP_STATUS_CODE: int(
139137
status_code
140138
),
@@ -167,7 +165,7 @@ def test_hooks(self):
167165
expected = "PATCH - /some/path"
168166

169167
def request_hook(span: Span, params: aiohttp.TraceRequestStartParams):
170-
span.update_name("{} - {}".format(params.method, params.url.path))
168+
span.update_name(f"{params.method} - {params.url.path}")
171169

172170
def response_hook(
173171
span: Span,
@@ -198,7 +196,7 @@ def response_hook(
198196
)
199197
self.assertEqual(
200198
span.attributes[SpanAttributes.HTTP_URL],
201-
"http://{}:{}{}".format(host, port, path),
199+
f"http://{host}:{port}{path}",
202200
)
203201
self.assertEqual(
204202
span.attributes[SpanAttributes.HTTP_STATUS_CODE], HTTPStatus.OK
@@ -227,9 +225,7 @@ def strip_query_params(url: yarl.URL) -> str:
227225
(StatusCode.UNSET, None),
228226
{
229227
SpanAttributes.HTTP_METHOD: "GET",
230-
SpanAttributes.HTTP_URL: "http://{}:{}/some/path".format(
231-
host, port
232-
),
228+
SpanAttributes.HTTP_URL: f"http://{host}:{port}/some/path",
233229
SpanAttributes.HTTP_STATUS_CODE: int(HTTPStatus.OK),
234230
},
235231
)
@@ -290,9 +286,7 @@ async def request_handler(request):
290286
(StatusCode.ERROR, None),
291287
{
292288
SpanAttributes.HTTP_METHOD: "GET",
293-
SpanAttributes.HTTP_URL: "http://{}:{}/test_timeout".format(
294-
host, port
295-
),
289+
SpanAttributes.HTTP_URL: f"http://{host}:{port}/test_timeout",
296290
},
297291
)
298292
]
@@ -319,9 +313,7 @@ async def request_handler(request):
319313
(StatusCode.ERROR, None),
320314
{
321315
SpanAttributes.HTTP_METHOD: "GET",
322-
SpanAttributes.HTTP_URL: "http://{}:{}/test_too_many_redirects".format(
323-
host, port
324-
),
316+
SpanAttributes.HTTP_URL: f"http://{host}:{port}/test_too_many_redirects",
325317
},
326318
)
327319
]
@@ -399,7 +391,7 @@ def test_instrument(self):
399391
span = self.assert_spans(1)
400392
self.assertEqual("GET", span.attributes[SpanAttributes.HTTP_METHOD])
401393
self.assertEqual(
402-
"http://{}:{}/test-path".format(host, port),
394+
f"http://{host}:{port}/test-path",
403395
span.attributes[SpanAttributes.HTTP_URL],
404396
)
405397
self.assertEqual(200, span.attributes[SpanAttributes.HTTP_STATUS_CODE])
@@ -502,13 +494,13 @@ def strip_query_params(url: yarl.URL) -> str:
502494
)
503495
span = self.assert_spans(1)
504496
self.assertEqual(
505-
"http://{}:{}/test-path".format(host, port),
497+
f"http://{host}:{port}/test-path",
506498
span.attributes[SpanAttributes.HTTP_URL],
507499
)
508500

509501
def test_hooks(self):
510502
def request_hook(span: Span, params: aiohttp.TraceRequestStartParams):
511-
span.update_name("{} - {}".format(params.method, params.url.path))
503+
span.update_name(f"{params.method} - {params.url.path}")
512504

513505
def response_hook(
514506
span: Span,

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,9 @@ def get_default_span_details(scope: dict) -> Tuple[str, dict]:
152152
Returns:
153153
a tuple of the span name, and any attributes to attach to the span.
154154
"""
155-
span_name = scope.get("path", "").strip() or "HTTP {}".format(
156-
scope.get("method", "").strip()
155+
span_name = (
156+
scope.get("path", "").strip()
157+
or f"HTTP {scope.get('method', '').strip()}"
157158
)
158159

159160
return span_name, {}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def _common_request( # pylint: disable=too-many-locals
123123
endpoint_name = getattr(instance, "host").split(".")[0]
124124

125125
with self._tracer.start_as_current_span(
126-
"{}.command".format(endpoint_name), kind=SpanKind.CONSUMER,
126+
f"{endpoint_name}.command", kind=SpanKind.CONSUMER,
127127
) as span:
128128
span.set_attribute("endpoint", endpoint_name)
129129
if args:

instrumentation/opentelemetry-instrumentation-boto/tests/test_boto_instrumentation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
def assert_span_http_status_code(span, code):
3636
"""Assert on the span's 'http.status_code' tag"""
3737
tag = span.attributes[SpanAttributes.HTTP_STATUS_CODE]
38-
assert tag == code, "%r != %r" % (tag, code)
38+
assert tag == code, f"{tag} != {code}"
3939

4040

4141
class TestBotoInstrumentor(TestBase):

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def _patched_api_call(self, original_func, instance, args, kwargs):
151151
result = None
152152

153153
with self._tracer.start_as_current_span(
154-
"{}".format(service_name), kind=SpanKind.CLIENT,
154+
f"{service_name}", kind=SpanKind.CLIENT,
155155
) as span:
156156
# inject trace context into payload headers for lambda Invoke
157157
if BotocoreInstrumentor._is_lambda_invoke(

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def _trace_prerun(self, *args, **kwargs):
136136

137137
logger.debug("prerun signal start task_id=%s", task_id)
138138

139-
operation_name = "{0}/{1}".format(_TASK_RUN, task.name)
139+
operation_name = f"{_TASK_RUN}/{task.name}"
140140
span = self._tracer.start_span(
141141
operation_name, context=tracectx, kind=trace.SpanKind.CONSUMER
142142
)
@@ -178,7 +178,7 @@ def _trace_before_publish(self, *args, **kwargs):
178178
if task is None or task_id is None:
179179
return
180180

181-
operation_name = "{0}/{1}".format(_TASK_APPLY_ASYNC, task.name)
181+
operation_name = f"{_TASK_APPLY_ASYNC}/{task.name}"
182182
span = self._tracer.start_span(
183183
operation_name, kind=trace.SpanKind.PRODUCER
184184
)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def set_attributes_from_context(span, context):
106106

107107
# set attribute name if not set specially for a key
108108
if attribute_name is None:
109-
attribute_name = "celery.{}".format(key)
109+
attribute_name = f"celery.{key}"
110110

111111
span.set_attribute(attribute_name, value)
112112

instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def fn_task():
159159
utils.retrieve_span(fn_task, task_id), (None, None)
160160
)
161161
except Exception as ex: # pylint: disable=broad-except
162-
self.fail("Exception was raised: %s" % ex)
162+
self.fail(f"Exception was raised: {ex}")
163163

164164
def test_task_id_from_protocol_v1(self):
165165
# ensures a `task_id` is properly returned when Protocol v1 is used.

instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def _get_span_name(request):
130130
return match.view_name
131131

132132
except Resolver404:
133-
return "HTTP {}".format(request.method)
133+
return f"HTTP {request.method}"
134134

135135
def process_request(self, request):
136136
# request.META is a dictionary containing all available HTTP headers
@@ -213,7 +213,7 @@ def process_response(self, request, response):
213213
if activation and span:
214214
add_response_attributes(
215215
span,
216-
"{} {}".format(response.status_code, response.reason_phrase),
216+
f"{response.status_code} {response.reason_phrase}",
217217
response,
218218
)
219219

instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,7 @@ def test_trace_response_headers(self):
349349
)
350350
self.assertEqual(
351351
response.headers["traceresponse"],
352-
"00-{0}-{1}-01".format(
353-
format_trace_id(span.get_span_context().trace_id),
354-
format_span_id(span.get_span_context().span_id),
355-
),
352+
f"00-{format_trace_id(span.get_span_context().trace_id)}-{format_span_id(span.get_span_context().span_id)}-01",
356353
)
357354
self.memory_exporter.clear()
358355

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ def wrapper(wrapped, _, args, kwargs):
192192
for member in _ATTRIBUTES_FROM_RESULT:
193193
if member in rv:
194194
span.set_attribute(
195-
"elasticsearch.{0}".format(member),
196-
str(rv[member]),
195+
f"elasticsearch.{member}", str(rv[member]),
197196
)
198197

199198
if callable(response_hook):

instrumentation/opentelemetry-instrumentation-elasticsearch/tests/test_elasticsearch.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def _test_trace_error(self, code, exc):
169169
self.assertFalse(span.status.is_ok)
170170
self.assertEqual(span.status.status_code, code)
171171
self.assertEqual(
172-
span.status.description, "{}: {}".format(type(exc).__name__, exc)
172+
span.status.description, f"{type(exc).__name__}: {exc}"
173173
)
174174

175175
def test_parent(self, request_mock):

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,7 @@ def process_resource(self, req, resp, resource, params):
264264

265265
resource_name = resource.__class__.__name__
266266
span.set_attribute("falcon.resource", resource_name)
267-
span.update_name(
268-
"{0}.on_{1}".format(resource_name, req.method.lower())
269-
)
267+
span.update_name(f"{resource_name}.on_{req.method.lower()}")
270268

271269
def process_response(
272270
self, req, resp, resource, req_succeeded=None
@@ -294,6 +292,7 @@ def process_response(
294292
else:
295293
status = "500"
296294
reason = "{}: {}".format(exc_type.__name__, exc)
295+
reason = f"{exc_type.__name__}: {exc}"
297296

298297
status = status.split(" ")[0]
299298
try:

instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ def _test_method(self, method):
8484
spans = self.memory_exporter.get_finished_spans()
8585
self.assertEqual(len(spans), 1)
8686
span = spans[0]
87-
self.assertEqual(
88-
span.name, "HelloWorldResource.on_{0}".format(method.lower())
89-
)
87+
self.assertEqual(span.name, f"HelloWorldResource.on_{method.lower()}")
9088
self.assertEqual(span.status.status_code, StatusCode.UNSET)
9189
self.assertEqual(
9290
span.status.description, None,
@@ -209,10 +207,7 @@ def test_trace_response(self):
209207
)
210208
self.assertEqual(
211209
headers["traceresponse"],
212-
"00-{0}-{1}-01".format(
213-
format_trace_id(span.get_span_context().trace_id),
214-
format_span_id(span.get_span_context().span_id),
215-
),
210+
f"00-{format_trace_id(span.get_span_context().trace_id)}-{format_span_id(span.get_span_context().span_id)}-01",
216211
)
217212

218213
set_global_response_propagator(orig)

instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,7 @@ def test_trace_response(self):
165165
)
166166
self.assertEqual(
167167
headers["traceresponse"],
168-
"00-{0}-{1}-01".format(
169-
trace.format_trace_id(span.get_span_context().trace_id),
170-
trace.format_span_id(span.get_span_context().span_id),
171-
),
168+
f"00-{trace.format_trace_id(span.get_span_context().trace_id)}-{trace.format_span_id(span.get_span_context().span_id)}-01",
172169
)
173170

174171
set_global_response_propagator(orig)

instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def _intercept(self, request, metadata, client_info, invoker):
137137
span.set_status(
138138
Status(
139139
status_code=StatusCode.ERROR,
140-
description="{}: {}".format(type(exc).__name__, exc),
140+
description=f"{type(exc).__name__}: {exc}",
141141
)
142142
)
143143
span.record_exception(exc)

instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_server.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ def abort(self, code, details):
122122
)
123123
self._active_span.set_status(
124124
Status(
125-
status_code=StatusCode.ERROR,
126-
description="{}:{}".format(code, details),
125+
status_code=StatusCode.ERROR, description=f"{code}:{details}",
127126
)
128127
)
129128
return self._servicer_context.abort(code, details)
@@ -142,7 +141,7 @@ def set_code(self, code):
142141
self._active_span.set_status(
143142
Status(
144143
status_code=StatusCode.ERROR,
145-
description="{}:{}".format(code, details),
144+
description=f"{code}:{details}",
146145
)
147146
)
148147
return self._servicer_context.set_code(code)
@@ -153,7 +152,7 @@ def set_details(self, details):
153152
self._active_span.set_status(
154153
Status(
155154
status_code=StatusCode.ERROR,
156-
description="{}:{}".format(self.code, details),
155+
description=f"{self.code}:{details}",
157156
)
158157
)
159158
return self._servicer_context.set_details(details)

instrumentation/opentelemetry-instrumentation-grpc/tests/_server.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,6 @@ def create_test_server(port):
8686
TestServer(), server
8787
)
8888

89-
server.add_insecure_port("localhost:{}".format(port))
89+
server.add_insecure_port(f"localhost:{port}")
9090

9191
return server

0 commit comments

Comments
 (0)