From 74290ea500827ddbc590cc06facbb2d969f62ff4 Mon Sep 17 00:00:00 2001 From: Francisco Bento da Silva Neto Date: Mon, 12 Apr 2021 11:45:56 -0300 Subject: [PATCH] New FactoriesProvider interface New FactoriesProvider interface with ServiceLoader logic for dynamically loading Factories. --- .../cassandra/exporter/FactoriesProvider.java | 25 +++++++++++++++++++ .../zegelin/cassandra/exporter/Harvester.java | 4 +++ .../MBeanGroupMetricFamilyCollector.java | 3 ++- 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 common/src/main/java/com/zegelin/cassandra/exporter/FactoriesProvider.java diff --git a/common/src/main/java/com/zegelin/cassandra/exporter/FactoriesProvider.java b/common/src/main/java/com/zegelin/cassandra/exporter/FactoriesProvider.java new file mode 100644 index 0000000..0bea14d --- /dev/null +++ b/common/src/main/java/com/zegelin/cassandra/exporter/FactoriesProvider.java @@ -0,0 +1,25 @@ +package com.zegelin.cassandra.exporter; + +import com.zegelin.cassandra.exporter.MBeanGroupMetricFamilyCollector.Factory; +import com.zegelin.cassandra.exporter.cli.HarvesterOptions; + +import java.util.List; +import java.util.ServiceLoader; + +/** + * Service Provider interface whose implementations will be loaded via {@link ServiceLoader}. + * + *

Allows runtime modules to provide pluggable metrics via classpath registration. + * + */ +public interface FactoriesProvider +{ + /** + * Provides a list of {@link Factory} to be registered on the exporter. + * + * @param metadataFactory + * @param options + * @return factories + */ + List getFactories(final MetadataFactory metadataFactory, final HarvesterOptions options); +} diff --git a/common/src/main/java/com/zegelin/cassandra/exporter/Harvester.java b/common/src/main/java/com/zegelin/cassandra/exporter/Harvester.java index ae433c9..2765157 100644 --- a/common/src/main/java/com/zegelin/cassandra/exporter/Harvester.java +++ b/common/src/main/java/com/zegelin/cassandra/exporter/Harvester.java @@ -141,6 +141,10 @@ public boolean equals(final Object o) { protected Harvester(final MetadataFactory metadataFactory, final HarvesterOptions options) { this.collectorFactories = new ArrayList<>(new FactoriesSupplier(metadataFactory, options).get()); + + ServiceLoader providers = ServiceLoader.load(FactoriesProvider.class); + providers.forEach(p -> collectorFactories.addAll(p.getFactories(metadataFactory, options))); + this.metadataFactory = metadataFactory; this.exclusions = options.exclusions; this.enabledGlobalLabels = options.globalLabels; diff --git a/common/src/main/java/com/zegelin/cassandra/exporter/MBeanGroupMetricFamilyCollector.java b/common/src/main/java/com/zegelin/cassandra/exporter/MBeanGroupMetricFamilyCollector.java index 2d2f803..70e1533 100644 --- a/common/src/main/java/com/zegelin/cassandra/exporter/MBeanGroupMetricFamilyCollector.java +++ b/common/src/main/java/com/zegelin/cassandra/exporter/MBeanGroupMetricFamilyCollector.java @@ -37,7 +37,8 @@ public MBeanGroupMetricFamilyCollector removeMBean(final ObjectName mBeanName) { public abstract Stream collect(); - protected interface Factory { + public interface Factory + { /** * Create a {@link MBeanGroupMetricFamilyCollector} for the given MBean, or null if this collector * doesn't support the given MBean.