Skip to content

Commit

Permalink
Compress response with gzip
Browse files Browse the repository at this point in the history
  • Loading branch information
thekondor committed Nov 22, 2022
1 parent 1893d47 commit aa641f7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
12 changes: 10 additions & 2 deletions druid_exporter/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
from druid_exporter import collector
from prometheus_client import generate_latest, make_wsgi_app, REGISTRY
from gevent.pywsgi import WSGIServer
from wsgigzip import gzip

log = logging.getLogger(__name__)

class DruidWSGIApp(object):

def __init__(self, post_uri, druid_collector, prometheus_app, encoding):
self.prometheus_app = prometheus_app
self.druid_collector = druid_collector
Expand Down Expand Up @@ -59,6 +59,14 @@ def __call__(self, environ, start_response):
return ''


def with_gzip_compression(handle_cb):
@gzip(compress_level=6)
def handle(environ, start_response):
return handle_cb(environ, start_response)

return handle


def main():
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--listen', metavar='ADDRESS',
Expand Down Expand Up @@ -86,7 +94,7 @@ def main():
druid_wsgi_app = DruidWSGIApp(args.uri, druid_collector,
prometheus_app, args.encoding)

httpd = WSGIServer((address, int(port)), druid_wsgi_app)
httpd = WSGIServer((address, int(port)), with_gzip_compression(druid_wsgi_app))
httpd.serve_forever()

if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
install_requires=[
'prometheus-client==0.7.1',
'gevent',
'wsgigzip'
],
entry_points={
'console_scripts': [
Expand Down

0 comments on commit aa641f7

Please sign in to comment.