|
| 1 | +import { Counter, Gauge, Histogram, Registry } from "prom-client"; |
| 2 | + |
| 3 | +export interface ConsumerPoolMetricsOptions { |
| 4 | + register?: Registry; |
| 5 | + prefix?: string; |
| 6 | +} |
| 7 | + |
| 8 | +export class ConsumerPoolMetrics { |
| 9 | + private readonly register: Registry; |
| 10 | + private readonly prefix: string; |
| 11 | + |
| 12 | + // Current state metrics |
| 13 | + public readonly consumerCount: Gauge; |
| 14 | + public readonly queueLength: Gauge; |
| 15 | + public readonly smoothedQueueLength: Gauge; |
| 16 | + public readonly targetConsumerCount: Gauge; |
| 17 | + public readonly scalingStrategy: Gauge; |
| 18 | + |
| 19 | + // Scaling operation metrics |
| 20 | + public readonly scalingOperationsTotal: Counter; |
| 21 | + public readonly consumersAddedTotal: Counter; |
| 22 | + public readonly consumersRemovedTotal: Counter; |
| 23 | + public readonly scalingCooldownsApplied: Counter; |
| 24 | + |
| 25 | + // Performance metrics |
| 26 | + public readonly queueLengthUpdatesTotal: Counter; |
| 27 | + public readonly batchesProcessedTotal: Counter; |
| 28 | + |
| 29 | + constructor(opts: ConsumerPoolMetricsOptions = {}) { |
| 30 | + this.register = opts.register ?? new Registry(); |
| 31 | + this.prefix = opts.prefix ?? "queue_consumer_pool"; |
| 32 | + |
| 33 | + // Current state metrics |
| 34 | + this.consumerCount = new Gauge({ |
| 35 | + name: `${this.prefix}_consumer_count`, |
| 36 | + help: "Current number of active queue consumers", |
| 37 | + labelNames: ["strategy"], |
| 38 | + registers: [this.register], |
| 39 | + }); |
| 40 | + |
| 41 | + this.queueLength = new Gauge({ |
| 42 | + name: `${this.prefix}_queue_length`, |
| 43 | + help: "Current queue length (median of recent samples)", |
| 44 | + registers: [this.register], |
| 45 | + }); |
| 46 | + |
| 47 | + this.smoothedQueueLength = new Gauge({ |
| 48 | + name: `${this.prefix}_smoothed_queue_length`, |
| 49 | + help: "EWMA smoothed queue length", |
| 50 | + registers: [this.register], |
| 51 | + }); |
| 52 | + |
| 53 | + this.targetConsumerCount = new Gauge({ |
| 54 | + name: `${this.prefix}_target_consumer_count`, |
| 55 | + help: "Target number of consumers calculated by scaling strategy", |
| 56 | + labelNames: ["strategy"], |
| 57 | + registers: [this.register], |
| 58 | + }); |
| 59 | + |
| 60 | + this.scalingStrategy = new Gauge({ |
| 61 | + name: `${this.prefix}_scaling_strategy_info`, |
| 62 | + help: "Information about the active scaling strategy (1 = active, 0 = inactive)", |
| 63 | + labelNames: ["strategy"], |
| 64 | + registers: [this.register], |
| 65 | + }); |
| 66 | + |
| 67 | + // Scaling operation metrics |
| 68 | + this.scalingOperationsTotal = new Counter({ |
| 69 | + name: `${this.prefix}_scaling_operations_total`, |
| 70 | + help: "Total number of scaling operations performed", |
| 71 | + labelNames: ["direction", "strategy"], |
| 72 | + registers: [this.register], |
| 73 | + }); |
| 74 | + |
| 75 | + this.consumersAddedTotal = new Counter({ |
| 76 | + name: `${this.prefix}_consumers_added_total`, |
| 77 | + help: "Total number of consumers added", |
| 78 | + registers: [this.register], |
| 79 | + }); |
| 80 | + |
| 81 | + this.consumersRemovedTotal = new Counter({ |
| 82 | + name: `${this.prefix}_consumers_removed_total`, |
| 83 | + help: "Total number of consumers removed", |
| 84 | + registers: [this.register], |
| 85 | + }); |
| 86 | + |
| 87 | + this.scalingCooldownsApplied = new Counter({ |
| 88 | + name: `${this.prefix}_scaling_cooldowns_applied_total`, |
| 89 | + help: "Number of times scaling was prevented due to cooldown", |
| 90 | + labelNames: ["direction"], |
| 91 | + registers: [this.register], |
| 92 | + }); |
| 93 | + |
| 94 | + this.queueLengthUpdatesTotal = new Counter({ |
| 95 | + name: `${this.prefix}_queue_length_updates_total`, |
| 96 | + help: "Total number of queue length updates received", |
| 97 | + registers: [this.register], |
| 98 | + }); |
| 99 | + |
| 100 | + this.batchesProcessedTotal = new Counter({ |
| 101 | + name: `${this.prefix}_batches_processed_total`, |
| 102 | + help: "Total number of metric batches processed", |
| 103 | + registers: [this.register], |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Update all gauge metrics with current state |
| 109 | + */ |
| 110 | + updateState(state: { |
| 111 | + consumerCount: number; |
| 112 | + queueLength?: number; |
| 113 | + smoothedQueueLength: number; |
| 114 | + targetConsumerCount: number; |
| 115 | + strategy: string; |
| 116 | + }) { |
| 117 | + this.consumerCount.set({ strategy: state.strategy }, state.consumerCount); |
| 118 | + |
| 119 | + if (state.queueLength !== undefined) { |
| 120 | + this.queueLength.set(state.queueLength); |
| 121 | + } |
| 122 | + |
| 123 | + this.smoothedQueueLength.set(state.smoothedQueueLength); |
| 124 | + this.targetConsumerCount.set({ strategy: state.strategy }, state.targetConsumerCount); |
| 125 | + |
| 126 | + // Set strategy info (1 for active strategy, 0 for others) |
| 127 | + ["none", "smooth", "aggressive"].forEach((s) => { |
| 128 | + this.scalingStrategy.set({ strategy: s }, s === state.strategy ? 1 : 0); |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Record a scaling operation |
| 134 | + */ |
| 135 | + recordScalingOperation(direction: "up" | "down" | "none", strategy: string, count: number) { |
| 136 | + if (direction !== "none") { |
| 137 | + this.scalingOperationsTotal.inc({ direction, strategy }); |
| 138 | + |
| 139 | + if (direction === "up") { |
| 140 | + this.consumersAddedTotal.inc(count); |
| 141 | + } else { |
| 142 | + this.consumersRemovedTotal.inc(count); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Record that scaling was prevented by cooldown |
| 149 | + */ |
| 150 | + recordCooldownApplied(direction: "up" | "down") { |
| 151 | + this.scalingCooldownsApplied.inc({ direction }); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Record a queue length update |
| 156 | + */ |
| 157 | + recordQueueLengthUpdate() { |
| 158 | + this.queueLengthUpdatesTotal.inc(); |
| 159 | + } |
| 160 | +} |
0 commit comments