You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "nastra (via GitHub)" <gi...@apache.org> on 2023/04/27 06:15:48 UTC

[GitHub] [iceberg] nastra commented on a diff in pull request #7444: AWS: Add SQS MetricsReporter

nastra commented on code in PR #7444:
URL: https://github.com/apache/iceberg/pull/7444#discussion_r1178665979


##########
aws/src/main/java/org/apache/iceberg/aws/metrics/SqsMetricsReporter.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.iceberg.aws.metrics;
+
+import java.util.Map;
+import org.apache.iceberg.aws.AwsProperties;
+import org.apache.iceberg.metrics.CommitReport;
+import org.apache.iceberg.metrics.CommitReportParser;
+import org.apache.iceberg.metrics.MetricsReport;
+import org.apache.iceberg.metrics.MetricsReporter;
+import org.apache.iceberg.metrics.ScanReport;
+import org.apache.iceberg.metrics.ScanReportParser;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
+import software.amazon.awssdk.services.sqs.SqsClient;
+import software.amazon.awssdk.services.sqs.model.SendMessageRequest;
+
+/** An implementation of {@link MetricsReporter} which reports {@link MetricsReport} to SQS */
+public class SqsMetricsReporter implements MetricsReporter {

Review Comment:
   just a general FYI that we're aiming for making all `MetricsReporter` instances fully `Serializable` as part of https://github.com/apache/iceberg/pull/7370. So would be good to consider this here as well. 



##########
aws/src/main/java/org/apache/iceberg/aws/metrics/SqsMetricsReporter.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.iceberg.aws.metrics;
+
+import java.util.Map;
+import org.apache.iceberg.aws.AwsProperties;
+import org.apache.iceberg.metrics.CommitReport;
+import org.apache.iceberg.metrics.CommitReportParser;
+import org.apache.iceberg.metrics.MetricsReport;
+import org.apache.iceberg.metrics.MetricsReporter;
+import org.apache.iceberg.metrics.ScanReport;
+import org.apache.iceberg.metrics.ScanReportParser;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
+import software.amazon.awssdk.services.sqs.SqsClient;
+import software.amazon.awssdk.services.sqs.model.SendMessageRequest;
+
+/** An implementation of {@link MetricsReporter} which reports {@link MetricsReport} to SQS */
+public class SqsMetricsReporter implements MetricsReporter {
+
+  private static final Logger LOG = LoggerFactory.getLogger(SqsMetricsReporter.class);
+
+  private SqsClient sqsClient;
+
+  private String sqsQueueUrl;
+
+  @Override
+  public void initialize(Map<String, String> properties) {
+    AwsProperties awsProperties = new AwsProperties(properties);
+    sqsClient = SqsClient.builder().httpClient(UrlConnectionHttpClient.builder().build()).build();
+    sqsQueueUrl = awsProperties.sqsQueueUrl();
+    Preconditions.checkArgument(
+        null != sqsQueueUrl, "%s should be be set", AwsProperties.SQS_QUEUE_URL);
+  }
+
+  @Override
+  public void report(MetricsReport report) {
+    String message = null;
+    if (report instanceof CommitReport) {
+      message = CommitReportParser.toJson((CommitReport) report);
+    } else if (report instanceof ScanReport) {
+      message = ScanReportParser.toJson((ScanReport) report);
+    }
+
+    LOG.info("Received metrics report: {}", message);
+    sqsClient.sendMessage(
+        SendMessageRequest.builder().messageBody(message).queueUrl(sqsQueueUrl).build());
+    sqsClient.close();

Review Comment:
   +1 on wrapping this in a try-finally. See also my comment above



##########
aws/src/main/java/org/apache/iceberg/aws/metrics/SqsMetricsReporter.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.iceberg.aws.metrics;
+
+import java.util.Map;
+import org.apache.iceberg.aws.AwsProperties;
+import org.apache.iceberg.metrics.CommitReport;
+import org.apache.iceberg.metrics.CommitReportParser;
+import org.apache.iceberg.metrics.MetricsReport;
+import org.apache.iceberg.metrics.MetricsReporter;
+import org.apache.iceberg.metrics.ScanReport;
+import org.apache.iceberg.metrics.ScanReportParser;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
+import software.amazon.awssdk.services.sqs.SqsClient;
+import software.amazon.awssdk.services.sqs.model.SendMessageRequest;
+
+/** An implementation of {@link MetricsReporter} which reports {@link MetricsReport} to SQS */
+public class SqsMetricsReporter implements MetricsReporter {
+
+  private static final Logger LOG = LoggerFactory.getLogger(SqsMetricsReporter.class);
+
+  private SqsClient sqsClient;
+
+  private String sqsQueueUrl;
+
+  @Override
+  public void initialize(Map<String, String> properties) {
+    AwsProperties awsProperties = new AwsProperties(properties);
+    sqsClient = SqsClient.builder().httpClient(UrlConnectionHttpClient.builder().build()).build();
+    sqsQueueUrl = awsProperties.sqsQueueUrl();
+    Preconditions.checkArgument(
+        null != sqsQueueUrl, "%s should be be set", AwsProperties.SQS_QUEUE_URL);
+  }
+
+  @Override
+  public void report(MetricsReport report) {
+    String message = null;
+    if (report instanceof CommitReport) {
+      message = CommitReportParser.toJson((CommitReport) report);
+    } else if (report instanceof ScanReport) {
+      message = ScanReportParser.toJson((ScanReport) report);
+    }

Review Comment:
   typically the expectation of a `MetricsReporter` is that it doesn't throw when `report` is called as otherwise it would interfere with scans/commits.
   For example, the `RESTMetricsReporter` exits early if the `report` ever turns out to be null (which technically shouldn't happen) and executes all operations in a `try-catch`



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org