|
| 1 | +import { Registry, Counter, Gauge } from "prom-client"; |
| 2 | +import express from "express"; |
| 3 | +import { PriceInfo } from "./interface"; |
| 4 | +import { Logger } from "pino"; |
| 5 | +import { UpdateCondition } from "./price-config"; |
| 6 | + |
| 7 | +// Define the metrics we want to track |
| 8 | +export class PricePusherMetrics { |
| 9 | + private registry: Registry; |
| 10 | + private server: express.Express; |
| 11 | + private logger: Logger; |
| 12 | + |
| 13 | + // Metrics for price feed updates |
| 14 | + public lastPublishedTime: Gauge<string>; |
| 15 | + public priceUpdateAttempts: Counter<string>; |
| 16 | + public priceFeedsTotal: Gauge<string>; |
| 17 | + // Wallet metrics |
| 18 | + public walletBalance: Gauge<string>; |
| 19 | + |
| 20 | + constructor(logger: Logger) { |
| 21 | + this.logger = logger; |
| 22 | + this.registry = new Registry(); |
| 23 | + this.server = express(); |
| 24 | + |
| 25 | + // Register the default metrics (memory, CPU, etc.) |
| 26 | + this.registry.setDefaultLabels({ app: "price_pusher" }); |
| 27 | + |
| 28 | + // Create metrics |
| 29 | + this.lastPublishedTime = new Gauge({ |
| 30 | + name: "pyth_price_last_published_time", |
| 31 | + help: "The last published time of a price feed in unix timestamp", |
| 32 | + labelNames: ["price_id", "alias"], |
| 33 | + registers: [this.registry], |
| 34 | + }); |
| 35 | + |
| 36 | + this.priceUpdateAttempts = new Counter({ |
| 37 | + name: "pyth_price_update_attempts_total", |
| 38 | + help: "Total number of price update attempts with their trigger condition and status", |
| 39 | + labelNames: ["price_id", "alias", "trigger", "status"], |
| 40 | + registers: [this.registry], |
| 41 | + }); |
| 42 | + |
| 43 | + this.priceFeedsTotal = new Gauge({ |
| 44 | + name: "pyth_price_feeds_total", |
| 45 | + help: "Total number of price feeds being monitored", |
| 46 | + registers: [this.registry], |
| 47 | + }); |
| 48 | + |
| 49 | + // Wallet balance metric |
| 50 | + this.walletBalance = new Gauge({ |
| 51 | + name: "pyth_wallet_balance", |
| 52 | + help: "Current wallet balance of the price pusher in native token units", |
| 53 | + labelNames: ["wallet_address", "network"], |
| 54 | + registers: [this.registry], |
| 55 | + }); |
| 56 | + |
| 57 | + // Setup the metrics endpoint |
| 58 | + this.server.get("/metrics", async (req, res) => { |
| 59 | + res.set("Content-Type", this.registry.contentType); |
| 60 | + res.end(await this.registry.metrics()); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + // Start the metrics server |
| 65 | + public start(port: number): void { |
| 66 | + this.server.listen(port, () => { |
| 67 | + this.logger.info(`Metrics server started on port ${port}`); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + // Update the last published time for a price feed |
| 72 | + public updateLastPublishedTime( |
| 73 | + priceId: string, |
| 74 | + alias: string, |
| 75 | + priceInfo: PriceInfo, |
| 76 | + ): void { |
| 77 | + this.lastPublishedTime.set( |
| 78 | + { price_id: priceId, alias }, |
| 79 | + priceInfo.publishTime, |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + // Record a successful price update |
| 84 | + public recordPriceUpdate( |
| 85 | + priceId: string, |
| 86 | + alias: string, |
| 87 | + trigger: string = "yes", |
| 88 | + ): void { |
| 89 | + this.priceUpdateAttempts.inc({ |
| 90 | + price_id: priceId, |
| 91 | + alias, |
| 92 | + trigger: trigger.toLowerCase(), |
| 93 | + status: "success", |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + // Record update condition status (YES/NO/EARLY) |
| 98 | + public recordUpdateCondition( |
| 99 | + priceId: string, |
| 100 | + alias: string, |
| 101 | + condition: UpdateCondition, |
| 102 | + ): void { |
| 103 | + const triggerLabel = UpdateCondition[condition].toLowerCase(); |
| 104 | + // Only record as 'skipped' when the condition is NO |
| 105 | + if (condition === UpdateCondition.NO) { |
| 106 | + this.priceUpdateAttempts.inc({ |
| 107 | + price_id: priceId, |
| 108 | + alias, |
| 109 | + trigger: triggerLabel, |
| 110 | + status: "skipped", |
| 111 | + }); |
| 112 | + } |
| 113 | + // YES and EARLY don't increment the counter here - they'll be counted |
| 114 | + // when recordPriceUpdate or recordPriceUpdateError is called |
| 115 | + } |
| 116 | + |
| 117 | + // Record a price update error |
| 118 | + public recordPriceUpdateError( |
| 119 | + priceId: string, |
| 120 | + alias: string, |
| 121 | + trigger: string = "yes", |
| 122 | + ): void { |
| 123 | + this.priceUpdateAttempts.inc({ |
| 124 | + price_id: priceId, |
| 125 | + alias, |
| 126 | + trigger: trigger.toLowerCase(), |
| 127 | + status: "error", |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + // Set the number of price feeds |
| 132 | + public setPriceFeedsTotal(count: number): void { |
| 133 | + this.priceFeedsTotal.set(count); |
| 134 | + } |
| 135 | + |
| 136 | + // Update wallet balance |
| 137 | + public updateWalletBalance( |
| 138 | + walletAddress: string, |
| 139 | + network: string, |
| 140 | + balance: bigint | number, |
| 141 | + ): void { |
| 142 | + // Convert to number for compatibility with prometheus |
| 143 | + const balanceNum = |
| 144 | + typeof balance === "bigint" ? Number(balance) / 1e18 : balance; |
| 145 | + this.walletBalance.set( |
| 146 | + { wallet_address: walletAddress, network }, |
| 147 | + balanceNum, |
| 148 | + ); |
| 149 | + this.logger.debug( |
| 150 | + `Updated wallet balance metric: ${walletAddress} = ${balanceNum}`, |
| 151 | + ); |
| 152 | + } |
| 153 | +} |
0 commit comments