@@ -111,7 +111,7 @@ def write_to_textfile(path, registry):
111
111
os .rename (tmppath , path )
112
112
113
113
114
- def push_to_gateway (gateway , job , registry , grouping_key = None , timeout = None ):
114
+ def push_to_gateway (gateway , job , registry , grouping_key = None , timeout = None , handler = None ):
115
115
'''Push metrics to the given pushgateway.
116
116
117
117
`gateway` the url for your push gateway. Either of the form
@@ -126,10 +126,10 @@ def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None):
126
126
127
127
This overwrites all metrics with the same job and grouping_key.
128
128
This uses the PUT HTTP method.'''
129
- _use_gateway ('PUT' , gateway , job , registry , grouping_key , timeout )
129
+ _use_gateway ('PUT' , gateway , job , registry , grouping_key , timeout , handler )
130
130
131
131
132
- def pushadd_to_gateway (gateway , job , registry , grouping_key = None , timeout = None ):
132
+ def pushadd_to_gateway (gateway , job , registry , grouping_key = None , timeout = None , handler = None ):
133
133
'''PushAdd metrics to the given pushgateway.
134
134
135
135
`gateway` the url for your push gateway. Either of the form
@@ -144,10 +144,10 @@ def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None):
144
144
145
145
This replaces metrics with the same name, job and grouping_key.
146
146
This uses the POST HTTP method.'''
147
- _use_gateway ('POST' , gateway , job , registry , grouping_key , timeout )
147
+ _use_gateway ('POST' , gateway , job , registry , grouping_key , timeout , handler )
148
148
149
149
150
- def delete_from_gateway (gateway , job , grouping_key = None , timeout = None ):
150
+ def delete_from_gateway (gateway , job , grouping_key = None , timeout = None , handler = None ):
151
151
'''Delete metrics from the given pushgateway.
152
152
153
153
`gateway` the url for your push gateway. Either of the form
@@ -161,10 +161,10 @@ def delete_from_gateway(gateway, job, grouping_key=None, timeout=None):
161
161
162
162
This deletes metrics with the given job and grouping_key.
163
163
This uses the DELETE HTTP method.'''
164
- _use_gateway ('DELETE' , gateway , job , None , grouping_key , timeout )
164
+ _use_gateway ('DELETE' , gateway , job , None , grouping_key , timeout , handler )
165
165
166
166
167
- def _use_gateway (method , gateway , job , registry , grouping_key , timeout ):
167
+ def _use_gateway (method , gateway , job , registry , grouping_key , timeout , handler ):
168
168
gateway_url = urlparse (gateway )
169
169
if not gateway_url .scheme :
170
170
gateway = 'http://{0}' .format (gateway )
@@ -182,10 +182,14 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout):
182
182
request = Request (url , data = data )
183
183
request .add_header ('Content-Type' , CONTENT_TYPE_LATEST )
184
184
request .get_method = lambda : method
185
- resp = build_opener (HTTPHandler ).open (request , timeout = timeout )
186
- if resp .code >= 400 :
187
- raise IOError ("error talking to pushgateway: {0} {1}" .format (
188
- resp .code , resp .msg ))
185
+ if handler is None :
186
+ resp = build_opener (handler ).open (request , timeout = timeout )
187
+ if resp .code >= 400 :
188
+ raise IOError ("error talking to pushgateway: {0} {1}" .format (
189
+ resp .code , resp .msg ))
190
+ else :
191
+ handler (url = url , method = lambda : method , timeout = timeout ,
192
+ headers = [('Content-Type' , CONTENT_TYPE_LATEST )], content = data )
189
193
190
194
def instance_ip_grouping_key ():
191
195
'''Grouping key with instance set to the IP Address of this host.'''
0 commit comments