Skip to content

Commit 245cab2

Browse files
committed
feat: add unit to view instrument selection criteria (#3647)
Signed-off-by: Julian Labatut <[email protected]>
1 parent cbbdfd3 commit 245cab2

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

packages/sdk-metrics/src/view/InstrumentSelector.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,23 @@
1515
*/
1616

1717
import { InstrumentType } from '../InstrumentDescriptor';
18-
import { PatternPredicate, Predicate } from './Predicate';
18+
import { ExactPredicate, PatternPredicate, Predicate } from './Predicate';
1919

2020
export interface InstrumentSelectorCriteria {
2121
name?: string;
2222
type?: InstrumentType;
23+
unit?: string;
2324
}
2425

2526
export class InstrumentSelector {
2627
private _nameFilter: Predicate;
2728
private _type?: InstrumentType;
29+
private _unitFilter: Predicate;
2830

2931
constructor(criteria?: InstrumentSelectorCriteria) {
3032
this._nameFilter = new PatternPredicate(criteria?.name ?? '*');
3133
this._type = criteria?.type;
34+
this._unitFilter = new ExactPredicate(criteria?.unit);
3235
}
3336

3437
getType() {
@@ -38,4 +41,8 @@ export class InstrumentSelector {
3841
getNameFilter() {
3942
return this._nameFilter;
4043
}
44+
45+
getUnitFilter() {
46+
return this._unitFilter;
47+
}
4148
}

packages/sdk-metrics/src/view/RegistrationConflicts.ts

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export function getTypeConflictResolutionRecipe(
5959
const selector: InstrumentSelectorCriteria = {
6060
name: otherDescriptor.name,
6161
type: otherDescriptor.type,
62+
unit: otherDescriptor.unit,
6263
};
6364

6465
const selectorString = JSON.stringify(selector);
@@ -73,6 +74,7 @@ export function getDescriptionResolutionRecipe(
7374
const selector: InstrumentSelectorCriteria = {
7475
name: otherDescriptor.name,
7576
type: otherDescriptor.type,
77+
unit: otherDescriptor.unit,
7678
};
7779

7880
const selectorString = JSON.stringify(selector);

packages/sdk-metrics/src/view/View.ts

+13
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ export type ViewOptions = {
8383
* instrumentName: 'my.instruments.requests'
8484
*/
8585
instrumentName?: string;
86+
/**
87+
* Instrument selection criteria:
88+
* The unit of the Instrument(s).
89+
*
90+
* @example <caption>select all instruments with unit 'ms'</caption>
91+
* instrumentUnit: 'ms'
92+
*/
93+
instrumentUnit?: string;
8694
/**
8795
* Instrument selection criteria:
8896
* The name of the Meter. No wildcard support, name must match the meter exactly.
@@ -113,6 +121,7 @@ function isSelectorNotProvided(options: ViewOptions): boolean {
113121
return (
114122
options.instrumentName == null &&
115123
options.instrumentType == null &&
124+
options.instrumentUnit == null &&
116125
options.meterName == null &&
117126
options.meterVersion == null &&
118127
options.meterSchemaUrl == null
@@ -161,6 +170,9 @@ export class View {
161170
* @param viewOptions.instrumentType
162171
* Instrument selection criteria:
163172
* The original type of the Instrument(s).
173+
* @param viewOptions.instrumentUnit
174+
* Instrument selection criteria:
175+
* The unit of the Instrument(s).
164176
* @param viewOptions.meterName
165177
* Instrument selection criteria:
166178
* The name of the Meter. No wildcard support, name must match the meter exactly.
@@ -213,6 +225,7 @@ export class View {
213225
this.instrumentSelector = new InstrumentSelector({
214226
name: viewOptions.instrumentName,
215227
type: viewOptions.instrumentType,
228+
unit: viewOptions.instrumentUnit,
216229
});
217230
this.meterSelector = new MeterSelector({
218231
name: viewOptions.meterName,

packages/sdk-metrics/src/view/ViewRegistry.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ export class ViewRegistry {
4848
return (
4949
(selector.getType() === undefined ||
5050
instrument.type === selector.getType()) &&
51-
selector.getNameFilter().match(instrument.name)
51+
selector.getNameFilter().match(instrument.name) &&
52+
selector.getUnitFilter().match(instrument.unit)
5253
);
5354
}
5455

0 commit comments

Comments
 (0)