def test_prefix_nested(): c = Config() c.set("statsd_prefix", "test.asdf.") logger = Statsd(c) logger.sock = MockSocket(False) logger.info("Blah", extra={"mtype": "gauge", "metric": "gunicorn.test", "value": 666}) assert logger.sock.msgs[0] == b"test.asdf.gunicorn.test:666|g"
def test_prefix(): c = Config() c.set("statsd_prefix", "test.") logger = Statsd(c) logger.sock = MockSocket(False) logger.info("Blah", extra={"mtype": "gauge", "metric": "gunicorn.test", "value": 666}) assert logger.sock.msgs[0] == b"test.gunicorn.test:666|g"
def test_prefix_multiple_dots(): c = Config() c.set("statsd_prefix", "test...") logger = Statsd(c) logger.sock = MockSocket(False) logger.info("Blah", extra={"mtype": "gauge", "metric": "gunicorn.test", "value": 666}) t.eq(logger.sock.msgs[0], "test.gunicorn.test:666|g")
def test_dogstatsd_tags(): c = Config() tags = 'yucatan,libertine:rhubarb' c.set('dogstatsd_tags', tags) logger = Statsd(c) logger.sock = MockSocket(False) logger.info("Twill", extra={"mtype": "gauge", "metric": "barb.westerly", "value": 2}) assert logger.sock.msgs[0] == b"barb.westerly:2|g|#" + tags.encode('ascii')
def test_instrument(): logger = Statsd(Config()) # Capture logged messages sio = io.StringIO() logger.error_log.addHandler(logging.StreamHandler(sio)) logger.sock = MockSocket(False) # Regular message logger.info("Blah", extra={ "mtype": "gauge", "metric": "gunicorn.test", "value": 666 }) assert logger.sock.msgs[0] == b"gunicorn.test:666|g" assert sio.getvalue() == "Blah\n" logger.sock.reset() # Only metrics, no logging logger.info("", extra={ "mtype": "gauge", "metric": "gunicorn.test", "value": 666 }) assert logger.sock.msgs[0] == b"gunicorn.test:666|g" assert sio.getvalue() == "Blah\n" # log is unchanged logger.sock.reset() # Debug logging also supports metrics logger.debug("", extra={ "mtype": "gauge", "metric": "gunicorn.debug", "value": 667 }) assert logger.sock.msgs[0] == b"gunicorn.debug:667|g" assert sio.getvalue() == "Blah\n" # log is unchanged logger.sock.reset() logger.critical("Boom") assert logger.sock.msgs[0] == b"gunicorn.log.critical:1|c|@1.0" logger.sock.reset() logger.access(SimpleNamespace(status="200 OK"), None, {}, timedelta(seconds=7)) assert logger.sock.msgs[0] == b"gunicorn.request.duration:7000.0|ms" assert logger.sock.msgs[1] == b"gunicorn.requests:1|c|@1.0" assert logger.sock.msgs[2] == b"gunicorn.request.status.200:1|c|@1.0"
def test_statsd_fail(): "UDP socket fails" logger = Statsd(Config()) logger.sock = MockSocket(True) logger.info("No impact on logging") logger.debug("No impact on logging") logger.critical("No impact on logging") logger.error("No impact on logging") logger.warning("No impact on logging") logger.exception("No impact on logging")
def test_instrument(): logger = Statsd(Config()) # Capture logged messages sio = io.StringIO() logger.error_log.addHandler(logging.StreamHandler(sio)) logger.sock = MockSocket(False) # Regular message logger.info("Blah", extra={"mtype": "gauge", "metric": "gunicorn.test", "value": 666}) assert logger.sock.msgs[0] == b"gunicorn.test:666|g" assert sio.getvalue() == "Blah\n" logger.sock.reset() # Only metrics, no logging logger.info("", extra={"mtype": "gauge", "metric": "gunicorn.test", "value": 666}) assert logger.sock.msgs[0] == b"gunicorn.test:666|g" assert sio.getvalue() == "Blah\n" # log is unchanged logger.sock.reset() # Debug logging also supports metrics logger.debug("", extra={"mtype": "gauge", "metric": "gunicorn.debug", "value": 667}) assert logger.sock.msgs[0] == b"gunicorn.debug:667|g" assert sio.getvalue() == "Blah\n" # log is unchanged logger.sock.reset() logger.critical("Boom") assert logger.sock.msgs[0] == b"gunicorn.log.critical:1|c|@1.0" logger.sock.reset() logger.access(SimpleNamespace(status="200 OK"), None, {}, timedelta(seconds=7)) assert logger.sock.msgs[0] == b"gunicorn.request.duration:7000.0|ms" assert logger.sock.msgs[1] == b"gunicorn.requests:1|c|@1.0" assert logger.sock.msgs[2] == b"gunicorn.request.status.200:1|c|@1.0"