-
Notifications
You must be signed in to change notification settings - Fork 835
Add handler parameter to pushgateway functions to permit HTTP AUTH etc. (refs issue #60) #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Allow a custom handler to be provided, so that the caller can provide code which carried out basic auth, https client certificate validation or other arbitrary schemes and access methods such as using different types of proxy.
Provide pydoc for the new handler function to the various pushgateway functions, provide parameter descriptions in the documentatation for the push_to_gateway function to avoid excessive copy-and-paste.
|
I found I needed to pass additional arguments to the handler, such as the username/password, so I have updated the patch to include a 'handler_args' parameter, and included a simple 'basic_auth' handler too. Usage then becomes something like this... |
|
Can you rebase this? I'd have presumed that any args are embedded in the handler, so there's no need to pass args beyond that. |
|
Rebased. Re: 'args embedded in the handler' - I'm not sure what you mean or how to do that. The only way other way I could think to minimise the footprint was to change it from a function to an object, but that didn't feel right for some reason, so I used the 'kwargs' approach I've seen used elsewhere. My python isn't that sharp though, so I'm open to better suggestions. Anyway, have now tested this branch more widely with several clients and they are now pushing data happily to push gateway services that are using https+basicauth, so I can at least report it works for me. Also, I've just seen the py26 travis build failure (URLError), but I can't seem to figure out wtf would cause that, other than perhaps a python26/urllib2 bug. I can't see how it would be related to this patch, unless I'm missing something? |
|
That's still not rebased.
Use a closure. So instead of: do This works with classes too. |
Allow a custom handler to be provided, so that the caller can provide code which carried out basic auth, https client certificate validation or other arbitrary schemes and access methods such as using different types of proxy.
Provide pydoc for the new handler function to the various pushgateway functions, provide parameter descriptions in the documentatation for the push_to_gateway function to avoid excessive copy-and-paste.
|
@brian-brazil - ok, i think i've got it - how does it look now? |
| def my_auth_handler(url, method, timeout, headers, data): | ||
| username = os.environ['PUSHGW_USERNAME'] | ||
| password = os.environ['PUSHGW_PASSWORD'] | ||
| return basic_auth_handler(url, method, timeout, headers, data, username, password) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd hardcode the user/pw here to keep it simpler.
If you use an env example, some users will be confused and think it only works if it comes from the env.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example updated.
prometheus_client/exposition.py
Outdated
| raise IOError("error talking to pushgateway: {0} {1}".format( | ||
| resp.code, resp.msg)) | ||
| headers=[('Content-Type', CONTENT_TYPE_LATEST)] | ||
| if handler is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The handler isn't mutable, so you can put it as a default in the def line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I can see how that would work, as the function is called with a handler of None by other functions in exposition.py. Do you mean make 'default_handler' the default instead of None for all of them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean make 'default_handler' the default instead of None for all of them?
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
prometheus_client/exposition.py
Outdated
|
|
||
| from . import core | ||
|
|
||
| from .handlers.base import handler as default_handler |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd put these in this file to keep things all in one place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand. Do you mean something like...
from .handlers.base import default_handler
and in base.py...
default_handler = handler
or something else???
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean put all of this in exposition.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
|
||
| def handler(url, method, timeout, headers, data, username = None, password = None): | ||
| def handle(): | ||
| '''Handler that implements HTTP Basic Auth by setting auth headers if |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first sentence in a docstring should be on one line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, done.
|
|
||
|
|
||
| def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None): | ||
| def default_handler(url, method, timeout, headers, data): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a docstring to indicate this is for use with the pgw functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
|
||
| from prometheus_client.exposition import default_handler | ||
|
|
||
| def handler(url, method, timeout, headers, data, username = None, password = None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should go in exposition.py too.
No spaces around the = here
|
The unittests aren't passing on all version of Python, could you fix that up and we can get this submitted? |
This reverts commit 62e8012.
|
I'm not having much luck fixing the python2.6 test. I have no py26 environment locally to reproduce it with, and from what I can determine from the error/stacktrace, it's more of an issue with python2.6's urllib2 implementation on the server than it is a regression caused by this patch, unless you can see something that I'm missing. |
|
Doing some testing, it seems to be the lack of a http:// on the url that's causing the issue. |
|
That's the same conclusion I came to, hence the last patch and revert. I've just tried another workaround, which seems to have cleared it now. Anyway, I couldn't see anything in the patch that would affect that behaviour, so I'm guessing something changed in the py26 test environment since the test was passing. I'd be interested to see if the master branch (unpatched) still passes that test. |
|
Thanks! |
Takes the branch by @tim-seoss (PR #120), fixes a minor regression, moves the default handler code out to be more easily re-used/adapted, and adds a basic unit test for a custom handler.
A simple handler function for adding Basic Auth might then look like this...
@brian-brazil - your thoughts?