mirror of
https://github.com/Unleash/unleash.git
synced 2025-08-04 13:48:56 +02:00
feat: default origin for impact metrics (#10362)
This commit is contained in:
parent
e125c0f072
commit
1ab8275996
@ -118,7 +118,7 @@ test('should store impact metrics in memory and be able to retrieve them', async
|
|||||||
'# TYPE unleash_counter_labeled_counter counter',
|
'# TYPE unleash_counter_labeled_counter counter',
|
||||||
);
|
);
|
||||||
expect(metricsText).toMatch(
|
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',
|
'# TYPE unleash_counter_bulk_counter counter',
|
||||||
);
|
);
|
||||||
expect(metricsText).toMatch(
|
expect(metricsText).toMatch(
|
||||||
/unleash_counter_bulk_counter{unleash_source="bulk"} 15/,
|
/unleash_counter_bulk_counter{unleash_source="bulk",unleash_origin="sdk"} 15/,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -39,10 +39,10 @@ describe('MetricsTranslator', () => {
|
|||||||
'# TYPE unleash_counter_labeled_counter counter',
|
'# TYPE unleash_counter_labeled_counter counter',
|
||||||
);
|
);
|
||||||
expect(result).toContain(
|
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(
|
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);
|
const result1 = await translator.translateAndSerializeMetrics(metrics1);
|
||||||
expect(result1).toContain(
|
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(
|
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 = [
|
const metrics2 = [
|
||||||
@ -146,10 +146,10 @@ describe('MetricsTranslator', () => {
|
|||||||
const result2 = await translator.translateAndSerializeMetrics(metrics2);
|
const result2 = await translator.translateAndSerializeMetrics(metrics2);
|
||||||
|
|
||||||
expect(result2).toContain(
|
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(
|
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',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -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 {
|
translateMetric(metric: Metric): Counter<string> | Gauge<string> | null {
|
||||||
const prefixedName = `unleash_${metric.type}_${metric.name}`;
|
const prefixedName = `unleash_${metric.type}_${metric.name}`;
|
||||||
const existingMetric = this.registry.getSingleMetric(prefixedName);
|
const existingMetric = this.registry.getSingleMetric(prefixedName);
|
||||||
|
|
||||||
const allLabelNames = new Set<string>();
|
const allLabelNames = new Set<string>();
|
||||||
|
allLabelNames.add('unleash_origin');
|
||||||
for (const sample of metric.samples) {
|
for (const sample of metric.samples) {
|
||||||
if (sample.labels) {
|
if (sample.labels) {
|
||||||
Object.keys(sample.labels).forEach((label) =>
|
Object.keys(sample.labels).forEach((label) =>
|
||||||
@ -81,14 +91,10 @@ export class MetricsTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const sample of metric.samples) {
|
for (const sample of metric.samples) {
|
||||||
if (sample.labels) {
|
|
||||||
counter.inc(
|
counter.inc(
|
||||||
this.transformLabels(sample.labels),
|
this.transformLabels(this.addOriginLabel(sample)),
|
||||||
sample.value,
|
sample.value,
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
counter.inc(sample.value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return counter;
|
return counter;
|
||||||
@ -118,14 +124,10 @@ export class MetricsTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const sample of metric.samples) {
|
for (const sample of metric.samples) {
|
||||||
if (sample.labels) {
|
|
||||||
gauge.set(
|
gauge.set(
|
||||||
this.transformLabels(sample.labels),
|
this.transformLabels(this.addOriginLabel(sample)),
|
||||||
sample.value,
|
sample.value,
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
gauge.set(sample.value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return gauge;
|
return gauge;
|
||||||
|
Loading…
Reference in New Issue
Block a user