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 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
12 changes: 7 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,7 @@ import {
MetricOptions,
MeasureHandle,
SpanContext,
LabelSet,
} from '@opentelemetry/types';

/**
Expand Down Expand Up @@ -67,12 +68,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 LabelSet used to associate with this metric handle.
*/
getHandle(labelValues: string[]): T {
getHandle(labels: LabelSet): T {
return this._handle;
}

Expand All @@ -85,9 +86,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 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
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 { LabelSet } from '@opentelemetry/types';

describe('NoopMeter', () => {
it('should not crash', () => {
const meter = new NoopMeter();
const counter = meter.createCounter('some-name');
const key1 = 'key1';
const key2 = 'key2';
const labels: LabelSet = { [key1]: 'val1', [key2]: 'val2' };
// ensure NoopMetric does not crash.
counter.setCallback(() => {
assert.fail('callback occurred');
});
counter.getHandle(['val1', 'val2']).add(1);
counter.getHandle(labels).add(1);
counter.getDefaultHandle().add(1);
counter.removeHandle(['val1', 'val2']);
counter.removeHandle(labels);

// 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(labels), 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(labels), 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(labels), NOOP_GAUGE_HANDLE);

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

const measureWithOptions = meter.createMeasure('some-name', options);
Expand Down
13 changes: 6 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 _labels: types.LabelSet,
private readonly _logger: types.Logger
) {}

Expand All @@ -50,23 +49,23 @@ export class CounterHandle implements types.CounterHandle {
*/
getTimeSeries(timestamp: types.HrTime): TimeSeries {
return {
labelValues: this._labelValues.map(value => ({ value })),
labelValues: Object.values(this._labels).map(value => ({ value })),
xiao-lix marked this conversation as resolved.
Show resolved Hide resolved
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 _labels: types.LabelSet,
private readonly _logger: types.Logger
) {}

Expand All @@ -88,7 +87,7 @@ export class GaugeHandle implements types.GaugeHandle {
*/
getTimeSeries(timestamp: types.HrTime): TimeSeries {
return {
labelValues: this._labelValues.map(value => ({ value })),
labelValues: Object.values(this._labels).map(value => ({ value })),
points: [{ value: this._data, timestamp }],
};
}
Expand Down
26 changes: 13 additions & 13 deletions packages/opentelemetry-metrics/src/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ 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 labels the LabelSet used to associate with this metric handle.
*/
getHandle(labelValues: string[]): T {
const hash = hashLabelValues(labelValues);
getHandle(labels: types.LabelSet): T {
const hash = hashLabelValues(Object.values(labels));
xiao-lix marked this conversation as resolved.
Show resolved Hide resolved
Copy link

@lzchen lzchen Oct 31, 2019

Choose a reason for hiding this comment

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

Consider storing the hash value of a label set values in the actual object. This way, you don't have to perform an expensive conversion every single time you get a handle.

EDIT: Actually, I think the whole point of making LabelSet's a first-class API concept is to avoid the repeated expensive canonicalizing.

Copy link

Choose a reason for hiding this comment

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

This leads to the question: should LabelSet objects be canocalized by default, or only when created by meter.Labels(...)?
Thoughts? @mayurkale22 @xiao-lix

Copy link
Member

Choose a reason for hiding this comment

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

It was my impression that storing the canonicalized/serialized version in the LabelSet object in order to avoid re-computation was one of the primary reasons for their existence.

if (this._handles.has(hash)) return this._handles.get(hash)!;

const handle = this._makeHandle(labelValues);
const handle = this._makeHandle(labels);
this._handles.set(hash, handle);
return handle;
}
Expand All @@ -58,10 +58,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 labels the LabelSet used to associate with this metric handle.
*/
removeHandle(labelValues: string[]): void {
this._handles.delete(hashLabelValues(labelValues));
removeHandle(labels: types.LabelSet): void {
this._handles.delete(hashLabelValues(Object.values(labels)));
}

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

protected abstract _makeHandle(labelValues: string[]): T;
protected abstract _makeHandle(labels: 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(labels: types.LabelSet): CounterHandle {
return new CounterHandle(
this._disabled,
this._monotonic,
labelValues,
labels,
this._logger
);
}
Expand All @@ -100,11 +100,11 @@ export class GaugeMetric extends Metric<GaugeHandle> {
constructor(name: string, options: MetricOptions) {
super(name, options);
}
protected _makeHandle(labelValues: string[]): GaugeHandle {
protected _makeHandle(labels: types.LabelSet): GaugeHandle {
return new GaugeHandle(
this._disabled,
this._monotonic,
labelValues,
labels,
this._logger
);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/opentelemetry-metrics/src/export/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* opentelemetry-proto/opentelemetry/proto/metrics/v1/metrics.proto
*/

import { HrTime } from '@opentelemetry/types';
import { HrTime, LabelSet } from '@opentelemetry/types';
import { Resource, ExportResult } from '@opentelemetry/base';

xiao-lix marked this conversation as resolved.
Show resolved Hide resolved
export interface ReadableMetric {
Expand Down Expand Up @@ -61,8 +61,8 @@ export interface MetricDescriptor {
readonly unit: string;
/** MetricDescriptor type */
readonly type: MetricDescriptorType;
/** The label keys associated with the metric descriptor. */
readonly labelKeys: string[];
xiao-lix marked this conversation as resolved.
Show resolved Hide resolved
/** The label set associated with the metric descriptor. */
readonly labels: LabelSet;
xiao-lix marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down 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
4 changes: 0 additions & 4 deletions packages/opentelemetry-metrics/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ export interface MetricOptions {
/** The unit of the Metric values. */
unit: string;

xiao-lix marked this conversation as resolved.
Show resolved Hide resolved
/** The list of label keys for the Metric. */
labelKeys: string[];
xiao-lix marked this conversation as resolved.
Show resolved Hide resolved

/** The map of constant labels for the Metric. */
constantLabels?: Map<string, string>;

Expand Down Expand Up @@ -63,5 +60,4 @@ export const DEFAULT_METRIC_OPTIONS = {
disabled: false,
description: '',
unit: '1',
labelKeys: [],
};
Loading