import { Label } from './types'; export class NeverCaseError extends Error { constructor(_value: never) { super('should never happen'); } } // based on the openmetrics-documentation, the 3 symbols we have to handle are: // - \n ... the newline character // - \ ... the backslash character // - " ... the double-quote character export function escapeLabelValueInExactSelector(labelValue: string): string { return labelValue.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/"/g, '\\"'); } export function makeSelector(metricName: string, labels?: Label[], labelName?: string): string { if (!labels || labels.length === 0) { return metricName; } const allLabels = [...labels].filter((label) => label.name !== labelName && label.value !== ''); const allLabelTexts = allLabels.map((label) => { return `${label.name}${label.op}"${escapeLabelValueInExactSelector(label.value)}"`; }); return `${metricName}{${allLabelTexts.join(',')}}`; }