Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.

Commit ad50e35

Browse files
committed
address comments
Change-Id: I21f8d0f49749722d9b22b0db949da35df7672d22
1 parent 36c19f4 commit ad50e35

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/csm/MetricsImpl.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@
8585
import javax.annotation.concurrent.GuardedBy;
8686

8787
public class MetricsImpl implements Metrics, Closeable {
88+
89+
public static final String CUSTOM_METRIC = "bigtable.internal.enable-custom-metric";
90+
8891
private final ApiTracerFactory userTracerFactory;
8992
private final @Nullable OpenTelemetrySdk internalOtel;
9093
private final @Nullable MetricRegistry.RecorderRegistry internalRecorder;
@@ -314,11 +317,11 @@ public static OpenTelemetrySdk createBuiltinOtel(
314317
executor));
315318

316319
Optional<Boolean> enableCustomMetric =
317-
Optional.ofNullable(System.getenv("BIGTABLE_CUSTOM_METRIC")).map(Boolean::parseBoolean);
320+
Optional.ofNullable(System.getProperty(CUSTOM_METRIC)).map(Boolean::parseBoolean);
318321
if (enableCustomMetric.isPresent() && enableCustomMetric.get()) {
322+
// Monitored resource and project id are detected at export time
319323
MetricConfiguration metricConfig =
320324
MetricConfiguration.builder()
321-
.setProjectId(clientInfo.getInstanceName().getProjectId())
322325
.setCredentials(credentials)
323326
.setInstrumentationLibraryLabelsEnabled(false)
324327
.build();

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/csm/metrics/CustomAttemptLatency.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.google.bigtable.v2.ClusterInformation;
1919
import com.google.bigtable.v2.PeerInfo;
20+
import com.google.cloud.bigtable.data.v2.internal.csm.MetricsImpl;
2021
import com.google.cloud.bigtable.data.v2.internal.csm.attributes.ClientInfo;
2122
import com.google.cloud.bigtable.data.v2.internal.csm.attributes.MethodInfo;
2223
import com.google.cloud.bigtable.data.v2.internal.csm.attributes.Util;
@@ -26,6 +27,7 @@
2627
import io.opentelemetry.api.metrics.DoubleHistogram;
2728
import io.opentelemetry.api.metrics.Meter;
2829
import java.time.Duration;
30+
import java.util.Optional;
2931
import 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

Comments
 (0)