Skip to content

Commit

Permalink
Add metrics prefix setting (#319)
Browse files Browse the repository at this point in the history
* Add metrics prefix setting

* Review fix
  • Loading branch information
evgeny-stakewise authored Apr 11, 2024
1 parent 0ba62db commit dc264ee
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/commands/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
DEFAULT_MAX_FEE_PER_GAS_GWEI,
DEFAULT_METRICS_HOST,
DEFAULT_METRICS_PORT,
DEFAULT_METRICS_PREFIX,
LOG_FORMATS,
LOG_PLAIN,
settings,
Expand Down Expand Up @@ -102,6 +103,13 @@
envvar='METRICS_HOST',
default=DEFAULT_METRICS_HOST,
)
@click.option(
'--metrics-prefix',
type=str,
help=f'The prometheus metrics prefix. Default is {DEFAULT_METRICS_PREFIX}.',
envvar='METRICS_PREFIX',
default=DEFAULT_METRICS_PREFIX,
)
@click.option(
'--metrics-port',
type=int,
Expand Down Expand Up @@ -202,6 +210,7 @@ def start(
enable_metrics: bool,
metrics_host: str,
metrics_port: int,
metrics_prefix: str,
data_dir: str,
log_level: str,
log_format: str,
Expand Down Expand Up @@ -234,6 +243,7 @@ def start(
enable_metrics=enable_metrics,
metrics_host=metrics_host,
metrics_port=metrics_port,
metrics_prefix=metrics_prefix,
network=network,
deposit_data_file=deposit_data_file,
keystores_dir=keystores_dir,
Expand Down
10 changes: 10 additions & 0 deletions src/commands/start_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
DEFAULT_MAX_FEE_PER_GAS_GWEI,
DEFAULT_METRICS_HOST,
DEFAULT_METRICS_PORT,
DEFAULT_METRICS_PREFIX,
LOG_FORMATS,
LOG_PLAIN,
settings,
Expand Down Expand Up @@ -99,6 +100,13 @@
envvar='METRICS_PORT',
default=DEFAULT_METRICS_PORT,
)
@click.option(
'--metrics-prefix',
type=str,
help=f'The prometheus metrics prefix. Default is {DEFAULT_METRICS_PREFIX}.',
envvar='METRICS_PREFIX',
default=DEFAULT_METRICS_PREFIX,
)
@click.option(
'-v',
'--verbose',
Expand Down Expand Up @@ -179,6 +187,7 @@ def start_api(
enable_metrics: bool,
metrics_host: str,
metrics_port: int,
metrics_prefix: str,
data_dir: str,
log_level: str,
log_format: str,
Expand Down Expand Up @@ -208,6 +217,7 @@ def start_api(
enable_metrics=enable_metrics,
metrics_host=metrics_host,
metrics_port=metrics_port,
metrics_prefix=metrics_prefix,
network=network,
deposit_data_file=deposit_data_file,
hot_wallet_file=hot_wallet_file,
Expand Down
47 changes: 38 additions & 9 deletions src/common/metrics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import cast

from prometheus_client import Gauge, Info, start_http_server
from sw_utils import InterruptHandler
Expand All @@ -13,22 +14,48 @@
# pylint: disable-next=too-few-public-methods
class Metrics:
def __init__(self):
self.app_version = Info('app_version', 'V3 Operator version', namespace='stakewise')
self.block_number = Gauge('block_number', 'Current block number', namespace='stakewise')
self.slot_number = Gauge('slot_number', 'Current slot number', namespace='stakewise')
self.wallet_balance = Gauge('wallet_balance', 'Current wallet balance', namespace='stakewise')
self.outdated_signatures = Gauge('outdated_signatures', 'The number of outdated signatures', namespace='stakewise')
self.stakeable_assets = Gauge('stakeable_assets', 'The amount of stakeable assets', namespace='stakewise')
self.app_version = Info(
'app_version', 'V3 Operator version', namespace=settings.metrics_prefix
)
self.block_number = Gauge(
'block_number', 'Current block number', namespace=settings.metrics_prefix
)
self.slot_number = Gauge(
'slot_number', 'Current slot number', namespace=settings.metrics_prefix
)
self.wallet_balance = Gauge(
'wallet_balance', 'Current wallet balance', namespace=settings.metrics_prefix
)
self.outdated_signatures = Gauge(
'outdated_signatures',
'The number of outdated signatures',
namespace=settings.metrics_prefix,
)
self.stakeable_assets = Gauge(
'stakeable_assets', 'The amount of stakeable assets', namespace=settings.metrics_prefix
)
self.unused_validator_keys = Gauge(
'unused_validator_keys', 'The number of unused validator keys in deposit data file', namespace='stakewise'
'unused_validator_keys',
'The number of unused validator keys in deposit data file',
namespace=settings.metrics_prefix,
)

def set_app_version(self):
self.app_version.info({'version': src.__version__})


metrics = Metrics()
metrics.set_app_version()
# pylint: disable-next=too-few-public-methods
class LazyMetrics:
def __init__(self):
self._metrics: Metrics | None = None

def __getattr__(self, item):
if self._metrics is None:
self._metrics = Metrics()
return getattr(self._metrics, item)


metrics = cast(Metrics, LazyMetrics())

logger = logging.getLogger(__name__)

Expand All @@ -40,6 +67,8 @@ async def metrics_server() -> None:

class MetricsTask(BaseTask):
async def process_block(self, interrupt_handler: InterruptHandler) -> None:
metrics.set_app_version()

chain_state = await get_chain_finalized_head()
metrics.block_number.set(await execution_client.eth.get_block_number())
metrics.slot_number.set(chain_state.consensus_block)
4 changes: 4 additions & 0 deletions src/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

DEFAULT_METRICS_HOST = '127.0.0.1'
DEFAULT_METRICS_PORT = 9100
DEFAULT_METRICS_PREFIX = 'sw_operator'

DEFAULT_API_HOST = '127.0.0.1'
DEFAULT_API_PORT = 8000
Expand Down Expand Up @@ -46,6 +47,7 @@ class Settings(metaclass=Singleton):
enable_metrics: bool
metrics_host: str
metrics_port: int
metrics_prefix: str
deposit_data_file: Path
keystores_dir: Path
keystores_password_dir: Path
Expand Down Expand Up @@ -97,6 +99,7 @@ def set(
enable_metrics: bool = False,
metrics_port: int = DEFAULT_METRICS_PORT,
metrics_host: str = DEFAULT_METRICS_HOST,
metrics_prefix: str = DEFAULT_METRICS_PREFIX,
max_fee_per_gas_gwei: int = DEFAULT_MAX_FEE_PER_GAS_GWEI,
deposit_data_file: str | None = None,
keystores_dir: str | None = None,
Expand Down Expand Up @@ -128,6 +131,7 @@ def set(
self.enable_metrics = enable_metrics
self.metrics_host = metrics_host
self.metrics_port = metrics_port
self.metrics_prefix = metrics_prefix
self.max_fee_per_gas_gwei = max_fee_per_gas_gwei

self.deposit_data_file = (
Expand Down

0 comments on commit dc264ee

Please sign in to comment.