Skip to content

Commit

Permalink
Merge pull request #14 from acoustid/prometheus
Browse files Browse the repository at this point in the history
Add prometheus metrics
  • Loading branch information
lalinsky authored Aug 21, 2024
2 parents 6064cc5 + d5cc1c3 commit 617b0ec
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 77 deletions.
14 changes: 14 additions & 0 deletions mbslave/replication.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@
import configparser as ConfigParser
from contextlib import ExitStack

import prometheus_client

logger = logging.getLogger(__name__)


CURRENT_SCHEMA_SEQUENCE = prometheus_client.Gauge('mbslave_current_schema_sequence', 'Current schema sequence number')
CURRENT_REPLICATION_REQUENCE = prometheus_client.Gauge('mbslave_current_replication_sequence', 'Current replication sequence number')
PROCESSED_PACKETS = prometheus_client.Counter('mbslave_processed_packets_total', 'Total number of processed packets')


def str_to_bool(x):
return x.lower() in ('1', 'on', 'true')

Expand Down Expand Up @@ -492,16 +499,22 @@ def mbslave_sync_main(config: Config, args: argparse.Namespace) -> None:

hook_class = ReplicationHook

if args.prometheus_server_port:
prometheus_client.start_http_server(args.prometheus_server_port)

while True:
cursor = db.cursor()
cursor.execute("SELECT current_schema_sequence, current_replication_sequence FROM %s.replication_control" % config.schemas.name('musicbrainz'))
schema_seq, replication_seq = cursor.fetchone()
CURRENT_SCHEMA_SEQUENCE.set(schema_seq)
CURRENT_REPLICATION_REQUENCE.set(replication_seq)
replication_seq += 1
hook = hook_class(config, db, config)
try:
with download_packet(base_url, token, replication_seq) as packet:
try:
process_tar(packet, db, config, ignored_schemas, ignored_tables, schema_seq, replication_seq, hook)
PROCESSED_PACKETS.inc()
except MismatchedSchemaError:
if not args.keep_running:
raise
Expand Down Expand Up @@ -755,6 +768,7 @@ def main():
parser_sync = subparsers.add_parser('sync')
parser_sync.add_argument('-r', '--keep-running', dest='keep_running', action='store_true',
help='keep running until the script is explicitly terminated')
parser_sync.add_argument('--prometheus-server-port', type=int)
parser_sync.set_defaults(func=mbslave_sync_main)

parser_remap_schema = subparsers.add_parser('remap-schema')
Expand Down
93 changes: 17 additions & 76 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ authors = ["Lukáš Lalinský <[email protected]>"]
license = "MIT"

[tool.poetry.dependencies]
python = "^3.7"
python = "^3.8"
psycopg2 = "^2.9.2"
six = "^1.16.0"
prometheus-client = "^0.20.0"

[tool.poetry.dev-dependencies]
flake8 = "^5.0.0"
Expand Down

0 comments on commit 617b0ec

Please sign in to comment.