Skip to content

Commit

Permalink
feat: add unit to view instrument selection criteria (#3647)
Browse files Browse the repository at this point in the history
Signed-off-by: Julian Labatut <[email protected]>
  • Loading branch information
jlabatut committed Mar 1, 2023
1 parent 74ca04f commit ff95dd4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
9 changes: 8 additions & 1 deletion packages/sdk-metrics/src/view/InstrumentSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -38,4 +41,8 @@ export class InstrumentSelector {
getNameFilter() {
return this._nameFilter;
}

getUnitFilter() {
return this._unitFilter;
}
}
2 changes: 2 additions & 0 deletions packages/sdk-metrics/src/view/RegistrationConflicts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function getTypeConflictResolutionRecipe(
const selector: InstrumentSelectorCriteria = {
name: otherDescriptor.name,
type: otherDescriptor.type,
unit: otherDescriptor.unit,
};

const selectorString = JSON.stringify(selector);
Expand All @@ -73,6 +74,7 @@ export function getDescriptionResolutionRecipe(
const selector: InstrumentSelectorCriteria = {
name: otherDescriptor.name,
type: otherDescriptor.type,
unit: otherDescriptor.unit,
};

const selectorString = JSON.stringify(selector);
Expand Down
13 changes: 13 additions & 0 deletions packages/sdk-metrics/src/view/View.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ export type ViewOptions = {
* instrumentName: 'my.instruments.requests'
*/
instrumentName?: string;
/**
* Instrument selection criteria:
* The unit of the Instrument(s).
*
* @example <caption>select all instruments with unit 'ms'</caption>
* instrumentUnit: 'ms'
*/
instrumentUnit?: string;
/**
* Instrument selection criteria:
* The name of the Meter. No wildcard support, name must match the meter exactly.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk-metrics/src/view/ViewRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down

0 comments on commit ff95dd4

Please sign in to comment.