Add statsd to the base JupyterHub app

Not actually emitting any metrics yet
This commit is contained in:
YuviPanda
2016-03-31 16:10:34 -07:00
parent 62a5e9dbce
commit 3dca0df55f
3 changed files with 35 additions and 0 deletions

View File

@@ -12,6 +12,7 @@ import signal
import socket
import sys
import threading
import statsd
from datetime import datetime
from distutils.version import LooseVersion as V
from getpass import getuser
@@ -59,6 +60,10 @@ from .utils import (
from .auth import Authenticator, PAMAuthenticator
from .spawner import Spawner, LocalProcessSpawner
# For faking stats
from .emptyclass import EmptyClass
common_aliases = {
'log-level': 'Application.log_level',
'f': 'JupyterHub.config_file',
@@ -492,6 +497,22 @@ class JupyterHub(Application):
help="Extra log handlers to set on JupyterHub logger",
).tag(config=True)
@property
def statsd(self):
if hasattr(self, '_statsd'):
return self._statsd
if self.statsd_host:
self._statsd = statsd.StatsClient(
self.statsd_host,
self.statsd_port,
self.statsd_prefix
)
return self._statsd
else:
# return an empty mock object!
self._statsd = EmptyClass()
return self._statsd
def init_logging(self):
# This prevents double log messages because tornado use a root logger that
# self.log is a child of. The logging module dipatches log messages to a log

13
jupyterhub/emptyclass.py Normal file
View File

@@ -0,0 +1,13 @@
"""
Simple empty class that returns itself for all functions called on it.
This allows us to call any method of any name on this, and it'll return another
instance of itself that'll allow any method to be called on it.
Primarily used to mock out the statsd client when statsd is not being used
"""
class EmptyClass:
def empty_function(self, *args, **kwargs):
return self
def __getattr__(self, attr):
return self.empty_function

View File

@@ -2,5 +2,6 @@ traitlets>=4.1
tornado>=4.1
jinja2
pamela
statsd
sqlalchemy>=1.0
requests