MCPcopy
hub / github.com/benoitc/gunicorn / Statsd

Class Statsd

gunicorn/instrument/statsd.py:23–140  ·  view source on GitHub ↗

statsD-based instrumentation, that passes as a logger

Source from the content-addressed store, hash-verified

21
22
23class Statsd(Logger):
24 """statsD-based instrumentation, that passes as a logger
25 """
26 def __init__(self, cfg):
27 Logger.__init__(self, cfg)
28 self.prefix = sub(r"^(.+[^.]+)\.*$", "\\g<1>.", cfg.statsd_prefix)
29
30 if isinstance(cfg.statsd_host, str):
31 address_family = socket.AF_UNIX
32 else:
33 address_family = socket.AF_INET
34
35 try:
36 self.sock = socket.socket(address_family, socket.SOCK_DGRAM)
37 self.sock.connect(cfg.statsd_host)
38 except Exception:
39 self.sock = None
40
41 self.dogstatsd_tags = cfg.dogstatsd_tags
42
43 # Log errors and warnings
44 def critical(self, msg, *args, **kwargs):
45 Logger.critical(self, msg, *args, **kwargs)
46 self.increment("gunicorn.log.critical", 1)
47
48 def error(self, msg, *args, **kwargs):
49 Logger.error(self, msg, *args, **kwargs)
50 self.increment("gunicorn.log.error", 1)
51
52 def warning(self, msg, *args, **kwargs):
53 Logger.warning(self, msg, *args, **kwargs)
54 self.increment("gunicorn.log.warning", 1)
55
56 def exception(self, msg, *args, **kwargs):
57 Logger.exception(self, msg, *args, **kwargs)
58 self.increment("gunicorn.log.exception", 1)
59
60 # Special treatment for info, the most common log level
61 def info(self, msg, *args, **kwargs):
62 self.log(logging.INFO, msg, *args, **kwargs)
63
64 # skip the run-of-the-mill logs
65 def debug(self, msg, *args, **kwargs):
66 self.log(logging.DEBUG, msg, *args, **kwargs)
67
68 def log(self, lvl, msg, *args, **kwargs):
69 """Log a given statistic if metric, value and type are present
70 """
71 try:
72 extra = kwargs.get("extra", None)
73 if extra is not None:
74 metric = extra.get(METRIC_VAR, None)
75 value = extra.get(VALUE_VAR, None)
76 typ = extra.get(MTYPE_VAR, None)
77 if metric and value and typ:
78 if typ == GAUGE_TYPE:
79 self.gauge(metric, value)
80 elif typ == COUNTER_TYPE:

Callers 8

test_statsd_failFunction · 0.90
test_dogstatsd_tagsFunction · 0.90
test_instrumentFunction · 0.90
test_prefixFunction · 0.90
test_prefix_no_dotFunction · 0.90
test_prefix_nestedFunction · 0.90

Calls

no outgoing calls

Tested by 8

test_statsd_failFunction · 0.72
test_dogstatsd_tagsFunction · 0.72
test_instrumentFunction · 0.72
test_prefixFunction · 0.72
test_prefix_no_dotFunction · 0.72
test_prefix_nestedFunction · 0.72