Skip to content

Commit

Permalink
metrics: init all centre-ids, add connetion metric to registrar
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Mar 28, 2024
1 parent 586efdb commit 98f8d8d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
11 changes: 11 additions & 0 deletions wis2-gdc-management/wis2_gdc/registrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ def get_wcmp2(self, wnm: dict) -> dict:
raise

LOGGER.debug(f'Fetching {wcmp2_url}')

try:
r = requests.get(wcmp2_url)
r.raise_for_status()
return r.json()
except requests.exceptions.HTTPError as err:
LOGGER.warning(err)
self._process_record_metric(
self.metadata['id'], 'downloaded_errors_total',
[BROKER_URL, CENTRE_ID])

return requests.get(wcmp2_url).json()

def register(self, metadata: dict) -> None:
Expand Down
7 changes: 6 additions & 1 deletion wis2-gdc-metrics-collector/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ FROM python:3-alpine
COPY ./metrics_collector.py /app/metrics_collector.py
COPY ./requirements.txt /app/requirements.txt

RUN pip install -r /app/requirements.txt
RUN pip install -r /app/requirements.txt && \
mkdir -p /data && \
wget -P /data https://wmo-im.github.io/wis2-topic-hierarchy/wth-bundle.zip && \
cd /data && \
unzip /data/wth-bundle.zip

ENV WIS2_GDC_CENTRE_ID_CSV="/data/centre-id.csv"
ENV PYTHONUNBUFFERED="true"

ENTRYPOINT [ "python3","-u","/app/metrics_collector.py" ]
48 changes: 42 additions & 6 deletions wis2-gdc-metrics-collector/metrics_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#
###############################################################################

import csv
import json
import logging
import os
Expand All @@ -38,6 +39,7 @@
API_URL = os.environ['WIS2_GDC_API_URL']
BROKER_URL = os.environ['WIS2_GDC_BROKER_URL']
CENTRE_ID = os.environ['WIS2_GDC_CENTRE_ID']
CENTRE_ID_CSV = os.environ['WIS2_GDC_CENTRE_ID_CSV']
GB = urlparse(os.environ['WIS2_GDC_GB'])
GB_TOPIC = os.environ['WIS2_GDC_GB_TOPIC']
HTTP_PORT = 8006
Expand Down Expand Up @@ -73,7 +75,7 @@
)

METRIC_RECOMMENDED_TOTAL = Counter(
'wmo_wis2_gdc_recommendedcore_total',
'wmo_wis2_gdc_recommended_total',
'Number of recommended metadata records',
['centre_id', 'report_by']
)
Expand Down Expand Up @@ -108,11 +110,44 @@
['top', 'centre_id', 'report_by']
)

METRIC_INFO.info({
'centre_id': CENTRE_ID,
'url': API_URL,
'subscribed_to': f'{GB.scheme}://{GB.hostname}:{GB.port} (topic: {GB_TOPIC})' # noqa
})
METRIC_CONNECTED_FLAG = Gauge(
'wmo_wis2_gdc_connected_flag',
'Connection status from GDC to to centre',
['centre_id', 'report_by']
)

METRIC_DOWNLOADED_ERRORS_TOTAL = Gauge(
'wmo_wis2_gdc_downloaded_errors_total',
'Number of metadata download errors',
['centre_id', 'report_by']
)


def init_metrics() -> None:
"""
Initializes metrics on startup
:returns: `None`
"""

METRIC_INFO.info({
'centre_id': CENTRE_ID,
'url': API_URL,
'subscribed_to': f'{GB.scheme}://{GB.hostname}:{GB.port} (topic: {GB_TOPIC})' # noqa
})

METRIC_CONNECTED_FLAG.labels(
centre_id=GB.hostname, report_by=CENTRE_ID).inc(1)

with open(CENTRE_ID_CSV) as fh:
reader = csv.DictReader(fh)
for row in reader:
labels = [row['Name'], CENTRE_ID]

METRIC_PASSED_TOTAL.labels(*labels).inc(0)
METRIC_FAILED_TOTAL.labels(*labels).inc(0)
METRIC_CORE_TOTAL.labels(*labels).inc(0)
METRIC_RECOMMENDED_TOTAL.labels(*labels).inc(0)


def collect_metrics() -> None:
Expand Down Expand Up @@ -167,4 +202,5 @@ def _sub_message(client, userdata, msg):
if __name__ == '__main__':
LOGGER.info(f'Starting metrics collector server on port {HTTP_PORT}')
start_http_server(HTTP_PORT)
init_metrics()
collect_metrics()

0 comments on commit 98f8d8d

Please sign in to comment.