mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
Allow specifying a UDS socket address through --statsd-host
This commit is contained in:
parent
4b91ca1e9f
commit
15abac7e81
@ -359,21 +359,15 @@ statsd_host
|
|||||||
* ``--statsd-host STATSD_ADDR``
|
* ``--statsd-host STATSD_ADDR``
|
||||||
* ``None``
|
* ``None``
|
||||||
|
|
||||||
``host:port`` of the statsd server to log to.
|
The address of the StatsD server to log to.
|
||||||
|
|
||||||
|
Address is a string of the form:
|
||||||
|
|
||||||
|
* ``unix://PATH`` : for a unix domain socket.
|
||||||
|
* ``HOST:PORT`` : for a network address
|
||||||
|
|
||||||
.. versionadded:: 19.1
|
.. versionadded:: 19.1
|
||||||
|
|
||||||
.. _statsd-socket:
|
|
||||||
|
|
||||||
statsd_socket
|
|
||||||
~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
* ``--statsd-socket STATSD_SOCKET``
|
|
||||||
* ``None``
|
|
||||||
|
|
||||||
Unix domain socket of the statsd server to log to.
|
|
||||||
Supersedes ``statsd_host`` if provided.
|
|
||||||
|
|
||||||
.. _dogstatsd-tags:
|
.. _dogstatsd-tags:
|
||||||
|
|
||||||
dogstatsd_tags
|
dogstatsd_tags
|
||||||
|
|||||||
@ -144,11 +144,7 @@ class Config(object):
|
|||||||
# if default logger is in use, and statsd is on, automagically switch
|
# if default logger is in use, and statsd is on, automagically switch
|
||||||
# to the statsd logger
|
# to the statsd logger
|
||||||
if uri == LoggerClass.default:
|
if uri == LoggerClass.default:
|
||||||
statsd_address = 'statsd_socket' in self.settings and \
|
if 'statsd_host' in self.settings and self.settings['statsd_host'].value is not None:
|
||||||
self.settings['statsd_socket'].value or \
|
|
||||||
'statsd_host' in self.settings and \
|
|
||||||
self.settings['statsd_host'].value
|
|
||||||
if statsd_address is not None:
|
|
||||||
uri = "gunicorn.instrument.statsd.Statsd"
|
uri = "gunicorn.instrument.statsd.Statsd"
|
||||||
|
|
||||||
logger_class = util.load_class(
|
logger_class = util.load_class(
|
||||||
@ -499,15 +495,17 @@ def validate_chdir(val):
|
|||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
def validate_hostport(val):
|
def validate_address(val):
|
||||||
val = validate_string(val)
|
val = validate_string(val)
|
||||||
if val is None:
|
if val is None:
|
||||||
return None
|
return None
|
||||||
elements = val.split(":")
|
|
||||||
if len(elements) == 2:
|
try:
|
||||||
return (elements[0], int(elements[1]))
|
address = util.parse_address(val, default_port='8125')
|
||||||
else:
|
except RuntimeError:
|
||||||
raise TypeError("Value must consist of: hostname:port")
|
raise TypeError("Value must be one of ('host:port', 'unix://PATH')")
|
||||||
|
|
||||||
|
return address
|
||||||
|
|
||||||
|
|
||||||
def validate_reload_engine(val):
|
def validate_reload_engine(val):
|
||||||
@ -1475,25 +1473,18 @@ class StatsdHost(Setting):
|
|||||||
cli = ["--statsd-host"]
|
cli = ["--statsd-host"]
|
||||||
meta = "STATSD_ADDR"
|
meta = "STATSD_ADDR"
|
||||||
default = None
|
default = None
|
||||||
validator = validate_hostport
|
validator = validate_address
|
||||||
desc = """\
|
desc = """\
|
||||||
``host:port`` of the statsd server to log to.
|
The address of the StatsD server to log to.
|
||||||
|
|
||||||
|
Address is a string of the form:
|
||||||
|
|
||||||
|
* ``unix://PATH`` : for a unix domain socket.
|
||||||
|
* ``HOST:PORT`` : for a network address
|
||||||
|
|
||||||
.. versionadded:: 19.1
|
.. versionadded:: 19.1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class StatsdSocket(Setting):
|
|
||||||
name = "statsd_socket"
|
|
||||||
section = "Logging"
|
|
||||||
cli = ["--statsd-socket"]
|
|
||||||
meta = "STATSD_SOCKET"
|
|
||||||
default = None
|
|
||||||
validator = validate_string
|
|
||||||
desc = """\
|
|
||||||
Unix domain socket of the statsd server to log to.
|
|
||||||
Supersedes ``statsd_host`` if provided.
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Datadog Statsd (dogstatsd) tags. https://docs.datadoghq.com/developers/dogstatsd/
|
# Datadog Statsd (dogstatsd) tags. https://docs.datadoghq.com/developers/dogstatsd/
|
||||||
class DogstatsdTags(Setting):
|
class DogstatsdTags(Setting):
|
||||||
name = "dogstatsd_tags"
|
name = "dogstatsd_tags"
|
||||||
|
|||||||
@ -23,22 +23,17 @@ class Statsd(Logger):
|
|||||||
"""statsD-based instrumentation, that passes as a logger
|
"""statsD-based instrumentation, that passes as a logger
|
||||||
"""
|
"""
|
||||||
def __init__(self, cfg):
|
def __init__(self, cfg):
|
||||||
"""host, port: statsD server
|
|
||||||
"""
|
|
||||||
Logger.__init__(self, cfg)
|
Logger.__init__(self, cfg)
|
||||||
self.prefix = sub(r"^(.+[^.]+)\.*$", "\\g<1>.", cfg.statsd_prefix)
|
self.prefix = sub(r"^(.+[^.]+)\.*$", "\\g<1>.", cfg.statsd_prefix)
|
||||||
|
|
||||||
if cfg.statsd_socket:
|
if isinstance(cfg.statsd_host, str):
|
||||||
address_family = socket.AF_UNIX
|
address_family = socket.AF_UNIX
|
||||||
address = cfg.statsd_socket
|
else:
|
||||||
elif cfg.statsd_host:
|
|
||||||
address_family = socket.AF_INET
|
address_family = socket.AF_INET
|
||||||
host, port = cfg.statsd_host
|
|
||||||
address = (host, int(port))
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.sock = socket.socket(address_family, socket.SOCK_DGRAM)
|
self.sock = socket.socket(address_family, socket.SOCK_DGRAM)
|
||||||
self.sock.connect(address)
|
self.sock.connect(cfg.statsd_host)
|
||||||
except Exception:
|
except Exception:
|
||||||
self.sock = None
|
self.sock = None
|
||||||
|
|
||||||
|
|||||||
@ -306,20 +306,26 @@ def test_nworkers_changed():
|
|||||||
assert c.nworkers_changed(1, 2, 3) == 3
|
assert c.nworkers_changed(1, 2, 3) == 3
|
||||||
|
|
||||||
|
|
||||||
def test_statsd_host_changes_logger():
|
def test_statsd_host():
|
||||||
|
c = config.Config()
|
||||||
|
assert c.statsd_host is None
|
||||||
|
c.set("statsd_host", "localhost")
|
||||||
|
assert c.statsd_host == ("localhost", 8125)
|
||||||
|
c.set("statsd_host", "statsd:7777")
|
||||||
|
assert c.statsd_host == ("statsd", 7777)
|
||||||
|
c.set("statsd_host", "unix:/path/to.sock")
|
||||||
|
assert c.statsd_host == "/path/to.sock"
|
||||||
|
pytest.raises(TypeError, c.set, "statsd_host", 666)
|
||||||
|
pytest.raises(TypeError, c.set, "statsd_host", "host:string")
|
||||||
|
|
||||||
|
|
||||||
|
def test_statsd_changes_logger():
|
||||||
c = config.Config()
|
c = config.Config()
|
||||||
assert c.logger_class == glogging.Logger
|
assert c.logger_class == glogging.Logger
|
||||||
c.set('statsd_host', 'localhost:12345')
|
c.set('statsd_host', 'localhost:12345')
|
||||||
assert c.logger_class == statsd.Statsd
|
assert c.logger_class == statsd.Statsd
|
||||||
|
|
||||||
|
|
||||||
def test_statsd_socket_changes_logger():
|
|
||||||
c = config.Config()
|
|
||||||
assert c.logger_class == glogging.Logger
|
|
||||||
c.set('statsd_socket', '/var/run/sock')
|
|
||||||
assert c.logger_class == statsd.Statsd
|
|
||||||
|
|
||||||
|
|
||||||
class MyLogger(glogging.Logger):
|
class MyLogger(glogging.Logger):
|
||||||
# dummy custom logger class for testing
|
# dummy custom logger class for testing
|
||||||
pass
|
pass
|
||||||
|
|||||||
@ -59,6 +59,18 @@ def test_statsd_fail():
|
|||||||
logger.exception("No impact on logging")
|
logger.exception("No impact on logging")
|
||||||
|
|
||||||
|
|
||||||
|
def test_statsd_host_initialization():
|
||||||
|
c = Config()
|
||||||
|
c.set('statsd_host', 'unix:test.sock')
|
||||||
|
logger = Statsd(c)
|
||||||
|
logger.info("Can be initialized and used with a UDS socket")
|
||||||
|
|
||||||
|
# Can be initialized and used with a UDP address
|
||||||
|
c.set('statsd_host', 'host:8080')
|
||||||
|
logger = Statsd(c)
|
||||||
|
logger.info("Can be initialized and used with a UDP socket")
|
||||||
|
|
||||||
|
|
||||||
def test_dogstatsd_tags():
|
def test_dogstatsd_tags():
|
||||||
c = Config()
|
c = Config()
|
||||||
tags = 'yucatan,libertine:rhubarb'
|
tags = 'yucatan,libertine:rhubarb'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user