Skip to content

Commit

Permalink
Merge branch 'main' into lizf.update-grpc-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lizthegrey committed Apr 21, 2021
2 parents 07b35bc + d3989d3 commit 1975f3f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
Expand Up @@ -111,17 +111,19 @@ function stringify(
let labelsStr = '';

for (const [key, val] of Object.entries(labels)) {
const sanitizedLabelName = sanitizePrometheusMetricName(key);
hasLabel = true;
labelsStr += `${labelsStr.length > 0 ? ',' : ''}${key}="${escapeLabelValue(
val
)}"`;
labelsStr += `${
labelsStr.length > 0 ? ',' : ''
}${sanitizedLabelName}="${escapeLabelValue(val)}"`;
}
if (additionalLabels) {
for (const [key, val] of Object.entries(additionalLabels)) {
const sanitizedLabelName = sanitizePrometheusMetricName(key);
hasLabel = true;
labelsStr += `${
labelsStr.length > 0 ? ',' : ''
}${key}="${escapeLabelValue(val)}"`;
}${sanitizedLabelName}="${escapeLabelValue(val)}"`;
}
}

Expand Down
Expand Up @@ -487,6 +487,34 @@ describe('PrometheusSerializer', () => {
`} 1 ${mockedHrTimeMs}\n`
);
});

it('should sanitize label names', async () => {
const serializer = new PrometheusSerializer();

const meter = new MeterProvider({
processor: new ExactProcessor(SumAggregator),
}).getMeter('test');
const counter = meter.createCounter('test') as CounterMetric;
// if you try to use a label name like account-id prometheus will complain
// with an error like:
// error while linting: text format parsing error in line 282: expected '=' after label name, found '-'
counter
.bind(({
'account-id': '123456',
} as unknown) as Labels)
.add(1);
const records = await counter.getMetricRecord();
const record = records[0];

const result = serializer.serializeRecord(
record.descriptor.name,
record
);
assert.strictEqual(
result,
`test{account_id="123456"} 1 ${mockedHrTimeMs}\n`
);
});
});
});
});
Expand Up @@ -160,6 +160,10 @@ export abstract class InstrumentationBase<T = any>
}
}
}

public isEnabled() {
return this._enabled;
}
}

function isSupported(supportedVersions: string[], version: string): boolean {
Expand Down

0 comments on commit 1975f3f

Please sign in to comment.