Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 2x | import client from 'prom-client'; import EventEmitter from 'events'; import { Knex } from 'knex'; import * as events from './metric-events'; import { DB_POOL_UPDATE, FEATURE_ARCHIVED, FEATURE_CREATED, FEATURE_REVIVED, FEATURE_STRATEGY_ADD, FEATURE_STRATEGY_REMOVE, FEATURE_STRATEGY_UPDATE, FEATURE_UPDATED, CLIENT_METRICS, } from './types/events'; import { IUnleashConfig } from './types/option'; import { IUnleashStores } from './types/stores'; import { hoursToMilliseconds, minutesToMilliseconds } from 'date-fns'; import Timer = NodeJS.Timer; export default class MetricsMonitor { timer?: Timer; poolMetricsTimer?: Timer; constructor() { this.timer = null; this.poolMetricsTimer = null; } startMonitoring( config: IUnleashConfig, stores: IUnleashStores, version: string, eventBus: EventEmitter, db: Knex, ): Promise<void> { Iif (!config.server.serverMetrics) { return; } const { eventStore, featureToggleStore, userStore, projectStore } = stores; client.collectDefaultMetrics(); const requestDuration = new client.Summary({ name: 'http_request_duration_milliseconds', help: 'App response time', labelNames: ['path', 'method', 'status'], percentiles: [0.1, 0.5, 0.9, 0.95, 0.99], }); const dbDuration = new client.Summary({ name: 'db_query_duration_seconds', help: 'DB query duration time', labelNames: ['store', 'action'], percentiles: [0.1, 0.5, 0.9, 0.95, 0.99], }); const featureToggleUpdateTotal = new client.Counter({ name: 'feature_toggle_update_total', help: 'Number of times a toggle has been updated', labelNames: ['toggle'], }); const featureToggleUsageTotal = new client.Counter({ name: 'feature_toggle_usage_total', help: 'Number of times a feature toggle has been used', labelNames: ['toggle', 'active', 'appName'], }); const featureTogglesTotal = new client.Gauge({ name: 'feature_toggles_total', help: 'Number of feature toggles', labelNames: ['version'], }); const usersTotal = new client.Gauge({ name: 'users_total', help: 'Number of users', }); const projectsTotal = new client.Gauge({ name: 'projects_total', help: 'Number of projects', }); async function collectStaticCounters() { let togglesCount: number = 0; let usersCount: number; let projectsCount: number; try { togglesCount = await featureToggleStore.count({ archived: false, }); usersCount = await userStore.count(); projectsCount = await projectStore.count(); // eslint-disable-next-line no-empty } catch (e) {} featureTogglesTotal.reset(); featureTogglesTotal.labels(version).set(togglesCount); Iif (usersCount) { usersTotal.reset(); usersTotal.set(usersCount); } Iif (projectsCount) { projectsTotal.reset(); projectsTotal.set(projectsCount); } } collectStaticCounters(); this.timer = setInterval( () => collectStaticCounters(), hoursToMilliseconds(2), ).unref(); eventBus.on( events.REQUEST_TIME, ({ path, method, time, statusCode }) => { requestDuration.labels(path, method, statusCode).observe(time); }, ); eventBus.on(events.DB_TIME, ({ store, action, time }) => { dbDuration.labels(store, action).observe(time); }); eventStore.on(FEATURE_CREATED, ({ featureName }) => { featureToggleUpdateTotal.labels(featureName).inc(); }); eventStore.on(FEATURE_UPDATED, ({ featureName }) => { featureToggleUpdateTotal.labels(featureName).inc(); }); eventStore.on(FEATURE_STRATEGY_ADD, ({ featureName }) => { featureToggleUpdateTotal.labels(featureName).inc(); }); eventStore.on(FEATURE_STRATEGY_REMOVE, ({ featureName }) => { featureToggleUpdateTotal.labels(featureName).inc(); }); eventStore.on(FEATURE_STRATEGY_UPDATE, ({ featureName }) => { featureToggleUpdateTotal.labels(featureName).inc(); }); eventStore.on(FEATURE_ARCHIVED, ({ featureName }) => { featureToggleUpdateTotal.labels(featureName).inc(); }); eventStore.on(FEATURE_REVIVED, ({ featureName }) => { featureToggleUpdateTotal.labels(featureName).inc(); }); eventBus.on(CLIENT_METRICS, (m) => { // eslint-disable-next-line no-restricted-syntax for (const entry of Object.entries(m.bucket.toggles)) { featureToggleUsageTotal .labels(entry[0], 'true', m.appName) // @ts-ignore .inc(entry[1].yes); featureToggleUsageTotal .labels(entry[0], 'false', m.appName) // @ts-ignore .inc(entry[1].no); } }); this.configureDbMetrics(db, eventBus); } stopMonitoring(): void { clearInterval(this.timer); clearInterval(this.poolMetricsTimer); } configureDbMetrics(db: Knex, eventBus: EventEmitter): void { if (db && db.client) { const dbPoolMin = new client.Gauge({ name: 'db_pool_min', help: 'Minimum DB pool size', }); dbPoolMin.set(db.client.pool.min); const dbPoolMax = new client.Gauge({ name: 'db_pool_max', help: 'Maximum DB pool size', }); dbPoolMax.set(db.client.pool.max); const dbPoolFree = new client.Gauge({ name: 'db_pool_free', help: 'Current free connections in DB pool', }); const dbPoolUsed = new client.Gauge({ name: 'db_pool_used', help: 'Current connections in use in DB pool', }); const dbPoolPendingCreates = new client.Gauge({ name: 'db_pool_pending_creates', help: 'how many asynchronous create calls are running in DB pool', }); const dbPoolPendingAcquires = new client.Gauge({ name: 'db_pool_pending_acquires', help: 'how many acquires are waiting for a resource to be released in DB pool', }); eventBus.on(DB_POOL_UPDATE, (data) => { dbPoolFree.set(data.free); dbPoolUsed.set(data.used); dbPoolPendingCreates.set(data.pendingCreates); dbPoolPendingAcquires.set(data.pendingAcquires); }); this.registerPoolMetrics(db.client.pool, eventBus); this.poolMetricsTimer = setInterval( () => this.registerPoolMetrics(db.client.pool, eventBus), minutesToMilliseconds(1), ); this.poolMetricsTimer.unref(); } } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types registerPoolMetrics(pool: any, eventBus: EventEmitter) { try { eventBus.emit(DB_POOL_UPDATE, { used: pool.numUsed(), free: pool.numFree(), pendingCreates: pool.numPendingCreates(), pendingAcquires: pool.numPendingAcquires(), }); // eslint-disable-next-line no-empty } catch (e) {} } } export function createMetricsMonitor(): MetricsMonitor { return new MetricsMonitor(); } module.exports = { createMetricsMonitor, }; |