Skip to content

Commit

Permalink
chore: alias static labels (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan authored and mayurkale22 committed Dec 2, 2019
1 parent 9297a4d commit c2ddd0f
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions packages/opentelemetry-metrics/src/Meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export class Meter implements types.Meter {
private readonly _metrics = new Map<string, Metric<BaseHandle>>();
private readonly _exporters: MetricExporter[] = [];

readonly labels = Meter.labels;

/**
* Constructs a new Meter instance.
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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:
*
Expand Down

0 comments on commit c2ddd0f

Please sign in to comment.