|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.bigtable.data.v2.stub.metrics; |
| 17 | + |
| 18 | +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.METER_NAME; |
| 19 | +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.OUTSTANDING_RPCS_PER_CHANNEL_NAME; |
| 20 | +import static com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsConstants.PER_CONNECTION_ERROR_COUNT_NAME; |
| 21 | + |
| 22 | +import com.google.api.core.InternalApi; |
| 23 | +import com.google.cloud.bigtable.gaxx.grpc.BigtableChannelObserver; |
| 24 | +import com.google.cloud.bigtable.gaxx.grpc.BigtableChannelPoolObserver; |
| 25 | +import io.opentelemetry.api.OpenTelemetry; |
| 26 | +import io.opentelemetry.api.common.Attributes; |
| 27 | +import io.opentelemetry.api.metrics.LongHistogram; |
| 28 | +import io.opentelemetry.api.metrics.Meter; |
| 29 | +import java.util.List; |
| 30 | +import java.util.concurrent.ScheduledExecutorService; |
| 31 | +import java.util.concurrent.ScheduledFuture; |
| 32 | +import java.util.concurrent.TimeUnit; |
| 33 | +import java.util.concurrent.atomic.AtomicReference; |
| 34 | +import java.util.logging.Logger; |
| 35 | +import javax.annotation.Nullable; |
| 36 | + |
| 37 | +@InternalApi("For internal use only") |
| 38 | +public class ChannelPoolMetricsTracer implements Runnable { |
| 39 | + private static final Logger logger = Logger.getLogger(ChannelPoolMetricsTracer.class.getName()); |
| 40 | + |
| 41 | + private static final int SAMPLING_PERIOD_SECONDS = 60; |
| 42 | + private final LongHistogram outstandingRpcsHistogram; |
| 43 | + private final LongHistogram perConnectionErrorCountHistogram; |
| 44 | + |
| 45 | + private final AtomicReference<BigtableChannelPoolObserver> bigtableChannelInsightsProviderRef = |
| 46 | + new AtomicReference<>(); |
| 47 | + private final AtomicReference<String> lbPolicyRef = new AtomicReference<>("ROUND_ROBIN"); |
| 48 | + private final Attributes commonAttrs; |
| 49 | + |
| 50 | + // Attributes for unary and streaming RPCs, built on demand in run() |
| 51 | + @Nullable private Attributes unaryAttributes; |
| 52 | + @Nullable private Attributes streamingAttributes; |
| 53 | + |
| 54 | + public ChannelPoolMetricsTracer(OpenTelemetry openTelemetry, Attributes commonAttrs) { |
| 55 | + Meter meter = openTelemetry.getMeter(METER_NAME); |
| 56 | + this.commonAttrs = commonAttrs; |
| 57 | + this.outstandingRpcsHistogram = |
| 58 | + meter |
| 59 | + .histogramBuilder(OUTSTANDING_RPCS_PER_CHANNEL_NAME) |
| 60 | + .ofLongs() |
| 61 | + .setDescription( |
| 62 | + "A distribution of the number of outstanding RPCs per connection in the client pool, sampled periodically.") |
| 63 | + .setUnit("1") |
| 64 | + .build(); |
| 65 | + |
| 66 | + this.perConnectionErrorCountHistogram = |
| 67 | + meter |
| 68 | + .histogramBuilder(PER_CONNECTION_ERROR_COUNT_NAME) |
| 69 | + .ofLongs() |
| 70 | + .setDescription("Distribution of counts of channels per 'error count per minute'.") |
| 71 | + .setUnit("1") |
| 72 | + .build(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Registers the provider for the channel pool entries. This should be called by the component |
| 77 | + * that creates the BigtableChannelPool. |
| 78 | + */ |
| 79 | + public void registerChannelInsightsProvider(BigtableChannelPoolObserver channelInsightsProvider) { |
| 80 | + this.bigtableChannelInsightsProviderRef.set(channelInsightsProvider); |
| 81 | + } |
| 82 | + |
| 83 | + /** Register the current lb policy * */ |
| 84 | + public void registerLoadBalancingStrategy(String lbPolicy) { |
| 85 | + this.lbPolicyRef.set(lbPolicy); |
| 86 | + } |
| 87 | + |
| 88 | + /** Starts the periodic collection. */ |
| 89 | + public ScheduledFuture<?> start(ScheduledExecutorService scheduler) { |
| 90 | + return scheduler.scheduleAtFixedRate( |
| 91 | + this, SAMPLING_PERIOD_SECONDS, SAMPLING_PERIOD_SECONDS, TimeUnit.SECONDS); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void run() { |
| 96 | + BigtableChannelPoolObserver channelInsightsProvider = bigtableChannelInsightsProviderRef.get(); |
| 97 | + if (channelInsightsProvider == null) { |
| 98 | + logger.warning("No Bigtable ChannelPoolObserver available"); |
| 99 | + return; // Not registered yet |
| 100 | + } |
| 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 | + } |
| 109 | + List<? extends BigtableChannelObserver> channelInsights = |
| 110 | + channelInsightsProvider.getChannelInfos(); |
| 111 | + if (channelInsights == null || channelInsights.isEmpty()) { |
| 112 | + return; |
| 113 | + } |
| 114 | + 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(); |
| 120 | + |
| 121 | + long currentOutstandingUnaryRpcs = info.getOutstandingUnaryRpcs(); |
| 122 | + 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); |
| 127 | + |
| 128 | + long errors = info.getAndResetErrorCount(); |
| 129 | + perConnectionErrorCountHistogram.record(errors, commonAttrs); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments