Skip to content

Commit

Permalink
feat(opentelemetry-sdk-metrics-base): update SDK to use attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
pirgeo committed Oct 6, 2021
1 parent 085eb2e commit fb6c327
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export abstract class ObservableBaseMetric
this._callback = callback || NOOP_CALLBACK;
}

protected _makeInstrument(labels: api.Labels): BoundObservableBase {
protected _makeInstrument(attributes: api.Attributes): BoundObservableBase {
return new BoundObservableBase(
labels,
attributes,
this._disabled,
this._valueType,
this._processor.aggregatorFor(this._descriptor)
Expand All @@ -66,8 +66,8 @@ export abstract class ObservableBaseMetric
}

protected _processResults(observerResult: ObserverResult): void {
observerResult.values.forEach((value, labels) => {
const instrument = this.bind(labels);
observerResult.values.forEach((value, attributes) => {
const instrument = this.bind(attributes);
instrument.update(value);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ export class BatchObserverResult implements api.BatchObserverResult {
this._callback = callback;
}

observe(labels: api.Labels, observations: api.Observation[]): void {
observe(attributes: api.Attributes, observations: api.Observation[]): void {
if (this.cancelled || !this._callback) {
return;
}
observations.forEach(observation => {
observation.observer.bind(labels).update(observation.value);
observation.observer.bind(attributes).update(observation.value);
});
if (!this._immediate) {
this._immediate = setImmediate(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ import { Aggregator } from './export/types';
* the TimeSeries.
*/
export class BaseBoundInstrument {
protected _labels: api.Labels;
protected _attributes: api.Attributes;

constructor(
labels: api.Labels,
attributes: api.Attributes,
private readonly _disabled: boolean,
private readonly _valueType: api.ValueType,
private readonly _aggregator: Aggregator
) {
this._labels = labels;
this._attributes = attributes;
}

update(value: number): void {
if (this._disabled) return;
if (typeof value !== 'number') {
diag.error(
`Metric cannot accept a non-number value for ${Object.values(
this._labels
this._attributes
)}.`
);
return;
Expand All @@ -48,7 +48,7 @@ export class BaseBoundInstrument {
if (this._valueType === api.ValueType.INT && !Number.isInteger(value)) {
diag.warn(
`INT value type cannot accept a floating-point value for ${Object.values(
this._labels
this._attributes
)}, ignoring the fractional digits.`
);
value = Math.trunc(value);
Expand All @@ -57,8 +57,8 @@ export class BaseBoundInstrument {
this._aggregator.update(value);
}

getLabels(): api.Labels {
return this._labels;
getAttributes(): api.Attributes {
return this._attributes;
}

getAggregator(): Aggregator {
Expand All @@ -68,23 +68,23 @@ export class BaseBoundInstrument {

/**
* BoundCounter allows the SDK to observe/record a single metric event. The
* value of single instrument in the `Counter` associated with specified Labels.
* value of single instrument in the `Counter` associated with specified Attributes.
*/
export class BoundCounter
extends BaseBoundInstrument
implements api.BoundCounter {
constructor(
labels: api.Labels,
attributes: api.Attributes,
disabled: boolean,
valueType: api.ValueType,
aggregator: Aggregator
) {
super(labels, disabled, valueType, aggregator);
super(attributes, disabled, valueType, aggregator);
}

add(value: number): void {
if (value < 0) {
diag.error(`Counter cannot descend for ${Object.values(this._labels)}`);
diag.error(`Counter cannot descend for ${Object.values(this._attributes)}`);
return;
}

Expand All @@ -95,18 +95,18 @@ export class BoundCounter
/**
* BoundUpDownCounter allows the SDK to observe/record a single metric event.
* The value of single instrument in the `UpDownCounter` associated with
* specified Labels.
* specified Attributes.
*/
export class BoundUpDownCounter
extends BaseBoundInstrument
implements api.BoundCounter {
constructor(
labels: api.Labels,
attributes: api.Attributes,
disabled: boolean,
valueType: api.ValueType,
aggregator: Aggregator
) {
super(labels, disabled, valueType, aggregator);
super(attributes, disabled, valueType, aggregator);
}

add(value: number): void {
Expand All @@ -121,12 +121,12 @@ export class BoundHistogram
extends BaseBoundInstrument
implements api.BoundHistogram {
constructor(
labels: api.Labels,
attributes: api.Attributes,
disabled: boolean,
valueType: api.ValueType,
aggregator: Aggregator
) {
super(labels, disabled, valueType, aggregator);
super(attributes, disabled, valueType, aggregator);
}

record(value: number): void {
Expand All @@ -141,11 +141,11 @@ export class BoundObservableBase
extends BaseBoundInstrument
implements api.BoundObservableBase {
constructor(
labels: api.Labels,
attributes: api.Attributes,
disabled: boolean,
valueType: api.ValueType,
aggregator: Aggregator
) {
super(labels, disabled, valueType, aggregator);
super(attributes, disabled, valueType, aggregator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export class CounterMetric extends Metric<BoundCounter> implements api.Counter {
) {
super(name, options, MetricKind.COUNTER, resource, instrumentationLibrary);
}
protected _makeInstrument(labels: api.Labels): BoundCounter {
protected _makeInstrument(attributes: api.Attributes): BoundCounter {
return new BoundCounter(
labels,
attributes,
this._disabled,
this._valueType,
this._processor.aggregatorFor(this._descriptor)
Expand All @@ -45,10 +45,10 @@ export class CounterMetric extends Metric<BoundCounter> implements api.Counter {
/**
* Adds the given value to the current value. Values cannot be negative.
* @param value the value to add.
* @param [labels = {}] key-values pairs that are associated with a specific metric
* @param [attributes = {}] key-values pairs that are associated with a specific metric
* that you want to record.
*/
add(value: number, labels: api.Labels = {}): void {
this.bind(labels).add(value);
add(value: number, attributes: api.Attributes = {}): void {
this.bind(attributes).add(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ export class HistogramMetric
);
}

protected _makeInstrument(labels: api.Labels): BoundHistogram {
protected _makeInstrument(attributes: api.Attributes): BoundHistogram {
return new BoundHistogram(
labels,
attributes,
this._disabled,
this._valueType,
this._processor.aggregatorFor(this._descriptor)
);
}

record(value: number, labels: api.Labels = {}): void {
this.bind(labels).record(value);
record(value: number, attributes: api.Attributes = {}): void {
this.bind(attributes).record(value);
}
}
22 changes: 11 additions & 11 deletions experimental/packages/opentelemetry-sdk-metrics-base/src/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { InstrumentationLibrary } from '@opentelemetry/core';
import { Resource } from '@opentelemetry/resources';
import { BaseBoundInstrument } from './BoundInstrument';
import { MetricDescriptor, MetricKind, MetricRecord } from './export/types';
import { hashLabels } from './Utils';
import { hashAttributes as hashAttributes } from './Utils';

/** This is a SDK implementation of {@link Metric} interface. */
export abstract class Metric<T extends BaseBoundInstrument>
Expand Down Expand Up @@ -51,28 +51,28 @@ export abstract class Metric<T extends BaseBoundInstrument>
}

/**
* Returns an Instrument associated with specified Labels.
* Returns an Instrument associated with specified Attributes.
* It is recommended to keep a reference to the Instrument instead of always
* calling this method for each operation.
* @param labels key-values pairs that are associated with a specific metric
* @param attributes key-values pairs that are associated with a specific metric
* that you want to record.
*/
bind(labels: api.Labels): T {
const hash = hashLabels(labels);
bind(attributes: api.Attributes): T {
const hash = hashAttributes(attributes);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (this._instruments.has(hash)) return this._instruments.get(hash)!;

const instrument = this._makeInstrument(labels);
const instrument = this._makeInstrument(attributes);
this._instruments.set(hash, instrument);
return instrument;
}

/**
* Removes the Instrument from the metric, if it is present.
* @param labels key-values pairs that are associated with a specific metric.
* @param attributes key-values pairs that are associated with a specific metric.
*/
unbind(labels: api.Labels): void {
this._instruments.delete(hashLabels(labels));
unbind(attributes: api.Attributes): void {
this._instruments.delete(hashAttributes(attributes));
}

/**
Expand All @@ -98,7 +98,7 @@ export abstract class Metric<T extends BaseBoundInstrument>
resolve(
Array.from(this._instruments.values()).map(instrument => ({
descriptor: this._descriptor,
labels: instrument.getLabels(),
attributes: instrument.getAttributes(),
aggregator: instrument.getAggregator(),
aggregationTemporality: this.getAggregationTemporality(),
resource: this.resource,
Expand All @@ -119,5 +119,5 @@ export abstract class Metric<T extends BaseBoundInstrument>
};
}

protected abstract _makeInstrument(labels: api.Labels): T;
protected abstract _makeInstrument(attributes: api.Attributes): T;
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export class ObservableCounterMetric
}

protected override _processResults(observerResult: ObserverResult): void {
observerResult.values.forEach((value, labels) => {
const instrument = this.bind(labels);
observerResult.values.forEach((value, attributes) => {
const instrument = this.bind(attributes);
// ObservableCounter is monotonic which means it should only accept values
// greater or equal then previous value
const previous = instrument.getAggregator().toPoint();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

import {
ObserverResult as TypeObserverResult,
Labels,
Attributes,
} from '@opentelemetry/api-metrics';

/**
* Implementation of {@link TypeObserverResult}
*/
export class ObserverResult implements TypeObserverResult {
values: Map<Labels, number> = new Map<Labels, number>();
values: Map<Attributes, number> = new Map<Attributes, number>();

observe(value: number, labels: Labels): void {
this.values.set(labels, value);
observe(value: number, attributes: Attributes): void {
this.values.set(attributes, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export class UpDownCounterMetric
instrumentationLibrary
);
}
protected _makeInstrument(labels: api.Labels): BoundUpDownCounter {
protected _makeInstrument(attributes: api.Attributes): BoundUpDownCounter {
return new BoundUpDownCounter(
labels,
attributes,
this._disabled,
this._valueType,
this._processor.aggregatorFor(this._descriptor)
Expand All @@ -53,10 +53,10 @@ export class UpDownCounterMetric
/**
* Adds the given value to the current value. Values cannot be negative.
* @param value the value to add.
* @param [labels = {}] key-values pairs that are associated with a specific
* @param [attributes = {}] key-values pairs that are associated with a specific
* metric that you want to record.
*/
add(value: number, labels: api.Labels = {}): void {
this.bind(labels).add(value);
add(value: number, attributes: api.Attributes = {}): void {
this.bind(attributes).add(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { Labels } from '@opentelemetry/api-metrics';
import { Attributes } from '@opentelemetry/api-metrics';

/**
* Type guard to remove nulls from arrays
Expand All @@ -26,18 +26,18 @@ export function notNull<T>(value: T | null): value is T {
}

/**
* Converting the unordered labels into unique identifier string.
* @param labels user provided unordered Labels.
* Converting the unordered attributes into unique identifier string.
* @param attributes user provided unordered Attributes.
*/
export function hashLabels(labels: Labels): string {
let keys = Object.keys(labels);
export function hashAttributes(attributes: Attributes): string {
let keys = Object.keys(attributes);
if (keys.length === 0) return '';

keys = keys.sort();
return keys.reduce((result, key) => {
if (result.length > 2) {
result += ',';
}
return (result += key + ':' + labels[key]);
return (result += key + ':' + attributes[key]);
}, '|#');
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class ConsoleMetricExporter implements MetricExporter {
): void {
for (const metric of metrics) {
console.log(metric.descriptor);
console.log(metric.labels);
console.log(metric.attributes);
const point = metric.aggregator.toPoint();
if (typeof point.value === 'number') {
console.log('value: ' + point.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export abstract class Processor {
}

/**
* Processor which retains all dimensions/labels. It accepts all records and
* Processor which retains all dimensions/attributes. It accepts all records and
* passes them for exporting.
*/
export class UngroupedProcessor extends Processor {
Expand All @@ -70,9 +70,9 @@ export class UngroupedProcessor extends Processor {
}

process(record: MetricRecord): void {
const labels = Object.keys(record.labels)
.map(k => `${k}=${record.labels[k]}`)
const attributes = Object.keys(record.attributes)
.map(k => `${k}=${record.attributes[k]}`)
.join(',');
this._batchMap.set(record.descriptor.name + labels, record);
this._batchMap.set(record.descriptor.name + attributes, record);
}
}
Loading

0 comments on commit fb6c327

Please sign in to comment.