Skip to content

Commit 6009899

Browse files
bz2brian-brazil
authored andcommitted
Set default timeout of pushgateway actions to 30s (prometheus#237)
* Set default timeout of pushgateway actions to 30s Explicitly passing `None` will still trigger the no timeout behaviour of the underlying Python http/socket modules.
1 parent 4dd660c commit 6009899

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

prometheus_client/exposition.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
PYTHON26_OR_OLDER = sys.version_info < (2, 7)
3232

33+
3334
def make_wsgi_app(registry=core.REGISTRY):
3435
'''Create a WSGI app which serves the metrics from a registry.'''
3536
def prometheus_app(environ, start_response):
@@ -179,7 +180,9 @@ def handle():
179180
return handle
180181

181182

182-
def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, handler=default_handler):
183+
def push_to_gateway(
184+
gateway, job, registry, grouping_key=None, timeout=30,
185+
handler=default_handler):
183186
'''Push metrics to the given pushgateway.
184187
185188
`gateway` the url for your push gateway. Either of the form
@@ -190,7 +193,7 @@ def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, han
190193
`grouping_key` please see the pushgateway documentation for details.
191194
Defaults to None
192195
`timeout` is how long push will attempt to connect before giving up.
193-
Defaults to None
196+
Defaults to 30s, can be set to None for no timeout.
194197
`handler` is an optional function which can be provided to perform
195198
requests to the 'gateway'.
196199
Defaults to None, in which case an http or https request
@@ -221,7 +224,9 @@ def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, han
221224
_use_gateway('PUT', gateway, job, registry, grouping_key, timeout, handler)
222225

223226

224-
def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, handler=default_handler):
227+
def pushadd_to_gateway(
228+
gateway, job, registry, grouping_key=None, timeout=30,
229+
handler=default_handler):
225230
'''PushAdd metrics to the given pushgateway.
226231
227232
`gateway` the url for your push gateway. Either of the form
@@ -232,7 +237,7 @@ def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None,
232237
`grouping_key` please see the pushgateway documentation for details.
233238
Defaults to None
234239
`timeout` is how long push will attempt to connect before giving up.
235-
Defaults to None
240+
Defaults to 30s, can be set to None for no timeout.
236241
`handler` is an optional function which can be provided to perform
237242
requests to the 'gateway'.
238243
Defaults to None, in which case an http or https request
@@ -245,7 +250,8 @@ def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None,
245250
_use_gateway('POST', gateway, job, registry, grouping_key, timeout, handler)
246251

247252

248-
def delete_from_gateway(gateway, job, grouping_key=None, timeout=None, handler=default_handler):
253+
def delete_from_gateway(
254+
gateway, job, grouping_key=None, timeout=30, handler=default_handler):
249255
'''Delete metrics from the given pushgateway.
250256
251257
`gateway` the url for your push gateway. Either of the form
@@ -255,7 +261,7 @@ def delete_from_gateway(gateway, job, grouping_key=None, timeout=None, handler=d
255261
`grouping_key` please see the pushgateway documentation for details.
256262
Defaults to None
257263
`timeout` is how long delete will attempt to connect before giving up.
258-
Defaults to None
264+
Defaults to 30s, can be set to None for no timeout.
259265
`handler` is an optional function which can be provided to perform
260266
requests to the 'gateway'.
261267
Defaults to None, in which case an http or https request

tests/test_exposition.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ def test_delete_with_groupingkey(self):
172172
def test_push_with_handler(self):
173173
def my_test_handler(url, method, timeout, headers, data):
174174
headers.append(['X-Test-Header', 'foobar'])
175+
# Handler should be passed sane default timeout
176+
self.assertEqual(timeout, 30)
175177
return default_handler(url, method, timeout, headers, data)
176178
push_to_gateway(self.address, "my_job", self.registry, handler=my_test_handler)
177179
self.assertEqual(self.requests[0][0].command, 'PUT')

0 commit comments

Comments
 (0)