Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement labelset #463

Merged
merged 24 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3cf95a0
feat: implement labelset
xiao-lix Oct 28, 2019
860fe6d
fix: linting
xiao-lix Oct 29, 2019
51ef29e
fix: doc strings
xiao-lix Oct 29, 2019
3599b53
fix: expose labels function in Meter
xiao-lix Oct 30, 2019
05d1c63
fix: linting
xiao-lix Oct 30, 2019
46110cc
Merge branch 'master' into xiao/labelset
xiao-lix Oct 31, 2019
821ad67
fix: address comments
xiao-lix Oct 31, 2019
ad37bde
fix: conflicts
xiao-lix Nov 1, 2019
dea892c
fix: udpate LabelSet logic
xiao-lix Nov 2, 2019
2a7e093
fix: linting
xiao-lix Nov 2, 2019
cfa46e5
fix: conflicts
xiao-lix Nov 4, 2019
bcfda83
Merge branch 'master' into xiao/labelset
xiao-lix Nov 4, 2019
9e9fc27
fix: address comments
xiao-lix Nov 4, 2019
79f1b9f
Merge branch 'xiao/labelset' of github.com:xiao-lix/opentelemetry-js …
xiao-lix Nov 4, 2019
88e5980
fix: update "encoded" to "identifier"
xiao-lix Nov 4, 2019
12751f3
Merge branch 'master' into xiao/labelset
mayurkale22 Nov 4, 2019
cd4d862
Merge branch 'master' into xiao/labelset
mayurkale22 Nov 5, 2019
167394e
chore: update naming
xiao-lix Nov 6, 2019
3b240cb
Merge branch 'xiao/labelset' of github.com:xiao-lix/opentelemetry-js …
xiao-lix Nov 6, 2019
2969570
fix: conflicts
xiao-lix Nov 7, 2019
eca2236
fix: conflicts
xiao-lix Nov 7, 2019
0d1e26d
Merge branch 'master' into xiao/labelset
Nov 7, 2019
58717cb
chore: update let to const
xiao-lix Nov 7, 2019
3727d8f
Merge branch 'xiao/labelset' of github.com:xiao-lix/opentelemetry-js …
xiao-lix Nov 7, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions packages/opentelemetry-core/src/metrics/NoopMeter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
MetricOptions,
MeasureHandle,
SpanContext,
LabelSet,
Labels,
} from '@opentelemetry/types';

