-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use prometheus to expose consistency statistics #9
Open
eachristgr
wants to merge
1
commit into
dmwm:master
Choose a base branch
from
eachristgr:prometheus
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import json, os, argparse | ||
from typing import Tuple, Dict, List, Optional | ||
|
||
from rucio.common.config import config_get | ||
from rucio.core.monitor import MetricManager | ||
|
||
PROBES_PREFIX = config_get('monitor', 'prometheus_prefix', raise_exception=False, default='') | ||
probe_metrics = MetricManager(prefix=PROBES_PREFIX) | ||
|
||
def get_prometheus_config() -> Tuple[List, str, Dict]: | ||
prom_servers = config_get('monitor', 'prometheus_servers', raise_exception=False, default='') | ||
if prom_servers != '': | ||
prom_servers = prom_servers.split(',') | ||
else: | ||
prom_servers = [] | ||
prom_prefix = config_get('monitor', 'prometheus_prefix', raise_exception=False, default='') | ||
prom_label_config = config_get('monitor', 'prometheus_labels', raise_exception=False, default=None) | ||
if prom_label_config: | ||
try: | ||
prom_labels = json.loads(prom_label_config) | ||
except ValueError: | ||
prom_labels = None | ||
else: | ||
prom_labels = None | ||
return prom_servers, prom_prefix, prom_labels | ||
|
||
class PrometheusPusher: | ||
""" | ||
A context manager to abstract the business of configuring and pushing to prometheus | ||
""" | ||
|
||
def __init__(self, prefix: "Optional[str]" = PROBES_PREFIX, job_name: "Optional[str]" = None): | ||
self.job_name = job_name | ||
self.servers, _dummy, self.labels = get_prometheus_config() | ||
self.prefix = prefix | ||
|
||
self.manager = MetricManager(prefix=self.prefix, push_gateways=self.servers) | ||
# self.manager = MetricManager(prefix=self.prefix, push_gateways=[os.getenv('PROMETHEUS_PUSHGATEWAY_ENDPOINT')]) # Used for local development | ||
|
||
def __enter__(self) -> "MetricManager": | ||
""" | ||
Return the Rucio metrics manager | ||
:return: | ||
""" | ||
return self.manager | ||
|
||
def __exit__(self, exc_type, exc_value, exc_traceback): | ||
self.manager.push_metrics_to_gw(job=self.job_name, grouping_key=self.labels) | ||
|
||
scan_jobs_dict = { | ||
'site_cmp3': ['dbdump_before', 'scanner', 'dbdump_after', 'cmp3', 'diffs', 'missing_action', 'dark_action', 'empty_action'] | ||
} | ||
|
||
|
||
if __name__ == '__main__': | ||
|
||
parser = argparse.ArgumentParser(description="Push metrics from a JSON file to prometheus") | ||
parser.add_argument("filename", type=argparse.FileType('r'), help="Path to the JSON file") | ||
parser.add_argument("scan_type", choices=list(scan_jobs_dict.keys()), help=f"Scan types. Allowed values: {list(scan_jobs_dict.keys())}") | ||
args = parser.parse_args() | ||
|
||
# Read the JSON file | ||
try: | ||
with args.filename as json_file: | ||
data = json.load(json_file) | ||
except FileNotFoundError: | ||
print(f"Error: File not found: {args.filename}") | ||
data = None | ||
except json.JSONDecodeError: | ||
print(f"Error: Invalid JSON format in file: {args.filename}") | ||
data = None | ||
|
||
# Work on stats | ||
if data: | ||
RSE = data.get('rse', '') | ||
|
||
# Push metrics relative to the scan | ||
with PrometheusPusher(job_name=args.scan_type) as manager: | ||
if data: | ||
for scan_stat_key in data: | ||
if scan_stat_key not in scan_jobs_dict[args.scan_type]: | ||
scan_stat_value = data[scan_stat_key] | ||
if isinstance(scan_stat_value, (int, float)): | ||
manager.gauge(name=".".join([scan_stat_key, '{rse}']), documentation=f'{scan_stat_key} of {args.scan_type}').labels(rse=RSE).set(scan_stat_value) | ||
|
||
# Push metrics relative to internal scan jobs | ||
for job_key in scan_jobs_dict[args.scan_type]: | ||
with PrometheusPusher(job_name=job_key) as manager: | ||
|
||
job_dict = None if not data else data.get(job_key, None) | ||
health_value = 0 if not(job_dict and job_dict.get('status', None) == "done") else 1 | ||
manager.gauge(name=".".join(['health', '{rse}']), documentation=f'Health status of {job_key} (1: Good, 0: Bad)').labels(rse=RSE).set(health_value) | ||
|
||
# Metrics are not removed from the pushgateway | ||
# If a metric is not set when a new job is updated, its value in pushgateway will become NaN | ||
# For these reasons no other checks are needed | ||
|
||
if job_dict: | ||
for stat_key in job_dict: | ||
|
||
stat_value = job_dict[stat_key] | ||
|
||
if isinstance(stat_value, (int, float)): | ||
manager.gauge(name=".".join([stat_key, '{rse}']), documentation=f'{stat_key} of {job_key}').labels(rse=RSE).set(stat_value) | ||
|
||
if job_key == 'scanner' and stat_key == 'roots': | ||
|
||
SERVER = job_dict.get('server', '') | ||
|
||
if type(stat_value) == list: | ||
for root_dict in stat_value: | ||
|
||
ROOT = root_dict.get('root', '') | ||
root_failed = 1 if root_dict.get('root_failed', None) else 0 | ||
manager.gauge(name=".".join([stat_key, 'root_failed', '{rse}', '{server}', '{root}']), documentation=f'Root_failed of {job_key} for given RSE and root (1: False, 0: True)').labels(rse=RSE, server=SERVER, root=ROOT).set(root_failed) | ||
|
||
for root_stat_key in root_dict: | ||
root_stat_value = root_dict[root_stat_key] | ||
if isinstance(root_stat_value, (int, float)): | ||
manager.gauge(name=".".join([stat_key, root_stat_key, '{rse}', '{server}', '{root}']), documentation=f'{root_stat_key} of {job_key} for given RSE and root').labels(rse=RSE, server=SERVER, root=ROOT).set(root_stat_value) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/opt/rucio/etc/rucio.cfg
already exist in the container and is created bydocker/rucio.cfg.j2
. This file can be used instead of/consistency/rucio-client.cfg
which is hard coded. This way environment variables can be used to define the appropriate configuration, that will be a bit different for integration and production.It is also noted that the information stored in
/consistency/rucio-client.cfg
was also moved todocker/rucio.cfg.j2
.