@@ -786,11 +786,11 @@ class Meter {
786
786
createCounter (
787
787
name : string ,
788
788
options ?: MetricOptions ,
789
- ) : Counter | NoopInstrument {
790
- if ( ! METRICS_ENABLED ) return new NoopInstrument ( ) ;
789
+ ) : Counter {
791
790
if ( options ?. valueType !== undefined && options ?. valueType !== 1 ) {
792
791
throw new Error ( "Only valueType: DOUBLE is supported" ) ;
793
792
}
793
+ if ( ! METRICS_ENABLED ) return new Counter ( null , false ) ;
794
794
activateInstrumentationLibrary ( this . #instrumentationLibrary) ;
795
795
const instrument = op_otel_metric_create_counter (
796
796
name ,
@@ -804,11 +804,11 @@ class Meter {
804
804
createUpDownCounter (
805
805
name : string ,
806
806
options ?: MetricOptions ,
807
- ) : Counter | NoopInstrument {
808
- if ( ! METRICS_ENABLED ) return new NoopInstrument ( ) ;
807
+ ) : Counter {
809
808
if ( options ?. valueType !== undefined && options ?. valueType !== 1 ) {
810
809
throw new Error ( "Only valueType: DOUBLE is supported" ) ;
811
810
}
811
+ if ( ! METRICS_ENABLED ) return new Counter ( null , true ) ;
812
812
activateInstrumentationLibrary ( this . #instrumentationLibrary) ;
813
813
const instrument = op_otel_metric_create_up_down_counter (
814
814
name ,
@@ -822,11 +822,11 @@ class Meter {
822
822
createGauge (
823
823
name : string ,
824
824
options ?: MetricOptions ,
825
- ) : Gauge | NoopInstrument {
826
- if ( ! METRICS_ENABLED ) return new NoopInstrument ( ) ;
825
+ ) : Gauge {
827
826
if ( options ?. valueType !== undefined && options ?. valueType !== 1 ) {
828
827
throw new Error ( "Only valueType: DOUBLE is supported" ) ;
829
828
}
829
+ if ( ! METRICS_ENABLED ) return new Gauge ( null ) ;
830
830
activateInstrumentationLibrary ( this . #instrumentationLibrary) ;
831
831
const instrument = op_otel_metric_create_gauge (
832
832
name ,
@@ -840,11 +840,11 @@ class Meter {
840
840
createHistogram (
841
841
name : string ,
842
842
options ?: MetricOptions ,
843
- ) : Histogram | NoopInstrument {
844
- if ( ! METRICS_ENABLED ) return new NoopInstrument ( ) ;
843
+ ) : Histogram {
845
844
if ( options ?. valueType !== undefined && options ?. valueType !== 1 ) {
846
845
throw new Error ( "Only valueType: DOUBLE is supported" ) ;
847
846
}
847
+ if ( ! METRICS_ENABLED ) return new Histogram ( null ) ;
848
848
activateInstrumentationLibrary ( this . #instrumentationLibrary) ;
849
849
const instrument = op_otel_metric_create_histogram (
850
850
name ,
@@ -859,11 +859,11 @@ class Meter {
859
859
createObservableCounter (
860
860
name : string ,
861
861
options ?: MetricOptions ,
862
- ) : Observable | NoopInstrument {
863
- if ( ! METRICS_ENABLED ) return new NoopInstrument ( ) ;
862
+ ) : Observable {
864
863
if ( options ?. valueType !== undefined && options ?. valueType !== 1 ) {
865
864
throw new Error ( "Only valueType: DOUBLE is supported" ) ;
866
865
}
866
+ if ( ! METRICS_ENABLED ) new Observable ( new ObservableResult ( null , true ) ) ;
867
867
activateInstrumentationLibrary ( this . #instrumentationLibrary) ;
868
868
const instrument = op_otel_metric_create_observable_counter (
869
869
name ,
@@ -877,11 +877,11 @@ class Meter {
877
877
createObservableGauge (
878
878
name : string ,
879
879
options ?: MetricOptions ,
880
- ) : Observable | NoopInstrument {
881
- if ( ! METRICS_ENABLED ) return new NoopInstrument ( ) ;
880
+ ) : Observable {
882
881
if ( options ?. valueType !== undefined && options ?. valueType !== 1 ) {
883
882
throw new Error ( "Only valueType: DOUBLE is supported" ) ;
884
883
}
884
+ if ( ! METRICS_ENABLED ) new Observable ( new ObservableResult ( null , false ) ) ;
885
885
activateInstrumentationLibrary ( this . #instrumentationLibrary) ;
886
886
const instrument = op_otel_metric_create_observable_gauge (
887
887
name ,
@@ -895,11 +895,11 @@ class Meter {
895
895
createObservableUpDownCounter (
896
896
name : string ,
897
897
options ?: MetricOptions ,
898
- ) : Observable | NoopInstrument {
899
- if ( ! METRICS_ENABLED ) return new NoopInstrument ( ) ;
898
+ ) : Observable {
900
899
if ( options ?. valueType !== undefined && options ?. valueType !== 1 ) {
901
900
throw new Error ( "Only valueType: DOUBLE is supported" ) ;
902
901
}
902
+ if ( ! METRICS_ENABLED ) new Observable ( new ObservableResult ( null , false ) ) ;
903
903
activateInstrumentationLibrary ( this . #instrumentationLibrary) ;
904
904
const instrument = op_otel_metric_create_observable_up_down_counter (
905
905
name ,
@@ -932,22 +932,16 @@ class Meter {
932
932
}
933
933
}
934
934
935
- class NoopInstrument {
936
- add ( _value : number , _attributes ?: MetricAttributes , _context ?: Context ) { }
937
- record ( _value : number , _attributes ?: MetricAttributes , _context ?: Context ) { }
938
- addCallback ( _callback : ObservableCallback ) { }
939
- removeCallback ( _callback : ObservableCallback ) { }
940
- }
941
-
942
935
type BatchObservableCallback = (
943
936
observableResult : BatchObservableResult ,
944
937
) => void | Promise < void > ;
945
938
946
939
function record (
947
- instrument : Instrument ,
940
+ instrument : Instrument | null ,
948
941
value : number ,
949
942
attributes ?: MetricAttributes ,
950
943
) {
944
+ if ( instrument === null ) return ;
951
945
if ( attributes === undefined ) {
952
946
op_otel_metric_record0 ( instrument , value ) ;
953
947
} else {
@@ -1006,10 +1000,11 @@ function record(
1006
1000
}
1007
1001
1008
1002
function recordObservable (
1009
- instrument : Instrument ,
1003
+ instrument : Instrument | null ,
1010
1004
value : number ,
1011
1005
attributes ?: MetricAttributes ,
1012
1006
) {
1007
+ if ( instrument === null ) return ;
1013
1008
if ( attributes === undefined ) {
1014
1009
op_otel_metric_observable_record0 ( instrument , value ) ;
1015
1010
} else {
@@ -1068,10 +1063,10 @@ function recordObservable(
1068
1063
}
1069
1064
1070
1065
class Counter {
1071
- #instrument: Instrument ;
1066
+ #instrument: Instrument | null ;
1072
1067
#upDown: boolean ;
1073
1068
1074
- constructor ( instrument : Instrument , upDown : boolean ) {
1069
+ constructor ( instrument : Instrument | null , upDown : boolean ) {
1075
1070
this . #instrument = instrument ;
1076
1071
this . #upDown = upDown ;
1077
1072
}
@@ -1085,9 +1080,9 @@ class Counter {
1085
1080
}
1086
1081
1087
1082
class Gauge {
1088
- #instrument: Instrument ;
1083
+ #instrument: Instrument | null ;
1089
1084
1090
- constructor ( instrument : Instrument ) {
1085
+ constructor ( instrument : Instrument | null ) {
1091
1086
this . #instrument = instrument ;
1092
1087
}
1093
1088
@@ -1101,9 +1096,9 @@ class Gauge {
1101
1096
}
1102
1097
1103
1098
class Histogram {
1104
- #instrument: Instrument ;
1099
+ #instrument: Instrument | null ;
1105
1100
1106
- constructor ( instrument : Instrument ) {
1101
+ constructor ( instrument : Instrument | null ) {
1107
1102
this . #instrument = instrument ;
1108
1103
}
1109
1104
@@ -1148,10 +1143,10 @@ class Observable {
1148
1143
}
1149
1144
1150
1145
class ObservableResult {
1151
- #instrument: Instrument ;
1146
+ #instrument: Instrument | null ;
1152
1147
#isRegularCounter: boolean ;
1153
1148
1154
- constructor ( instrument : Instrument , isRegularCounter : boolean ) {
1149
+ constructor ( instrument : Instrument | null , isRegularCounter : boolean ) {
1155
1150
this . #instrument = instrument ;
1156
1151
this . #isRegularCounter = isRegularCounter ;
1157
1152
}
0 commit comments