Skip to content

Commit 589c66c

Browse files
nidhiii-27Dhriti07
andauthored
feat(storage): wire logical operation metrics in Retrying and StorageOptions (#14695)
Wires client-level metrics lifecycle and retrier operation metrics into StorageOptions and Retrying. - Initializes `StorageClientMetrics` provider and lifecycle management in `StorageOptions`. - Instruments `Retrying` to record operation attempts, durations, and status attributes. - Adds comprehensive unit tests (`StorageOptionsTest`, `StorageClientMetricsTest`, `RetryingMetricsTest`). Stacked on #14728. --------- Co-authored-by: Dhriti07 <56169283+Dhriti07@users.noreply.github.com>
1 parent 7c266f2 commit 589c66c

11 files changed

Lines changed: 1398 additions & 152 deletions

‎java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageOptions.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,9 @@ public Storage create(StorageOptions options) {
10081008
OpenTelemetry otel = options.getOpenTelemetry();
10091009
DefaultRetrier retrier =
10101010
new DefaultRetrier(
1011-
OtelStorageDecorator.retryContextDecorator(otel), grpcStorageOptions);
1011+
OtelStorageDecorator.retryContextDecorator(otel),
1012+
grpcStorageOptions,
1013+
grpcStorageOptions.getStorageClientMetrics());
10121014
if (ZeroCopyReadinessChecker.isReady()) {
10131015
LOGGER.config("zero-copy protobuf deserialization available, using it");
10141016
StorageStubSettings baseSettings =

‎java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/OpenTelemetryBootstrappingUtils.java‎

Lines changed: 141 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.common.collect.ImmutableList;
2727
import com.google.common.collect.ImmutableSet;
2828
import com.google.common.collect.Iterables;
29+
import com.google.common.net.HostAndPort;
2930
import io.grpc.ManagedChannelBuilder;
3031
import io.grpc.opentelemetry.GrpcOpenTelemetry;
3132
import io.opentelemetry.api.common.AttributeKey;
@@ -103,28 +104,12 @@ static ChannelConfigurator enableGrpcMetrics(
103104
GCPResourceProvider resourceProvider = new GCPResourceProvider();
104105
Attributes detectedAttributes = resourceProvider.getAttributes();
105106

106-
@Nullable String detectedProjectId =
107-
detectedAttributes.get(AttributeKey.stringKey("cloud.account.id"));
108-
if (projectId == null && detectedProjectId == null) {
109-
log.warning(
110-
"Unable to determine the Project ID in order to report metrics. No gRPC client metrics"
111-
+ " will be reported.");
107+
String projectIdToUse =
108+
resolveProjectIdForMetrics(detectedAttributes, projectId, "gRPC client metrics");
109+
if (projectIdToUse == null) {
112110
return channelConfigurator != null ? channelConfigurator : ChannelConfigurator.identity();
113111
}
114112

115-
String projectIdToUse = detectedProjectId == null ? projectId : detectedProjectId;
116-
if (!projectIdToUse.equals(projectId)) {
117-
log.warning(
118-
"The Project ID configured for gRPC client metrics is "
119-
+ projectIdToUse
120-
+ ", but the Project ID of the storage client is "
121-
+ projectId
122-
+ ". Make sure that the service account in use has the required metric writing role "
123-
+ "(roles/monitoring.metricWriter) in the project "
124-
+ projectIdToUse
125-
+ ", or metrics will not be written.");
126-
}
127-
128113
String metricServiceEndpoint = getCloudMonitoringEndpoint(endpoint, universeDomain);
129114
SdkMeterProvider provider =
130115
createMeterProvider(
@@ -206,7 +191,143 @@ static String getCloudMonitoringEndpoint(String endpoint, String universeDomain)
206191
metricServiceEndpoint = match;
207192
}
208193
}
209-
return metricServiceEndpoint + ":" + endpoint.split(":")[1];
194+
HostAndPort parsed = parseHostAndPort(endpoint);
195+
int port = parsed != null ? parsed.getPortOrDefault(443) : 443;
196+
return metricServiceEndpoint + ":" + port;
197+
}
198+
199+
@Nullable
200+
private static HostAndPort parseHostAndPort(@Nullable String host) {
201+
if (host == null || host.trim().isEmpty()) {
202+
return null;
203+
}
204+
String s = host.trim();
205+
if (s.startsWith("google-c2p:///")) {
206+
s = s.substring("google-c2p:///".length());
207+
} else if (s.startsWith("https://")) {
208+
s = s.substring("https://".length());
209+
} else if (s.startsWith("http://")) {
210+
s = s.substring("http://".length());
211+
}
212+
int slashIdx = s.indexOf('/');
213+
if (slashIdx >= 0) {
214+
s = s.substring(0, slashIdx);
215+
}
216+
if (s.isEmpty()) {
217+
return null;
218+
}
219+
try {
220+
return HostAndPort.fromString(s);
221+
} catch (IllegalArgumentException e) {
222+
return null;
223+
}
224+
}
225+
226+
static String formatStorageEndpoint(@Nullable String host) {
227+
HostAndPort parsed = parseHostAndPort(host);
228+
if (parsed == null) {
229+
return "storage.googleapis.com:443";
230+
}
231+
return parsed.withDefaultPort(443).toString();
232+
}
233+
234+
@Nullable
235+
static String extractServerAddress(@Nullable String host) {
236+
HostAndPort parsed = parseHostAndPort(host);
237+
return parsed != null ? parsed.getHost() : null;
238+
}
239+
240+
@Nullable
241+
private static String resolveProjectIdForMetrics(
242+
Attributes detectedAttributes,
243+
@Nullable String configuredProjectId,
244+
String metricsTypeLabel) {
245+
@Nullable String detectedProjectId =
246+
detectedAttributes.get(AttributeKey.stringKey("cloud.account.id"));
247+
if (detectedProjectId != null && detectedProjectId.trim().isEmpty()) {
248+
detectedProjectId = null;
249+
}
250+
@Nullable String cleanConfiguredProjectId =
251+
(configuredProjectId != null && !configuredProjectId.trim().isEmpty())
252+
? configuredProjectId.trim()
253+
: null;
254+
if (cleanConfiguredProjectId == null && detectedProjectId == null) {
255+
log.warning(
256+
"Unable to determine the Project ID in order to report metrics. No "
257+
+ metricsTypeLabel
258+
+ " will be reported.");
259+
return null;
260+
}
261+
String projectIdToUse =
262+
detectedProjectId != null ? detectedProjectId : cleanConfiguredProjectId;
263+
if (cleanConfiguredProjectId != null && !projectIdToUse.equals(cleanConfiguredProjectId)) {
264+
log.warning(
265+
"The Project ID configured for "
266+
+ metricsTypeLabel
267+
+ " is "
268+
+ projectIdToUse
269+
+ ", but the Project ID of the storage client is "
270+
+ cleanConfiguredProjectId
271+
+ ". Make sure that the service account in use has the required metric writing"
272+
+ " role (roles/monitoring.metricWriter) in the project "
273+
+ projectIdToUse
274+
+ ", or metrics will not be written.");
275+
}
276+
return projectIdToUse;
277+
}
278+
279+
@Nullable
280+
static SdkMeterProvider createDefaultClientMeterProvider(
281+
@Nullable String host,
282+
@Nullable String configuredProjectId,
283+
@Nullable String universeDomain,
284+
@Nullable Duration metricInterval,
285+
String api) {
286+
try {
287+
GCPResourceProvider resourceProvider = new GCPResourceProvider();
288+
Attributes detectedAttributes = resourceProvider.getAttributes();
289+
return createDefaultClientMeterProvider(
290+
host, configuredProjectId, universeDomain, metricInterval, api, detectedAttributes);
291+
} catch (Exception e) {
292+
log.warning("Failed to create default client MeterProvider: " + e.getMessage());
293+
return null;
294+
}
295+
}
296+
297+
@VisibleForTesting
298+
@Nullable
299+
static SdkMeterProvider createDefaultClientMeterProvider(
300+
@Nullable String host,
301+
@Nullable String configuredProjectId,
302+
@Nullable String universeDomain,
303+
@Nullable Duration metricInterval,
304+
String api,
305+
Attributes detectedAttributes) {
306+
try {
307+
String projectIdToUse =
308+
resolveProjectIdForMetrics(
309+
detectedAttributes, configuredProjectId, "storage client metrics");
310+
if (projectIdToUse == null) {
311+
return null;
312+
}
313+
Attributes attributesToUse =
314+
Attributes.builder()
315+
.putAll(detectedAttributes)
316+
.put(AttributeKey.stringKey("cloud.account.id"), projectIdToUse)
317+
.build();
318+
String formattedEndpoint = formatStorageEndpoint(host);
319+
String metricServiceEndpoint = getCloudMonitoringEndpoint(formattedEndpoint, universeDomain);
320+
return createClientMeterProvider(
321+
metricServiceEndpoint,
322+
projectIdToUse,
323+
attributesToUse,
324+
metricInterval,
325+
/* shouldSuppressExceptions= */ true,
326+
api);
327+
} catch (Exception e) {
328+
log.warning("Failed to create default client MeterProvider: " + e.getMessage());
329+
return null;
330+
}
210331
}
211332

212333
static final ImmutableList<String> CLIENT_LATENCY_HISTOGRAMS =

0 commit comments

Comments
 (0)