Skip to content

Commit

Permalink
Statsd middleware for request timing.
Browse files Browse the repository at this point in the history
  • Loading branch information
dannon committed Sep 30, 2015
1 parent 6d5ee0b commit 5673487
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
8 changes: 7 additions & 1 deletion config/galaxy.ini.sample
Expand Up @@ -654,14 +654,20 @@ use_interactive = True
# lot of CPU.
#use_heartbeat = False


# Log to Sentry
# Sentry is an open source logging and error aggregation platform. Setting
# sentry_dsn will enable the Sentry middleware and errors will be sent to the
# indicated sentry instance. This connection string is available in your
# sentry instance under <project_name> -> Settings -> API Keys.
#sentry_dsn = None

# Log to statsd
# Statsd is an external statistics aggregator (https://github.com/etsy/statsd)
# Enabling the following options will cause galaxy to log request timing and
# other statistics to the configured statsd instance.
#statsd_host=
#statsd_port=8125

# -- Data Libraries

# These library upload options are described in much more detail in the wiki:
Expand Down
3 changes: 3 additions & 0 deletions lib/galaxy/config.py
Expand Up @@ -424,6 +424,9 @@ def __init__( self, **kwargs ):
self.new_lib_browse = string_as_bool( kwargs.get( 'new_lib_browse', False ) )
# Error logging with sentry
self.sentry_dsn = kwargs.get( 'sentry_dsn', None )
# Statistics and profiling with statsd
self.statsd_host = kwargs.get( 'statsd_host', '')
self.statsd_port = int( kwargs.get( 'statsd_port', 24224 ) )
# Logging with fluentd
self.fluent_log = string_as_bool( kwargs.get( 'fluent_log', False ) )
self.fluent_host = kwargs.get( 'fluent_host', 'localhost' )
Expand Down
29 changes: 29 additions & 0 deletions lib/galaxy/web/framework/middleware/statsd.py
@@ -0,0 +1,29 @@
"""
Middleware for sending request statistics to statsd
"""
from __future__ import absolute_import
import time
import statsd


class StatsdMiddleware(object):
"""
This logging middleware will log request durations to the configured statsd
instance.
"""

def __init__(self,
application,
statsd_host,
statsd_port):
self.application = application
self.statsd_client = statsd.StatsClient(statsd_host, statsd_port, prefix='galaxy')

def __call__(self, environ, start_response):
import pprint
pprint.pprint(environ)
start_time = time.time()
req = self.application(environ, start_response)
dt = int((time.time() - start_time) * 1000)
self.statsd_client.timing(environ.get('PATH_INFO', "NOPATH"), dt)
return req
6 changes: 6 additions & 0 deletions lib/galaxy/webapps/galaxy/buildapp.py
Expand Up @@ -689,6 +689,12 @@ def wrap_in_middleware( app, global_conf, **local_conf ):
from galaxy.web.framework.middleware.translogger import TransLogger
app = TransLogger( app )
log.debug( "Enabling 'trans logger' middleware" )
# Statsd request timing and profiling
statsd_host = conf.get('statsd_host', None)
if statsd_host:
from galaxy.web.framework.middleware.statsd import StatsdMiddleware
app = StatsdMiddleware( app, statsd_host, int(conf.get('statsd_port', 32768 )))
log.debug( "Enabling 'trans logger' middleware" )
# X-Forwarded-Host handling
from galaxy.web.framework.middleware.xforwardedhost import XForwardedHostMiddleware
app = XForwardedHostMiddleware( app )
Expand Down

0 comments on commit 5673487

Please sign in to comment.