singhpk234 commented on code in PR #16235:
URL: https://github.com/apache/iceberg/pull/16235#discussion_r3470224898


##########
aws/src/main/java/org/apache/iceberg/aws/s3/S3FileIOProperties.java:
##########
@@ -1197,4 +1209,63 @@ private <T> T loadSdkPluginConfigurations(String impl, 
Map<String, String> prope
   public Map<String, String> properties() {
     return allProperties;
   }
+
+  public String metricsPublisherImpl() {
+    return metricsPublisherImpl;
+  }
+
+  /**
+   * Configure a custom {@link MetricPublisher} for an S3 client.
+   *
+   * <p>Sample usage:
+   *
+   * <pre>
+   *     
S3Client.builder().applyMutation(s3FileIOProperties::applyMetricsPublisherConfiguration)
+   * </pre>
+   */
+  public <T extends S3BaseClientBuilder<T, ?>> void 
applyMetricsPublisherConfiguration(T builder) {
+    if (metricsPublisherImpl != null) {
+      ClientOverrideConfiguration.Builder configBuilder =
+          null != builder.overrideConfiguration()
+              ? builder.overrideConfiguration().toBuilder()
+              : ClientOverrideConfiguration.builder();
+      builder.overrideConfiguration(
+          configBuilder.addMetricPublisher(loadMetricPublisher()).build());
+    }
+  }
+
+  private MetricPublisher loadMetricPublisher() {
+    // Phase 1: look up the factory. A NoSuchMethodException here means the 
class does not
+    // declare `create(Map)` — fall back to the no-arg constructor path. Any 
OTHER exception
+    // from a factory that DOES exist (e.g. the factory itself throws) should 
surface via the
+    // wrapping IllegalArgumentException in phase 2 so users can distinguish 
"wrong signature"
+    // from "misconfigured factory".
+    DynMethods.StaticMethod factory = null;
+    try {
+      factory =
+          DynMethods.builder("create")
+              .hiddenImpl(metricsPublisherImpl, Map.class)
+              .buildStaticChecked();
+    } catch (NoSuchMethodException e) {
+      // Expected when the implementation doesn't provide a create(Map) 
factory — fall through.
+    }
+
+    // Phase 2: invoke whichever path we found. Exceptions here are real 
failures and are
+    // surfaced with the precise path that failed so the user can diagnose.
+    try {
+      if (factory != null) {
+        return (MetricPublisher) factory.invoke(allProperties);
+      }
+      return Class.forName(metricsPublisherImpl)
+          .asSubclass(MetricPublisher.class)
+          .getDeclaredConstructor()
+          .newInstance();
+    } catch (Exception e) {

Review Comment:
   can we catch the expection we expect to be thrown here instead ? 



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to