diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index d054a29f93..82ba13b648 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -16,7 +16,7 @@ jobs: NPM_CONFIG_UNSAFE_PERM: true steps: - name: Checkout - uses: actions/checkout@v1 + uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: @@ -49,14 +49,14 @@ jobs: browser-tests-stable: runs-on: ubuntu-latest container: - image: circleci/node:14-browsers + image: circleci/node:16-browsers env: NPM_CONFIG_UNSAFE_PERM: true steps: - - name: Checkout - uses: actions/checkout@v1 - name: Permission Setup run: sudo chmod -R 777 /github /__w + - name: Checkout + uses: actions/checkout@v2 - name: restore lerna uses: actions/cache@v2 @@ -126,7 +126,7 @@ jobs: NPM_CONFIG_UNSAFE_PERM: true steps: - name: Checkout - uses: actions/checkout@v1 + uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: @@ -163,14 +163,14 @@ jobs: browser-tests-experimental: runs-on: ubuntu-latest container: - image: circleci/node:14-browsers + image: circleci/node:16-browsers env: NPM_CONFIG_UNSAFE_PERM: true steps: - - name: Checkout - uses: actions/checkout@v1 - name: Permission Setup run: sudo chmod -R 777 /github /__w + - name: Checkout + uses: actions/checkout@v2 - name: restore lerna uses: actions/cache@v2 diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/InstrumentDescriptor.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/InstrumentDescriptor.ts index 8aa57c9f68..92ba0f7017 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/InstrumentDescriptor.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/InstrumentDescriptor.ts @@ -15,9 +15,19 @@ */ import { MetricOptions, ValueType } from '@opentelemetry/api-metrics-wip'; -import { InstrumentType } from './Instruments'; import { View } from './view/View'; +/** + * Supported types of metric instruments. + */ + export enum InstrumentType { + COUNTER = 'COUNTER', + HISTOGRAM = 'HISTOGRAM', + UP_DOWN_COUNTER = 'UP_DOWN_COUNTER', + OBSERVABLE_COUNTER = 'OBSERVABLE_COUNTER', + OBSERVABLE_GAUGE = 'OBSERVABLE_GAUGE', + OBSERVABLE_UP_DOWN_COUNTER = 'OBSERVABLE_UP_DOWN_COUNTER', +} export interface InstrumentDescriptor { readonly name: string; diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/Instruments.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/Instruments.ts index decf704794..60dbc9e364 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/Instruments.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/Instruments.ts @@ -19,17 +19,6 @@ import * as metrics from '@opentelemetry/api-metrics-wip'; import { InstrumentDescriptor } from './InstrumentDescriptor'; import { WritableMetricStorage } from './state/WritableMetricStorage'; -// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument - -export enum InstrumentType { - COUNTER = 'COUNTER', - HISTOGRAM = 'HISTOGRAM', - UP_DOWN_COUNTER = 'UP_DOWN_COUNTER', - OBSERVABLE_COUNTER = 'OBSERVABLE_COUNTER', - OBSERVABLE_GAUGE = 'OBSERVABLE_GAUGE', - OBSERVABLE_UP_DOWN_COUNTER = 'OBSERVABLE_UP_DOWN_COUNTER', -} - export class SyncInstrument { constructor(private _writableMetricStorage: WritableMetricStorage, private _descriptor: InstrumentDescriptor) { } @@ -42,13 +31,25 @@ export class SyncInstrument { } } -export class UpDownCounter extends SyncInstrument implements metrics.Counter { +/** + * The class implements {@link metrics.UpDownCounter} interface. + */ +export class UpDownCounter extends SyncInstrument implements metrics.UpDownCounter { + /** + * Increment value of counter by the input. Inputs may be negative. + */ add(value: number, attributes?: metrics.Attributes, ctx?: api.Context): void { this.aggregate(value, attributes, ctx); } } +/** + * The class implements {@link metrics.Counter} interface. + */ export class Counter extends SyncInstrument implements metrics.Counter { + /** + * Increment value of counter by the input. Inputs may not be negative. + */ add(value: number, attributes?: metrics.Attributes, ctx?: api.Context): void { if (value < 0) { api.diag.warn(`negative value provided to counter ${this.getName()}: ${value}`); @@ -59,7 +60,13 @@ export class Counter extends SyncInstrument implements metrics.Counter { } } +/** + * The class implements {@link metrics.Histogram} interface. + */ export class Histogram extends SyncInstrument implements metrics.Histogram { + /** + * Records a measurement. Value of the measurement must not be negative. + */ record(value: number, attributes?: metrics.Attributes, ctx?: api.Context): void { this.aggregate(value, attributes, ctx); } diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/Meter.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/Meter.ts index 7808db28e2..cc4c54545c 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/Meter.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/Meter.ts @@ -16,8 +16,8 @@ import * as metrics from '@opentelemetry/api-metrics-wip'; import { InstrumentationLibrary } from '@opentelemetry/core'; -import { createInstrumentDescriptor, InstrumentDescriptor } from './InstrumentDescriptor'; -import { Counter, Histogram, InstrumentType, UpDownCounter } from './Instruments'; +import { createInstrumentDescriptor, InstrumentDescriptor, InstrumentType } from './InstrumentDescriptor'; +import { Counter, Histogram, UpDownCounter } from './Instruments'; import { MeterProviderSharedState } from './state/MeterProviderSharedState'; import { MultiMetricStorage } from './state/MultiWritableMetricStorage'; import { SyncMetricStorage } from './state/SyncMetricStorage'; @@ -28,40 +28,46 @@ import { MetricCollectorHandle } from './state/MetricCollector'; import { HrTime } from '@opentelemetry/api'; import { AsyncMetricStorage } from './state/AsyncMetricStorage'; -// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#meter - +/** + * This class implements the {@link metrics.Meter} interface. + */ export class Meter implements metrics.Meter { private _metricStorageRegistry = new Map(); - // instrumentation library required by spec to be on meter - // spec requires provider config changes to apply to previously created meters, achieved by holding a reference to the provider constructor(private _meterProviderSharedState: MeterProviderSharedState, private _instrumentationLibrary: InstrumentationLibrary) { this._meterProviderSharedState.meters.push(this); } - /** this exists just to prevent ts errors from unused variables and may be removed */ - getInstrumentationLibrary(): InstrumentationLibrary { - return this._instrumentationLibrary; - } - + /** + * Create a {@link metrics.Histogram} instrument. + */ 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); } + /** + * Create a {@link metrics.Counter} instrument. + */ 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); } + /** + * Create a {@link metrics.UpDownCounter} instrument. + */ 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); } + /** + * Create a ObservableGauge instrument. + */ createObservableGauge( name: string, callback: metrics.ObservableCallback, @@ -71,6 +77,9 @@ export class Meter implements metrics.Meter { this._registerAsyncMetricStorage(descriptor, callback); } + /** + * Create a ObservableCounter instrument. + */ createObservableCounter( name: string, callback: metrics.ObservableCallback, @@ -80,6 +89,9 @@ export class Meter implements metrics.Meter { this._registerAsyncMetricStorage(descriptor, callback); } + /** + * Create a ObservableUpDownCounter instrument. + */ createObservableUpDownCounter( name: string, callback: metrics.ObservableCallback, @@ -112,6 +124,12 @@ export class Meter implements metrics.Meter { }); } + /** + * @internal + * @param collector opaque handle of {@link MetricCollector} which initiated the collection. + * @param collectionTime the HrTime at which the collection was initiated. + * @returns the list of {@link MetricData} collected. + */ async collect(collector: MetricCollectorHandle, collectionTime: HrTime): Promise { const result = await Promise.all(Array.from(this._metricStorageRegistry.values()).map(metricStorage => { return metricStorage.collect( diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/MeterProvider.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/MeterProvider.ts index d7b30e3081..d9e6d951a1 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/MeterProvider.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/MeterProvider.ts @@ -25,13 +25,18 @@ import { MeterSelector } from './view/MeterSelector'; import { View } from './view/View'; import { MetricCollector } from './state/MetricCollector'; -// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#meterprovider - -export type MeterProviderOptions = { +/** + * MeterProviderOptions provides an interface for configuring a MeterProvider. + */ +export interface MeterProviderOptions { + /** Resource associated with metric telemetry */ resource?: Resource; -}; +} -export class MeterProvider { +/** + * This class implements the {@link metrics.MeterProvider} interface. + */ +export class MeterProvider implements metrics.MeterProvider { private _sharedState: MeterProviderSharedState; private _shutdown = false; @@ -39,6 +44,9 @@ export class MeterProvider { this._sharedState = new MeterProviderSharedState(options?.resource ?? Resource.empty()); } + /** + * Get a meter with the configuration of the 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) { @@ -49,6 +57,12 @@ export class MeterProvider { return new Meter(this._sharedState, { name, version, schemaUrl: options.schemaUrl }); } + /** + * Register a {@link MetricReader} to the meter provider. After the + * registration, the MetricReader can start metrics collection. + * + * @param metricReader the metric reader to be registered. + */ addMetricReader(metricReader: MetricReader) { const collector = new MetricCollector(this._sharedState, metricReader); metricReader.setMetricProducer(collector); diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/ObservableResult.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/ObservableResult.ts index c10bf7514e..7ca9f9aea1 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/ObservableResult.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/ObservableResult.ts @@ -17,9 +17,18 @@ import * as metrics from '@opentelemetry/api-metrics-wip'; import { AttributeHashMap } from './state/HashMap'; +/** + * The class implements {@link metrics.observableResult} interface. + */ export class ObservableResult implements metrics.ObservableResult { + /** + * @internal + */ buffer = new AttributeHashMap(); + /** + * Observe a measurement of the value associated with the given attributes. + */ observe(value: number, attributes: metrics.Attributes = {}): void { this.buffer.set(attributes, value); } diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/exemplar/AlignedHistogramBucketExemplarReservoir.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/exemplar/AlignedHistogramBucketExemplarReservoir.ts index 9c09abf9cf..f69487ec06 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/exemplar/AlignedHistogramBucketExemplarReservoir.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/exemplar/AlignedHistogramBucketExemplarReservoir.ts @@ -22,9 +22,8 @@ import { FixedSizeExemplarReservoirBase } from './ExemplarReservoir'; /** * AlignedHistogramBucketExemplarReservoir takes the same boundaries - * configuration of a Histogram. This alogorithm keeps the last seen measurement + * configuration of a Histogram. This algorithm keeps the last seen measurement * that falls within a histogram bucket. - * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#exemplar-defaults */ export class AlignedHistogramBucketExemplarReservoir extends FixedSizeExemplarReservoirBase { private _boundaries: number[]; diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/export/AggregationTemporality.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/export/AggregationTemporality.ts index 744d936d01..6cc6d1231b 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/export/AggregationTemporality.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/export/AggregationTemporality.ts @@ -16,8 +16,6 @@ /** * AggregationTemporality indicates the way additive quantities are expressed. - * - * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/datamodel.md#temporality */ export enum AggregationTemporality { DELTA, diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/export/MetricData.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/export/MetricData.ts index d989baa0b7..736c83034a 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/export/MetricData.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/export/MetricData.ts @@ -21,31 +21,63 @@ import { Resource } from '@opentelemetry/resources'; import { InstrumentDescriptor } from '../InstrumentDescriptor'; import { Histogram } from '../aggregator/types'; +/** + * Basic metric data fields. + */ export interface BaseMetricData { + /** + * Resource associated with metric telemetry. + */ readonly resource: Resource; + /** + * InstrumentationLibrary which created the metric instrument. + */ readonly instrumentationLibrary: InstrumentationLibrary; + /** + * InstrumentDescriptor which describes the metric instrument. + */ readonly instrumentDescriptor: InstrumentDescriptor; + /** + * PointDataType of the metric instrument. + */ readonly pointDataType: PointDataType, } +/** + * Represents a metric data aggregated by either a LastValueAggregation or + * SumAggregation. + */ export interface SingularMetricData extends BaseMetricData { readonly pointDataType: PointDataType.SINGULAR, readonly pointData: PointData[], } +/** + * Represents a metric data aggregated by a HistogramAggregation. + */ export interface HistogramMetricData extends BaseMetricData { readonly pointDataType: PointDataType.HISTOGRAM, readonly pointData: PointData[], } +/** + * Represents an aggregated metric data. + */ export type MetricData = SingularMetricData | HistogramMetricData; +/** + * The aggregated point data type. + */ export enum PointDataType { SINGULAR, HISTOGRAM, EXPONENTIAL_HISTOGRAM, } +/** + * Represents an aggregated point data with start time, end time and their + * associated attributes and points. + */ export interface PointData { /** * The start epoch timestamp of the PointData, usually the time when diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/index.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/index.ts index 35622792a2..ba9b60de02 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/index.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/index.ts @@ -14,6 +14,15 @@ * limitations under the License. */ -export { MeterProvider, MeterProviderOptions } from './MeterProvider'; +export * from './export/AggregationTemporality'; +export * from './export/MetricData'; export * from './export/MetricExporter'; +export * from './export/MetricProducer'; export * from './export/MetricReader'; +export * from './export/PeriodicExportingMetricReader'; +export { InstrumentDescriptor, InstrumentType } from './InstrumentDescriptor'; +export * from './Instruments'; +export * from './Meter'; +export * from './MeterProvider'; +export * from './ObservableResult'; +export { TimeoutError } from './utils'; diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/view/Aggregation.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/view/Aggregation.ts index b2b7b6a32c..601461238a 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/view/Aggregation.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/view/Aggregation.ts @@ -17,14 +17,11 @@ import * as api from '@opentelemetry/api'; import { Aggregator, SumAggregator, DropAggregator, LastValueAggregator, HistogramAggregator } from '../aggregator'; import { Accumulation } from '../aggregator/types'; -import { InstrumentDescriptor } from '../InstrumentDescriptor'; -import { InstrumentType } from '../Instruments'; +import { InstrumentDescriptor, InstrumentType } from '../InstrumentDescriptor'; import { Maybe } from '../utils'; -// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#aggregation - /** - * Configures how measurements are combined into metrics for {@link View}s. + * Configures how measurements are combined into metrics for views. * * Aggregation provides a set of built-in aggregations via static methods. */ @@ -52,6 +49,9 @@ export abstract class Aggregation { } } +/** + * The default drop aggregation. + */ export class DropAggregation extends Aggregation { private static DEFAULT_INSTANCE = new DropAggregator(); createAggregator(_instrument: InstrumentDescriptor) { @@ -59,6 +59,9 @@ export class DropAggregation extends Aggregation { } } +/** + * The default sum aggregation. + */ export class SumAggregation extends Aggregation { private static DEFAULT_INSTANCE = new SumAggregator(); createAggregator(_instrument: InstrumentDescriptor) { @@ -66,6 +69,9 @@ export class SumAggregation extends Aggregation { } } +/** + * The default last value aggregation. + */ export class LastValueAggregation extends Aggregation { private static DEFAULT_INSTANCE = new LastValueAggregator(); createAggregator(_instrument: InstrumentDescriptor) { @@ -73,15 +79,23 @@ export class LastValueAggregation extends Aggregation { } } +/** + * The default histogram aggregation. + */ export class HistogramAggregation extends Aggregation { - // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#histogram-aggregation private static DEFAULT_INSTANCE = new HistogramAggregator([0, 5, 10, 25, 50, 75, 100, 250, 500, 1000]); createAggregator(_instrument: InstrumentDescriptor) { return HistogramAggregation.DEFAULT_INSTANCE; } } +/** + * The explicit bucket histogram aggregation. + */ export class ExplicitBucketHistogramAggregation extends Aggregation { + /** + * @param _boundaries the bucket boundaries of the histogram aggregation + */ constructor(private _boundaries: number[]) { super(); } @@ -91,7 +105,9 @@ export class ExplicitBucketHistogramAggregation extends Aggregation { } } -// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#default-aggregation +/** + * The default aggregation. + */ export class DefaultAggregation extends Aggregation { private _resolve(instrument: InstrumentDescriptor): Aggregation { // cast to unknown to disable complaints on the (unreachable) fallback. diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/view/AttributesProcessor.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/view/AttributesProcessor.ts index 5ed8d2c0ab..0a6aa4706e 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/view/AttributesProcessor.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/view/AttributesProcessor.ts @@ -43,4 +43,22 @@ export class NoopAttributesProcessor extends AttributesProcessor { } } +/** + * {@link AttributesProcessor} that filters by allowed attribute names and drops any names that are not in the + * allow list. + */ +export class FilteringAttributesProcessor extends AttributesProcessor { + constructor(private _allowedAttributeNames: string[]) { + super(); + } + + process(incoming: Attributes, _context: Context): Attributes { + const filteredAttributes: Attributes = {}; + Object.keys(incoming) + .filter(attributeName => this._allowedAttributeNames.includes(attributeName)) + .forEach(attributeName => filteredAttributes[attributeName] = incoming[attributeName]); + return filteredAttributes; + } +} + const NOOP = new NoopAttributesProcessor; diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/src/view/InstrumentSelector.ts b/experimental/packages/opentelemetry-sdk-metrics-base/src/view/InstrumentSelector.ts index c7dc6fa28e..e7bec3a44a 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/src/view/InstrumentSelector.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/src/view/InstrumentSelector.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { InstrumentType } from '../Instruments'; +import { InstrumentType } from '../InstrumentDescriptor'; import { PatternPredicate, Predicate } from './Predicate'; export interface InstrumentSelectorCriteria { diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/util.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/util.ts index 301a2a9e44..917277e436 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/util.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/util.ts @@ -18,8 +18,8 @@ import { Attributes, ValueType } from '@opentelemetry/api-metrics'; import { InstrumentationLibrary } from '@opentelemetry/core'; import { Resource } from '@opentelemetry/resources'; import * as assert from 'assert'; -import { InstrumentDescriptor } from '../src/InstrumentDescriptor'; -import { Histogram, InstrumentType } from '../src/Instruments'; +import { InstrumentDescriptor, InstrumentType } from '../src/InstrumentDescriptor'; +import { Histogram } from '../src/Instruments'; import { MetricData, PointData, PointDataType } from '../src/export/MetricData'; import { Measurement } from '../src/Measurement'; import { isNotNullish } from '../src/utils'; diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/view/Aggregation.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/view/Aggregation.test.ts index 717c74a1ad..6ce420a511 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/view/Aggregation.test.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/view/Aggregation.test.ts @@ -22,8 +22,7 @@ import { LastValueAggregator, SumAggregator, } from '../../src/aggregator'; -import { InstrumentDescriptor } from '../../src/InstrumentDescriptor'; -import { InstrumentType } from '../../src/Instruments'; +import { InstrumentDescriptor, InstrumentType } from '../../src/InstrumentDescriptor'; import { Aggregation, DefaultAggregation, diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/view/AttributesProcessor.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/view/AttributesProcessor.test.ts index fd7ea105fb..1a0db429e5 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/view/AttributesProcessor.test.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/view/AttributesProcessor.test.ts @@ -17,6 +17,7 @@ import * as assert from 'assert'; import { context } from '@opentelemetry/api'; import { NoopAttributesProcessor } from '../../src/view/AttributesProcessor'; +import { FilteringAttributesProcessor } from '../../src/view/AttributesProcessor'; describe('NoopAttributesProcessor', () => { const processor = new NoopAttributesProcessor(); @@ -30,3 +31,25 @@ describe('NoopAttributesProcessor', () => { ); }); }); + +describe('FilteringAttributesProcessor', () => { + it('should not add keys when attributes do not exist', () => { + const processor = new FilteringAttributesProcessor(['foo', 'bar']); + assert.deepStrictEqual( + processor.process({}, context.active()), {}); + }); + + it('should only keep allowed attributes', () => { + const processor = new FilteringAttributesProcessor(['foo', 'bar']); + assert.deepStrictEqual( + processor.process({ + foo: 'fooValue', + bar: 'barValue', + baz: 'bazValue' + }, context.active()), + { + foo: 'fooValue', + bar: 'barValue' + }); + }); +}); diff --git a/experimental/packages/opentelemetry-sdk-metrics-base/test/view/ViewRegistry.test.ts b/experimental/packages/opentelemetry-sdk-metrics-base/test/view/ViewRegistry.test.ts index dd1321a400..db5e0cf380 100644 --- a/experimental/packages/opentelemetry-sdk-metrics-base/test/view/ViewRegistry.test.ts +++ b/experimental/packages/opentelemetry-sdk-metrics-base/test/view/ViewRegistry.test.ts @@ -15,7 +15,7 @@ */ import * as assert from 'assert'; -import { InstrumentType } from '../../src/Instruments'; +import { InstrumentType } from '../../src/InstrumentDescriptor'; import { ViewRegistry } from '../../src/view/ViewRegistry'; import { View } from '../../src/view/View'; import { InstrumentSelector } from '../../src/view/InstrumentSelector';