UNPKG

@voicenter-team/mysql-dynamic-cluster

Version:

Galera cluster with implementation of dynamic choose mysql server for queries, caching, hashing it and metrics

179 lines 6.71 kB
"use strict"; /** * Created by Bohdan on Sep, 2021 */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const io_1 = __importDefault(require("@pm2/io")); const MetricsInterfaces_1 = require("../types/MetricsInterfaces"); const Logger_1 = __importDefault(require("../utils/Logger")); const metrics_1 = require("@pm2/io/build/main/services/metrics"); /** * Real-time metrics */ class Metrics { constructor() { this.metricsRepository = {}; } /** * Initialize metrics * @param clusterName cluster name used for prefix in metric names * @param showKeys show keys instead names */ init(clusterName, showKeys) { this.clusterName = clusterName; this.showKeys = showKeys; } /** * set value in metric with type Metric * @param metric metric object * @param value the value what need to change * @param options extra options for metric like pool name or service name */ set(metric, value, options) { if (!Metrics._isMetricTypeValid(metric, MetricsInterfaces_1.MetricType.METRIC)) return; this._getMetricValues(metric, options).forEach(metricValue => { metricValue.set(value); }); } /** * Increase value by 1 in metric with type Counter * @param metric metric object * @param options extra options for metric like pool name or service name */ inc(metric, options) { if (!Metrics._isMetricTypeValid(metric, MetricsInterfaces_1.MetricType.COUNTER)) return; this._getMetricValues(metric, options).forEach(metricValue => { metricValue.inc(); }); } /** * Decrease value by 1 in metric with type Counter * @param metric metric object * @param options extra options for metric like pool name or service name */ dec(metric, options) { if (!Metrics._isMetricTypeValid(metric, MetricsInterfaces_1.MetricType.COUNTER)) return; this._getMetricValues(metric, options).forEach(metricValue => { metricValue.dec(); }); } /** * Mark the state to compute frequency in metric with type Meter * @param metric metric object * @param options extra options for metric like pool name or service name */ mark(metric, options) { if (!Metrics._isMetricTypeValid(metric, MetricsInterfaces_1.MetricType.METER)) return; this._getMetricValues(metric, options).forEach(metricValue => { metricValue.mark(); }); } update(metric, value, options) { if (!Metrics._isMetricTypeValid(metric, MetricsInterfaces_1.MetricType.HISTOGRAM)) return; this._getMetricValues(metric, options).forEach(metricValue => { metricValue.update(value); }); } /** * Check if type is valid for current metric * @param metric metric object * @param type type what need to compare with current metric * @private */ static _isMetricTypeValid(metric, type) { if (metric.type === type) return true; Logger_1.default.error(`Metric type of ${metric.key} is not valid. Should be ${MetricsInterfaces_1.MetricType[type]}`); return false; } /** * Get formatted metric key and name with prefix by cluster name and extra options * @param metric metric object * @param useService use service options to generate prop * @param options extra options for metric like pool name or service name * @private */ _generatePrefixes(metric, options, useService = false) { const newMetric = { key: `${this.clusterName}_`, name: `[${this.clusterName}] `, type: metric.type }; if ((options === null || options === void 0 ? void 0 : options.service) && useService) { newMetric.key += `${options.service.id}_`; newMetric.name += `[${options.service.name}] `; } if (options === null || options === void 0 ? void 0 : options.pool) { newMetric.key += `${options.pool.id}_`; newMetric.name += `[${options.pool.name}] `; } newMetric.key += metric.key; newMetric.name = metric.name && !this.showKeys ? newMetric.name + metric.name : newMetric.key; return newMetric; } /** * Get all metric separated to service metric and common * @param metric metric object * @param options extra options for metric like pool name or/and service name * @private */ _getMetricValues(metric, options) { const metrics = []; const metricValues = []; if (options === null || options === void 0 ? void 0 : options.service) { metrics.push(this._generatePrefixes(metric, options, true)); } metrics.push(this._generatePrefixes(metric, options)); metrics.forEach(metricProp => { if (!this.metricsRepository[metricProp.key]) { this._createMetric(metricProp); } metricValues.push(this.metricsRepository[metricProp.key]); }); return metricValues; } /** * Create metric * @param metric metric object * @private */ _createMetric(metric) { const metricKey = metric.key; const metricName = metric.name; switch (metric.type) { case MetricsInterfaces_1.MetricType.METRIC: this.metricsRepository[metricKey] = io_1.default.metric({ name: metricName }); break; case MetricsInterfaces_1.MetricType.COUNTER: this.metricsRepository[metricKey] = io_1.default.counter({ name: metricName }); break; case MetricsInterfaces_1.MetricType.METER: this.metricsRepository[metricKey] = io_1.default.meter({ name: metricName }); break; case MetricsInterfaces_1.MetricType.HISTOGRAM: this.metricsRepository[metricKey] = io_1.default.histogram({ name: metricName, measurement: metrics_1.MetricMeasurements.mean }); break; default: Logger_1.default.error(`Metric type ${MetricsInterfaces_1.MetricType[metric.type]} doesn't exist`); } } } exports.default = new Metrics(); //# sourceMappingURL=Metrics.js.map