tillrohrmann commented on a change in pull request #8373: 
[FLINK-11922][metrics] Support MetricReporter factories 
URL: https://github.com/apache/flink/pull/8373#discussion_r282400366
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/metrics/ReporterSetup.java
 ##########
 @@ -131,46 +137,119 @@ private static ReporterSetup createReporterSetup(String 
reporterName, MetricConf
                        }
                }
 
-               List<Tuple2<String, Configuration>> reporterConfigurations;
-
                if (namedReporters.isEmpty()) {
-                       reporterConfigurations = Collections.emptyList();
-               } else {
-                       reporterConfigurations = new 
ArrayList<>(namedReporters.size());
+                       return Collections.emptyList();
+               }
 
-                       for (String namedReporter: namedReporters) {
-                               DelegatingConfiguration delegatingConfiguration 
= new DelegatingConfiguration(
-                                       configuration,
-                                       ConfigConstants.METRICS_REPORTER_PREFIX 
+ namedReporter + '.');
+               List<Tuple2<String, Configuration>> reporterConfigurations = 
new ArrayList<>(namedReporters.size());
 
-                               
reporterConfigurations.add(Tuple2.of(namedReporter, (Configuration) 
delegatingConfiguration));
-                       }
+               for (String namedReporter: namedReporters) {
+                       DelegatingConfiguration delegatingConfiguration = new 
DelegatingConfiguration(
+                               configuration,
+                               ConfigConstants.METRICS_REPORTER_PREFIX + 
namedReporter + '.');
+
+                       reporterConfigurations.add(Tuple2.of(namedReporter, 
(Configuration) delegatingConfiguration));
                }
 
+               final Collection<MetricReporterFactory> reporterFactories = 
loadReporterFactories();
                List<ReporterSetup> reporterArguments = new 
ArrayList<>(reporterConfigurations.size());
                for (Tuple2<String, Configuration> reporterConfiguration: 
reporterConfigurations) {
                        String reporterName = reporterConfiguration.f0;
                        Configuration reporterConfig = reporterConfiguration.f1;
 
                        try {
-                               final String reporterClassName = 
reporterConfig.getString(ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, null);
-                               if (reporterClassName == null) {
-                                       LOG.error("No reporter class set for 
reporter " + reporterName + ". Metrics might not be exposed/reported.");
-                                       continue;
-                               }
-
-                               Class<?> reporterClass = 
Class.forName(reporterClassName);
-                               MetricReporter reporter = (MetricReporter) 
reporterClass.newInstance();
+                               Optional<MetricReporter> metricReporterOptional 
= loadReporter(reporterName, reporterConfig, reporterFactories);
+                               if (metricReporterOptional.isPresent()) {
+                                       MetricReporter reporter = 
metricReporterOptional.get();
 
-                               MetricConfig metricConfig = new MetricConfig();
-                               reporterConfig.addAllToProperties(metricConfig);
+                                       MetricConfig metricConfig = new 
MetricConfig();
+                                       
reporterConfig.addAllToProperties(metricConfig);
 
-                               
reporterArguments.add(createReporterSetup(reporterName, metricConfig, 
reporter));
+                                       
reporterArguments.add(createReporterSetup(reporterName, metricConfig, 
reporter));
+                               }
                        }
                        catch (Throwable t) {
                                LOG.error("Could not instantiate metrics 
reporter {}. Metrics might not be exposed/reported.", reporterName, t);
                        }
                }
                return reporterArguments;
        }
+
+       private static Collection<MetricReporterFactory> 
loadReporterFactories() {
+               final ServiceLoader<MetricReporterFactory> serviceLoader = 
ServiceLoader.load(MetricReporterFactory.class);
+
+               final Collection<MetricReporterFactory> reporterFactories = new 
ArrayList<>();
+               final Iterator<MetricReporterFactory> factoryIterator = 
serviceLoader.iterator();
+               // do not use streams or for-each loops here because they do 
not allow catching individual ServiceConfigurationErrors
+               // such an error might be caused if the META-INF/services 
contains an entry to a non-existing factory class
+               while (factoryIterator.hasNext()) {
+                       try {
+                               MetricReporterFactory factory = 
factoryIterator.next();
+                               reporterFactories.add(factory);
+                       } catch (Exception | ServiceConfigurationError e) {
+                               LOG.warn("Error while loading reporter 
factory.", e);
+                       }
+               }
+
+               return Collections.unmodifiableCollection(reporterFactories);
+       }
+
+       private static Optional<MetricReporter> loadReporter(
+                       final String reporterName,
+                       final Configuration reporterConfig,
+                       final Collection<MetricReporterFactory> 
reporterFactories)
+                       throws ClassNotFoundException, IllegalAccessException, 
InstantiationException {
+
+               final String reporterClassName = 
reporterConfig.getString(ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, null);
+               final String factoryClassName = 
reporterConfig.getString(ConfigConstants.METRICS_REPORTER_FACTORY_CLASS_SUFFIX, 
null);
+
+               if (factoryClassName != null) {
+                       return loadViaFactory(factoryClassName, reporterName, 
reporterConfig, reporterFactories);
+               }
+
+               if (reporterClassName != null) {
+                       final Class<?> reporterClass = 
Class.forName(reporterClassName);
+
+                       final InstantiateViaFactory 
alternativeFactoryAnnotation = 
reporterClass.getAnnotation(InstantiateViaFactory.class);
+                       if (alternativeFactoryAnnotation != null) {
+                               final String alternativeFactoryClassName = 
alternativeFactoryAnnotation.factoryClassName();
+                               LOG.info("The reporter configuration of {} is 
out-dated (but still supported)." +
+                                               " Please configure a factory 
class instead: '{}{}.{}: {}' to ensure that the configuration" +
+                                               " continues to work with future 
versions.",
+                                       reporterName,
+                                       ConfigConstants.METRICS_REPORTER_PREFIX,
+                                       reporterName,
+                                       
ConfigConstants.METRICS_REPORTER_FACTORY_CLASS_SUFFIX,
+                                       alternativeFactoryClassName);
+                               return 
loadViaFactory(alternativeFactoryClassName, reporterName, reporterConfig, 
reporterFactories);
+                       }
+
+                       return Optional.of((MetricReporter) 
reporterClass.newInstance());
+               }
+
+               LOG.warn("No reporter class nor factory set for reporter {}. 
Metrics might not be exposed/reported.", reporterName);
+               return Optional.empty();
+       }
+
+       private static Optional<MetricReporter> loadViaFactory(
+                       final String factoryClassName,
+                       final String reporterName,
+                       final Configuration reporterConfig,
+                       final Collection<MetricReporterFactory> 
reporterFactories) {
+
+               final Optional<MetricReporterFactory> factory = 
reporterFactories.stream()
+                       .filter(factoryCandidate -> 
factoryClassName.equals(factoryCandidate.getClass().getName()))
+                       .findAny();
+
+               if (!factory.isPresent()) {
+                       LOG.warn("The reporter factory ({}) could not be found 
for reporter {}.", factoryClassName, reporterName);
+                       return Optional.empty();
+               } else {
 
 Review comment:
   Here we could use `factory.map(f -> f.createMetricReporter())`

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to