Repository navigation
fix(it): retry transient Cloud Monitoring errors when reading load test metrics #4650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
aasthabharill
merged 6 commits into
GoogleCloudPlatform:main
from
aasthabharill:fix/lt-monitoring-retry
Nov 30, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c90631d
test: retry transient Cloud Monitoring errors when reading load test …
aasthabharill 859140d
fix(it): use the Cloud Monitoring guide's backoff for metric read ret…
aasthabharill e3970d5
fix(it): trim the retry backoff comment
aasthabharill 337359f
fix(it): log transient Monitoring errors without the stack trace
aasthabharill d39b181
fix(it): cap total retry time for Monitoring reads and import Duration
aasthabharill 45928cc
fix(it): keep the protobuf Duration import in MonitoringClient
aasthabharill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
...-cloud-platform/src/test/java/org/apache/beam/it/gcp/monitoring/MonitoringClientTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| /* | ||
| * Copyright (C) 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package org.apache.beam.it.gcp.monitoring; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
| import static org.junit.Assert.assertThrows; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import com.google.api.gax.rpc.ApiException; | ||
| import com.google.api.gax.rpc.ApiExceptionFactory; | ||
| import com.google.api.gax.rpc.StatusCode; | ||
| import com.google.cloud.monitoring.v3.MetricServiceClient; | ||
| import com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse; | ||
| import com.google.monitoring.v3.ListTimeSeriesRequest; | ||
| import com.google.monitoring.v3.Point; | ||
| import com.google.monitoring.v3.TimeSeries; | ||
| import com.google.monitoring.v3.TypedValue; | ||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| /** Unit tests for {@link MonitoringClient}. */ | ||
| @RunWith(JUnit4.class) | ||
| public final class MonitoringClientTest { | ||
|
|
||
| private static final ListTimeSeriesRequest REQUEST = ListTimeSeriesRequest.getDefaultInstance(); | ||
|
|
||
| private MetricServiceClient metricServiceClient; | ||
| private MonitoringClient monitoringClient; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| metricServiceClient = mock(MetricServiceClient.class); | ||
| monitoringClient = | ||
| MonitoringClient.withMonitoringClient( | ||
| metricServiceClient, Duration.ofMillis(1), Duration.ofMillis(2), Duration.ofMinutes(1)); | ||
| } | ||
|
|
||
| @Test | ||
| public void listTimeSeriesAsDouble_transientErrorThenSuccess_retriesAndReturnsValues() { | ||
| ListTimeSeriesPagedResponse response = responseWithDoubles(1.5, 2.5); | ||
| when(metricServiceClient.listTimeSeries(any(ListTimeSeriesRequest.class))) | ||
| .thenThrow(apiException(StatusCode.Code.INTERNAL)) | ||
| .thenThrow(apiException(StatusCode.Code.UNAVAILABLE)) | ||
| .thenThrow(apiException(StatusCode.Code.DEADLINE_EXCEEDED)) | ||
| .thenReturn(response); | ||
|
|
||
| List<Double> values = monitoringClient.listTimeSeriesAsDouble(REQUEST); | ||
|
|
||
| assertThat(values).containsExactly(1.5, 2.5).inOrder(); | ||
| verify(metricServiceClient, times(4)).listTimeSeries(any(ListTimeSeriesRequest.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void listTimeSeriesAsDouble_transientErrorWhilePaging_retriesWholeRead() { | ||
| ListTimeSeriesPagedResponse failingResponse = mock(ListTimeSeriesPagedResponse.class); | ||
| when(failingResponse.iterateAll()).thenThrow(apiException(StatusCode.Code.INTERNAL)); | ||
| ListTimeSeriesPagedResponse response = responseWithDoubles(3.0); | ||
| when(metricServiceClient.listTimeSeries(any(ListTimeSeriesRequest.class))) | ||
| .thenReturn(failingResponse) | ||
| .thenReturn(response); | ||
|
|
||
| List<Double> values = monitoringClient.listTimeSeriesAsDouble(REQUEST); | ||
|
|
||
| assertThat(values).containsExactly(3.0); | ||
| } | ||
|
|
||
| @Test | ||
| public void listTimeSeriesAsDouble_nonTransientError_failsWithoutRetry() { | ||
| when(metricServiceClient.listTimeSeries(any(ListTimeSeriesRequest.class))) | ||
| .thenThrow(apiException(StatusCode.Code.RESOURCE_EXHAUSTED)); | ||
|
|
||
| ApiException thrown = | ||
| assertThrows(ApiException.class, () -> monitoringClient.listTimeSeriesAsDouble(REQUEST)); | ||
|
|
||
| assertThat(thrown.getStatusCode().getCode()).isEqualTo(StatusCode.Code.RESOURCE_EXHAUSTED); | ||
| verify(metricServiceClient, times(1)).listTimeSeries(any(ListTimeSeriesRequest.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void listTimeSeriesAsLong_transientErrorNeverClears_failsAfterRetries() { | ||
| when(metricServiceClient.listTimeSeries(any(ListTimeSeriesRequest.class))) | ||
| .thenThrow(apiException(StatusCode.Code.INTERNAL)); | ||
|
|
||
| ApiException thrown = | ||
| assertThrows(ApiException.class, () -> monitoringClient.listTimeSeriesAsLong(REQUEST)); | ||
|
|
||
| assertThat(thrown.getStatusCode().getCode()).isEqualTo(StatusCode.Code.INTERNAL); | ||
| // One initial attempt plus six retries. | ||
| verify(metricServiceClient, times(7)).listTimeSeries(any(ListTimeSeriesRequest.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void listTimeSeriesAsDouble_maxRetryDurationExceeded_stopsRetrying() { | ||
| MonitoringClient shortDurationClient = | ||
| MonitoringClient.withMonitoringClient( | ||
| metricServiceClient, Duration.ofMillis(1), Duration.ofMillis(2), Duration.ofMillis(50)); | ||
| // The first attempt alone outlasts the 50ms limit, so no retry should follow it. | ||
| when(metricServiceClient.listTimeSeries(any(ListTimeSeriesRequest.class))) | ||
| .thenAnswer( | ||
| invocation -> { | ||
| Thread.sleep(200); | ||
| throw apiException(StatusCode.Code.INTERNAL); | ||
| }); | ||
|
|
||
| ApiException thrown = | ||
| assertThrows(ApiException.class, () -> shortDurationClient.listTimeSeriesAsDouble(REQUEST)); | ||
|
|
||
| assertThat(thrown.getStatusCode().getCode()).isEqualTo(StatusCode.Code.INTERNAL); | ||
| verify(metricServiceClient, times(1)).listTimeSeries(any(ListTimeSeriesRequest.class)); | ||
| } | ||
|
|
||
| private static ListTimeSeriesPagedResponse responseWithDoubles(double... values) { | ||
| TimeSeries.Builder timeSeries = TimeSeries.newBuilder(); | ||
| for (double value : values) { | ||
| timeSeries.addPoints( | ||
| Point.newBuilder().setValue(TypedValue.newBuilder().setDoubleValue(value))); | ||
| } | ||
| ListTimeSeriesPagedResponse response = mock(ListTimeSeriesPagedResponse.class); | ||
| when(response.iterateAll()).thenReturn(List.of(timeSeries.build())); | ||
| return response; | ||
| } | ||
|
|
||
| private static ApiException apiException(StatusCode.Code code) { | ||
| StatusCode statusCode = | ||
| new StatusCode() { | ||
| @Override | ||
| public Code getCode() { | ||
| return code; | ||
| } | ||
|
|
||
| @Override | ||
| public Object getTransportCode() { | ||
| return null; | ||
| } | ||
| }; | ||
| return ApiExceptionFactory.createException( | ||
| new RuntimeException("simulated " + code), statusCode, false); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.