Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Prometheus metrics refactor #8484

Merged
merged 8 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ beforeEach(() => {
createFakeGetProductionChanges(),
);

const { collectDbMetrics } = registerPrometheusMetrics(
const { collectAggDbMetrics } = registerPrometheusMetrics(
config,
stores,
undefined as unknown as string,
config.eventBus,
instanceStatsService,
);
updateMetrics = collectDbMetrics;
updateMetrics = collectAggDbMetrics;

jest.spyOn(clientInstanceStore, 'getDistinctApplicationsCount');
jest.spyOn(instanceStatsService, 'getStats');
Expand Down
8 changes: 4 additions & 4 deletions src/lib/metrics-gauge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test('should collect registered metrics', async () => {
map: (result) => ({ value: result }),
});

await dbMetrics.refreshDbMetrics();
await dbMetrics.refreshMetrics();

const metrics = await prometheusRegister.metrics();
expect(metrics).toMatch(/my_metric 42/);
Expand All @@ -43,7 +43,7 @@ test('should collect registered metrics with labels', async () => {
map: (result) => ({ value: result, labels: { test: 'case' } }),
});

await dbMetrics.refreshDbMetrics();
await dbMetrics.refreshMetrics();

const metrics = await prometheusRegister.metrics();
expect(metrics).toMatch(
Expand All @@ -68,7 +68,7 @@ test('should collect multiple registered metrics with and without labels', async
map: (result) => ({ value: result, labels: { euler: 'number' } }),
});

await dbMetrics.refreshDbMetrics();
await dbMetrics.refreshMetrics();

const metrics = await prometheusRegister.metrics();
expect(metrics).toMatch(/my_first_metric 42/);
Expand All @@ -91,7 +91,7 @@ test('should support different label and value pairs', async () => {
],
});

await dbMetrics.refreshDbMetrics();
await dbMetrics.refreshMetrics();

const metrics = await prometheusRegister.metrics();
expect(metrics).toMatch(
Expand Down
32 changes: 27 additions & 5 deletions src/lib/metrics-gauge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type MapResult<R, L extends string> = (
type GaugeDefinition<T, L extends string> = {
name: string;
help: string;
labelNames: L[];
labelNames?: L[];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Support gauges without labels

query: Query<T>;
map: MapResult<T, L>;
};
Expand All @@ -37,17 +37,39 @@ export class DbMetricsMonitor {
return Array.isArray(value) ? value : [value];
}

private async fetch<T, L extends string>(
definition: GaugeDefinition<T, L>,
): Promise<MetricValue<L>[]> {
const result = await definition.query();
if (
result !== undefined &&
result !== null &&
(!Array.isArray(result) || result.length > 0)
) {
const resultArray = this.asArray(definition.map(result));
resultArray
.filter((r) => typeof r.value !== 'number')
.forEach((r) => {
this.log.debug(
`Invalid value for ${definition.name}: ${r.value}. Value must be an number.`,
);
});
return resultArray.filter((r) => typeof r.value === 'number');
}
return [];
}

registerGaugeDbMetric<T, L extends string>(
definition: GaugeDefinition<T, L>,
): Task {
const gauge = createGauge(definition);
const task = async () => {
try {
const result = await definition.query();
if (result !== null && result !== undefined) {
const results = this.asArray(definition.map(result));
const results = await this.fetch(definition);
if (results.length > 0) {
gauge.reset();
for (const r of results) {
// when r.value is zero, we are writing a zero value to the gauge which might not be what we want in some cases
if (r.labels) {
gauge.labels(r.labels).set(r.value);
} else {
Expand All @@ -63,7 +85,7 @@ export class DbMetricsMonitor {
return task;
}

refreshDbMetrics = async () => {
refreshMetrics = async () => {
const tasks = Array.from(this.updaters.entries()).map(
([name, updater]) => ({ name, task: updater.task }),
);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ beforeAll(async () => {
},
};

const { collectDbMetrics, collectStaticCounters } =
const { collectAggDbMetrics, collectStaticCounters } =
registerPrometheusMetrics(
config,
stores,
'4.0.0',
eventBus,
statsService,
);
refreshDbMetrics = collectDbMetrics;
refreshDbMetrics = collectAggDbMetrics;
await collectStaticCounters();
});

Expand Down
Loading
Loading