diff --git a/deluge/core/metrics.py b/deluge/core/metrics.py index a505e681e..620a3799e 100644 --- a/deluge/core/metrics.py +++ b/deluge/core/metrics.py @@ -1,17 +1,12 @@ import json import logging +import os import re -import sys from datetime import datetime, timezone -from typing import Optional from prometheus_client import Counter logger = logging.getLogger(__name__) -for handler in logger.handlers: - logger.removeHandler(handler) - -logger.addHandler(logging.StreamHandler(sys.stdout)) torrent_pieces_downloaded = Counter( name='deluge_torrent_pieces_downloaded', @@ -21,6 +16,7 @@ torrent_pieces_downloaded = Counter( NID = re.compile(r'\(([a-zA-Z0-9]+)\)') + def metric_record( node: str, name: str, @@ -32,14 +28,16 @@ def metric_record( 'timestamp': datetime.now(timezone.utc).isoformat(), 'name': name, 'value': value, - 'node': node if node is not None else '', + 'node': node, 'torrent_name': torrent_name, }) return f'>>{record}' + class Metrics: def __init__(self, core: 'deluge.core.Core'): - self.peer_id: Optional[str] = None + # This is hack, we should add it to the deluge config. + self.node_id = os.environ.get('DELUGE_NODE_ID', '') core.session.post_dht_stats() def handle_alert(self, alert): @@ -47,21 +45,12 @@ class Metrics: handler = getattr(self, f'_{alert_type}', lambda _: None) handler(alert) - def _dht_stats(self, alert): - # Since the node id is not exposed in the alert by libtorrent's Python binding, - # we need to extract the digest from the string representation. - result = NID.search(alert.message()) - if result is None: - raise Exception('Could not extract node id from DHT stats alert') - self.peer_id = result.group(1) - def _piece_finished(self, alert): logger.info( metric_record( - node=self.peer_id, + node=self.node_id, name='deluge_piece_downloaded', torrent_name=alert.torrent_name, value=alert.piece_index, ) ) - diff --git a/docker/deluge.Dockerfile b/docker/deluge.Dockerfile index 48eb9b5c0..835afef05 100644 --- a/docker/deluge.Dockerfile +++ b/docker/deluge.Dockerfile @@ -12,6 +12,7 @@ ENV DELUGE_LISTEN_PORTS=6891,6892 ENV DELUGE_DAEMON_USERNAME=user ENV DELUGE_DAEMON_PASSWORD=password ENV DELUGE_LOG_LEVEL=debug +ENV DELUGE_NODE_ID='unset' RUN mkdir -p ${DELUGE_APP} ${DELUGE_CONFIG_DIR} WORKDIR ${DELUGE_APP}