1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-31 13:47:02 +02:00

feat: prefix unleash impact metrics labels (#10350)

This commit is contained in:
Mateusz Kwasniewski 2025-07-14 11:47:45 +02:00 committed by GitHub
parent fe471419b4
commit ca485959b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 11 deletions

View File

@ -118,7 +118,7 @@ test('should store impact metrics in memory and be able to retrieve them', async
'# TYPE unleash_counter_labeled_counter counter',
);
expect(metricsText).toMatch(
/unleash_counter_labeled_counter{foo="bar"} 15/,
/unleash_counter_labeled_counter{unleash_foo="bar"} 15/,
);
});
@ -167,6 +167,6 @@ test('should store impact metrics sent via bulk metrics endpoint', async () => {
'# TYPE unleash_counter_bulk_counter counter',
);
expect(metricsText).toMatch(
/unleash_counter_bulk_counter{source="bulk"} 15/,
/unleash_counter_bulk_counter{unleash_source="bulk"} 15/,
);
});

View File

@ -39,9 +39,11 @@ describe('MetricsTranslator', () => {
'# TYPE unleash_counter_labeled_counter counter',
);
expect(result).toContain(
'unleash_counter_labeled_counter{foo="bar"} 5',
'unleash_counter_labeled_counter{unleash_foo="bar"} 5',
);
expect(result).toContain(
'unleash_gauge_test_gauge{unleash_env="prod"} 10',
);
expect(result).toContain('unleash_gauge_test_gauge{env="prod"} 10');
});
it('should ignore unsupported metric types', async () => {
@ -110,10 +112,10 @@ describe('MetricsTranslator', () => {
const result1 = await translator.translateAndSerializeMetrics(metrics1);
expect(result1).toContain(
'unleash_counter_counter_with_labels{foo="bar"} 5',
'unleash_counter_counter_with_labels{unleash_foo="bar"} 5',
);
expect(result1).toContain(
'unleash_gauge_gauge_with_labels{env="prod"} 10',
'unleash_gauge_gauge_with_labels{unleash_env="prod"} 10',
);
const metrics2 = [
@ -144,10 +146,10 @@ describe('MetricsTranslator', () => {
const result2 = await translator.translateAndSerializeMetrics(metrics2);
expect(result2).toContain(
'unleash_counter_counter_with_labels{foo="bar",baz="qux"} 15',
'unleash_counter_counter_with_labels{unleash_foo="bar",unleash_baz="qux"} 15',
);
expect(result2).toContain(
'unleash_gauge_gauge_with_labels{env="prod",region="us-east"} 20',
'unleash_gauge_gauge_with_labels{unleash_env="prod",unleash_region="us-east"} 20',
);
});
});

View File

@ -30,6 +30,17 @@ export class MetricsTranslator {
);
}
private transformLabels(
labels: Record<string, string | number>,
): Record<string, string | number> {
return Object.fromEntries(
Object.entries(labels).map(([labelKey, value]) => [
`unleash_${labelKey}`,
value,
]),
);
}
translateMetric(metric: Metric): Counter<string> | Gauge<string> | null {
const prefixedName = `unleash_${metric.type}_${metric.name}`;
const existingMetric = this.registry.getSingleMetric(prefixedName);
@ -38,7 +49,7 @@ export class MetricsTranslator {
for (const sample of metric.samples) {
if (sample.labels) {
Object.keys(sample.labels).forEach((label) =>
allLabelNames.add(label),
allLabelNames.add(`unleash_${label}`),
);
}
}
@ -71,7 +82,10 @@ export class MetricsTranslator {
for (const sample of metric.samples) {
if (sample.labels) {
counter.inc(sample.labels, sample.value);
counter.inc(
this.transformLabels(sample.labels),
sample.value,
);
} else {
counter.inc(sample.value);
}
@ -105,7 +119,10 @@ export class MetricsTranslator {
for (const sample of metric.samples) {
if (sample.labels) {
gauge.set(sample.labels, sample.value);
gauge.set(
this.transformLabels(sample.labels),
sample.value,
);
} else {
gauge.set(sample.value);
}