From c2ddd0f01ab5544b174828a1283a6635f786fea8 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Mon, 2 Dec 2019 17:08:21 -0500 Subject: [PATCH] chore: alias static labels (#576) --- packages/opentelemetry-metrics/src/Meter.ts | 44 +++++++++++---------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/packages/opentelemetry-metrics/src/Meter.ts b/packages/opentelemetry-metrics/src/Meter.ts index 34622af07e..ba2b74cd56 100644 --- a/packages/opentelemetry-metrics/src/Meter.ts +++ b/packages/opentelemetry-metrics/src/Meter.ts @@ -42,6 +42,8 @@ export class Meter implements types.Meter { private readonly _metrics = new Map>(); private readonly _exporters: MetricExporter[] = []; + readonly labels = Meter.labels; + /** * Constructs a new Meter instance. */ @@ -150,6 +152,27 @@ export class Meter implements types.Meter { this._exporters.push(exporter); } + /** + * Provide a pre-computed re-useable LabelSet by + * converting the unordered labels into a canonicalized + * set of lables with an unique identifier, useful for pre-aggregation. + * @param labels user provided unordered Labels. + */ + static labels(labels: types.Labels): types.LabelSet { + const keys = Object.keys(labels).sort(); + const identifier = keys.reduce((result, key) => { + if (result.length > 2) { + result += ','; + } + return (result += key + ':' + labels[key]); + }, '|#'); + const sortedLabels: types.Labels = {}; + keys.forEach(key => { + sortedLabels[key] = labels[key]; + }); + return new LabelSet(identifier, sortedLabels); + } + /** * Send a single metric by name to all registered exporters */ @@ -188,27 +211,6 @@ export class Meter implements types.Meter { this._metrics.set(name, metric); } - /** - * Provide a pre-computed re-useable LabelSet by - * converting the unordered labels into a canonicalized - * set of labels with an unique identifier, useful for pre-aggregation. - * @param labels user provided unordered Labels. - */ - labels(labels: types.Labels): types.LabelSet { - const keys = Object.keys(labels).sort(); - const identifier = keys.reduce((result, key) => { - if (result.length > 2) { - result += ','; - } - return (result += key + ':' + labels[key]); - }, '|#'); - const sortedLabels: types.Labels = {}; - keys.forEach(key => { - sortedLabels[key] = labels[key]; - }); - return new LabelSet(identifier, sortedLabels); - } - /** * Ensure a metric name conforms to the following rules: *