Skip to content

Commit

Permalink
Merge branch 'add-service-list' into 'master'
Browse files Browse the repository at this point in the history
Add metric for the full service list

See merge request ix.ai/cioban!87
  • Loading branch information
tlex committed Apr 2, 2024
2 parents c2ac6d4 + 3bc676b commit b26ef61
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ENV CRYPTOGRAPHY_DONT_BUILD_RUST="1"
RUN set -xeu; \
mkdir -p /work/wheels; \
apk add \
py3-pip \
python3-dev \
openssl-dev \
gcc \
Expand All @@ -16,8 +17,7 @@ RUN set -xeu; \
openssl-dev \
cargo \
; \
python3 -m ensurepip; \
pip3 install -U \
pip3 install -U --break-system-packages \
wheel \
pip

Expand All @@ -32,13 +32,13 @@ COPY --from=builder /work /

RUN set -xeu; \
ls -lashi /wheels; \
apk add --no-cache python3; \
python3 -m ensurepip; \
pip3 install --no-cache-dir -U pip;\
apk add --no-cache py3-pip; \
pip3 install --no-cache-dir --break-system-packages -U pip;\
pip3 install \
--no-index \
--no-cache-dir \
--find-links /wheels \
--break-system-packages \
--requirement /cioban/requirements.txt \
; \
rm -rf /wheels
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,12 @@ update_run_seconds_sum 43.92592599400086
update_run_seconds_created 1.5672812321329722e+09
# HELP service_updated_total Shows if a service has been updated
# TYPE service_updated_total counter
service_updated_total{id="pqs6wtscm1tq6yiqrmu4wv0of",name="smtp_smtp",short_id="pqs6wtscm1"} 1.0
service_updated_total{id="2pg5mnnwt7ged4klus6x88qm1",name="smtp_smtp"} 1.0
# TYPE service_updated_created gauge
service_updated_created{id="pqs6wtscm1tq6yiqrmu4wv0of",name="smtp_smtp",short_id="pqs6wtscm1"} 1.567281276077023e+09
service_updated_created{id="2pg5mnnwt7ged4klus6x88qm1",name="smtp_smtp"} 1.567281276077023e+09
# HELP service_info Information about a service
# TYPE service_info gauge
service_info{id="2pg5mnnwt7ged4klus6x88qm1",image_name="ghcr.io/ix-ai/smtp:latest",image_sha256="73629c8a2e0896d4591b6b3e884eb17bac14007a2352d9e977cf5706a5c33a9a",name="smtp_smtp",short_id="2pg5mnnwt7ge"} 1.0
# HELP cioban_info Information about cioban
# TYPE cioban_info gauge
cioban_info{version="0.7.0"} 1.0
Expand Down
12 changes: 10 additions & 2 deletions cioban/cioban.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def run(self):
start_http_server(self.settings['prometheus_port']) # starts the prometheus metrics server
log.info(f"Listening on port {self.settings['prometheus_port']}")
while True:
self.get_services()
if self.settings['schedule_time']:
self.__set_timer()
log.info(f'Sleeping for {self.sleep} {self.sleep_type}')
Expand Down Expand Up @@ -174,7 +175,7 @@ def _run(self):
service_name = service.name
image_with_digest = service.attrs['Spec']['TaskTemplate']['ContainerSpec']['Image']
image, image_sha = self.__get_image_parts(image_with_digest)
prometheus.PROM_SVC_UPDATE_COUNTER.labels(service_name, service.id, service.short_id, image).inc(0)
prometheus.PROM_SVC_UPDATE_COUNTER.labels(service_name, service.id).inc(0)
update_image = self.__get_updated_image(image_sha=image_sha, image=image)
service_updated = False
if update_image:
Expand All @@ -198,7 +199,7 @@ def _run(self):
updating = False

if service_updated:
prometheus.PROM_SVC_UPDATE_COUNTER.labels(service_name, service.id, service.short_id, image).inc(1)
prometheus.PROM_SVC_UPDATE_COUNTER.labels(service_name, service.id).inc(1)
notify = {
'service_name': service_name,
'service_short_id': service.short_id,
Expand All @@ -217,6 +218,13 @@ def _run(self):

def get_services(self):
""" gets the list of services and filters out the black listed """

# first call to get the full list
# this will allow us to set a metric `service_info`
for service in self.docker.services.list():
image = service.attrs['Spec']['TaskTemplate']['ContainerSpec']['Image'].split('@sha256:')
prometheus.PROM_SVC_INFO.labels(service.name,service.id,service.short_id,image[0],image[1]).set(1)

services = self.docker.services.list(filters=self.settings['filter_services'])
for blacklist_service in self.settings['blacklist_services']:
for service in services:
Expand Down
15 changes: 13 additions & 2 deletions cioban/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import logging
import os
# pylint: disable=deprecated-module
from distutils.util import strtobool

log = logging.getLogger("cioban")

Expand Down Expand Up @@ -62,3 +60,16 @@ def gather_environ(keys=None) -> dict:
def short_msg(msg: str, chars=150) -> str:
""" Truncates the message to {chars} characters and adds three dots at the end """
return (str(msg)[:chars] + '..') if len(str(msg)) > chars else str(msg)

def strtobool(val):
"""Convert a string representation of truth to true (1) or false (0).
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
'val' is anything else.
"""
val = val.lower()
if val in ('y', 'yes', 't', 'true', 'on', '1'):
return 1
if val in ('n', 'no', 'f', 'false', 'off', '0'):
return 0
raise ValueError(f"invalid truth value {(val,)}")
11 changes: 9 additions & 2 deletions cioban/lib/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
# -*- coding: utf-8 -*-
""" Initializes the prometheus metrics """

from prometheus_client import Summary, Counter, Info, Enum
from prometheus_client import Summary, Counter, Info, Enum, Gauge

# Prometheus metrics
PROM_UPDATE_SUMMARY = Summary('update_run_seconds', 'Time spent processing updates')
PROM_SVC_UPDATE_COUNTER = Counter(
'service_updated', 'Shows if a service has been updated', [
'name',
'id'
]
)
PROM_SVC_INFO = Gauge(
'service_info', 'Information about a service', [
'name',
'id',
'short_id',
'image'
'image_name',
'image_sha256'
]
)
PROM_INFO = Info('cioban', 'Information about cioban')
Expand Down
6 changes: 3 additions & 3 deletions cioban/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
prometheus_client==0.17.1
docker==6.1.3
prometheus_client==0.20.0
docker==7.0.0
pause==0.3
pygelf==0.4.2
ix-notifiers==0.0.259196408
cronsim==2.5
python-dateutil==2.8.2
python-dateutil==2.9.0
setuptools

0 comments on commit b26ef61

Please sign in to comment.