-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug-1898341, bug-1898345: add "host" tag, fix metrics key prefix
This adds a `host` tag to emitted metrics when running in GCP. This is derived from `HOSTNAME` if it exists, otherwise it defaults to `socket.gethostname()` like our other services. This changes Sentry and logging to use `HOSTNAME` configuration variable rather than `HOST_ID`. This brings us in line with other services as we migrate to GCP. This also adds `"socorro"` prefix to all emitted keys, but only for the GCP environments. This brings keys in line with our other services. In order to do this, I had to create a singleton `METRICS` and then rework everything to use that.
- Loading branch information
Showing
27 changed files
with
283 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
"""Holds Markus utility functions and global state.""" | ||
|
||
import logging | ||
import os | ||
|
||
import markus | ||
from markus.filters import AddTagFilter | ||
|
||
|
||
_IS_MARKUS_SETUP = False | ||
|
||
# NOTE(willkg): this checks the CLOUD_PROVIDER environ directly here because this can't | ||
# import socorro.settings; this is temporary--once we finish the GCP migration, we can | ||
# remove this | ||
_CLOUD_PROVIDER = os.environ.get("CLOUD_PROVIDER", "AWS").upper() | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
# NOTE(willkg): we need to set the prefix selectively because in AWS we don't have the | ||
# "socorro" key prefix, but in GCP we want one | ||
if _CLOUD_PROVIDER == "GCP": | ||
METRICS = markus.get_metrics("socorro") | ||
else: | ||
METRICS = markus.get_metrics() | ||
|
||
|
||
def set_up_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, METRICS | ||
if _IS_MARKUS_SETUP: | ||
return | ||
|
||
markus_backends = [ | ||
{ | ||
"class": "markus.backends.datadog.DatadogMetrics", | ||
"options": { | ||
"statsd_host": statsd_host, | ||
"statsd_port": statsd_port, | ||
}, | ||
} | ||
] | ||
if debug: | ||
markus_backends.append( | ||
{ | ||
"class": "markus.backends.logging.LoggingMetrics", | ||
"options": { | ||
"logger_name": "markus", | ||
"leader": "METRICS", | ||
}, | ||
} | ||
) | ||
|
||
if _CLOUD_PROVIDER == "GCP" and hostname: | ||
METRICS.filters.append(AddTagFilter(f"host:{hostname}")) | ||
|
||
markus.configure(markus_backends) | ||
|
||
_IS_MARKUS_SETUP = True | ||
|
||
|
||
def build_prefix(*parts): | ||
new_prefix = [] | ||
for part in parts: | ||
part = part.strip() | ||
if part: | ||
new_prefix.append(part) | ||
|
||
return ".".join(parts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.