Skip to content

Commit

Permalink
bug-1898341: change HOST_ID to HOSTNAME and add host tag
Browse files Browse the repository at this point in the history
This changes HOST_ID to HOSTNAME to match the convention and what we get
in GCP.

If the hostname is set, this will add a "host:{hostname}" tag to all
emitted metrics.
  • Loading branch information
willkg committed May 23, 2024
1 parent bdfb721 commit 5bdc213
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
17 changes: 10 additions & 7 deletions antenna/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import logging.config
from pathlib import Path
import socket
import sys

from everett.manager import (
Expand Down Expand Up @@ -69,7 +70,7 @@ def configure_sentry(app_config):
set_up_sentry(
sentry_dsn=app_config("secret_sentry_dsn"),
release=get_release_name(app_config("basedir")),
host_id=app_config("host_id"),
host_id=app_config("hostname"),
# Disable frame-local variables
with_locals=False,
# Disable request data from being added to Sentry events
Expand Down Expand Up @@ -132,14 +133,15 @@ class Config:
"will be used instead."
),
)
host_id = Option(
default="",
hostname = Option(
default=socket.gethostname(),
doc=(
"Identifier for the host that is running Antenna. This identifies this Antenna "
"instance in the logs and makes it easier to correlate Antenna logs with "
"other data. For example, the value could be a public hostname, an instance id, "
"or something like that. If you do not set this, then socket.gethostname() is "
"used instead."
"other data. For example, the value could be a an instance id, pod id, "
"or something like that.\n"
"\n"
"If you do not set this, then ``socket.gethostname()`` is used instead."
),
)

Expand Down Expand Up @@ -211,6 +213,7 @@ def setup(self):
setup_metrics(
statsd_host=self.config("statsd_host"),
statsd_port=self.config("statsd_port"),
hostname=self.config("hostname"),
debug=self.config("local_dev_env"),
)

Expand Down Expand Up @@ -281,7 +284,7 @@ def get_app(config_manager=None):
setup_logging(
logging_level=app_config("logging_level"),
debug=app_config("local_dev_env"),
host_id=app_config("host_id"),
host_id=app_config("hostname"),
processname="antenna",
)

Expand Down
14 changes: 9 additions & 5 deletions antenna/libmarkus.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@
import logging

import markus
from markus.filters import AddTagFilter


_IS_MARKUS_SETUP = False

LOGGER = logging.getLogger(__name__)
METRICS = markus.get_metrics()


def setup_metrics(statsd_host, statsd_port, debug=False):
def setup_metrics(statsd_host, statsd_port, hostname, debug=False):
"""Initialize and configures the metrics system.
:arg statsd_host: the statsd host to send metrics to
:arg statsd_port: the port on the host to send metrics to
:arg hostname: the host name
:arg debug: whether or not to additionally log metrics to the logger
"""
global _IS_MARKUS_SETUP
global _IS_MARKUS_SETUP, METRICS
if _IS_MARKUS_SETUP:
return

Expand All @@ -45,9 +48,10 @@ def setup_metrics(statsd_host, statsd_port, debug=False):
},
}
)
markus.configure(markus_backends)

_IS_MARKUS_SETUP = True
if hostname:
METRICS.filters.append(AddTagFilter(f"host:{hostname}"))

markus.configure(markus_backends)

METRICS = markus.get_metrics()
_IS_MARKUS_SETUP = True
2 changes: 1 addition & 1 deletion tests/unittest/test_sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"packages": [{"name": "pypi:sentry-sdk", "version": ANY}],
"version": ANY,
},
"server_name": "",
"server_name": ANY,
"timestamp": ANY,
"transaction": "/__broken__",
"transaction_info": {"source": "route"},
Expand Down

0 comments on commit 5bdc213

Please sign in to comment.