1
- from __future__ import unicode_literals
2
-
3
1
import base64
4
2
from contextlib import closing
5
3
from http .server import BaseHTTPRequestHandler
19
17
from .registry import REGISTRY
20
18
from .utils import floatToGoString
21
19
22
- CONTENT_TYPE_LATEST = str ( 'text/plain; version=0.0.4; charset=utf-8' )
20
+ CONTENT_TYPE_LATEST = 'text/plain; version=0.0.4; charset=utf-8'
23
21
"""Content type of the latest text format"""
24
22
PYTHON376_OR_NEWER = sys .version_info > (3 , 7 , 5 )
25
23
@@ -85,7 +83,7 @@ def _bake_output(registry, accept_header, params):
85
83
if 'name[]' in params :
86
84
registry = registry .restricted_registry (params ['name[]' ])
87
85
output = encoder (registry )
88
- return str ( '200 OK' ) , (str ( 'Content-Type' ) , content_type ), output
86
+ return '200 OK' , ('Content-Type' , content_type ), output
89
87
90
88
91
89
def make_wsgi_app (registry = REGISTRY ):
@@ -97,8 +95,8 @@ def prometheus_app(environ, start_response):
97
95
params = parse_qs (environ .get ('QUERY_STRING' , '' ))
98
96
if environ ['PATH_INFO' ] == '/favicon.ico' :
99
97
# Serve empty response for browsers
100
- status = str ( '200 OK' )
101
- header = (str ( '' ), str ( '' ) )
98
+ status = '200 OK'
99
+ header = ('' , '' )
102
100
output = b''
103
101
else :
104
102
# Bake output
@@ -143,17 +141,16 @@ def generate_latest(registry=REGISTRY):
143
141
def sample_line (line ):
144
142
if line .labels :
145
143
labelstr = '{{{0}}}' .format (',' .join (
146
- ['{0 }="{1 }"' .format (
144
+ ['{}="{}"' .format (
147
145
k , v .replace ('\\ ' , r'\\' ).replace ('\n ' , r'\n' ).replace ('"' , r'\"' ))
148
146
for k , v in sorted (line .labels .items ())]))
149
147
else :
150
148
labelstr = ''
151
149
timestamp = ''
152
150
if line .timestamp is not None :
153
151
# Convert to milliseconds.
154
- timestamp = ' {0:d}' .format (int (float (line .timestamp ) * 1000 ))
155
- return '{0}{1} {2}{3}\n ' .format (
156
- line .name , labelstr , floatToGoString (line .value ), timestamp )
152
+ timestamp = f' { int (float (line .timestamp ) * 1000 ):d} '
153
+ return f'{ line .name } { labelstr } { floatToGoString (line .value )} { timestamp } \n '
157
154
158
155
output = []
159
156
for metric in registry .collect ():
@@ -175,9 +172,9 @@ def sample_line(line):
175
172
elif mtype == 'unknown' :
176
173
mtype = 'untyped'
177
174
178
- output .append ('# HELP {0 } {1 }\n ' .format (
175
+ output .append ('# HELP {} {}\n ' .format (
179
176
mname , metric .documentation .replace ('\\ ' , r'\\' ).replace ('\n ' , r'\n' )))
180
- output .append ('# TYPE {0 } {1 }\n ' . format ( mname , mtype ) )
177
+ output .append (f '# TYPE { mname } { mtype } \n ' )
181
178
182
179
om_samples = {}
183
180
for s in metric .samples :
@@ -193,9 +190,9 @@ def sample_line(line):
193
190
raise
194
191
195
192
for suffix , lines in sorted (om_samples .items ()):
196
- output .append ('# HELP {0}{1 } {2 }\n ' .format (metric .name , suffix ,
193
+ output .append ('# HELP {}{ } {}\n ' .format (metric .name , suffix ,
197
194
metric .documentation .replace ('\\ ' , r'\\' ).replace ('\n ' , r'\n' )))
198
- output .append ('# TYPE {0}{1 } gauge\n ' . format ( metric . name , suffix ) )
195
+ output .append (f '# TYPE { metric . name } { suffix } gauge\n ' )
199
196
output .extend (lines )
200
197
return '' .join (output ).encode ('utf-8' )
201
198
@@ -250,7 +247,7 @@ def write_to_textfile(path, registry):
250
247
251
248
This is intended for use with the Node exporter textfile collector.
252
249
The path must end in .prom for the textfile collector to process it."""
253
- tmppath = '%s.%s.%s' % ( path , os .getpid (), threading .current_thread ().ident )
250
+ tmppath = f' { path } . { os .getpid ()} . { threading .current_thread ().ident } '
254
251
with open (tmppath , 'wb' ) as f :
255
252
f .write (generate_latest (registry ))
256
253
@@ -270,8 +267,7 @@ def handle():
270
267
request .add_header (k , v )
271
268
resp = build_opener (base_handler ).open (request , timeout = timeout )
272
269
if resp .code >= 400 :
273
- raise IOError ("error talking to pushgateway: {0} {1}" .format (
274
- resp .code , resp .msg ))
270
+ raise OSError (f"error talking to pushgateway: { resp .code } { resp .msg } " )
275
271
276
272
return handle
277
273
@@ -308,7 +304,7 @@ def handle():
308
304
"""Handler that implements HTTP Basic Auth.
309
305
"""
310
306
if username is not None and password is not None :
311
- auth_value = '{0 }:{1 }' .format ( username , password ). encode ('utf-8' )
307
+ auth_value = f' { username } :{ password } ' .encode ()
312
308
auth_token = base64 .b64encode (auth_value )
313
309
auth_header = b'Basic ' + auth_token
314
310
headers .append (['Authorization' , auth_header ])
@@ -418,8 +414,8 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler)
418
414
PYTHON376_OR_NEWER
419
415
and gateway_url .scheme not in ['http' , 'https' ]
420
416
):
421
- gateway = 'http://{0}' . format ( gateway )
422
- url = '{0 }/metrics/{1 }/{2 }' .format (gateway , * _escape_grouping_key ("job" , job ))
417
+ gateway = f 'http://{ gateway } '
418
+ url = '{}/metrics/{}/{}' .format (gateway , * _escape_grouping_key ("job" , job ))
423
419
424
420
data = b''
425
421
if method != 'DELETE' :
@@ -428,7 +424,7 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler)
428
424
if grouping_key is None :
429
425
grouping_key = {}
430
426
url += '' .join (
431
- '/{0 }/{1 }' .format (* _escape_grouping_key (str (k ), str (v )))
427
+ '/{}/{}' .format (* _escape_grouping_key (str (k ), str (v )))
432
428
for k , v in sorted (grouping_key .items ()))
433
429
434
430
handler (
0 commit comments