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

Commit dfea280

Browse files
chore: wire up the new typesafe metrics
Change-Id: I9a0c78a1e42a6c42afe144bef77d9db9eeb1f641
1 parent 3c44862 commit dfea280

23 files changed

Lines changed: 243 additions & 466 deletions

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.auth.Credentials;
2222
import com.google.cloud.bigtable.Version;
2323
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
24+
import com.google.cloud.bigtable.data.v2.internal.csm.MetricRegistry.RecorderRegistry;
2425
import com.google.cloud.bigtable.data.v2.internal.csm.attributes.ClientInfo;
2526
import com.google.cloud.bigtable.data.v2.stub.metrics.BigtableCloudMonitoringExporter;
2627
import com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants;
@@ -57,6 +58,8 @@
5758
import javax.annotation.Nullable;
5859

5960
public class MetricsImpl implements Metrics, Closeable {
61+
private final MetricRegistry metricRegistry;
62+
6063
private final ApiTracerFactory userTracerFactory;
6164
private final @Nullable OpenTelemetrySdk internalOtel;
6265
private final @Nullable OpenTelemetry userOtel;
@@ -69,12 +72,14 @@ public class MetricsImpl implements Metrics, Closeable {
6972
private final List<ScheduledFuture<?>> tasks = new ArrayList<>();
7073

7174
public MetricsImpl(
75+
ClientInfo clientInfo,
7276
ApiTracerFactory userTracerFactory,
7377
OpenTelemetrySdk internalOtel,
7478
OpenTelemetry userOtel,
7579
Tagger ocTagger,
7680
StatsRecorder ocRecorder,
7781
ScheduledExecutorService executor) {
82+
metricRegistry = new MetricRegistry();
7883
this.userTracerFactory = Preconditions.checkNotNull(userTracerFactory);
7984

8085
this.internalOtel = internalOtel;
@@ -96,7 +101,9 @@ public MetricsImpl(
96101
.build();
97102

98103
if (internalOtel != null) {
99-
this.channelPoolMetricsTracer = new ChannelPoolMetricsTracer(internalOtel);
104+
this.channelPoolMetricsTracer =
105+
new ChannelPoolMetricsTracer(
106+
metricRegistry.newRecorderRegistry(internalOtel.getMeterProvider()), clientInfo);
100107
} else {
101108
this.channelPoolMetricsTracer = null;
102109
}
@@ -132,10 +139,14 @@ public ApiTracerFactory createTracerFactory(ClientInfo clientInfo) {
132139
.add(userTracerFactory);
133140

134141
if (internalOtel != null) {
135-
tracerFactories.add(createOtelMetricsFactory(internalOtel, clientInfo));
142+
tracerFactories.add(
143+
createOtelMetricsFactory(
144+
metricRegistry.newRecorderRegistry(internalOtel.getMeterProvider()), clientInfo));
136145
}
137146
if (userOtel != null) {
138-
tracerFactories.add(createOtelMetricsFactory(userOtel, clientInfo));
147+
tracerFactories.add(
148+
createOtelMetricsFactory(
149+
metricRegistry.newRecorderRegistry(userOtel.getMeterProvider()), clientInfo));
139150
}
140151

141152
return new CompositeTracerFactory(tracerFactories.build());
@@ -220,8 +231,8 @@ private static ApiTracerFactory createOCMetricsFactory(
220231
}
221232

222233
private static BuiltinMetricsTracerFactory createOtelMetricsFactory(
223-
OpenTelemetry otel, ClientInfo clientInfo) {
234+
RecorderRegistry recorder, ClientInfo clientInfo) {
224235

225-
return BuiltinMetricsTracerFactory.create(otel, clientInfo);
236+
return BuiltinMetricsTracerFactory.create(recorder, clientInfo);
226237
}
227238
}

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,56 @@
1616

1717
package com.google.cloud.bigtable.data.v2.internal.csm.attributes;
1818

19+
import com.google.bigtable.v2.PeerInfo;
1920
import com.google.bigtable.v2.PeerInfo.TransportType;
20-
import com.google.common.base.Preconditions;
21+
import com.google.bigtable.v2.ResponseParams;
2122
import java.util.Locale;
23+
import java.util.Optional;
24+
import javax.annotation.Nullable;
2225

2326
public class Util {
2427
static final String TRANSPORT_TYPE_PREFIX = "TRANSPORT_TYPE_";
2528

26-
public static String transportTypeToString(TransportType transportType) {
29+
public static String formatTransportZone(@Nullable PeerInfo peerInfo) {
30+
return Optional.ofNullable(peerInfo).map(PeerInfo::getApplicationFrontendZone).orElse("");
31+
}
2732

28-
Preconditions.checkArgument(
29-
transportType.name().startsWith(TRANSPORT_TYPE_PREFIX)
30-
|| transportType == TransportType.UNRECOGNIZED,
31-
"TransportType values must start with %s",
32-
TRANSPORT_TYPE_PREFIX);
33+
public static String formatTransportSubzone(@Nullable PeerInfo peerInfo) {
34+
return Optional.ofNullable(peerInfo).map(PeerInfo::getApplicationFrontendSubzone).orElse("");
35+
}
36+
37+
public static String formatTransportType(@Nullable PeerInfo peerInfo) {
38+
return transportTypeToString(
39+
Optional.ofNullable(peerInfo)
40+
.map(PeerInfo::getTransportType)
41+
.orElse(TransportType.TRANSPORT_TYPE_UNKNOWN));
42+
}
3343

44+
public static String transportTypeToString(TransportType transportType) {
3445
if (transportType == TransportType.TRANSPORT_TYPE_UNKNOWN) {
35-
return "session_none";
46+
return "none";
3647
}
3748
if (transportType == TransportType.UNRECOGNIZED) {
38-
return "session_unrecognized";
49+
return "unrecognized";
3950
}
4051

4152
return transportType
4253
.name()
4354
.substring(TRANSPORT_TYPE_PREFIX.length())
4455
.toLowerCase(Locale.ENGLISH);
4556
}
57+
58+
public static String formatClusterIdMetricLabel(@Nullable ResponseParams clusterInfo) {
59+
return Optional.ofNullable(clusterInfo)
60+
.map(ResponseParams::getClusterId)
61+
.filter(s -> !s.isEmpty())
62+
.orElse("<unspecified>");
63+
}
64+
65+
public static String formatZoneIdMetricLabel(@Nullable ResponseParams clusterInfo) {
66+
return Optional.ofNullable(clusterInfo)
67+
.map(ResponseParams::getZoneId)
68+
.filter(s -> !s.isEmpty())
69+
.orElse("global");
70+
}
4671
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.opentelemetry.api.metrics.DoubleHistogram;
3030
import io.opentelemetry.api.metrics.Meter;
3131
import java.time.Duration;
32+
import javax.annotation.Nullable;
3233

3334
public class TableApplicationBlockingLatency extends MetricWrapper<TableSchema> {
3435
private static final String NAME =
@@ -69,7 +70,7 @@ public void record(
6970
ClientInfo clientInfo,
7071
String tableId,
7172
MethodInfo methodInfo,
72-
ResponseParams clusterInfo,
73+
@Nullable ResponseParams clusterInfo,
7374
Duration duration) {
7475
Attributes attributes =
7576
getSchema()

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.opentelemetry.api.metrics.DoubleHistogram;
3131
import io.opentelemetry.api.metrics.Meter;
3232
import java.time.Duration;
33+
import javax.annotation.Nullable;
3334

3435
public class TableAttemptLatency extends MetricWrapper<TableSchema> {
3536
private static final String NAME = "bigtable.googleapis.com/internal/client/attempt_latencies";
@@ -67,7 +68,7 @@ private Recorder(Meter meter) {
6768
public void record(
6869
ClientInfo clientInfo,
6970
String tableId,
70-
ResponseParams clusterInfo,
71+
@Nullable ResponseParams clusterInfo,
7172
MethodInfo methodInfo,
7273
Status.Code code,
7374
Duration latency) {

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.opentelemetry.api.metrics.DoubleHistogram;
3232
import io.opentelemetry.api.metrics.Meter;
3333
import java.time.Duration;
34+
import javax.annotation.Nullable;
3435

3536
public class TableAttemptLatency2 extends MetricWrapper<TableSchema> {
3637
private static final String NAME = "bigtable.googleapis.com/internal/client/attempt_latencies2";
@@ -68,23 +69,21 @@ private Recorder(Meter meter) {
6869
public void record(
6970
ClientInfo clientInfo,
7071
String tableId,
71-
PeerInfo peerInfo,
72-
ResponseParams clusterInfo,
72+
@Nullable PeerInfo peerInfo,
73+
@Nullable ResponseParams clusterInfo,
7374
MethodInfo methodInfo,
7475
Status.Code code,
7576
Duration latency) {
7677

7778
Attributes attributes =
7879
getSchema()
7980
.createResourceAttrs(clientInfo, tableId, clusterInfo)
80-
.put(
81-
MetricLabels.TRANSPORT_TYPE,
82-
Util.transportTypeToString(peerInfo.getTransportType()))
81+
.put(MetricLabels.TRANSPORT_TYPE, Util.formatTransportType(peerInfo))
8382
.put(MetricLabels.STATUS_KEY, code.name())
8483
.put(MetricLabels.TRANSPORT_REGION, "")
8584
// To maintain backwards compat CLIENT_UID is set using sideband data in the exporter
86-
.put(MetricLabels.TRANSPORT_ZONE, peerInfo.getApplicationFrontendZone())
87-
.put(MetricLabels.TRANSPORT_SUBZONE, peerInfo.getApplicationFrontendSubzone())
85+
.put(MetricLabels.TRANSPORT_ZONE, Util.formatTransportZone(peerInfo))
86+
.put(MetricLabels.TRANSPORT_SUBZONE, Util.formatTransportSubzone(peerInfo))
8887
.put(MetricLabels.CLIENT_NAME, clientInfo.getClientName())
8988
.put(MetricLabels.APP_PROFILE_KEY, clientInfo.getAppProfileId())
9089
.put(MetricLabels.METHOD_KEY, methodInfo.getName())

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.opentelemetry.api.metrics.DoubleHistogram;
3030
import io.opentelemetry.api.metrics.Meter;
3131
import java.time.Duration;
32+
import javax.annotation.Nullable;
3233

3334
public class TableClientBlockingLatency extends MetricWrapper<TableSchema> {
3435
private static final String NAME = "bigtable.googleapis.com/internal/client/throttling_latencies";
@@ -69,7 +70,7 @@ public void record(
6970
ClientInfo clientInfo,
7071
String tableId,
7172
MethodInfo methodInfo,
72-
ResponseParams clusterInfo,
73+
@Nullable ResponseParams clusterInfo,
7374
Duration duration) {
7475
Attributes attributes =
7576
getSchema()

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.opentelemetry.api.common.Attributes;
2929
import io.opentelemetry.api.metrics.LongCounter;
3030
import io.opentelemetry.api.metrics.Meter;
31+
import javax.annotation.Nullable;
3132

3233
public class TableConnectivityErrorCount extends MetricWrapper<TableSchema> {
3334
private static final String NAME =
@@ -68,7 +69,7 @@ public void record(
6869
ClientInfo clientInfo,
6970
String tableId,
7071
MethodInfo methodInfo,
71-
ResponseParams clusterInfo,
72+
@Nullable ResponseParams clusterInfo,
7273
Status.Code code,
7374
long count) {
7475
Attributes attributes =

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.opentelemetry.api.common.Attributes;
2727
import io.opentelemetry.api.metrics.LongCounter;
2828
import io.opentelemetry.api.metrics.Meter;
29+
import javax.annotation.Nullable;
2930

3031
public class TableDebugTagCount extends MetricWrapper<TableSchema> {
3132
private static final String NAME = "bigtable.googleapis.com/internal/client/debug_tags";
@@ -63,7 +64,7 @@ public void record(
6364
ClientInfo clientInfo,
6465
String tableId,
6566
String tag,
66-
ResponseParams clusterInfo,
67+
@Nullable ResponseParams clusterInfo,
6768
long amount) {
6869
Attributes attributes =
6970
getSchema()

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.opentelemetry.api.metrics.DoubleHistogram;
3131
import io.opentelemetry.api.metrics.Meter;
3232
import java.time.Duration;
33+
import javax.annotation.Nullable;
3334

3435
public class TableFirstResponseLatency extends MetricWrapper<TableSchema> {
3536
private static final String NAME =
@@ -72,7 +73,7 @@ public void record(
7273
ClientInfo clientInfo,
7374
String tableId,
7475
MethodInfo methodInfo,
75-
ResponseParams clusterInfo,
76+
@Nullable ResponseParams clusterInfo,
7677
Status.Code code,
7778
Duration duration) {
7879
Attributes attributes =

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.opentelemetry.api.metrics.DoubleHistogram;
3131
import io.opentelemetry.api.metrics.Meter;
3232
import java.time.Duration;
33+
import javax.annotation.Nullable;
3334

3435
public class TableOperationLatency extends MetricWrapper<TableSchema> {
3536
private static final String NAME = "bigtable.googleapis.com/internal/client/operation_latencies";
@@ -70,7 +71,7 @@ public void record(
7071
ClientInfo clientInfo,
7172
String tableId,
7273
MethodInfo methodInfo,
73-
ResponseParams clusterInfo,
74+
@Nullable ResponseParams clusterInfo,
7475
Status.Code code,
7576
Duration duration) {
7677
Attributes attributes =

0 commit comments

Comments
 (0)