|
| 1 | +""" |
| 2 | +Metric registry using prometheus_client. |
| 3 | +
|
| 4 | +Provides pre-defined metrics for a consensus client. |
| 5 | +Exposes metrics in Prometheus text format via the /metrics endpoint. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from prometheus_client import ( |
| 11 | + CollectorRegistry, |
| 12 | + Counter, |
| 13 | + Gauge, |
| 14 | + Histogram, |
| 15 | + generate_latest, |
| 16 | +) |
| 17 | + |
| 18 | +# Create a dedicated registry for lean-spec metrics. |
| 19 | +# |
| 20 | +# Using a dedicated registry avoids pollution from default Python process metrics. |
| 21 | +REGISTRY = CollectorRegistry() |
| 22 | + |
| 23 | +# ----------------------------------------------------------------------------- |
| 24 | +# Node Information |
| 25 | +# ----------------------------------------------------------------------------- |
| 26 | + |
| 27 | +head_slot = Gauge( |
| 28 | + "lean_head_slot", |
| 29 | + "Current head slot", |
| 30 | + registry=REGISTRY, |
| 31 | +) |
| 32 | + |
| 33 | +current_slot = Gauge( |
| 34 | + "lean_current_slot", |
| 35 | + "Current time slot", |
| 36 | + registry=REGISTRY, |
| 37 | +) |
| 38 | + |
| 39 | +justified_slot = Gauge( |
| 40 | + "lean_justified_slot", |
| 41 | + "Latest justified slot", |
| 42 | + registry=REGISTRY, |
| 43 | +) |
| 44 | + |
| 45 | +finalized_slot = Gauge( |
| 46 | + "lean_finalized_slot", |
| 47 | + "Latest finalized slot", |
| 48 | + registry=REGISTRY, |
| 49 | +) |
| 50 | + |
| 51 | +validators_count = Gauge( |
| 52 | + "lean_validators_count", |
| 53 | + "Active validators", |
| 54 | + registry=REGISTRY, |
| 55 | +) |
| 56 | + |
| 57 | +# ----------------------------------------------------------------------------- |
| 58 | +# Block Processing |
| 59 | +# ----------------------------------------------------------------------------- |
| 60 | + |
| 61 | +blocks_processed = Counter( |
| 62 | + "lean_blocks_processed_total", |
| 63 | + "Total blocks processed", |
| 64 | + registry=REGISTRY, |
| 65 | +) |
| 66 | + |
| 67 | +block_processing_time = Histogram( |
| 68 | + "lean_block_processing_seconds", |
| 69 | + "Block processing duration", |
| 70 | + buckets=(0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5), |
| 71 | + registry=REGISTRY, |
| 72 | +) |
| 73 | + |
| 74 | +# ----------------------------------------------------------------------------- |
| 75 | +# Attestations |
| 76 | +# ----------------------------------------------------------------------------- |
| 77 | + |
| 78 | +attestations_received = Counter( |
| 79 | + "lean_attestations_received_total", |
| 80 | + "Total attestations received", |
| 81 | + registry=REGISTRY, |
| 82 | +) |
| 83 | + |
| 84 | +attestations_valid = Counter( |
| 85 | + "lean_attestations_valid_total", |
| 86 | + "Valid attestations", |
| 87 | + registry=REGISTRY, |
| 88 | +) |
| 89 | + |
| 90 | +attestations_invalid = Counter( |
| 91 | + "lean_attestations_invalid_total", |
| 92 | + "Invalid attestations", |
| 93 | + registry=REGISTRY, |
| 94 | +) |
| 95 | + |
| 96 | +# ----------------------------------------------------------------------------- |
| 97 | +# Network |
| 98 | +# ----------------------------------------------------------------------------- |
| 99 | + |
| 100 | +peers_connected = Gauge( |
| 101 | + "lean_peers_connected", |
| 102 | + "Connected peers", |
| 103 | + registry=REGISTRY, |
| 104 | +) |
| 105 | + |
| 106 | +# ----------------------------------------------------------------------------- |
| 107 | +# Consensus Events |
| 108 | +# ----------------------------------------------------------------------------- |
| 109 | + |
| 110 | +reorgs = Counter( |
| 111 | + "lean_reorgs_total", |
| 112 | + "Chain reorganizations", |
| 113 | + registry=REGISTRY, |
| 114 | +) |
| 115 | + |
| 116 | +# ----------------------------------------------------------------------------- |
| 117 | +# Validator Production |
| 118 | +# ----------------------------------------------------------------------------- |
| 119 | + |
| 120 | +blocks_proposed = Counter( |
| 121 | + "lean_blocks_proposed_total", |
| 122 | + "Blocks proposed by this node", |
| 123 | + registry=REGISTRY, |
| 124 | +) |
| 125 | + |
| 126 | +attestations_produced = Counter( |
| 127 | + "lean_attestations_produced_total", |
| 128 | + "Attestations produced by this node", |
| 129 | + registry=REGISTRY, |
| 130 | +) |
| 131 | + |
| 132 | + |
| 133 | +def generate_metrics() -> bytes: |
| 134 | + """ |
| 135 | + Generate Prometheus metrics output. |
| 136 | +
|
| 137 | + Returns: |
| 138 | + Prometheus text format output as bytes. |
| 139 | + """ |
| 140 | + return generate_latest(REGISTRY) |
0 commit comments