Skip to content

Commit b031c3b

Browse files
authored
Merge branch 'main' into instr-protected-enable
2 parents 5d84125 + 4f9d2b2 commit b031c3b

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,19 @@ function stringify(
111111
let labelsStr = '';
112112

113113
for (const [key, val] of Object.entries(labels)) {
114+
const sanitizedLabelName = sanitizePrometheusMetricName(key);
114115
hasLabel = true;
115-
labelsStr += `${labelsStr.length > 0 ? ',' : ''}${key}="${escapeLabelValue(
116-
val
117-
)}"`;
116+
labelsStr += `${
117+
labelsStr.length > 0 ? ',' : ''
118+
}${sanitizedLabelName}="${escapeLabelValue(val)}"`;
118119
}
119120
if (additionalLabels) {
120121
for (const [key, val] of Object.entries(additionalLabels)) {
122+
const sanitizedLabelName = sanitizePrometheusMetricName(key);
121123
hasLabel = true;
122124
labelsStr += `${
123125
labelsStr.length > 0 ? ',' : ''
124-
}${key}="${escapeLabelValue(val)}"`;
126+
}${sanitizedLabelName}="${escapeLabelValue(val)}"`;
125127
}
126128
}
127129

packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,34 @@ describe('PrometheusSerializer', () => {
487487
`} 1 ${mockedHrTimeMs}\n`
488488
);
489489
});
490+
491+
it('should sanitize label names', async () => {
492+
const serializer = new PrometheusSerializer();
493+
494+
const meter = new MeterProvider({
495+
processor: new ExactProcessor(SumAggregator),
496+
}).getMeter('test');
497+
const counter = meter.createCounter('test') as CounterMetric;
498+
// if you try to use a label name like account-id prometheus will complain
499+
// with an error like:
500+
// error while linting: text format parsing error in line 282: expected '=' after label name, found '-'
501+
counter
502+
.bind(({
503+
'account-id': '123456',
504+
} as unknown) as Labels)
505+
.add(1);
506+
const records = await counter.getMetricRecord();
507+
const record = records[0];
508+
509+
const result = serializer.serializeRecord(
510+
record.descriptor.name,
511+
record
512+
);
513+
assert.strictEqual(
514+
result,
515+
`test{account_id="123456"} 1 ${mockedHrTimeMs}\n`
516+
);
517+
});
490518
});
491519
});
492520
});

0 commit comments

Comments
 (0)