@@ -118,7 +118,7 @@ def write_to_textfile(path, registry):
118
118
os .rename (tmppath , path )
119
119
120
120
121
- def push_to_gateway (gateway , job , registry , grouping_key = None , timeout = None ):
121
+ def push_to_gateway (gateway , job , registry , grouping_key = None , timeout = None , handler = None ):
122
122
'''Push metrics to the given pushgateway.
123
123
124
124
`gateway` the url for your push gateway. Either of the form
@@ -133,10 +133,10 @@ def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None):
133
133
134
134
This overwrites all metrics with the same job and grouping_key.
135
135
This uses the PUT HTTP method.'''
136
- _use_gateway ('PUT' , gateway , job , registry , grouping_key , timeout )
136
+ _use_gateway ('PUT' , gateway , job , registry , grouping_key , timeout , handler )
137
137
138
138
139
- def pushadd_to_gateway (gateway , job , registry , grouping_key = None , timeout = None ):
139
+ def pushadd_to_gateway (gateway , job , registry , grouping_key = None , timeout = None , handler = None ):
140
140
'''PushAdd metrics to the given pushgateway.
141
141
142
142
`gateway` the url for your push gateway. Either of the form
@@ -151,10 +151,10 @@ def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None):
151
151
152
152
This replaces metrics with the same name, job and grouping_key.
153
153
This uses the POST HTTP method.'''
154
- _use_gateway ('POST' , gateway , job , registry , grouping_key , timeout )
154
+ _use_gateway ('POST' , gateway , job , registry , grouping_key , timeout , handler )
155
155
156
156
157
- def delete_from_gateway (gateway , job , grouping_key = None , timeout = None ):
157
+ def delete_from_gateway (gateway , job , grouping_key = None , timeout = None , handler = None ):
158
158
'''Delete metrics from the given pushgateway.
159
159
160
160
`gateway` the url for your push gateway. Either of the form
@@ -168,10 +168,10 @@ def delete_from_gateway(gateway, job, grouping_key=None, timeout=None):
168
168
169
169
This deletes metrics with the given job and grouping_key.
170
170
This uses the DELETE HTTP method.'''
171
- _use_gateway ('DELETE' , gateway , job , None , grouping_key , timeout )
171
+ _use_gateway ('DELETE' , gateway , job , None , grouping_key , timeout , handler )
172
172
173
173
174
- def _use_gateway (method , gateway , job , registry , grouping_key , timeout ):
174
+ def _use_gateway (method , gateway , job , registry , grouping_key , timeout , handler ):
175
175
gateway_url = urlparse (gateway )
176
176
if not gateway_url .scheme :
177
177
gateway = 'http://{0}' .format (gateway )
@@ -189,10 +189,14 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout):
189
189
request = Request (url , data = data )
190
190
request .add_header ('Content-Type' , CONTENT_TYPE_LATEST )
191
191
request .get_method = lambda : method
192
- resp = build_opener (HTTPHandler ).open (request , timeout = timeout )
193
- if resp .code >= 400 :
194
- raise IOError ("error talking to pushgateway: {0} {1}" .format (
195
- resp .code , resp .msg ))
192
+ if handler is None :
193
+ resp = build_opener (handler ).open (request , timeout = timeout )
194
+ if resp .code >= 400 :
195
+ raise IOError ("error talking to pushgateway: {0} {1}" .format (
196
+ resp .code , resp .msg ))
197
+ else :
198
+ handler (url = url , method = lambda : method , timeout = timeout ,
199
+ headers = [('Content-Type' , CONTENT_TYPE_LATEST )], content = data )
196
200
197
201
def instance_ip_grouping_key ():
198
202
'''Grouping key with instance set to the IP Address of this host.'''
0 commit comments