diff --git a/packages/sdk-metrics/src/view/InstrumentSelector.ts b/packages/sdk-metrics/src/view/InstrumentSelector.ts index e7bec3a44a2..9b8006267f7 100644 --- a/packages/sdk-metrics/src/view/InstrumentSelector.ts +++ b/packages/sdk-metrics/src/view/InstrumentSelector.ts @@ -15,20 +15,23 @@ */ import { InstrumentType } from '../InstrumentDescriptor'; -import { PatternPredicate, Predicate } from './Predicate'; +import { ExactPredicate, PatternPredicate, Predicate } from './Predicate'; export interface InstrumentSelectorCriteria { name?: string; type?: InstrumentType; + unit?: string; } export class InstrumentSelector { private _nameFilter: Predicate; private _type?: InstrumentType; + private _unitFilter: Predicate; constructor(criteria?: InstrumentSelectorCriteria) { this._nameFilter = new PatternPredicate(criteria?.name ?? '*'); this._type = criteria?.type; + this._unitFilter = new ExactPredicate(criteria?.unit); } getType() { @@ -38,4 +41,8 @@ export class InstrumentSelector { getNameFilter() { return this._nameFilter; } + + getUnitFilter() { + return this._unitFilter; + } } diff --git a/packages/sdk-metrics/src/view/RegistrationConflicts.ts b/packages/sdk-metrics/src/view/RegistrationConflicts.ts index e74add73349..dbb8073c8b8 100644 --- a/packages/sdk-metrics/src/view/RegistrationConflicts.ts +++ b/packages/sdk-metrics/src/view/RegistrationConflicts.ts @@ -59,6 +59,7 @@ export function getTypeConflictResolutionRecipe( const selector: InstrumentSelectorCriteria = { name: otherDescriptor.name, type: otherDescriptor.type, + unit: otherDescriptor.unit, }; const selectorString = JSON.stringify(selector); @@ -73,6 +74,7 @@ export function getDescriptionResolutionRecipe( const selector: InstrumentSelectorCriteria = { name: otherDescriptor.name, type: otherDescriptor.type, + unit: otherDescriptor.unit, }; const selectorString = JSON.stringify(selector); diff --git a/packages/sdk-metrics/src/view/View.ts b/packages/sdk-metrics/src/view/View.ts index 398f936536d..1e8d4fb0e05 100644 --- a/packages/sdk-metrics/src/view/View.ts +++ b/packages/sdk-metrics/src/view/View.ts @@ -83,6 +83,14 @@ export type ViewOptions = { * instrumentName: 'my.instruments.requests' */ instrumentName?: string; + /** + * Instrument selection criteria: + * The unit of the Instrument(s). + * + * @example select all instruments with unit 'ms' + * instrumentUnit: 'ms' + */ + instrumentUnit?: string; /** * Instrument selection criteria: * The name of the Meter. No wildcard support, name must match the meter exactly. @@ -113,6 +121,7 @@ function isSelectorNotProvided(options: ViewOptions): boolean { return ( options.instrumentName == null && options.instrumentType == null && + options.instrumentUnit == null && options.meterName == null && options.meterVersion == null && options.meterSchemaUrl == null @@ -161,6 +170,9 @@ export class View { * @param viewOptions.instrumentType * Instrument selection criteria: * The original type of the Instrument(s). + * @param viewOptions.instrumentUnit + * Instrument selection criteria: + * The unit of the Instrument(s). * @param viewOptions.meterName * Instrument selection criteria: * The name of the Meter. No wildcard support, name must match the meter exactly. @@ -213,6 +225,7 @@ export class View { this.instrumentSelector = new InstrumentSelector({ name: viewOptions.instrumentName, type: viewOptions.instrumentType, + unit: viewOptions.instrumentUnit, }); this.meterSelector = new MeterSelector({ name: viewOptions.meterName, diff --git a/packages/sdk-metrics/src/view/ViewRegistry.ts b/packages/sdk-metrics/src/view/ViewRegistry.ts index 265f699bf9a..5f4f367e920 100644 --- a/packages/sdk-metrics/src/view/ViewRegistry.ts +++ b/packages/sdk-metrics/src/view/ViewRegistry.ts @@ -48,7 +48,8 @@ export class ViewRegistry { return ( (selector.getType() === undefined || instrument.type === selector.getType()) && - selector.getNameFilter().match(instrument.name) + selector.getNameFilter().match(instrument.name) && + selector.getUnitFilter().match(instrument.unit) ); }