Skip to content

Commit b1788e9

Browse files
committed
do noop metrics differently
1 parent 46a03f9 commit b1788e9

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

Diff for: ext/telemetry/telemetry.ts

+26-31
Original file line numberDiff line numberDiff line change
@@ -786,11 +786,11 @@ class Meter {
786786
createCounter(
787787
name: string,
788788
options?: MetricOptions,
789-
): Counter | NoopInstrument {
790-
if (!METRICS_ENABLED) return new NoopInstrument();
789+
): Counter {
791790
if (options?.valueType !== undefined && options?.valueType !== 1) {
792791
throw new Error("Only valueType: DOUBLE is supported");
793792
}
793+
if (!METRICS_ENABLED) return new Counter(null, false);
794794
activateInstrumentationLibrary(this.#instrumentationLibrary);
795795
const instrument = op_otel_metric_create_counter(
796796
name,
@@ -804,11 +804,11 @@ class Meter {
804804
createUpDownCounter(
805805
name: string,
806806
options?: MetricOptions,
807-
): Counter | NoopInstrument {
808-
if (!METRICS_ENABLED) return new NoopInstrument();
807+
): Counter {
809808
if (options?.valueType !== undefined && options?.valueType !== 1) {
810809
throw new Error("Only valueType: DOUBLE is supported");
811810
}
811+
if (!METRICS_ENABLED) return new Counter(null, true);
812812
activateInstrumentationLibrary(this.#instrumentationLibrary);
813813
const instrument = op_otel_metric_create_up_down_counter(
814814
name,
@@ -822,11 +822,11 @@ class Meter {
822822
createGauge(
823823
name: string,
824824
options?: MetricOptions,
825-
): Gauge | NoopInstrument {
826-
if (!METRICS_ENABLED) return new NoopInstrument();
825+
): Gauge {
827826
if (options?.valueType !== undefined && options?.valueType !== 1) {
828827
throw new Error("Only valueType: DOUBLE is supported");
829828
}
829+
if (!METRICS_ENABLED) return new Gauge(null);
830830
activateInstrumentationLibrary(this.#instrumentationLibrary);
831831
const instrument = op_otel_metric_create_gauge(
832832
name,
@@ -840,11 +840,11 @@ class Meter {
840840
createHistogram(
841841
name: string,
842842
options?: MetricOptions,
843-
): Histogram | NoopInstrument {
844-
if (!METRICS_ENABLED) return new NoopInstrument();
843+
): Histogram {
845844
if (options?.valueType !== undefined && options?.valueType !== 1) {
846845
throw new Error("Only valueType: DOUBLE is supported");
847846
}
847+
if (!METRICS_ENABLED) return new Histogram(null);
848848
activateInstrumentationLibrary(this.#instrumentationLibrary);
849849
const instrument = op_otel_metric_create_histogram(
850850
name,
@@ -859,11 +859,11 @@ class Meter {
859859
createObservableCounter(
860860
name: string,
861861
options?: MetricOptions,
862-
): Observable | NoopInstrument {
863-
if (!METRICS_ENABLED) return new NoopInstrument();
862+
): Observable {
864863
if (options?.valueType !== undefined && options?.valueType !== 1) {
865864
throw new Error("Only valueType: DOUBLE is supported");
866865
}
866+
if (!METRICS_ENABLED) new Observable(new ObservableResult(null, true));
867867
activateInstrumentationLibrary(this.#instrumentationLibrary);
868868
const instrument = op_otel_metric_create_observable_counter(
869869
name,
@@ -877,11 +877,11 @@ class Meter {
877877
createObservableGauge(
878878
name: string,
879879
options?: MetricOptions,
880-
): Observable | NoopInstrument {
881-
if (!METRICS_ENABLED) return new NoopInstrument();
880+
): Observable {
882881
if (options?.valueType !== undefined && options?.valueType !== 1) {
883882
throw new Error("Only valueType: DOUBLE is supported");
884883
}
884+
if (!METRICS_ENABLED) new Observable(new ObservableResult(null, false));
885885
activateInstrumentationLibrary(this.#instrumentationLibrary);
886886
const instrument = op_otel_metric_create_observable_gauge(
887887
name,
@@ -895,11 +895,11 @@ class Meter {
895895
createObservableUpDownCounter(
896896
name: string,
897897
options?: MetricOptions,
898-
): Observable | NoopInstrument {
899-
if (!METRICS_ENABLED) return new NoopInstrument();
898+
): Observable {
900899
if (options?.valueType !== undefined && options?.valueType !== 1) {
901900
throw new Error("Only valueType: DOUBLE is supported");
902901
}
902+
if (!METRICS_ENABLED) new Observable(new ObservableResult(null, false));
903903
activateInstrumentationLibrary(this.#instrumentationLibrary);
904904
const instrument = op_otel_metric_create_observable_up_down_counter(
905905
name,
@@ -932,22 +932,16 @@ class Meter {
932932
}
933933
}
934934

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-
942935
type BatchObservableCallback = (
943936
observableResult: BatchObservableResult,
944937
) => void | Promise<void>;
945938

946939
function record(
947-
instrument: Instrument,
940+
instrument: Instrument | null,
948941
value: number,
949942
attributes?: MetricAttributes,
950943
) {
944+
if (instrument === null) return;
951945
if (attributes === undefined) {
952946
op_otel_metric_record0(instrument, value);
953947
} else {
@@ -1006,10 +1000,11 @@ function record(
10061000
}
10071001

10081002
function recordObservable(
1009-
instrument: Instrument,
1003+
instrument: Instrument | null,
10101004
value: number,
10111005
attributes?: MetricAttributes,
10121006
) {
1007+
if (instrument === null) return;
10131008
if (attributes === undefined) {
10141009
op_otel_metric_observable_record0(instrument, value);
10151010
} else {
@@ -1068,10 +1063,10 @@ function recordObservable(
10681063
}
10691064

10701065
class Counter {
1071-
#instrument: Instrument;
1066+
#instrument: Instrument | null;
10721067
#upDown: boolean;
10731068

1074-
constructor(instrument: Instrument, upDown: boolean) {
1069+
constructor(instrument: Instrument | null, upDown: boolean) {
10751070
this.#instrument = instrument;
10761071
this.#upDown = upDown;
10771072
}
@@ -1085,9 +1080,9 @@ class Counter {
10851080
}
10861081

10871082
class Gauge {
1088-
#instrument: Instrument;
1083+
#instrument: Instrument | null;
10891084

1090-
constructor(instrument: Instrument) {
1085+
constructor(instrument: Instrument | null) {
10911086
this.#instrument = instrument;
10921087
}
10931088

@@ -1101,9 +1096,9 @@ class Gauge {
11011096
}
11021097

11031098
class Histogram {
1104-
#instrument: Instrument;
1099+
#instrument: Instrument | null;
11051100

1106-
constructor(instrument: Instrument) {
1101+
constructor(instrument: Instrument | null) {
11071102
this.#instrument = instrument;
11081103
}
11091104

@@ -1148,10 +1143,10 @@ class Observable {
11481143
}
11491144

11501145
class ObservableResult {
1151-
#instrument: Instrument;
1146+
#instrument: Instrument | null;
11521147
#isRegularCounter: boolean;
11531148

1154-
constructor(instrument: Instrument, isRegularCounter: boolean) {
1149+
constructor(instrument: Instrument | null, isRegularCounter: boolean) {
11551150
this.#instrument = instrument;
11561151
this.#isRegularCounter = isRegularCounter;
11571152
}

0 commit comments

Comments
 (0)