Normalize hostname for statsd

When doing the statsd reporting we need to normalize the
hostname. Otherwise if the hostname is an ip address we get scary
metrics like:

  zuul.executor.10.130.3.21.running_builds:0|g"

So we need to normalize these and replace the '.' and ':' by '_' like
we're doing that in nodepool.

Change-Id: Ide5517fb4a851f801c7ff0816d5e4f5876cc6401
This commit is contained in:
Tobias Henkel
2018-02-23 08:35:42 +01:00
parent 94c489437a
commit f176bbda24
3 changed files with 69 additions and 18 deletions

View File

@@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import copy
from zuul.lib.config import get_default
@@ -22,9 +23,51 @@ def get_statsd_config(config):
return (statsd_host, statsd_port, statsd_prefix)
def get_statsd(config):
def normalize_statsd_name(name):
name = name.replace('.', '_')
name = name.replace(':', '_')
return name
def get_statsd(config, extra_keys=None):
(statsd_host, statsd_port, statsd_prefix) = get_statsd_config(config)
if statsd_host is None:
return None
import statsd
return statsd.StatsClient(statsd_host, statsd_port, statsd_prefix)
class CustomStatsClient(statsd.StatsClient):
def __init__(self, host, port, prefix, extra=None):
self.extra_keys = copy.copy(extra) or {}
for key in self.extra_keys:
value = normalize_statsd_name(self.extra_keys[key])
self.extra_keys[key] = value
super().__init__(host, port, prefix)
def _format_stat(self, name, **keys):
format_keys = copy.copy(keys)
# we need to normalize all keys which go into the metric name
for key in format_keys.keys():
normalized_value = normalize_statsd_name(format_keys[key])
format_keys[key] = normalized_value
format_keys.update(self.extra_keys)
return name.format(**format_keys)
def gauge(self, stat, value, rate=1, delta=False, **format_keys):
stat = self._format_stat(stat, **format_keys)
super().gauge(stat, value, rate, delta)
def incr(self, stat, count=1, rate=1, **format_keys):
stat = self._format_stat(stat, **format_keys)
super().incr(stat, count, rate)
def timing(self, stat, delta, rate=1, **format_keys):
stat = self._format_stat(stat, **format_keys)
super().timing(stat, delta, rate)
return CustomStatsClient(
statsd_host, statsd_port, statsd_prefix, extra_keys)