|
| 1 | +from prometheus_client import Counter, Gauge, start_http_server |
| 2 | + |
| 3 | +# Identity address set once at startup via setup() |
| 4 | +identity_address: str = "" |
| 5 | + |
| 6 | + |
| 7 | +def setup(ia: str) -> None: |
| 8 | + global identity_address |
| 9 | + identity_address = ia |
| 10 | + |
| 11 | + |
| 12 | +# --------------------------------------------------------------------------- |
| 13 | +# State |
| 14 | +# --------------------------------------------------------------------------- |
| 15 | + |
| 16 | +VOTING_ROUND = Gauge( |
| 17 | + "flare_fsp_voting_round_current", |
| 18 | + "Current voting round ID", |
| 19 | +) |
| 20 | + |
| 21 | +REWARD_EPOCH = Gauge( |
| 22 | + "flare_fsp_reward_epoch_current", |
| 23 | + "Current reward epoch ID", |
| 24 | +) |
| 25 | + |
| 26 | +REGISTERED_CURRENT_EPOCH = Gauge( |
| 27 | + "flare_fsp_registered_current_epoch", |
| 28 | + "Whether the entity is in the active signing policy for the current epoch (0 or 1)", |
| 29 | + ["identity_address"], |
| 30 | +) |
| 31 | + |
| 32 | +REGISTERED_NEXT_EPOCH = Gauge( |
| 33 | + "flare_fsp_registered_next_epoch", |
| 34 | + "Whether the entity has registered for the next epoch (0 or 1)", |
| 35 | + ["identity_address"], |
| 36 | +) |
| 37 | + |
| 38 | +# --------------------------------------------------------------------------- |
| 39 | +# Submissions — Counters per protocol (ftso, fdc) and phase |
| 40 | +# (submit1, submit2, signatures) |
| 41 | +# --------------------------------------------------------------------------- |
| 42 | + |
| 43 | +SUBMIT_OK = Counter( |
| 44 | + "flare_fsp_submit_ok_total", |
| 45 | + "Total rounds where submission was present and valid", |
| 46 | + ["identity_address", "protocol", "phase"], |
| 47 | +) |
| 48 | + |
| 49 | +SUBMIT_MISSING = Counter( |
| 50 | + "flare_fsp_submit_missing_total", |
| 51 | + "Total rounds where submission was absent", |
| 52 | + ["identity_address", "protocol", "phase"], |
| 53 | +) |
| 54 | + |
| 55 | +SUBMIT_LATE = Counter( |
| 56 | + "flare_fsp_submit_late_total", |
| 57 | + "Total rounds where submission was sent after the allowed window", |
| 58 | + ["identity_address", "protocol", "phase"], |
| 59 | +) |
| 60 | + |
| 61 | +SUBMIT_EARLY = Counter( |
| 62 | + "flare_fsp_submit_early_total", |
| 63 | + "Total rounds where submission was sent before the allowed window", |
| 64 | + ["identity_address", "protocol", "phase"], |
| 65 | +) |
| 66 | + |
| 67 | +# --------------------------------------------------------------------------- |
| 68 | +# Minimal conditions |
| 69 | +# --------------------------------------------------------------------------- |
| 70 | + |
| 71 | +FTSO_ANCHOR_FEEDS_SUCCESS_RATE = Gauge( |
| 72 | + "flare_fsp_ftso_anchor_feeds_success_rate_bips", |
| 73 | + "FTSO anchor feeds success rate in bips (0-10000) over the last 2 hours", |
| 74 | + ["identity_address"], |
| 75 | +) |
| 76 | + |
| 77 | +FAST_UPDATE_BLOCKS_SINCE_LAST = Gauge( |
| 78 | + "flare_fsp_fast_update_blocks_since_last", |
| 79 | + "Number of blocks elapsed since the last fast update submission", |
| 80 | + ["identity_address"], |
| 81 | +) |
| 82 | + |
| 83 | +NODE_UPTIME_RATIO = Gauge( |
| 84 | + "flare_fsp_node_uptime_ratio", |
| 85 | + "Node uptime ratio over the sliding window (0.0 to 1.0)", |
| 86 | + ["identity_address", "node_id"], |
| 87 | +) |
| 88 | + |
| 89 | +FDC_PARTICIPATION_RATE = Gauge( |
| 90 | + "flare_fsp_fdc_participation_rate_bips", |
| 91 | + "FDC participation rate in bips (0-10000) over the last 2 hours", |
| 92 | + ["identity_address"], |
| 93 | +) |
| 94 | + |
| 95 | +# --------------------------------------------------------------------------- |
| 96 | +# Balance |
| 97 | +# --------------------------------------------------------------------------- |
| 98 | + |
| 99 | +ADDRESS_BALANCE = Gauge( |
| 100 | + "flare_fsp_address_balance_wei", |
| 101 | + "Address balance in wei", |
| 102 | + ["identity_address", "address", "role"], |
| 103 | +) |
| 104 | + |
| 105 | +# --------------------------------------------------------------------------- |
| 106 | +# Validation issues |
| 107 | +# --------------------------------------------------------------------------- |
| 108 | + |
| 109 | +REVEAL_OFFENCE = Counter( |
| 110 | + "flare_fsp_reveal_offence_total", |
| 111 | + "Total rounds where a reveal offence occurred" |
| 112 | + " (missing reveal after commit, or hash mismatch)", |
| 113 | + ["identity_address", "protocol"], |
| 114 | +) |
| 115 | + |
| 116 | +SIGNATURE_GRACE_PERIOD_MISSED = Counter( |
| 117 | + "flare_fsp_signature_grace_period_missed_total", |
| 118 | + "Total rounds where submitSignatures was sent after the grace period deadline", |
| 119 | + ["identity_address", "protocol"], |
| 120 | +) |
| 121 | + |
| 122 | +SIGNATURE_MISMATCH = Counter( |
| 123 | + "flare_fsp_signature_mismatch_total", |
| 124 | + "Total rounds where submitSignatures signature did not match finalization", |
| 125 | + ["identity_address", "protocol"], |
| 126 | +) |
| 127 | + |
| 128 | +# --------------------------------------------------------------------------- |
| 129 | +# Contract address issues |
| 130 | +# --------------------------------------------------------------------------- |
| 131 | + |
| 132 | +CONTRACT_ADDRESS_WRONG = Counter( |
| 133 | + "flare_fsp_contract_address_wrong_total", |
| 134 | + "Total times a wrong contract address was detected (submission or relay)", |
| 135 | + ["identity_address", "contract"], |
| 136 | +) |
| 137 | + |
| 138 | +# --------------------------------------------------------------------------- |
| 139 | +# Unclaimed rewards |
| 140 | +# --------------------------------------------------------------------------- |
| 141 | + |
| 142 | +UNCLAIMED_REWARDS = Gauge( |
| 143 | + "flare_fsp_unclaimed_rewards_wei", |
| 144 | + "Unclaimed reward amount in wei per address/epoch/claim_type", |
| 145 | + ["identity_address", "address", "reward_epoch", "claim_type"], |
| 146 | +) |
| 147 | + |
| 148 | + |
| 149 | +def initialize_labels(node_ids: list[str] | None = None) -> None: |
| 150 | + """Pre-initialize all label combinations so time series appear immediately at 0.""" |
| 151 | + assert identity_address, "metrics.setup() must be called before initialize_labels()" |
| 152 | + for protocol, phases in [ |
| 153 | + ("ftso", ["submit1", "submit2", "signatures"]), |
| 154 | + ("fdc", ["submit2", "signatures"]), |
| 155 | + ]: |
| 156 | + for phase in phases: |
| 157 | + SUBMIT_OK.labels( |
| 158 | + identity_address=identity_address, protocol=protocol, phase=phase |
| 159 | + ) |
| 160 | + SUBMIT_MISSING.labels( |
| 161 | + identity_address=identity_address, protocol=protocol, phase=phase |
| 162 | + ) |
| 163 | + SUBMIT_LATE.labels( |
| 164 | + identity_address=identity_address, protocol=protocol, phase=phase |
| 165 | + ) |
| 166 | + SUBMIT_EARLY.labels( |
| 167 | + identity_address=identity_address, protocol=protocol, phase=phase |
| 168 | + ) |
| 169 | + |
| 170 | + REVEAL_OFFENCE.labels(identity_address=identity_address, protocol=protocol) |
| 171 | + SIGNATURE_GRACE_PERIOD_MISSED.labels( |
| 172 | + identity_address=identity_address, protocol=protocol |
| 173 | + ) |
| 174 | + SIGNATURE_MISMATCH.labels(identity_address=identity_address, protocol=protocol) |
| 175 | + |
| 176 | + for contract in ["submission", "relay"]: |
| 177 | + CONTRACT_ADDRESS_WRONG.labels( |
| 178 | + identity_address=identity_address, contract=contract |
| 179 | + ) |
| 180 | + |
| 181 | + FAST_UPDATE_BLOCKS_SINCE_LAST.labels(identity_address=identity_address).set(0) |
| 182 | + |
| 183 | + if node_ids: |
| 184 | + for node_id in node_ids: |
| 185 | + NODE_UPTIME_RATIO.labels( |
| 186 | + identity_address=identity_address, node_id=node_id |
| 187 | + ).set(0) |
| 188 | + |
| 189 | + |
| 190 | +def start_metrics_server(port: int, address: str = "0.0.0.0") -> None: |
| 191 | + start_http_server(port, addr=address) |
0 commit comments