Skip to content

Commit

Permalink
Merge branch 'main' into add-grpc-compression
Browse files Browse the repository at this point in the history
  • Loading branch information
svetlanabrennan authored Mar 21, 2022
2 parents 92c83ac + 3fd1b1e commit 77ea22c
Show file tree
Hide file tree
Showing 25 changed files with 778 additions and 288 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class SyncInstrument {
/**
* The class implements {@link metrics.UpDownCounter} interface.
*/
export class UpDownCounter extends SyncInstrument implements metrics.UpDownCounter {
export class UpDownCounterInstrument extends SyncInstrument implements metrics.UpDownCounter {
/**
* Increment value of counter by the input. Inputs may be negative.
*/
Expand All @@ -52,7 +52,7 @@ export class UpDownCounter extends SyncInstrument implements metrics.UpDownCount
/**
* The class implements {@link metrics.Counter} interface.
*/
export class Counter extends SyncInstrument implements metrics.Counter {
export class CounterInstrument extends SyncInstrument implements metrics.Counter {
/**
* Increment value of counter by the input. Inputs may not be negative.
*/
Expand All @@ -69,7 +69,7 @@ export class Counter extends SyncInstrument implements metrics.Counter {
/**
* The class implements {@link metrics.Histogram} interface.
*/
export class Histogram extends SyncInstrument implements metrics.Histogram {
export class HistogramInstrument extends SyncInstrument implements metrics.Histogram {
/**
* Records a measurement. Value of the measurement must not be negative.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as metrics from '@opentelemetry/api-metrics-wip';
import { InstrumentationLibrary } from '@opentelemetry/core';
import { createInstrumentDescriptor, InstrumentDescriptor, InstrumentType } from './InstrumentDescriptor';
import { Counter, Histogram, UpDownCounter } from './Instruments';
import { CounterInstrument, HistogramInstrument, UpDownCounterInstrument } from './Instruments';
import { MeterProviderSharedState } from './state/MeterProviderSharedState';
import { MultiMetricStorage } from './state/MultiWritableMetricStorage';
import { SyncMetricStorage } from './state/SyncMetricStorage';
Expand Down Expand Up @@ -45,7 +45,7 @@ export class Meter implements metrics.Meter {
createHistogram(name: string, options?: metrics.HistogramOptions): metrics.Histogram {
const descriptor = createInstrumentDescriptor(name, InstrumentType.HISTOGRAM, options);
const storage = this._registerMetricStorage(descriptor);
return new Histogram(storage, descriptor);
return new HistogramInstrument(storage, descriptor);
}

/**
Expand All @@ -54,7 +54,7 @@ export class Meter implements metrics.Meter {
createCounter(name: string, options?: metrics.CounterOptions): metrics.Counter {
const descriptor = createInstrumentDescriptor(name, InstrumentType.COUNTER, options);
const storage = this._registerMetricStorage(descriptor);
return new Counter(storage, descriptor);
return new CounterInstrument(storage, descriptor);
}

/**
Expand All @@ -63,7 +63,7 @@ export class Meter implements metrics.Meter {
createUpDownCounter(name: string, options?: metrics.UpDownCounterOptions): metrics.UpDownCounter {
const descriptor = createInstrumentDescriptor(name, InstrumentType.UP_DOWN_COUNTER, options);
const storage = this._registerMetricStorage(descriptor);
return new UpDownCounter(storage, descriptor);
return new UpDownCounterInstrument(storage, descriptor);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import { InstrumentSelector } from './view/InstrumentSelector';
import { MeterSelector } from './view/MeterSelector';
import { View } from './view/View';
import { MetricCollector } from './state/MetricCollector';
import { Aggregation } from './view/Aggregation';
import { FilteringAttributesProcessor } from './view/AttributesProcessor';
import { InstrumentType } from './InstrumentDescriptor';
import { PatternPredicate } from './view/Predicate';

/**
* MeterProviderOptions provides an interface for configuring a MeterProvider.
Expand All @@ -33,6 +37,62 @@ export interface MeterProviderOptions {
resource?: Resource;
}

export type ViewOptions = {
/**
* If not provided, the Instrument name will be used by default. This will be used as the name of the metrics stream.
*/
name?: string,
/**
* If not provided, the Instrument description will be used by default.
*/
description?: string,
/**
* If provided, the attributes that are not in the list will be ignored.
* If not provided, all the attribute keys will be used by default.
*/
attributeKeys?: string[],
/**
* The {@link Aggregation} aggregation to be used.
*/
aggregation?: Aggregation,

// TODO: Add ExemplarReservoir
};

export type SelectorOptions = {
instrument?: {
/**
* The type of the Instrument(s).
*/
type?: InstrumentType,
/**
* Name of the Instrument(s) with wildcard support.
*/
name?: string,
}
meter?: {
/**
* The name of the Meter.
*/
name?: string;
/**
* The version of the Meter.
*/
version?: string;
/**
* The schema URL of the Meter.
*/
schemaUrl?: string;
}
};

function isViewOptionsEmpty(options: ViewOptions): boolean {
return (options.name == null &&
options.aggregation == null &&
options.attributeKeys == null &&
options.description == null);
}

/**
* This class implements the {@link metrics.MeterProvider} interface.
*/
Expand All @@ -50,8 +110,8 @@ export class MeterProvider implements metrics.MeterProvider {
getMeter(name: string, version = '', options: metrics.MeterOptions = {}): metrics.Meter {
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#meter-creation
if (this._shutdown) {
api.diag.warn('A shutdown MeterProvider cannot provide a Meter');
return metrics.NOOP_METER;
api.diag.warn('A shutdown MeterProvider cannot provide a Meter');
return metrics.NOOP_METER;
}

return new Meter(this._sharedState, { name, version, schemaUrl: options.schemaUrl });
Expand All @@ -69,9 +129,35 @@ export class MeterProvider implements metrics.MeterProvider {
this._sharedState.metricCollectors.push(collector);
}

addView(view: View, instrumentSelector: InstrumentSelector, meterSelector: MeterSelector) {
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view
this._sharedState.viewRegistry.addView(view, instrumentSelector, meterSelector);
addView(options: ViewOptions, selectorOptions?: SelectorOptions) {
if (isViewOptionsEmpty(options)) {
throw new Error('Cannot create view with no view arguments supplied');
}

// the SDK SHOULD NOT allow Views with a specified name to be declared with instrument selectors that
// may select more than one instrument (e.g. wild card instrument name) in the same Meter.
if (options.name != null &&
(selectorOptions?.instrument?.name == null ||
PatternPredicate.hasWildcard(selectorOptions.instrument.name))) {
throw new Error('Views with a specified name must be declared with an instrument selector that selects at most one instrument per meter.');
}

// Create AttributesProcessor if attributeKeys are defined set.
let attributesProcessor = undefined;
if (options.attributeKeys != null) {
attributesProcessor = new FilteringAttributesProcessor(options.attributeKeys);
}

const view = new View({
name: options.name,
description: options.description,
aggregation: options.aggregation,
attributesProcessor: attributesProcessor
});
const instrument = new InstrumentSelector(selectorOptions?.instrument);
const meter = new MeterSelector(selectorOptions?.meter);

this._sharedState.viewRegistry.addView(view, instrument, meter);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class DropAggregator implements Aggregator<undefined> {
}

toMetricData(
_instrumentDescriptor: InstrumentDescriptor,
_descriptor: InstrumentDescriptor,
_accumulationByAttributes: AccumulationRecord<undefined>[],
_startTime: HrTime,
_endTime: HrTime): Maybe<MetricData> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
AggregatorKind,
Histogram,
} from './types';
import { HistogramMetricData, PointDataType } from '../export/MetricData';
import { HistogramMetricData, DataPointType } from '../export/MetricData';
import { HrTime } from '@opentelemetry/api';
import { InstrumentDescriptor } from '../InstrumentDescriptor';
import { Maybe } from '../utils';
Expand Down Expand Up @@ -57,7 +57,7 @@ export class HistogramAccumulation implements Accumulation {
this._current.buckets.counts[this._boundaries.length] += 1;
}

toPoint(): Histogram {
toPointValue(): Histogram {
return this._current;
}
}
Expand Down Expand Up @@ -89,66 +89,66 @@ export class HistogramAggregator implements Aggregator<HistogramAccumulation> {
* merging accumulations with different boundaries.
*/
merge(previous: HistogramAccumulation, delta: HistogramAccumulation): HistogramAccumulation {
const previousPoint = previous.toPoint();
const deltaPoint = delta.toPoint();
const previousValue = previous.toPointValue();
const deltaValue = delta.toPointValue();

const previousCounts = previousPoint.buckets.counts;
const deltaCounts = deltaPoint.buckets.counts;
const previousCounts = previousValue.buckets.counts;
const deltaCounts = deltaValue.buckets.counts;

const mergedCounts = new Array(previousCounts.length);
for (let idx = 0; idx < previousCounts.length; idx++) {
mergedCounts[idx] = previousCounts[idx] + deltaCounts[idx];
}

return new HistogramAccumulation(previousPoint.buckets.boundaries, {
return new HistogramAccumulation(previousValue.buckets.boundaries, {
buckets: {
boundaries: previousPoint.buckets.boundaries,
boundaries: previousValue.buckets.boundaries,
counts: mergedCounts,
},
count: previousPoint.count + deltaPoint.count,
sum: previousPoint.sum + deltaPoint.sum,
count: previousValue.count + deltaValue.count,
sum: previousValue.sum + deltaValue.sum,
});
}

/**
* Returns a new DELTA aggregation by comparing two cumulative measurements.
*/
diff(previous: HistogramAccumulation, current: HistogramAccumulation): HistogramAccumulation {
const previousPoint = previous.toPoint();
const currentPoint = current.toPoint();
const previousValue = previous.toPointValue();
const currentValue = current.toPointValue();

const previousCounts = previousPoint.buckets.counts;
const currentCounts = currentPoint.buckets.counts;
const previousCounts = previousValue.buckets.counts;
const currentCounts = currentValue.buckets.counts;

const diffedCounts = new Array(previousCounts.length);
for (let idx = 0; idx < previousCounts.length; idx++) {
diffedCounts[idx] = currentCounts[idx] - previousCounts[idx];
}

return new HistogramAccumulation(previousPoint.buckets.boundaries, {
return new HistogramAccumulation(previousValue.buckets.boundaries, {
buckets: {
boundaries: previousPoint.buckets.boundaries,
boundaries: previousValue.buckets.boundaries,
counts: diffedCounts,
},
count: currentPoint.count - previousPoint.count,
sum: currentPoint.sum - previousPoint.sum,
count: currentValue.count - previousValue.count,
sum: currentValue.sum - previousValue.sum,
});
}

toMetricData(
metricDescriptor: InstrumentDescriptor,
descriptor: InstrumentDescriptor,
accumulationByAttributes: AccumulationRecord<HistogramAccumulation>[],
startTime: HrTime,
endTime: HrTime): Maybe<HistogramMetricData> {
return {
instrumentDescriptor: metricDescriptor,
pointDataType: PointDataType.HISTOGRAM,
pointData: accumulationByAttributes.map(([attributes, accumulation]) => {
descriptor,
dataPointType: DataPointType.HISTOGRAM,
dataPoints: accumulationByAttributes.map(([attributes, accumulation]) => {
return {
attributes,
startTime,
endTime,
point: accumulation.toPoint(),
value: accumulation.toPointValue(),
};
})
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { LastValue, AggregatorKind, Aggregator, Accumulation, AccumulationRecord } from './types';
import { HrTime } from '@opentelemetry/api';
import { hrTime, hrTimeToMicroseconds } from '@opentelemetry/core';
import { PointDataType, SingularMetricData } from '../export/MetricData';
import { DataPointType, SingularMetricData } from '../export/MetricData';
import { InstrumentDescriptor } from '../InstrumentDescriptor';
import { Maybe } from '../utils';

Expand All @@ -29,7 +29,7 @@ export class LastValueAccumulation implements Accumulation {
this.sampleTime = hrTime();
}

toPoint(): LastValue {
toPointValue(): LastValue {
return this._current;
}
}
Expand All @@ -50,7 +50,7 @@ export class LastValueAggregator implements Aggregator<LastValueAccumulation> {
merge(previous: LastValueAccumulation, delta: LastValueAccumulation): LastValueAccumulation {
// nanoseconds may lose precisions.
const latestAccumulation = hrTimeToMicroseconds(delta.sampleTime) >= hrTimeToMicroseconds(previous.sampleTime) ? delta : previous;
return new LastValueAccumulation(latestAccumulation.toPoint(), latestAccumulation.sampleTime);
return new LastValueAccumulation(latestAccumulation.toPointValue(), latestAccumulation.sampleTime);
}

/**
Expand All @@ -62,23 +62,23 @@ export class LastValueAggregator implements Aggregator<LastValueAccumulation> {
diff(previous: LastValueAccumulation, current: LastValueAccumulation): LastValueAccumulation {
// nanoseconds may lose precisions.
const latestAccumulation = hrTimeToMicroseconds(current.sampleTime) >= hrTimeToMicroseconds(previous.sampleTime) ? current : previous;
return new LastValueAccumulation(latestAccumulation.toPoint(), latestAccumulation.sampleTime);
return new LastValueAccumulation(latestAccumulation.toPointValue(), latestAccumulation.sampleTime);
}

toMetricData(
instrumentDescriptor: InstrumentDescriptor,
descriptor: InstrumentDescriptor,
accumulationByAttributes: AccumulationRecord<LastValueAccumulation>[],
startTime: HrTime,
endTime: HrTime): Maybe<SingularMetricData> {
return {
instrumentDescriptor,
pointDataType: PointDataType.SINGULAR,
pointData: accumulationByAttributes.map(([attributes, accumulation]) => {
descriptor,
dataPointType: DataPointType.SINGULAR,
dataPoints: accumulationByAttributes.map(([attributes, accumulation]) => {
return {
attributes,
startTime,
endTime,
point: accumulation.toPoint(),
value: accumulation.toPointValue(),
};
})
};
Expand Down
Loading

0 comments on commit 77ea22c

Please sign in to comment.