diff --git a/docs/development/k8s.md b/docs/development/k8s.md index da611b77c24..05e98ede4d1 100644 --- a/docs/development/k8s.md +++ b/docs/development/k8s.md @@ -409,15 +409,15 @@ this.context.apis.foundation.promMetrics.inc( Example Gauge using collect() callback: ```typescript -const self = this; // rename `this` to use inside collect() +const { context, getSlicesDispatched } = this; await this.context.apis.foundation.promMetrics.addGauge( 'slices_dispatched', // name 'number of slices a slicer has dispatched', // help or description ['class'], // label names specific to this metric function collect() { // callback fn updates value only when '/metrics' endpoint is hit - const slicesFinished = self.getSlicesDispatched(); // get current value from local momory + const slicesFinished = getSlicesDispatched(); // get current value from local momory const labels = { // 'set()' needs both default labels and labels specific to metric to match the correct gauge - ...self.context.apis.foundation.promMetrics.getDefaultLabels(), + ...context.apis.foundation.promMetrics.getDefaultLabels(), class: 'SlicerExecutionContext' }; this.set(labels, slicesFinished); // this refers to the Gauge diff --git a/package.json b/package.json index 17ce0379f1d..6efe6a5f07c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "teraslice-workspace", "displayName": "Teraslice", - "version": "1.5.1", + "version": "1.5.2", "private": true, "homepage": "https://github.com/terascope/teraslice", "bugs": { diff --git a/packages/job-components/package.json b/packages/job-components/package.json index f4e9a9f23c8..87b224925a3 100644 --- a/packages/job-components/package.json +++ b/packages/job-components/package.json @@ -1,7 +1,7 @@ { "name": "@terascope/job-components", "displayName": "Job Components", - "version": "0.74.1", + "version": "0.74.2", "description": "A teraslice library for validating jobs schemas, registering apis, and defining and running new Job APIs", "homepage": "https://github.com/terascope/teraslice/tree/master/packages/job-components#readme", "bugs": { @@ -36,6 +36,7 @@ "convict-format-with-moment": "^6.2.0", "convict-format-with-validator": "^6.2.0", "datemath-parser": "^1.0.6", + "prom-client": "^15.1.2", "uuid": "^9.0.1" }, "devDependencies": { diff --git a/packages/job-components/src/interfaces/context.ts b/packages/job-components/src/interfaces/context.ts index a4f38a8f1ce..ef5a7165418 100644 --- a/packages/job-components/src/interfaces/context.ts +++ b/packages/job-components/src/interfaces/context.ts @@ -106,7 +106,7 @@ export interface FoundationApis { getSystemEvents(): EventEmitter; getConnection(config: ConnectionConfig): { client: any }; createClient(config: ConnectionConfig): Promise<{ client: any }>; - promMetrics: tf.PromMetrics + promMetrics: tf.PromMetrics; } export interface LegacyFoundationApis { diff --git a/packages/job-components/src/test-helpers.ts b/packages/job-components/src/test-helpers.ts index 91270f73901..bbfbedcc583 100644 --- a/packages/job-components/src/test-helpers.ts +++ b/packages/job-components/src/test-helpers.ts @@ -6,6 +6,9 @@ import { makeISODate } from '@terascope/utils'; import { Terafoundation as tf } from '@terascope/types'; +import promClient, { + CollectFunction, Counter, Gauge, Histogram, Summary +} from 'prom-client'; import * as i from './interfaces'; function newId(prefix: string): string { @@ -111,21 +114,14 @@ export interface TestClients { export interface TestContextAPIs extends i.ContextAPIs { setTestClients(clients: TestClientConfig[]): void; getTestClients(): TestClients; + scrapePromMetrics(): Promise; } -export type MockPromMetrics = Record, - readonly buckets?: Array, - readonly percentiles?: Array, - readonly ageBuckets?: number, - readonly maxAgeSeconds?: number, - readonly functions: Set, - labels: Record - collect?: tf.CollectFunction | tf.CollectFunction - | tf.CollectFunction | tf.CollectFunction, -}>; +export interface MockPromMetrics { + metricList: tf.MetricList; + defaultLabels: Record; + prefix: string; +} type GetKeyOpts = { type: string; @@ -331,90 +327,120 @@ export class TestContext implements i.Context { }, promMetrics: { async init(config: tf.PromMetricsInitConfig) { - const { job_prom_metrics_enabled, tf_prom_metrics_enabled } = config; + const { + assignment, job_prom_metrics_add_default, job_prom_metrics_enabled, + tf_prom_metrics_add_default, tf_prom_metrics_enabled, + labels, prefix + } = config; if (ctx.mockPromMetrics) { throw new Error('Prom metrics API cannot be initialized more than once.'); } + const useDefaultMetrics = job_prom_metrics_add_default !== undefined + ? job_prom_metrics_add_default + : tf_prom_metrics_add_default; + + const defaultLabels = { + name: 'mockPromMetrics', + assignment, + ...labels + }; + + const finalPrefix = prefix || `teraslice_${config.assignment}_`; + + if (useDefaultMetrics) { + const defaultMetricsConfig = { + prefix: finalPrefix, + labels: defaultLabels + }; + promClient.collectDefaultMetrics(defaultMetricsConfig); + } + if (job_prom_metrics_enabled === true || (job_prom_metrics_enabled === undefined && tf_prom_metrics_enabled)) { - ctx.mockPromMetrics = {}; + ctx.mockPromMetrics = { + metricList: {}, + prefix: finalPrefix, + defaultLabels + }; return true; } logger.warn('Cannot create PromMetricsAPI because metrics are disabled.'); return false; }, - set(name: string, labelValues: Record, value: number): void { + set(name: string, labels: Record, value: number): void { if (ctx.mockPromMetrics) { - const metric = ctx.mockPromMetrics[name]; - if (!metric || !metric.functions) { + const metric = ctx.mockPromMetrics.metricList[name]; + if (!metric || !metric.functions || !metric.metric) { throw new Error(`Metric ${name} is not setup`); } - if (metric.functions.has('inc')) { - const labelKey = getLabelKey(labelValues); - metric.labels[labelKey] = { sum: 0, count: 0, value }; + if (metric.metric instanceof Gauge) { + const labelValues = Object.keys(labels).map((key) => labels[key]); + const res = metric.metric.labels(...labelValues.concat( + Object.values(this.getDefaultLabels()) + )); + res.set(value); + } else { + throw new Error(`set not available on ${name} metric`); } } }, - inc(name: string, labelValues: Record, value: number): void { + inc(name: string, labels: Record, value: number): void { if (ctx.mockPromMetrics) { - const metric = ctx.mockPromMetrics[name]; - if (!metric || !metric.functions) { + const metric = ctx.mockPromMetrics.metricList[name]; + if (!metric || !metric.functions || !metric.metric) { throw new Error(`Metric ${name} is not setup`); } - if (metric.functions.has('inc')) { - const labelKey = getLabelKey(labelValues); - if (Object.keys(metric.labels).includes(labelKey)) { - metric.labels[labelKey].value += value; - } else { - metric.labels[labelKey] = { sum: 0, count: 0, value }; - } + if (metric.metric instanceof Counter + || metric.metric instanceof Gauge + ) { + const labelValues = Object.keys(labels).map((key) => labels[key]); + const res = metric.metric.labels(...labelValues.concat( + Object.values(this.getDefaultLabels()) + )); + res.inc(value); + } else { + throw new Error(`inc not available on ${name} metric`); } } }, - dec(name: string, labelValues: Record, value: number): void { + dec(name: string, labels: Record, value: number): void { if (ctx.mockPromMetrics) { - const metric = ctx.mockPromMetrics[name]; - if (!metric || !metric.functions) { + const metric = ctx.mockPromMetrics.metricList[name]; + if (!metric || !metric.functions || !metric.metric) { throw new Error(`Metric ${name} is not setup`); } - if (metric.functions.has('dec')) { - const labelKey = getLabelKey(labelValues); - if (Object.keys(metric.labels).includes(labelKey)) { - metric.labels[labelKey].value -= value; - } else { - metric.labels[labelKey] = { sum: 0, count: 0, value: -value }; - } + + if (metric.metric instanceof Gauge) { + const labelValues = Object.keys(labels).map((key) => labels[key]); + const res = metric.metric.labels(...labelValues.concat( + Object.values(this.getDefaultLabels()) + )); + res.dec(value); + } else { + throw new Error(`dec not available on ${name} metric`); } } }, observe( name: string, - labelValues: Record, + labels: Record, value: number ): void { if (ctx.mockPromMetrics) { - const metric = ctx.mockPromMetrics[name]; - if (!metric || !metric.functions) { + const metric = ctx.mockPromMetrics.metricList[name]; + if (!metric || !metric.functions || !metric.metric) { throw new Error(`Metric ${name} is not setup`); } - if (metric.functions.has('observe')) { - const labelKey = getLabelKey(labelValues); - if (Object.keys(metric.labels).includes(labelKey)) { - metric.labels[labelKey] = { - sum: metric.labels[labelKey].sum += value, - count: metric.labels[labelKey].count + 1, - value: 0 - }; - } else { - metric.labels[labelKey] = { - sum: value, - count: 1, - value: 0 - }; - } + if (metric.metric instanceof Summary + || metric.metric instanceof Histogram) { + const labelValues = Object.keys(labels).map((key) => labels[key]); + const res = metric.metric.labels(...labelValues.concat( + Object.values(this.getDefaultLabels()) + )); + res.observe(value); } else { throw new Error(`observe not available on ${name} metric`); } @@ -424,17 +450,21 @@ export class TestContext implements i.Context { name: string, help: string, labelNames: Array, - collect?: tf.CollectFunction + collect?: CollectFunction ): Promise { if (ctx.mockPromMetrics) { if (!this.hasMetric(name)) { - ctx.mockPromMetrics[name] = { - name, + const fullname = ctx.mockPromMetrics.prefix + name; + const gauge = new Gauge({ + name: fullname, help, labelNames, - collect, - functions: new Set(['inc', 'dec', 'set']), - labels: {} + collect + }); + ctx.mockPromMetrics.metricList[name] = { + name, + metric: gauge, + functions: new Set(['inc', 'dec', 'set']) }; } else { logger.info(`metric ${name} already defined in metric list`); @@ -445,17 +475,21 @@ export class TestContext implements i.Context { name: string, help: string, labelNames: Array, - collect?: tf.CollectFunction + collect?: CollectFunction ): Promise { if (ctx.mockPromMetrics) { if (!this.hasMetric(name)) { - ctx.mockPromMetrics[name] = { - name, + const fullname = ctx.mockPromMetrics.prefix + name; + const counter = new Counter({ + name: fullname, help, labelNames, - collect, - functions: new Set(['inc']), - labels: {} + collect + }); + ctx.mockPromMetrics.metricList[name] = { + name, + metric: counter, + functions: new Set(['inc']) }; } else { logger.info(`metric ${name} already defined in metric list`); @@ -466,17 +500,23 @@ export class TestContext implements i.Context { name: string, help: string, labelNames: Array, - collect?: tf.CollectFunction + collect?: CollectFunction, + buckets: number[] = [0.1, 5, 15, 50, 100, 500] ): Promise { if (ctx.mockPromMetrics) { if (!this.hasMetric(name)) { - ctx.mockPromMetrics[name] = { - name, + const fullname = ctx.mockPromMetrics.prefix + name; + const histogram = new Histogram({ + name: fullname, help, labelNames, - collect, - functions: new Set(['observe']), - labels: {} + buckets, + collect + }); + ctx.mockPromMetrics.metricList[name] = { + name, + metric: histogram, + functions: new Set(['observe']) }; } else { logger.info(`metric ${name} already defined in metric list`); @@ -487,23 +527,27 @@ export class TestContext implements i.Context { name: string, help: string, labelNames: Array, - collect?: tf.CollectFunction, + collect?: CollectFunction, maxAgeSeconds = 600, ageBuckets = 5, percentiles: Array = [0.01, 0.1, 0.9, 0.99] ): Promise { if (ctx.mockPromMetrics) { if (!this.hasMetric(name)) { - ctx.mockPromMetrics[name] = { - name, + const fullname = ctx.mockPromMetrics.prefix + name; + const summary = new Summary({ + name: fullname, help, labelNames, - collect, + percentiles, maxAgeSeconds, ageBuckets, - percentiles, - functions: new Set(['observe']), - labels: {} + collect + }); + ctx.mockPromMetrics.metricList[name] = { + name, + metric: summary, + functions: new Set(['observe']) }; } else { logger.info(`metric ${name} already defined in metric list`); @@ -512,17 +556,30 @@ export class TestContext implements i.Context { }, hasMetric(name: string): boolean { if (ctx.mockPromMetrics) { - return (name in ctx.mockPromMetrics); + return (name in ctx.mockPromMetrics.metricList); } return false; }, async deleteMetric(name: string): Promise { let deleted = false; if (ctx.mockPromMetrics) { - deleted = delete ctx.mockPromMetrics[name]; + const fullname = ctx.mockPromMetrics.prefix + name; + if (ctx.mockPromMetrics.metricList[name]) { + deleted = delete ctx.mockPromMetrics.metricList[name]; + promClient.register.removeSingleMetric(fullname); + } else { + throw new Error(`metric ${name} not defined in metric list`); + } + return deleted; } return deleted; }, + getDefaultLabels() { + if (ctx.mockPromMetrics) { + return ctx.mockPromMetrics.defaultLabels; + } + return {}; + }, verifyAPI(): boolean { return ctx.mockPromMetrics !== null; }, @@ -572,6 +629,12 @@ export class TestContext implements i.Context { return clients; }, + async scrapePromMetrics(): Promise { + if (ctx.mockPromMetrics) { + return promClient.register.metrics(); + } + return ''; + }, } as TestContextAPIs; this.foundation = { diff --git a/packages/job-components/test/test-helpers-spec.ts b/packages/job-components/test/test-helpers-spec.ts index 45ea73add4f..8f2bcd8eb0a 100644 --- a/packages/job-components/test/test-helpers-spec.ts +++ b/packages/job-components/test/test-helpers-spec.ts @@ -186,7 +186,7 @@ describe('Test Helpers', () => { const context = new TestContext('test-prom-metrics'); context.sysconfig.teraslice.cluster_manager_type = 'kubernetes'; const config = { - assignment: 'cluster-master', + assignment: 'master', logger: debugLogger('test-helpers-spec-logger'), tf_prom_metrics_enabled: true, tf_prom_metrics_port: 3333, @@ -203,20 +203,22 @@ describe('Test Helpers', () => { }); it('should add, inc and delete counter', async () => { - await context.apis.foundation.promMetrics.addCounter('test_counter', 'test_counter help string', ['uuid'], function collect() { - this.inc(); + await context.apis.foundation.promMetrics.addCounter('test_counter', 'test_counter help string', ['uuid', 'name', 'assignment'], function collect() { + this.inc({ uuid: 'e&vgv%56' }, 1); }); - context.apis.foundation.promMetrics.inc('test_counter', {}, 1); + context.apis.foundation.promMetrics.inc('test_counter', { uuid: 'e&vgv%56' }, 1); expect(context.apis.foundation.promMetrics.hasMetric('test_counter')).toBe(true); expect(await context.apis.foundation.promMetrics.deleteMetric('test_counter')).toBe(true); }); it('should inc, dec, and set gauge', async () => { - await context.apis.foundation.promMetrics.addGauge('test_gauge', 'test_gauge help string', ['uuid']); + await context.apis.foundation.promMetrics.addGauge('test_gauge', 'help string', ['uuid', 'name', 'assignment']); context.apis.foundation.promMetrics.set('test_gauge', { uuid: '437Ev89h' }, 10); context.apis.foundation.promMetrics.inc('test_gauge', { uuid: '437Ev89h' }, 1); context.apis.foundation.promMetrics.dec('test_gauge', { uuid: '437Ev89h' }, 2); - expect(context.mockPromMetrics?.test_gauge.labels['uuid:437Ev89h,'].value).toBe(9); + const metrics: string = await context.apis.scrapePromMetrics(); + const sum = metrics.split('\n').filter((line) => line.includes('437Ev89h'))[0].split(' ')[1]; + expect(sum).toBe('9'); }); it('should throw if inc called on metric that doesn\'t exist', async () => { @@ -234,20 +236,27 @@ describe('Test Helpers', () => { .toThrow('Metric missing_test_gauge is not setup'); }); it('should add and observe summary', async () => { - await context.apis.foundation.promMetrics.addSummary('test_summary', 'test_summary help string', ['uuid']); + await context.apis.foundation.promMetrics.addSummary('test_summary', 'test_summary help string', ['uuid', 'name', 'assignment']); context.apis.foundation.promMetrics.observe('test_summary', { uuid: '34rhEqrX' }, 12); context.apis.foundation.promMetrics.observe('test_summary', { uuid: '34rhEqrX' }, 5); context.apis.foundation.promMetrics.observe('test_summary', { uuid: '34rhEqrX' }, 18); - expect(context.mockPromMetrics?.test_summary?.labels['uuid:34rhEqrX,']).toEqual({ sum: 35, count: 3, value: 0 }); + const metrics: string = await context.apis.scrapePromMetrics(); + const sum = metrics.split('\n').filter((line) => line.includes('test_summary_sum'))[0].split(' ')[1]; + const count = metrics.split('\n').filter((line) => line.includes('test_summary_count'))[0].split(' ')[1]; + expect(sum).toBe('35'); + expect(count).toBe('3'); }); it('should add and observe histogram', async () => { - await context.apis.foundation.promMetrics.addHistogram('test_histogram', 'test_histogram help string', ['uuid']); + await context.apis.foundation.promMetrics.addHistogram('test_histogram', 'test_histogram help string', ['uuid', 'name', 'assignment']); context.apis.foundation.promMetrics.observe('test_histogram', { uuid: 'dEF4Kby6' }, 10); context.apis.foundation.promMetrics.observe('test_histogram', { uuid: 'dEF4Kby6' }, 30); context.apis.foundation.promMetrics.observe('test_histogram', { uuid: 'dEF4Kby6' }, 2); - expect(context.mockPromMetrics?.test_histogram?.labels['uuid:dEF4Kby6,']) - .toEqual({ sum: 42, count: 3, value: 0 }); + const metrics: string = await context.apis.scrapePromMetrics(); + const sum = metrics.split('\n').filter((line) => line.includes('test_histogram_sum'))[0].split(' ')[1]; + const count = metrics.split('\n').filter((line) => line.includes('test_histogram_count'))[0].split(' ')[1]; + expect(sum).toBe('42'); + expect(count).toBe('3'); }); it('should throw if observe called on metric that doesn\'t exist', async () => { diff --git a/packages/terafoundation/package.json b/packages/terafoundation/package.json index 819a98fe88a..a0119244150 100644 --- a/packages/terafoundation/package.json +++ b/packages/terafoundation/package.json @@ -1,7 +1,7 @@ { "name": "terafoundation", "displayName": "Terafoundation", - "version": "0.63.1", + "version": "0.63.2", "description": "A Clustering and Foundation tool for Terascope Tools", "homepage": "https://github.com/terascope/teraslice/tree/master/packages/terafoundation#readme", "bugs": { diff --git a/packages/terafoundation/test/apis/prom-metrics-spec.ts b/packages/terafoundation/test/apis/prom-metrics-spec.ts index 35f45041b17..18728fc19ad 100644 --- a/packages/terafoundation/test/apis/prom-metrics-spec.ts +++ b/packages/terafoundation/test/apis/prom-metrics-spec.ts @@ -306,7 +306,7 @@ describe('promMetrics foundation API', () => { tf_prom_metrics_port: terafoundation.prom_metrics_port, tf_prom_metrics_add_default: terafoundation.prom_metrics_add_default, logger: debugLogger('prom-metrics-spec-logger'), - assignment: 'cluster-master', + assignment: 'master', labels: {}, prefix: 'foundation_test_' }; @@ -372,7 +372,7 @@ describe('promMetrics foundation API', () => { tf_prom_metrics_port: terafoundation.prom_metrics_port, tf_prom_metrics_add_default: terafoundation.prom_metrics_add_default, logger: debugLogger('prom-metrics-spec-logger'), - assignment: 'cluster-master', + assignment: 'master', labels: {}, prefix: 'foundation_test_' }; @@ -466,7 +466,7 @@ describe('promMetrics foundation API', () => { tf_prom_metrics_port: terafoundation.prom_metrics_port, tf_prom_metrics_add_default: terafoundation.prom_metrics_add_default, logger: debugLogger('prom-metrics-spec-logger'), - assignment: 'cluster-master', + assignment: 'master', prefix: 'foundation_test_' }; @@ -546,7 +546,7 @@ describe('promMetrics foundation API', () => { tf_prom_metrics_port: terafoundation.prom_metrics_port, tf_prom_metrics_add_default: terafoundation.prom_metrics_add_default, logger: debugLogger('prom-metrics-spec-logger'), - assignment: 'cluster-master', + assignment: 'master', prefix: 'foundation_test_' }; beforeAll(async () => { @@ -625,7 +625,7 @@ describe('promMetrics foundation API', () => { tf_prom_metrics_port: terafoundation.prom_metrics_port, tf_prom_metrics_add_default: terafoundation.prom_metrics_add_default, logger: debugLogger('prom-metrics-spec-logger'), - assignment: 'cluster-master', + assignment: 'master', prefix: 'foundation_test_', labels: { default1: 'value1' } }; @@ -642,7 +642,7 @@ describe('promMetrics foundation API', () => { it('should get all the default labels', () => { expect(context.apis.foundation.promMetrics.getDefaultLabels()).toEqual({ name: 'tera-test-labels', - assignment: 'cluster-master', + assignment: 'master', default1: 'value1' }); }); diff --git a/packages/terafoundation/test/test-context-spec.ts b/packages/terafoundation/test/test-context-spec.ts index a01f0f25ff9..d715e414568 100644 --- a/packages/terafoundation/test/test-context-spec.ts +++ b/packages/terafoundation/test/test-context-spec.ts @@ -130,7 +130,7 @@ describe('TestContext', () => { tf_prom_metrics_port: 3333, tf_prom_metrics_add_default: false, logger: context.logger, - assignment: 'cluster-master' + assignment: 'master' }; expect(await context.apis.foundation.promMetrics.init(config)).toBe(true); }); diff --git a/packages/teraslice-op-test-harness/package.json b/packages/teraslice-op-test-harness/package.json index 40b5604d058..c90f4fdf3f6 100644 --- a/packages/teraslice-op-test-harness/package.json +++ b/packages/teraslice-op-test-harness/package.json @@ -21,10 +21,10 @@ "bluebird": "^3.7.2" }, "devDependencies": { - "@terascope/job-components": "^0.74.1" + "@terascope/job-components": "^0.74.2" }, "peerDependencies": { - "@terascope/job-components": ">=0.74.1" + "@terascope/job-components": ">=0.74.2" }, "engines": { "node": ">=14.17.0", diff --git a/packages/teraslice-test-harness/package.json b/packages/teraslice-test-harness/package.json index 2bfb1e9e6da..afe48388d91 100644 --- a/packages/teraslice-test-harness/package.json +++ b/packages/teraslice-test-harness/package.json @@ -36,10 +36,10 @@ "fs-extra": "^11.2.0" }, "devDependencies": { - "@terascope/job-components": "^0.74.1" + "@terascope/job-components": "^0.74.2" }, "peerDependencies": { - "@terascope/job-components": ">=0.74.1" + "@terascope/job-components": ">=0.74.2" }, "engines": { "node": ">=14.17.0", diff --git a/packages/teraslice/package.json b/packages/teraslice/package.json index 0970a00c675..db2df751dc8 100644 --- a/packages/teraslice/package.json +++ b/packages/teraslice/package.json @@ -1,7 +1,7 @@ { "name": "teraslice", "displayName": "Teraslice", - "version": "1.5.1", + "version": "1.5.2", "description": "Distributed computing platform for processing JSON data", "homepage": "https://github.com/terascope/teraslice#readme", "bugs": { @@ -39,7 +39,7 @@ }, "dependencies": { "@terascope/elasticsearch-api": "^3.20.1", - "@terascope/job-components": "^0.74.1", + "@terascope/job-components": "^0.74.2", "@terascope/teraslice-messaging": "^0.42.1", "@terascope/types": "^0.17.1", "@terascope/utils": "^0.59.1", @@ -64,7 +64,7 @@ "semver": "^7.6.1", "socket.io": "^1.7.4", "socket.io-client": "^1.7.4", - "terafoundation": "^0.63.1", + "terafoundation": "^0.63.2", "uuid": "^9.0.1" }, "devDependencies": { diff --git a/packages/teraslice/src/lib/cluster/cluster_master.ts b/packages/teraslice/src/lib/cluster/cluster_master.ts index 56272dbae9e..720340a2709 100644 --- a/packages/teraslice/src/lib/cluster/cluster_master.ts +++ b/packages/teraslice/src/lib/cluster/cluster_master.ts @@ -150,7 +150,8 @@ export class ClusterMaster { tf_prom_metrics_enabled: terafoundation.prom_metrics_enabled, tf_prom_metrics_port: terafoundation.prom_metrics_port, logger: this.logger, - assignment: 'cluster_master' + assignment: 'master', + prefix: 'teraslice_' }); await this.setupPromMetrics(); @@ -229,7 +230,7 @@ export class ClusterMaster { */ await Promise.all([ this.context.apis.foundation.promMetrics.addGauge( - 'info', + 'master_info', 'Information about Teraslice cluster master', ['arch', 'clustering_type', 'name', 'node_version', 'platform', 'teraslice_version'] ), @@ -338,7 +339,7 @@ export class ClusterMaster { ]); this.context.apis.foundation.promMetrics.set( - 'info', + 'master_info', { arch: this.context.arch, clustering_type: this.context.sysconfig.teraslice.cluster_manager_type, diff --git a/packages/teraslice/src/lib/workers/execution-controller/index.ts b/packages/teraslice/src/lib/workers/execution-controller/index.ts index d79c28eb3fb..87c61461805 100644 --- a/packages/teraslice/src/lib/workers/execution-controller/index.ts +++ b/packages/teraslice/src/lib/workers/execution-controller/index.ts @@ -1113,9 +1113,7 @@ export class ExecutionController { * @link https://terascope.github.io/teraslice/docs/development/k8s#prometheus-metrics-api */ async setupPromMetrics() { - this.logger.info(`adding ${this.context.assignment} prom metrics...`); - // eslint-disable-next-line @typescript-eslint/no-this-alias - const self = this; + const { context, executionAnalytics } = this; await Promise.all([ this.context.apis.foundation.promMetrics.addGauge( 'info', @@ -1127,9 +1125,9 @@ export class ExecutionController { 'Number of slices processed by all workers', [], function collect() { - const slicesProcessed = self.executionAnalytics.get('processed'); + const slicesProcessed = executionAnalytics.get('processed'); const defaultLabels = { - ...self.context.apis.foundation.promMetrics.getDefaultLabels() + ...context.apis.foundation.promMetrics.getDefaultLabels() }; this.set(defaultLabels, slicesProcessed); } diff --git a/packages/teraslice/src/lib/workers/worker/index.ts b/packages/teraslice/src/lib/workers/worker/index.ts index d2dd4e55c94..228117e37eb 100644 --- a/packages/teraslice/src/lib/workers/worker/index.ts +++ b/packages/teraslice/src/lib/workers/worker/index.ts @@ -411,8 +411,7 @@ export class Worker { */ async setupPromMetrics() { this.logger.info(`adding ${this.context.assignment} prom metrics...`); - // eslint-disable-next-line @typescript-eslint/no-this-alias - const self = this; + const { context, getSlicesProcessed } = this; await Promise.all([ this.context.apis.foundation.promMetrics.addGauge( 'info', @@ -424,9 +423,9 @@ export class Worker { 'Number of slices the worker has processed', [], function collect() { - const slicesProcessed = self.getSlicesProcessed(); + const slicesProcessed = getSlicesProcessed(); const defaultLabels = { - ...self.context.apis.foundation.promMetrics.getDefaultLabels() + ...context.apis.foundation.promMetrics.getDefaultLabels() }; this.set(defaultLabels, slicesProcessed); }