/**
Expand Down Expand Up @@ -58,6 +60,10 @@ export class NoopMeter implements Meter {
createGauge(name: string, options?: MetricOptions): Metric<GaugeHandle> {
return NOOP_GAUGE_METRIC;
}

labels(labels: Labels): LabelSet {
return NOOP_LABEL_SET;
}
}

export class NoopMetric<T> implements Metric<T> {
Expand All @@ -67,12 +73,12 @@ export class NoopMetric<T> implements Metric<T> {
this._handle = handle;
}
/**
* Returns a Handle associated with specified label values.
* Returns a Handle associated with specified LabelSet.
* It is recommended to keep a reference to the Handle instead of always
* calling this method for every operations.
* @param labelValues the list of label values.
* @param labels the canonicalized LabelSet used to associate with this metric handle.
*/
getHandle(labelValues: string[]): T {
getHandle(labels: LabelSet): T {
return this._handle;
}

Expand All @@ -85,9 +91,10 @@ export class NoopMetric<T> implements Metric<T> {

/**
* Removes the Handle from the metric, if it is present.
* @param labelValues the list of label values.
* @param labels the canonicalized LabelSet used to associate with this metric handle.
*/
removeHandle(labelValues: string[]): void {
removeHandle(labels: LabelSet): void {
// @todo: implement this method
return;
xiao-lix marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -137,3 +144,5 @@ export const NOOP_MEASURE_HANDLE = new NoopMeasureHandle();
export const NOOP_MEASURE_METRIC = new NoopMetric<MeasureHandle>(
NOOP_MEASURE_HANDLE
);

export const NOOP_LABEL_SET = {} as LabelSet;
21 changes: 9 additions & 12 deletions packages/opentelemetry-core/test/metrics/NoopMeter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,26 @@ import {
NOOP_MEASURE_HANDLE,
NOOP_MEASURE_METRIC,
} from '../../src/metrics/NoopMeter';
import { Labels } from '@opentelemetry/types';

describe('NoopMeter', () => {
it('should not crash', () => {
const meter = new NoopMeter();
const counter = meter.createCounter('some-name');
const labels = {} as Labels;
const labelSet = meter.labels(labels);

// ensure NoopMetric does not crash.
counter.setCallback(() => {
assert.fail('callback occurred');
});
counter.getHandle(['val1', 'val2']).add(1);
counter.getHandle(labelSet).add(1);
counter.getDefaultHandle().add(1);
counter.removeHandle(['val1', 'val2']);
counter.removeHandle(labelSet);

// ensure the correct noop const is returned
assert.strictEqual(counter, NOOP_COUNTER_METRIC);
assert.strictEqual(
counter.getHandle(['val1', 'val2']),
NOOP_COUNTER_HANDLE
);
assert.strictEqual(counter.getHandle(labelSet), NOOP_COUNTER_HANDLE);
assert.strictEqual(counter.getDefaultHandle(), NOOP_COUNTER_HANDLE);
counter.clear();

Expand All @@ -61,23 +62,19 @@ describe('NoopMeter', () => {
// ensure the correct noop const is returned
assert.strictEqual(measure, NOOP_MEASURE_METRIC);
assert.strictEqual(measure.getDefaultHandle(), NOOP_MEASURE_HANDLE);
assert.strictEqual(
measure.getHandle(['val1', 'val2']),
NOOP_MEASURE_HANDLE
);
assert.strictEqual(measure.getHandle(labelSet), NOOP_MEASURE_HANDLE);

const gauge = meter.createGauge('some-name');
gauge.getDefaultHandle().set(1);

// ensure the correct noop const is returned
assert.strictEqual(gauge, NOOP_GAUGE_METRIC);
assert.strictEqual(gauge.getDefaultHandle(), NOOP_GAUGE_HANDLE);
assert.strictEqual(gauge.getHandle(['val1', 'val2']), NOOP_GAUGE_HANDLE);
assert.strictEqual(gauge.getHandle(labelSet), NOOP_GAUGE_HANDLE);

const options = {
component: 'tests',
description: 'the testing package',
labelKeys: ['key1', 'key2'],
};

const measureWithOptions = meter.createMeasure('some-name', options);
Expand Down
17 changes: 10 additions & 7 deletions packages/opentelemetry-metrics/src/Handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ import { TimeSeries } from './export/types';

/**
* CounterHandle allows the SDK to observe/record a single metric event. The
* value of single handle in the `Counter` associated with specified label
* values.
* value of single handle in the `Counter` associated with specified LabelSet.
*/
export class CounterHandle implements types.CounterHandle {
private _data = 0;

constructor(
private readonly _disabled: boolean,
private readonly _monotonic: boolean,
private readonly _labelValues: string[],
private readonly _labelSet: types.LabelSet,
private readonly _logger: types.Logger
) {}

Expand All @@ -50,23 +49,25 @@ export class CounterHandle implements types.CounterHandle {
*/
getTimeSeries(timestamp: types.HrTime): TimeSeries {
return {
labelValues: this._labelValues.map(value => ({ value })),
labelValues: Object.values(this._labelSet.labels).map(value => ({
value,
})),
points: [{ value: this._data, timestamp }],
};
}
}

/**
* GaugeHandle allows the SDK to observe/record a single metric event. The
* value of single handle in the `Gauge` associated with specified label values.
* value of single handle in the `Gauge` associated with specified LabelSet.
*/
export class GaugeHandle implements types.GaugeHandle {
private _data = 0;

constructor(
private readonly _disabled: boolean,
private readonly _monotonic: boolean,
private readonly _labelValues: string[],
private readonly _labelSet: types.LabelSet,
private readonly _logger: types.Logger
) {}

Expand All @@ -88,7 +89,9 @@ export class GaugeHandle implements types.GaugeHandle {
*/
getTimeSeries(timestamp: types.HrTime): TimeSeries {
return {
labelValues: this._labelValues.map(value => ({ value })),
labelValues: Object.values(this._labelSet.labels).map(value => ({
value,
})),
points: [{ value: this._data, timestamp }],
};
}
Expand Down
23 changes: 23 additions & 0 deletions packages/opentelemetry-metrics/src/Meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
DEFAULT_METRIC_OPTIONS,
DEFAULT_CONFIG,
MeterConfig,
LabelSet,
} from './types';

/**
Expand Down Expand Up @@ -116,6 +117,28 @@ export class Meter implements types.Meter {
return new GaugeMetric(name, opt);
}

/**
* Provide a pre-computed re-useable LabelSet by
* converting the unordered labels into a canonicalized
* set of lables with a unique labelSetKey, useful for pre-aggregation.
* @param labels user provided unordered Labels.
*/
labels(labels: types.Labels): types.LabelSet {
let keys = Object.keys(labels).sort();
mayurkale22 marked this conversation as resolved.
Show resolved Hide resolved
let labelSetKey = keys.reduce((result, key) => {
if (result.length > 2) {
result += ',';
}
result = result + key + ':' + labels[key];
return result;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

result += key + ':' + labels[key];
return result;

or maybe even just

return result += key + ':' + labels[key];

}, '|#');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generate labelSetKey as "|#key1:val1,key2:val2", not sure if we need the prefix "|#", just be consistent with GO here.

let sortedLabels = {} as types.Labels;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this work as let sortedLabels: types.Labels = {}? That's generally a better pattern because it makes sure that the object actually fits the type rather than doing a forceful cast to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@draffensperger Thanks, will fix it.

keys.forEach(key => {
sortedLabels[key] = labels[key];
});
return new LabelSet(labelSetKey, sortedLabels);
}

/**
* Ensure a metric name conforms to the following rules:
*
Expand Down
31 changes: 15 additions & 16 deletions packages/opentelemetry-metrics/src/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import * as types from '@opentelemetry/types';
import { hashLabelValues } from './Utils';
import { CounterHandle, GaugeHandle } from './Handle';
import { MetricOptions } from './types';

Expand All @@ -33,17 +32,17 @@ export abstract class Metric<T> implements types.Metric<T> {
}

/**
* Returns a Handle associated with specified label values.
* Returns a Handle associated with specified LabelSet.
* It is recommended to keep a reference to the Handle instead of always
* calling this method for each operation.
* @param labelValues the list of label values.
* @param labelSet the canonicalized LabelSet used to associate with this metric handle.
*/
getHandle(labelValues: string[]): T {
const hash = hashLabelValues(labelValues);
if (this._handles.has(hash)) return this._handles.get(hash)!;
getHandle(labelSet: types.LabelSet): T {
if (this._handles.has(labelSet.labelSetKey))
return this._handles.get(labelSet.labelSetKey)!;

const handle = this._makeHandle(labelValues);
this._handles.set(hash, handle);
const handle = this._makeHandle(labelSet);
this._handles.set(labelSet.labelSetKey, handle);
return handle;
}

Expand All @@ -58,10 +57,10 @@ export abstract class Metric<T> implements types.Metric<T> {

/**
* Removes the Handle from the metric, if it is present.
* @param labelValues the list of label values.
* @param labelSet the canonicalized LabelSet used to associate with this metric handle.
*/
removeHandle(labelValues: string[]): void {
this._handles.delete(hashLabelValues(labelValues));
removeHandle(labelSet: types.LabelSet): void {
this._handles.delete(labelSet.labelSetKey);
}

/**
Expand All @@ -77,19 +76,19 @@ export abstract class Metric<T> implements types.Metric<T> {
return;
}

protected abstract _makeHandle(labelValues: string[]): T;
protected abstract _makeHandle(labelSet: types.LabelSet): T;
}

/** This is a SDK implementation of Counter Metric. */
export class CounterMetric extends Metric<CounterHandle> {
constructor(name: string, options: MetricOptions) {
super(name, options);
}
protected _makeHandle(labelValues: string[]): CounterHandle {
protected _makeHandle(labelSet: types.LabelSet): CounterHandle {
return new CounterHandle(
this._disabled,
this._monotonic,
labelValues,
labelSet,
this._logger
);
}
Expand All @@ -100,11 +99,11 @@ export class GaugeMetric extends Metric<GaugeHandle> {
constructor(name: string, options: MetricOptions) {
super(name, options);
}
protected _makeHandle(labelValues: string[]): GaugeHandle {
protected _makeHandle(labelSet: types.LabelSet): GaugeHandle {
return new GaugeHandle(
this._disabled,
this._monotonic,
labelValues,
labelSet,
this._logger
);
}
Expand Down
12 changes: 0 additions & 12 deletions packages/opentelemetry-metrics/src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,3 @@
* See the License for the specific language governing permissions and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe delete Utils.ts?

* limitations under the License.
*/

const COMMA_SEPARATOR = ',';

/**
* Returns a string(comma separated) from the list of label values.
*
* @param labelValues The list of the label values.
* @returns The hashed label values string.
*/
export function hashLabelValues(labelValues: string[]): string {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With labelSetKey, there's no need to have this function.

return labelValues.sort().join(COMMA_SEPARATOR);
}
2 changes: 1 addition & 1 deletion packages/opentelemetry-metrics/src/export/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export enum MetricDescriptorType {
export interface TimeSeries {
/**
* The set of label values that uniquely identify this timeseries. Applies to
* all points. The order of label values must match that of label keys in the
* all points. The order of label values must match that of LabelSet keys in the
* metric descriptor.
*/
readonly labelValues: LabelValue[];
Expand Down
12 changes: 11 additions & 1 deletion packages/opentelemetry-metrics/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { LogLevel } from '@opentelemetry/core';
import { Logger } from '@opentelemetry/types';
import { Logger, Labels } from '@opentelemetry/types';

/** Options needed for SDK metric creation. */
export interface MetricOptions {
Expand Down Expand Up @@ -65,3 +65,13 @@ export const DEFAULT_METRIC_OPTIONS = {
unit: '1',
labelKeys: [],
};

export class LabelSet implements LabelSet {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few suggestions:

  1. Add JSDoc comment
  2. Move LabelSet to separate module (LabelSet.ts) (?)
  3. LabelSet implements LabelSet looks typo or is this intentional stuff?

labelSetKey: string;
labels: Labels;

constructor(labelSetKey: string, labels: Labels) {
this.labelSetKey = labelSetKey;
this.labels = labels;
}
}
Loading