Samrat002 commented on code in PR #28427: URL: https://github.com/apache/flink/pull/28427#discussion_r3619823305
########## flink-filesystems/flink-s3-fs-native/src/test/java/org/apache/flink/fs/s3native/metrics/AwsSdkMetricBridgeTest.java: ########## @@ -0,0 +1,231 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.flink.fs.s3native.metrics; + +import org.apache.flink.metrics.Counter; +import org.apache.flink.metrics.Histogram; +import org.apache.flink.metrics.MetricGroup; +import org.apache.flink.metrics.groups.UnregisteredMetricsGroup; + +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.core.metrics.CoreMetric; +import software.amazon.awssdk.http.HttpMetric; +import software.amazon.awssdk.metrics.MetricCollection; +import software.amazon.awssdk.metrics.MetricCollector; + +import java.time.Duration; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Tests for {@link AwsSdkMetricBridge}'s translation of SDK metric records into Flink metrics. */ +class AwsSdkMetricBridgeTest { + + @Test + void successfulCallIncrementsApiCallCountAndRecordsDuration() { + CapturingMetricGroup root = new CapturingMetricGroup(); + AwsSdkMetricBridge bridge = new AwsSdkMetricBridge(root); + + bridge.publish(apiCall("PutObject", Duration.ofMillis(120), true, 0, 200)); + + assertThat(root.count("op=PutObject/status_class=2xx/api_call_count")).isEqualTo(1L); + Histogram histogram = root.histograms.get("op=PutObject/api_call_duration_ms"); + assertThat(histogram).isNotNull(); + assertThat(histogram.getCount()).isEqualTo(1L); + assertThat(histogram.getStatistics().getMax()).isEqualTo(120L); + assertThat(root.counters).doesNotContainKey("op=PutObject/throttle_count"); + } + + @Test + void throttledCallIncrementsThrottleAndRetryCounts() { + CapturingMetricGroup root = new CapturingMetricGroup(); + AwsSdkMetricBridge bridge = new AwsSdkMetricBridge(root); + + // Two throttled attempts (503) followed by a successful one (200); RETRY_COUNT = 2. + bridge.publish(apiCall("UploadPart", Duration.ofMillis(900), true, 2, 503, 503, 200)); + + assertThat(root.count("op=UploadPart/throttle_count")).isEqualTo(2L); + assertThat(root.count("op=UploadPart/reason=throttled/retry_count")).isEqualTo(2L); + // The final attempt succeeded, so the overall call is classified 2xx. + assertThat(root.count("op=UploadPart/status_class=2xx/api_call_count")).isEqualTo(1L); + } + + @Test + void clientErrorIsClassifiedAs4xx() { + CapturingMetricGroup root = new CapturingMetricGroup(); + AwsSdkMetricBridge bridge = new AwsSdkMetricBridge(root); + + bridge.publish(apiCall("HeadObject", Duration.ofMillis(20), false, 0, 404)); + + assertThat(root.count("op=HeadObject/status_class=4xx/api_call_count")).isEqualTo(1L); + assertThat(root.counters).doesNotContainKey("op=HeadObject/throttle_count"); + } + + @Test + void allowlistRegistersOnlyTheListedMetrics() { + CapturingMetricGroup root = new CapturingMetricGroup(); + // Only api_call_count is allowed; duration, throttle and retry must be skipped. + AwsSdkMetricBridge bridge = + new AwsSdkMetricBridge( + root, + Collections.singletonList(AwsSdkMetricBridge.API_CALL_COUNT), + S3MetricHistogram.DEFAULT_WINDOW_SIZE); + + bridge.publish(apiCall("UploadPart", Duration.ofMillis(900), true, 2, 503, 503, 200)); + + assertThat(root.count("op=UploadPart/status_class=2xx/api_call_count")).isEqualTo(1L); + assertThat(root.histograms).doesNotContainKey("op=UploadPart/api_call_duration_ms"); + assertThat(root.counters).doesNotContainKey("op=UploadPart/throttle_count"); + assertThat(root.counters).doesNotContainKey("op=UploadPart/reason=throttled/retry_count"); + } + + @Test + void wildcardAllowlistRegistersEveryMetric() { + CapturingMetricGroup root = new CapturingMetricGroup(); + AwsSdkMetricBridge bridge = + new AwsSdkMetricBridge( + root, + Collections.singletonList("*"), + S3MetricHistogram.DEFAULT_WINDOW_SIZE); + + bridge.publish(apiCall("UploadPart", Duration.ofMillis(900), true, 2, 503, 503, 200)); + + assertThat(root.count("op=UploadPart/status_class=2xx/api_call_count")).isEqualTo(1L); + assertThat(root.histograms.get("op=UploadPart/api_call_duration_ms")).isNotNull(); + assertThat(root.count("op=UploadPart/throttle_count")).isEqualTo(2L); + assertThat(root.count("op=UploadPart/reason=throttled/retry_count")).isEqualTo(2L); + } + + @Test + void emptyAllowlistFallsBackToDefaults() { + CapturingMetricGroup root = new CapturingMetricGroup(); + AwsSdkMetricBridge bridge = + new AwsSdkMetricBridge( + root, Collections.emptyList(), S3MetricHistogram.DEFAULT_WINDOW_SIZE); + + bridge.publish(apiCall("PutObject", Duration.ofMillis(120), true, 0, 200)); + + // The five default metrics include api_call_count and api_call_duration_ms. + assertThat(root.count("op=PutObject/status_class=2xx/api_call_count")).isEqualTo(1L); + assertThat(root.histograms.get("op=PutObject/api_call_duration_ms")).isNotNull(); + } + + @Test + void serverErrorRetryIsClassifiedAs5xx() { Review Comment: Done in 47babe49e34. The shared layer now defines cloud-neutral `StatusClass` and `RetryReason` values and owns metric registration. `AwsSdkMetricBridge` explicitly contains only AWS SDK extraction and AWS HTTP-code mapping, including the AWS-specific 503 throttling treatment. ########## flink-filesystems/flink-s3-fs-native/src/test/java/org/apache/flink/fs/s3native/metrics/AwsSdkMetricBridgeTest.java: ########## @@ -0,0 +1,231 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.flink.fs.s3native.metrics; + +import org.apache.flink.metrics.Counter; +import org.apache.flink.metrics.Histogram; +import org.apache.flink.metrics.MetricGroup; +import org.apache.flink.metrics.groups.UnregisteredMetricsGroup; + +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.core.metrics.CoreMetric; +import software.amazon.awssdk.http.HttpMetric; +import software.amazon.awssdk.metrics.MetricCollection; +import software.amazon.awssdk.metrics.MetricCollector; + +import java.time.Duration; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Tests for {@link AwsSdkMetricBridge}'s translation of SDK metric records into Flink metrics. */ +class AwsSdkMetricBridgeTest { + + @Test + void successfulCallIncrementsApiCallCountAndRecordsDuration() { + CapturingMetricGroup root = new CapturingMetricGroup(); + AwsSdkMetricBridge bridge = new AwsSdkMetricBridge(root); + + bridge.publish(apiCall("PutObject", Duration.ofMillis(120), true, 0, 200)); + + assertThat(root.count("op=PutObject/status_class=2xx/api_call_count")).isEqualTo(1L); + Histogram histogram = root.histograms.get("op=PutObject/api_call_duration_ms"); + assertThat(histogram).isNotNull(); + assertThat(histogram.getCount()).isEqualTo(1L); + assertThat(histogram.getStatistics().getMax()).isEqualTo(120L); + assertThat(root.counters).doesNotContainKey("op=PutObject/throttle_count"); + } + + @Test + void throttledCallIncrementsThrottleAndRetryCounts() { + CapturingMetricGroup root = new CapturingMetricGroup(); + AwsSdkMetricBridge bridge = new AwsSdkMetricBridge(root); + + // Two throttled attempts (503) followed by a successful one (200); RETRY_COUNT = 2. + bridge.publish(apiCall("UploadPart", Duration.ofMillis(900), true, 2, 503, 503, 200)); + + assertThat(root.count("op=UploadPart/throttle_count")).isEqualTo(2L); + assertThat(root.count("op=UploadPart/reason=throttled/retry_count")).isEqualTo(2L); + // The final attempt succeeded, so the overall call is classified 2xx. + assertThat(root.count("op=UploadPart/status_class=2xx/api_call_count")).isEqualTo(1L); + } + + @Test + void clientErrorIsClassifiedAs4xx() { + CapturingMetricGroup root = new CapturingMetricGroup(); + AwsSdkMetricBridge bridge = new AwsSdkMetricBridge(root); + + bridge.publish(apiCall("HeadObject", Duration.ofMillis(20), false, 0, 404)); + + assertThat(root.count("op=HeadObject/status_class=4xx/api_call_count")).isEqualTo(1L); + assertThat(root.counters).doesNotContainKey("op=HeadObject/throttle_count"); + } + + @Test + void allowlistRegistersOnlyTheListedMetrics() { + CapturingMetricGroup root = new CapturingMetricGroup(); + // Only api_call_count is allowed; duration, throttle and retry must be skipped. + AwsSdkMetricBridge bridge = + new AwsSdkMetricBridge( + root, + Collections.singletonList(AwsSdkMetricBridge.API_CALL_COUNT), + S3MetricHistogram.DEFAULT_WINDOW_SIZE); + + bridge.publish(apiCall("UploadPart", Duration.ofMillis(900), true, 2, 503, 503, 200)); + + assertThat(root.count("op=UploadPart/status_class=2xx/api_call_count")).isEqualTo(1L); + assertThat(root.histograms).doesNotContainKey("op=UploadPart/api_call_duration_ms"); + assertThat(root.counters).doesNotContainKey("op=UploadPart/throttle_count"); + assertThat(root.counters).doesNotContainKey("op=UploadPart/reason=throttled/retry_count"); + } + + @Test + void wildcardAllowlistRegistersEveryMetric() { + CapturingMetricGroup root = new CapturingMetricGroup(); + AwsSdkMetricBridge bridge = + new AwsSdkMetricBridge( + root, + Collections.singletonList("*"), + S3MetricHistogram.DEFAULT_WINDOW_SIZE); + + bridge.publish(apiCall("UploadPart", Duration.ofMillis(900), true, 2, 503, 503, 200)); + + assertThat(root.count("op=UploadPart/status_class=2xx/api_call_count")).isEqualTo(1L); + assertThat(root.histograms.get("op=UploadPart/api_call_duration_ms")).isNotNull(); + assertThat(root.count("op=UploadPart/throttle_count")).isEqualTo(2L); + assertThat(root.count("op=UploadPart/reason=throttled/retry_count")).isEqualTo(2L); + } + + @Test + void emptyAllowlistFallsBackToDefaults() { + CapturingMetricGroup root = new CapturingMetricGroup(); + AwsSdkMetricBridge bridge = + new AwsSdkMetricBridge( + root, Collections.emptyList(), S3MetricHistogram.DEFAULT_WINDOW_SIZE); + + bridge.publish(apiCall("PutObject", Duration.ofMillis(120), true, 0, 200)); + + // The five default metrics include api_call_count and api_call_duration_ms. + assertThat(root.count("op=PutObject/status_class=2xx/api_call_count")).isEqualTo(1L); + assertThat(root.histograms.get("op=PutObject/api_call_duration_ms")).isNotNull(); + } + + @Test + void serverErrorRetryIsClassifiedAs5xx() { + CapturingMetricGroup root = new CapturingMetricGroup(); + AwsSdkMetricBridge bridge = new AwsSdkMetricBridge(root); + + bridge.publish(apiCall("GetObject", Duration.ofMillis(50), true, 1, 500, 200)); + + assertThat(root.count("op=GetObject/reason=5xx/retry_count")).isEqualTo(1L); + assertThat(root.counters).doesNotContainKey("op=GetObject/throttle_count"); + } + + @Test + void accumulatesAcrossMultipleCalls() { + CapturingMetricGroup root = new CapturingMetricGroup(); + AwsSdkMetricBridge bridge = new AwsSdkMetricBridge(root); + + bridge.publish(apiCall("GetObject", Duration.ofMillis(10), true, 0, 200)); + bridge.publish(apiCall("GetObject", Duration.ofMillis(30), true, 0, 200)); + + assertThat(root.count("op=GetObject/status_class=2xx/api_call_count")).isEqualTo(2L); + Histogram histogram = root.histograms.get("op=GetObject/api_call_duration_ms"); + assertThat(histogram.getCount()).isEqualTo(2L); + assertThat(histogram.getStatistics().getMin()).isEqualTo(10L); + assertThat(histogram.getStatistics().getMax()).isEqualTo(30L); + } + + @Test + void publishOfEmptyCollectionDoesNotThrowAndUsesUnknownOp() { + CapturingMetricGroup root = new CapturingMetricGroup(); + AwsSdkMetricBridge bridge = new AwsSdkMetricBridge(root); + + bridge.publish(MetricCollector.create("ApiCall").collect()); + + assertThat(root.counters.keySet()).anyMatch(key -> key.contains("op=Unknown")); + } + + private static MetricCollection apiCall( + String op, Duration duration, boolean successful, int retries, int... attemptStatuses) { + MetricCollector apiCall = MetricCollector.create("ApiCall"); + apiCall.reportMetric(CoreMetric.OPERATION_NAME, op); + apiCall.reportMetric(CoreMetric.API_CALL_DURATION, duration); + apiCall.reportMetric(CoreMetric.API_CALL_SUCCESSFUL, successful); + apiCall.reportMetric(CoreMetric.RETRY_COUNT, retries); + for (int status : attemptStatuses) { + MetricCollector attempt = apiCall.createChild("ApiCallAttempt"); + attempt.reportMetric(HttpMetric.HTTP_STATUS_CODE, status); + } + return apiCall.collect(); + } + + /** A {@link MetricGroup} that captures registered metrics keyed by their full label path. */ + private static final class CapturingMetricGroup extends UnregisteredMetricsGroup { Review Comment: Done in 47babe49e34. I removed the bespoke `CapturingMetricGroup` and switched both the AWS adapter and cloud-neutral recorder tests to Flink's existing `MetricListener` test utility. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
