diff options
author | Graham Dumpleton <Graham.Dumpleton@gmail.com> | 2015-03-29 21:21:08 +1100 |
---|---|---|
committer | Graham Dumpleton <Graham.Dumpleton@gmail.com> | 2015-03-29 21:21:08 +1100 |
commit | acfe756c14844702537163c6170e3f9476399af8 (patch) | |
tree | 6e5e8893b637548001c1f95b8d322e5f8c65b846 /tests/events.wsgi | |
parent | da7207be39ea5ecab37dfdc1a48d4bbefdd919f0 (diff) | |
download | mod_wsgi-acfe756c14844702537163c6170e3f9476399af8.tar.gz |
Add extended metrics collection and events system for monitoring requests.
Diffstat (limited to 'tests/events.wsgi')
-rw-r--r-- | tests/events.wsgi | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/events.wsgi b/tests/events.wsgi new file mode 100644 index 0000000..490b4c5 --- /dev/null +++ b/tests/events.wsgi @@ -0,0 +1,65 @@ +from __future__ import print_function + +import mod_wsgi +import traceback +import time +import os + +try: + mod_wsgi.request_data() +except RuntimeError: + print('INACTIVE') + +def wrapper(application): + def _application(environ, start_response): + print('WRAPPER', application) + return application(environ, start_response) + return _application + +def event_handler(name, **kwargs): + print('EVENT', name, kwargs, os.getpid(), mod_wsgi.application_group) + if name == 'request_started': + request = mod_wsgi.request_data() + print('REQUEST', request) + environ = kwargs['request_environ'] + start_time = time.time() + request['start_time'] = start_time + return dict(application=wrapper(kwargs['application'])) + elif name == 'request_finished': + request = mod_wsgi.request_data() + print('REQUEST', request) + print('FINISH', time.time()-request['start_time']) + print('PROCESS', mod_wsgi.process_metrics()) + elif name == 'request_exception': + exc_info = kwargs['exc_info'] + traceback.print_exception(*exc_info) + +print('EVENTS', mod_wsgi.event_callbacks) + +mod_wsgi.subscribe_events(event_handler) + +print('CALLBACKS', mod_wsgi.event_callbacks) + +def application(environ, start_response): + failure_mode = environ.get('HTTP_X_FAILURE_MODE', '') + + failure_mode = failure_mode.split() + + if 'application' in failure_mode: + raise RuntimeError('application') + + status = '200 OK' + output = b'Hello World!' + + response_headers = [('Content-type', 'text/plain'), + ('Content-Length', str(len(output)))] + start_response(status, response_headers) + + try: + yield output + + if 'yield' in failure_mode: + raise RuntimeError('yield') + finally: + if 'close' in failure_mode: + raise RuntimeError('close') |