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

feat: default origin for impact metrics (#10362)

This commit is contained in:
Mateusz Kwasniewski 2025-07-16 14:45:44 +02:00 committed by GitHub
parent e125c0f072
commit 1ab8275996
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 24 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{unleash_foo="bar"} 15/,
/unleash_counter_labeled_counter{unleash_foo="bar",unleash_origin="sdk"} 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{unleash_source="bulk"} 15/,
/unleash_counter_bulk_counter{unleash_source="bulk",unleash_origin="sdk"} 15/,
);
});

View File

@ -39,10 +39,10 @@ describe('MetricsTranslator', () => {
'# TYPE unleash_counter_labeled_counter counter',
);
expect(result).toContain(
'unleash_counter_labeled_counter{unleash_foo="bar"} 5',
'unleash_counter_labeled_counter{unleash_foo="bar",unleash_origin="sdk"} 5',
);
expect(result).toContain(
'unleash_gauge_test_gauge{unleash_env="prod"} 10',
'unleash_gauge_test_gauge{unleash_env="prod",unleash_origin="sdk"} 10',
);
});
@ -112,10 +112,10 @@ describe('MetricsTranslator', () => {
const result1 = await translator.translateAndSerializeMetrics(metrics1);
expect(result1).toContain(
'unleash_counter_counter_with_labels{unleash_foo="bar"} 5',
'unleash_counter_counter_with_labels{unleash_foo="bar",unleash_origin="sdk"} 5',
);
expect(result1).toContain(
'unleash_gauge_gauge_with_labels{unleash_env="prod"} 10',
'unleash_gauge_gauge_with_labels{unleash_env="prod",unleash_origin="sdk"} 10',
);
const metrics2 = [
@ -146,10 +146,10 @@ describe('MetricsTranslator', () => {
const result2 = await translator.translateAndSerializeMetrics(metrics2);
expect(result2).toContain(
'unleash_counter_counter_with_labels{unleash_foo="bar",unleash_baz="qux"} 15',
'unleash_counter_counter_with_labels{unleash_foo="bar",unleash_baz="qux",unleash_origin="sdk"} 15',
);
expect(result2).toContain(
'unleash_gauge_gauge_with_labels{unleash_env="prod",unleash_region="us-east"} 20',
'unleash_gauge_gauge_with_labels{unleash_env="prod",unleash_region="us-east",unleash_origin="sdk"} 20',
);
});
});

View File

@ -41,11 +41,21 @@ export class MetricsTranslator {
);
}
private addOriginLabel(
sample: MetricSample,
): Record<string, string | number> {
return {
...(sample.labels || {}),
origin: (sample.labels && sample.labels.origin) || 'sdk',
};
}
translateMetric(metric: Metric): Counter<string> | Gauge<string> | null {
const prefixedName = `unleash_${metric.type}_${metric.name}`;
const existingMetric = this.registry.getSingleMetric(prefixedName);
const allLabelNames = new Set<string>();
allLabelNames.add('unleash_origin');
for (const sample of metric.samples) {
if (sample.labels) {
Object.keys(sample.labels).forEach((label) =>
@ -81,14 +91,10 @@ export class MetricsTranslator {
}
for (const sample of metric.samples) {
if (sample.labels) {
counter.inc(
this.transformLabels(sample.labels),
sample.value,
);
} else {
counter.inc(sample.value);
}
counter.inc(
this.transformLabels(this.addOriginLabel(sample)),
sample.value,
);
}
return counter;
@ -118,14 +124,10 @@ export class MetricsTranslator {
}
for (const sample of metric.samples) {
if (sample.labels) {
gauge.set(
this.transformLabels(sample.labels),
sample.value,
);
} else {
gauge.set(sample.value);
}
gauge.set(
this.transformLabels(this.addOriginLabel(sample)),
sample.value,
);
}
return gauge;