2021-02-16 14:30:08 +01:00
|
|
|
import client from 'prom-client';
|
2021-04-22 10:07:10 +02:00
|
|
|
import EventEmitter from 'events';
|
|
|
|
import { Knex } from 'knex';
|
2021-04-29 10:21:29 +02:00
|
|
|
import * as events from './metric-events';
|
2021-02-16 14:30:08 +01:00
|
|
|
import {
|
2021-11-02 15:13:46 +01:00
|
|
|
DB_POOL_UPDATE,
|
2018-05-23 11:24:24 +02:00
|
|
|
FEATURE_ARCHIVED,
|
2021-11-02 15:13:46 +01:00
|
|
|
FEATURE_CREATED,
|
2018-05-23 11:24:24 +02:00
|
|
|
FEATURE_REVIVED,
|
2021-11-12 13:15:51 +01:00
|
|
|
FEATURE_STRATEGY_ADD,
|
|
|
|
FEATURE_STRATEGY_REMOVE,
|
|
|
|
FEATURE_STRATEGY_UPDATE,
|
2022-09-08 11:01:27 +02:00
|
|
|
FEATURE_ENVIRONMENT_ENABLED,
|
|
|
|
FEATURE_ENVIRONMENT_DISABLED,
|
|
|
|
FEATURE_VARIANTS_UPDATED,
|
|
|
|
FEATURE_METADATA_UPDATED,
|
2021-11-02 15:13:46 +01:00
|
|
|
FEATURE_UPDATED,
|
2021-12-09 21:02:58 +01:00
|
|
|
CLIENT_METRICS,
|
2022-07-22 11:00:22 +02:00
|
|
|
CLIENT_REGISTER,
|
2021-04-29 10:21:29 +02:00
|
|
|
} from './types/events';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { IUnleashConfig } from './types/option';
|
|
|
|
import { IUnleashStores } from './types/stores';
|
2021-11-02 15:13:46 +01:00
|
|
|
import { hoursToMilliseconds, minutesToMilliseconds } from 'date-fns';
|
2021-05-28 11:10:24 +02:00
|
|
|
import Timer = NodeJS.Timer;
|
2022-10-25 13:10:27 +02:00
|
|
|
import { InstanceStatsService } from './services/instance-stats-service';
|
2023-02-15 09:13:32 +01:00
|
|
|
import { ValidatedClientMetrics } from './services/client-metrics/schema';
|
2020-04-14 22:29:11 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
export default class MetricsMonitor {
|
2021-05-28 11:10:24 +02:00
|
|
|
timer?: Timer;
|
|
|
|
|
|
|
|
poolMetricsTimer?: Timer;
|
2021-02-16 14:30:08 +01:00
|
|
|
|
2020-12-16 14:49:11 +01:00
|
|
|
constructor() {
|
|
|
|
this.timer = null;
|
2021-05-28 11:10:24 +02:00
|
|
|
this.poolMetricsTimer = null;
|
2016-11-30 23:41:57 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
startMonitoring(
|
|
|
|
config: IUnleashConfig,
|
|
|
|
stores: IUnleashStores,
|
|
|
|
version: string,
|
|
|
|
eventBus: EventEmitter,
|
2022-10-25 13:10:27 +02:00
|
|
|
instanceStatsService: InstanceStatsService,
|
2021-08-12 15:04:37 +02:00
|
|
|
db: Knex,
|
2021-04-22 10:07:10 +02:00
|
|
|
): Promise<void> {
|
|
|
|
if (!config.server.serverMetrics) {
|
2020-12-16 14:49:11 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-08-03 13:34:10 +02:00
|
|
|
|
2022-10-25 13:10:27 +02:00
|
|
|
const { eventStore } = stores;
|
2017-06-28 14:21:05 +02:00
|
|
|
|
2020-12-16 14:49:11 +01:00
|
|
|
client.collectDefaultMetrics();
|
2020-02-28 14:50:32 +01:00
|
|
|
|
2020-12-16 14:49:11 +01:00
|
|
|
const requestDuration = new client.Summary({
|
|
|
|
name: 'http_request_duration_milliseconds',
|
|
|
|
help: 'App response time',
|
2022-09-30 15:28:50 +02:00
|
|
|
labelNames: ['path', 'method', 'status', 'appName'],
|
2021-06-07 10:34:32 +02:00
|
|
|
percentiles: [0.1, 0.5, 0.9, 0.95, 0.99],
|
2022-08-30 13:53:28 +02:00
|
|
|
maxAgeSeconds: 600,
|
|
|
|
ageBuckets: 5,
|
2020-12-16 14:49:11 +01:00
|
|
|
});
|
|
|
|
const dbDuration = new client.Summary({
|
|
|
|
name: 'db_query_duration_seconds',
|
|
|
|
help: 'DB query duration time',
|
|
|
|
labelNames: ['store', 'action'],
|
2021-06-07 10:34:32 +02:00
|
|
|
percentiles: [0.1, 0.5, 0.9, 0.95, 0.99],
|
2022-08-30 13:53:28 +02:00
|
|
|
maxAgeSeconds: 600,
|
|
|
|
ageBuckets: 5,
|
2020-12-16 14:49:11 +01:00
|
|
|
});
|
|
|
|
const featureToggleUpdateTotal = new client.Counter({
|
|
|
|
name: 'feature_toggle_update_total',
|
2022-09-08 11:01:27 +02:00
|
|
|
help: 'Number of times a toggle has been updated. Environment label would be "n/a" when it is not available, e.g. when a feature toggle is created.',
|
|
|
|
labelNames: ['toggle', 'project', 'environment'],
|
2020-12-16 14:49:11 +01:00
|
|
|
});
|
|
|
|
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'],
|
|
|
|
});
|
2021-08-27 10:10:14 +02:00
|
|
|
const usersTotal = new client.Gauge({
|
|
|
|
name: 'users_total',
|
|
|
|
help: 'Number of users',
|
|
|
|
});
|
|
|
|
const projectsTotal = new client.Gauge({
|
|
|
|
name: 'projects_total',
|
|
|
|
help: 'Number of projects',
|
|
|
|
});
|
2022-09-06 13:24:13 +02:00
|
|
|
const environmentsTotal = new client.Gauge({
|
|
|
|
name: 'environments_total',
|
|
|
|
help: 'Number of environments',
|
|
|
|
});
|
2022-10-25 13:10:27 +02:00
|
|
|
const groupsTotal = new client.Gauge({
|
|
|
|
name: 'groups_total',
|
|
|
|
help: 'Number of groups',
|
|
|
|
});
|
|
|
|
|
|
|
|
const rolesTotal = new client.Gauge({
|
|
|
|
name: 'roles_total',
|
|
|
|
help: 'Number of roles',
|
|
|
|
});
|
|
|
|
|
|
|
|
const segmentsTotal = new client.Gauge({
|
|
|
|
name: 'segments_total',
|
|
|
|
help: 'Number of segments',
|
|
|
|
});
|
|
|
|
|
|
|
|
const contextTotal = new client.Gauge({
|
|
|
|
name: 'context_total',
|
|
|
|
help: 'Number of context',
|
|
|
|
});
|
|
|
|
|
|
|
|
const strategiesTotal = new client.Gauge({
|
|
|
|
name: 'strategies_total',
|
|
|
|
help: 'Number of strategies',
|
|
|
|
});
|
|
|
|
|
2022-12-16 12:16:51 +01:00
|
|
|
const clientAppsTotal = new client.Gauge({
|
|
|
|
name: 'client_apps_total',
|
|
|
|
help: 'Number of registered client apps aggregated by range by last seen',
|
|
|
|
labelNames: ['range'],
|
|
|
|
});
|
|
|
|
|
2022-10-25 13:10:27 +02:00
|
|
|
const samlEnabled = new client.Gauge({
|
|
|
|
name: 'saml_enabled',
|
|
|
|
help: 'Whether SAML is enabled',
|
|
|
|
});
|
|
|
|
|
|
|
|
const oidcEnabled = new client.Gauge({
|
|
|
|
name: 'oidc_enabled',
|
|
|
|
help: 'Whether OIDC is enabled',
|
|
|
|
});
|
2020-02-28 14:50:32 +01:00
|
|
|
|
2022-07-22 11:00:22 +02:00
|
|
|
const clientSdkVersionUsage = new client.Counter({
|
|
|
|
name: 'client_sdk_versions',
|
|
|
|
help: 'Which sdk versions are being used',
|
|
|
|
labelNames: ['sdk_name', 'sdk_version'],
|
|
|
|
});
|
|
|
|
|
2023-03-17 14:10:21 +01:00
|
|
|
const optimal304DiffingCounter = new client.Counter({
|
|
|
|
name: 'optimal_304_diffing',
|
|
|
|
help: 'Count the Optimal 304 diffing with status',
|
|
|
|
labelNames: ['status'],
|
|
|
|
});
|
|
|
|
|
2021-08-27 10:10:14 +02:00
|
|
|
async function collectStaticCounters() {
|
2021-02-17 15:24:43 +01:00
|
|
|
try {
|
2022-10-25 13:10:27 +02:00
|
|
|
const stats = await instanceStatsService.getStats();
|
|
|
|
|
|
|
|
featureTogglesTotal.reset();
|
|
|
|
featureTogglesTotal.labels(version).set(stats.featureToggles);
|
2021-02-17 15:24:43 +01:00
|
|
|
|
2021-08-27 10:10:14 +02:00
|
|
|
usersTotal.reset();
|
2022-10-25 13:10:27 +02:00
|
|
|
usersTotal.set(stats.users);
|
|
|
|
|
2021-08-27 10:10:14 +02:00
|
|
|
projectsTotal.reset();
|
2022-10-25 13:10:27 +02:00
|
|
|
projectsTotal.set(stats.projects);
|
|
|
|
|
2022-09-06 13:24:13 +02:00
|
|
|
environmentsTotal.reset();
|
2022-10-25 13:10:27 +02:00
|
|
|
environmentsTotal.set(stats.environments);
|
|
|
|
|
|
|
|
groupsTotal.reset();
|
|
|
|
groupsTotal.set(stats.groups);
|
|
|
|
|
|
|
|
rolesTotal.reset();
|
|
|
|
rolesTotal.set(stats.roles);
|
|
|
|
|
|
|
|
segmentsTotal.reset();
|
|
|
|
segmentsTotal.set(stats.segments);
|
|
|
|
|
|
|
|
contextTotal.reset();
|
|
|
|
contextTotal.set(stats.contextFields);
|
|
|
|
|
|
|
|
strategiesTotal.reset();
|
|
|
|
strategiesTotal.set(stats.strategies);
|
|
|
|
|
|
|
|
samlEnabled.reset();
|
|
|
|
samlEnabled.set(stats.SAMLenabled ? 1 : 0);
|
|
|
|
|
|
|
|
oidcEnabled.reset();
|
|
|
|
oidcEnabled.set(stats.OIDCenabled ? 1 : 0);
|
2022-12-16 12:16:51 +01:00
|
|
|
|
|
|
|
clientAppsTotal.reset();
|
|
|
|
stats.clientApps.forEach((clientStat) =>
|
|
|
|
clientAppsTotal
|
|
|
|
.labels({ range: clientStat.range })
|
|
|
|
.set(clientStat.count),
|
|
|
|
);
|
2022-10-25 13:10:27 +02:00
|
|
|
} catch (e) {}
|
2020-12-16 14:49:11 +01:00
|
|
|
}
|
2016-12-01 17:43:08 +01:00
|
|
|
|
2022-10-25 13:10:27 +02:00
|
|
|
process.nextTick(() => {
|
|
|
|
collectStaticCounters();
|
|
|
|
this.timer = setInterval(
|
|
|
|
() => collectStaticCounters(),
|
|
|
|
hoursToMilliseconds(2),
|
|
|
|
).unref();
|
|
|
|
});
|
2018-05-23 11:24:24 +02:00
|
|
|
|
2020-12-16 14:49:11 +01:00
|
|
|
eventBus.on(
|
|
|
|
events.REQUEST_TIME,
|
2022-09-30 15:28:50 +02:00
|
|
|
({ path, method, time, statusCode, appName }) => {
|
|
|
|
requestDuration
|
|
|
|
.labels(path, method, statusCode, appName)
|
|
|
|
.observe(time);
|
2020-12-16 14:49:11 +01:00
|
|
|
},
|
|
|
|
);
|
2019-08-04 11:10:51 +02:00
|
|
|
|
2023-03-17 14:10:21 +01:00
|
|
|
eventBus.on('optimal304Differ', ({ status }) => {
|
|
|
|
optimal304DiffingCounter.labels(status).inc();
|
|
|
|
});
|
|
|
|
|
2020-12-16 14:49:11 +01:00
|
|
|
eventBus.on(events.DB_TIME, ({ store, action, time }) => {
|
|
|
|
dbDuration.labels(store, action).observe(time);
|
|
|
|
});
|
2018-11-28 15:50:49 +01:00
|
|
|
|
2022-09-08 11:01:27 +02:00
|
|
|
eventStore.on(FEATURE_CREATED, ({ featureName, project }) => {
|
|
|
|
featureToggleUpdateTotal.labels(featureName, project, 'n/a').inc();
|
|
|
|
});
|
|
|
|
eventStore.on(FEATURE_VARIANTS_UPDATED, ({ featureName, project }) => {
|
|
|
|
featureToggleUpdateTotal.labels(featureName, project, 'n/a').inc();
|
|
|
|
});
|
|
|
|
eventStore.on(FEATURE_METADATA_UPDATED, ({ featureName, project }) => {
|
|
|
|
featureToggleUpdateTotal.labels(featureName, project, 'n/a').inc();
|
|
|
|
});
|
|
|
|
eventStore.on(FEATURE_UPDATED, ({ featureName, project }) => {
|
|
|
|
featureToggleUpdateTotal
|
|
|
|
.labels(featureName, project, 'default')
|
|
|
|
.inc();
|
|
|
|
});
|
|
|
|
eventStore.on(
|
|
|
|
FEATURE_STRATEGY_ADD,
|
|
|
|
({ featureName, project, environment }) => {
|
|
|
|
featureToggleUpdateTotal
|
|
|
|
.labels(featureName, project, environment)
|
|
|
|
.inc();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
eventStore.on(
|
|
|
|
FEATURE_STRATEGY_REMOVE,
|
|
|
|
({ featureName, project, environment }) => {
|
|
|
|
featureToggleUpdateTotal
|
|
|
|
.labels(featureName, project, environment)
|
|
|
|
.inc();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
eventStore.on(
|
|
|
|
FEATURE_STRATEGY_UPDATE,
|
|
|
|
({ featureName, project, environment }) => {
|
|
|
|
featureToggleUpdateTotal
|
|
|
|
.labels(featureName, project, environment)
|
|
|
|
.inc();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
eventStore.on(
|
|
|
|
FEATURE_ENVIRONMENT_DISABLED,
|
|
|
|
({ featureName, project, environment }) => {
|
|
|
|
featureToggleUpdateTotal
|
|
|
|
.labels(featureName, project, environment)
|
|
|
|
.inc();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
eventStore.on(
|
|
|
|
FEATURE_ENVIRONMENT_ENABLED,
|
|
|
|
({ featureName, project, environment }) => {
|
|
|
|
featureToggleUpdateTotal
|
|
|
|
.labels(featureName, project, environment)
|
|
|
|
.inc();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
eventStore.on(FEATURE_ARCHIVED, ({ featureName, project }) => {
|
|
|
|
featureToggleUpdateTotal.labels(featureName, project, 'n/a').inc();
|
2021-11-12 13:15:51 +01:00
|
|
|
});
|
2022-09-08 11:01:27 +02:00
|
|
|
eventStore.on(FEATURE_REVIVED, ({ featureName, project }) => {
|
|
|
|
featureToggleUpdateTotal.labels(featureName, project, 'n/a').inc();
|
2020-12-16 14:49:11 +01:00
|
|
|
});
|
|
|
|
|
2023-02-15 09:13:32 +01:00
|
|
|
eventBus.on(CLIENT_METRICS, (m: ValidatedClientMetrics) => {
|
2021-04-22 10:07:10 +02:00
|
|
|
for (const entry of Object.entries(m.bucket.toggles)) {
|
2020-12-16 14:49:11 +01:00
|
|
|
featureToggleUsageTotal
|
2021-02-16 14:30:08 +01:00
|
|
|
.labels(entry[0], 'true', m.appName)
|
2021-04-22 10:07:10 +02:00
|
|
|
.inc(entry[1].yes);
|
2020-12-16 14:49:11 +01:00
|
|
|
featureToggleUsageTotal
|
2021-02-16 14:30:08 +01:00
|
|
|
.labels(entry[0], 'false', m.appName)
|
2021-04-22 10:07:10 +02:00
|
|
|
.inc(entry[1].no);
|
2020-12-16 14:49:11 +01:00
|
|
|
}
|
|
|
|
});
|
2022-09-27 11:06:06 +02:00
|
|
|
eventStore.on(CLIENT_REGISTER, (m) => {
|
2022-07-22 11:00:22 +02:00
|
|
|
if (m.sdkVersion && m.sdkVersion.indexOf(':') > -1) {
|
|
|
|
const [sdkName, sdkVersion] = m.sdkVersion.split(':');
|
|
|
|
clientSdkVersionUsage.labels(sdkName, sdkVersion).inc();
|
|
|
|
}
|
|
|
|
});
|
2021-02-04 14:14:46 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
this.configureDbMetrics(db, eventBus);
|
2020-12-16 14:49:11 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
stopMonitoring(): void {
|
2020-12-16 14:49:11 +01:00
|
|
|
clearInterval(this.timer);
|
2021-05-28 11:10:24 +02:00
|
|
|
clearInterval(this.poolMetricsTimer);
|
2020-12-16 14:49:11 +01:00
|
|
|
}
|
2021-02-04 14:14:46 +01:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
configureDbMetrics(db: Knex, eventBus: EventEmitter): void {
|
|
|
|
if (db && db.client) {
|
2021-02-04 14:14:46 +01:00
|
|
|
const dbPoolMin = new client.Gauge({
|
|
|
|
name: 'db_pool_min',
|
|
|
|
help: 'Minimum DB pool size',
|
|
|
|
});
|
2021-04-22 10:07:10 +02:00
|
|
|
dbPoolMin.set(db.client.pool.min);
|
2021-02-04 14:14:46 +01:00
|
|
|
const dbPoolMax = new client.Gauge({
|
|
|
|
name: 'db_pool_max',
|
|
|
|
help: 'Maximum DB pool size',
|
|
|
|
});
|
2021-04-22 10:07:10 +02:00
|
|
|
dbPoolMax.set(db.client.pool.max);
|
2021-02-04 14:14:46 +01:00
|
|
|
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',
|
2021-08-12 15:04:37 +02:00
|
|
|
help: 'how many asynchronous create calls are running in DB pool',
|
2021-02-04 14:14:46 +01:00
|
|
|
});
|
|
|
|
const dbPoolPendingAcquires = new client.Gauge({
|
|
|
|
name: 'db_pool_pending_acquires',
|
2021-08-12 15:04:37 +02:00
|
|
|
help: 'how many acquires are waiting for a resource to be released in DB pool',
|
2021-02-04 14:14:46 +01:00
|
|
|
});
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
eventBus.on(DB_POOL_UPDATE, (data) => {
|
2021-02-04 14:14:46 +01:00
|
|
|
dbPoolFree.set(data.free);
|
|
|
|
dbPoolUsed.set(data.used);
|
|
|
|
dbPoolPendingCreates.set(data.pendingCreates);
|
|
|
|
dbPoolPendingAcquires.set(data.pendingAcquires);
|
|
|
|
});
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
this.registerPoolMetrics(db.client.pool, eventBus);
|
2021-05-28 11:10:24 +02:00
|
|
|
this.poolMetricsTimer = setInterval(
|
2021-04-22 10:07:10 +02:00
|
|
|
() => this.registerPoolMetrics(db.client.pool, eventBus),
|
2021-11-02 15:13:46 +01:00
|
|
|
minutesToMilliseconds(1),
|
2021-02-04 14:14:46 +01:00
|
|
|
);
|
2021-05-28 11:10:24 +02:00
|
|
|
this.poolMetricsTimer.unref();
|
2021-02-04 14:14:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
registerPoolMetrics(pool: any, eventBus: EventEmitter) {
|
2021-02-17 15:24:43 +01:00
|
|
|
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) {}
|
2021-02-04 14:14:46 +01:00
|
|
|
}
|
2020-12-16 14:49:11 +01:00
|
|
|
}
|
2021-04-22 10:07:10 +02:00
|
|
|
export function createMetricsMonitor(): MetricsMonitor {
|
|
|
|
return new MetricsMonitor();
|
|
|
|
}
|
2020-12-16 14:49:11 +01:00
|
|
|
|
|
|
|
module.exports = {
|
2021-04-22 10:07:10 +02:00
|
|
|
createMetricsMonitor,
|
2016-12-04 14:09:37 +01:00
|
|
|
};
|