1717
1818import com .google .bigtable .v2 .ClusterInformation ;
1919import com .google .bigtable .v2 .PeerInfo ;
20+ import com .google .cloud .bigtable .data .v2 .internal .csm .MetricsImpl ;
2021import com .google .cloud .bigtable .data .v2 .internal .csm .attributes .ClientInfo ;
2122import com .google .cloud .bigtable .data .v2 .internal .csm .attributes .MethodInfo ;
2223import com .google .cloud .bigtable .data .v2 .internal .csm .attributes .Util ;
2627import io .opentelemetry .api .metrics .DoubleHistogram ;
2728import io .opentelemetry .api .metrics .Meter ;
2829import java .time .Duration ;
30+ import java .util .Optional ;
2931import javax .annotation .Nullable ;
3032
3133/**
@@ -40,13 +42,32 @@ public CustomAttemptLatency() {
4042 }
4143
4244 public Recorder newRecorder (Meter meter ) {
43- return new Recorder (meter );
45+ Optional <Boolean > enableCustomMetric =
46+ Optional .ofNullable (System .getProperty (MetricsImpl .CUSTOM_METRIC ))
47+ .map (Boolean ::parseBoolean );
48+ if (enableCustomMetric .isPresent () && enableCustomMetric .get ()) {
49+ return new RecorderImpl (meter );
50+ } else {
51+ // Skip recording high cardinality metric when it's disabled
52+ return new NoopRecorder ();
53+ }
54+ }
55+
56+ public abstract static class Recorder {
57+ public abstract void record (
58+ ClientInfo clientInfo ,
59+ String tableId ,
60+ @ Nullable PeerInfo peerInfo ,
61+ @ Nullable ClusterInformation clusterInfo ,
62+ MethodInfo methodInfo ,
63+ Status .Code code ,
64+ Duration latency );
4465 }
4566
46- public static class Recorder {
67+ public static class RecorderImpl extends Recorder {
4768 private final DoubleHistogram instrument ;
4869
49- private Recorder (Meter meter ) {
70+ private RecorderImpl (Meter meter ) {
5071 instrument =
5172 meter
5273 .histogramBuilder (NAME )
@@ -86,4 +107,18 @@ public void record(
86107 instrument .record (toMillis (latency ), attributes );
87108 }
88109 }
110+
111+ public static class NoopRecorder extends Recorder {
112+ @ Override
113+ public void record (
114+ ClientInfo clientInfo ,
115+ String tableId ,
116+ @ Nullable PeerInfo peerInfo ,
117+ @ Nullable ClusterInformation clusterInfo ,
118+ MethodInfo methodInfo ,
119+ Status .Code code ,
120+ Duration latency ) {
121+ // do nothing
122+ }
123+ }
89124}
0 commit comments