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

Commit ea7a0a0

Browse files
committed
fix(bigtable): drop redudant fields from internal otel metrics which are already in monitored resource
1 parent 60be349 commit ea7a0a0

3 files changed

Lines changed: 41 additions & 32 deletions

File tree

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/BigtableClientContext.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ public static BigtableClientContext create(EnhancedBigtableStubSettings settings
121121
.getInternalMetricsProvider()
122122
.createOtelProvider(settings, credentials, backgroundExecutor);
123123
if (internalOtel != null) {
124-
channelPoolMetricsTracer =
125-
new ChannelPoolMetricsTracer(
126-
internalOtel, EnhancedBigtableStub.createBuiltinAttributes(builder.build()));
124+
channelPoolMetricsTracer = new ChannelPoolMetricsTracer(internalOtel);
127125

128126
// Configure grpc metrics
129127
configureGrpcOtel(transportProvider, internalOtel);

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/metrics/ChannelPoolMetricsTracer.java

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.concurrent.TimeUnit;
3333
import java.util.concurrent.atomic.AtomicReference;
3434
import java.util.logging.Logger;
35-
import javax.annotation.Nullable;
3635

3736
@InternalApi("For internal use only")
3837
public class ChannelPoolMetricsTracer implements Runnable {
@@ -45,15 +44,11 @@ public class ChannelPoolMetricsTracer implements Runnable {
4544
private final AtomicReference<BigtableChannelPoolObserver> bigtableChannelInsightsProviderRef =
4645
new AtomicReference<>();
4746
private final AtomicReference<String> lbPolicyRef = new AtomicReference<>("ROUND_ROBIN");
48-
private final Attributes commonAttrs;
4947

5048
// Attributes for unary and streaming RPCs, built on demand in run()
51-
@Nullable private Attributes unaryAttributes;
52-
@Nullable private Attributes streamingAttributes;
5349

54-
public ChannelPoolMetricsTracer(OpenTelemetry openTelemetry, Attributes commonAttrs) {
50+
public ChannelPoolMetricsTracer(OpenTelemetry openTelemetry) {
5551
Meter meter = openTelemetry.getMeter(METER_NAME);
56-
this.commonAttrs = commonAttrs;
5752
this.outstandingRpcsHistogram =
5853
meter
5954
.histogramBuilder(OUTSTANDING_RPCS_PER_CHANNEL_NAME)
@@ -98,35 +93,54 @@ public void run() {
9893
logger.warning("No Bigtable ChannelPoolObserver available");
9994
return; // Not registered yet
10095
}
101-
String lbPolicy = lbPolicyRef.get();
102-
103-
// Build attributes if they haven't been built yet.
104-
if (unaryAttributes == null || streamingAttributes == null) {
105-
Attributes baseAttrs = commonAttrs.toBuilder().put("lb_policy", lbPolicy).build();
106-
this.unaryAttributes = baseAttrs.toBuilder().put("streaming", false).build();
107-
this.streamingAttributes = baseAttrs.toBuilder().put("streaming", true).build();
108-
}
10996
List<? extends BigtableChannelObserver> channelInsights =
11097
channelInsightsProvider.getChannelInfos();
11198
if (channelInsights == null || channelInsights.isEmpty()) {
11299
return;
113100
}
101+
102+
String lbPolicy = lbPolicyRef.get();
103+
104+
// Build the four permutations once per run to avoid allocations in the channel loop
105+
Attributes dpUnaryAttrs =
106+
Attributes.builder()
107+
.put("transport_type", "directpath")
108+
.put("streaming", false)
109+
.put("lb_policy", lbPolicy)
110+
.build();
111+
Attributes dpStreamingAttrs =
112+
Attributes.builder()
113+
.put("transport_type", "directpath")
114+
.put("streaming", true)
115+
.put("lb_policy", lbPolicy)
116+
.build();
117+
Attributes cpUnaryAttrs =
118+
Attributes.builder()
119+
.put("transport_type", "cloudpath")
120+
.put("streaming", false)
121+
.put("lb_policy", lbPolicy)
122+
.build();
123+
Attributes cpStreamingAttrs =
124+
Attributes.builder()
125+
.put("transport_type", "cloudpath")
126+
.put("streaming", true)
127+
.put("lb_policy", lbPolicy)
128+
.build();
129+
114130
for (BigtableChannelObserver info : channelInsights) {
115-
String transportTypeValue = info.isAltsChannel() ? "DIRECTPATH" : "CLOUDPATH";
116-
this.unaryAttributes =
117-
this.unaryAttributes.toBuilder().put("transport_type", transportTypeValue).build();
118-
this.streamingAttributes =
119-
this.streamingAttributes.toBuilder().put("transport_type", transportTypeValue).build();
131+
Attributes unaryAttrs = info.isAltsChannel() ? dpUnaryAttrs : cpUnaryAttrs;
132+
Attributes streamingAttrs = info.isAltsChannel() ? dpStreamingAttrs : cpStreamingAttrs;
120133

121134
long currentOutstandingUnaryRpcs = info.getOutstandingUnaryRpcs();
122135
long currentOutstandingStreamingRpcs = info.getOutstandingStreamingRpcs();
123-
// Record outstanding unary RPCs with streaming=false
124-
outstandingRpcsHistogram.record(currentOutstandingUnaryRpcs, unaryAttributes);
125-
// Record outstanding streaming RPCs with streaming=true
126-
outstandingRpcsHistogram.record(currentOutstandingStreamingRpcs, streamingAttributes);
136+
137+
// Record outstanding RPCs with the transport_type, streaming, and lb_policy attributes
138+
outstandingRpcsHistogram.record(currentOutstandingUnaryRpcs, unaryAttrs);
139+
outstandingRpcsHistogram.record(currentOutstandingStreamingRpcs, streamingAttrs);
127140

128141
long errors = info.getAndResetErrorCount();
129-
perConnectionErrorCountHistogram.record(errors, commonAttrs);
142+
// Record errors with empty attributes as requested
143+
perConnectionErrorCountHistogram.record(errors, Attributes.empty());
130144
}
131145
}
132146
}

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/metrics/ChannelPoolMetricsTracerTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public class ChannelPoolMetricsTracerTest {
6060
private ArgumentCaptor<Runnable> runnableCaptor;
6161

6262
private ChannelPoolMetricsTracer tracker;
63-
private Attributes baseAttributes;
6463

6564
@Mock private BigtableChannelPoolObserver mockInsightsProvider;
6665
@Mock private BigtableChannelObserver mockInsight1;
@@ -74,9 +73,7 @@ public void setUp() {
7473
OpenTelemetry openTelemetry =
7574
OpenTelemetrySdk.builder().setMeterProvider(meterProvider).build();
7675

77-
baseAttributes = Attributes.builder().build();
78-
79-
tracker = new ChannelPoolMetricsTracer(openTelemetry, baseAttributes);
76+
tracker = new ChannelPoolMetricsTracer(openTelemetry);
8077

8178
runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
8279
// Configure mockScheduler to capture the runnable when tracker.start() is called
@@ -114,7 +111,7 @@ private Attributes getExpectedErrorAttributes() {
114111

115112
private static Attributes getExpectedRpcAttributes(String lbPolicy, boolean streaming) {
116113
return Attributes.builder()
117-
.put(AttributeKey.stringKey("transport_type"), "CLOUDPATH")
114+
.put(AttributeKey.stringKey("transport_type"), "cloudpath")
118115
.put(AttributeKey.stringKey("lb_policy"), lbPolicy)
119116
.put(AttributeKey.booleanKey("streaming"), streaming)
120117
.build();

0 commit comments

Comments
 (0)