-
Notifications
You must be signed in to change notification settings - Fork 829
Added metrics framework and implementation #910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
For monitoring a system it is not really important how many items are in a queue. The important metric is how full the queue is. That is, how many items the queue has, relative to the maximum size of the queue.
d34a203
to
41e47e5
Compare
Hi @hylkevds thanks for this. It will take some time to me for review, but I'll do, just little bit slow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank's @hylkevds for thus PR.
I've given a first review step, in the client part of the code, where Moquette uses this.
It seems fair, but my big concerns is why using a custom API when we could leverage wide spread used metric APIs and implementations, like for example Dropwizard Metrics which defines client APIs and provide various implementations, like the one for Prometheus?
My main concern here is to not add any dependencies on any metrics libraries. So instead of choosing any, I made a minimal shim, with the actual metrics collection being done by a separate plugin that could trivially be written for any metrics system that matches the system already used by whatever Moquette is embedded into. |
Elaborating a bit more about the "custom API" thing: Metrics gathering has to be wired directly into the code. Regardless of which metrics library we choose, we, as authors of moquette, are the ones that define which metrics are gathered as Count, which as Histogram, etc. As far as I can tell, there is not yet a nice "SLF4J-API" equivalent for metrics. Dropwizard Metrics, OpenTelemetry and Proteus are all the equivalent of JUL, Log4J or Logback. They can expose metrics in each others formats, like the logging frameworks can write log files in different formats, but if we choose one, but the embedding application uses another, then there are now two frameworks active, that both expose their own metrics endpoint. Even if there were a Metrics-API library, it might still be a good idea to make a central metrics-gathering class for moquette, to avoid spreading |
~~I generally agree, but some dependencies are needed. Dropwizard Metrics (or Micrometer) are an API that has various implementations, it's more like a Logging API provided by Log4J or SLF4J. I understand your concern, but Moquette would just depend on the API not the implementation.~~ After reading you last comment, could agree with you. I'll move forward in reviewing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @hylkevds for all your hard work on this!
I've left some comments and a suggestion to improve readability of the MetricsManager init method
import io.moquette.broker.config.IConfig; | ||
|
||
/** | ||
* Interface that a metrics implementation must implement. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Interface that a metrics implementation must implement. | |
* Interface that a metrics implementation must implement. | |
* It mainly defines methods that are used to track Moquette metrics. |
|
||
/** | ||
* Notify the metrics provider about the number and size of session queues. This will | ||
* be called once. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this has to be called one, could we provide an abstract class for implementors that grant the single executions. Something like:
private boolean executed = false;
public void initSessionQueues(int queueCount, int queueSize) {
if (executed) {
throw new IllegalStateException("initSessioQueues must be called only once and was already executed");
}
...
}
* | ||
* You may elect to redistribute this code under either of these licenses. | ||
*/ | ||
package io.moquette.logging; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not logging but metering, so I would move into package io.moquette.metrics
@@ -0,0 +1,76 @@ | |||
/* | |||
* Copyright (c) 2012-2018 The original author or authors |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Copyright (c) 2012-2018 The original author or authors | |
* Copyright (c) 2012-2025 The original author or authors |
@@ -0,0 +1,69 @@ | |||
/* | |||
* Copyright (c) 2012-2018 The original author or authors |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Copyright (c) 2012-2018 The original author or authors | |
* Copyright (c) 2012-2025 The original author or authors |
...s_prometheus/src/test/java/io/moquette/metrics/prometheus/MetricsProviderPrometheusTest.java
Outdated
Show resolved
Hide resolved
public void deliveryComplete(IMqttDeliveryToken token) { | ||
// try { | ||
// token.waitForCompletion(1_000); | ||
// m_messages.offer(new ReceivedMessage(token.getMessage(), token.getTopics()[0])); | ||
// } catch (MqttException e) { | ||
// e.printStackTrace(); | ||
// } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public void deliveryComplete(IMqttDeliveryToken token) { | |
// try { | |
// token.waitForCompletion(1_000); | |
// m_messages.offer(new ReceivedMessage(token.getMessage(), token.getTopics()[0])); | |
// } catch (MqttException e) { | |
// e.printStackTrace(); | |
// } | |
} | |
public void deliveryComplete(IMqttDeliveryToken token) { | |
} |
I would avoid to copy commented code.
@@ -0,0 +1,90 @@ | |||
/* | |||
* Copyright (c) 2012-2018 The original author or authors |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Copyright (c) 2012-2018 The original author or authors | |
* Copyright (c) 2012-2025 The original author or authors |
*/ | ||
private static MetricsProvider metricsProvider = new MetricsProviderNull(); | ||
|
||
public static void init(IConfig config) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to cover with test this with a test resource file in test/resources/META-INF/services
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, I'll try.
Though it will be tested when the Prometheus implementation is built and tested.
public static void init(IConfig config) { | ||
ServiceLoader<MetricsProvider> loader = ServiceLoader.load(MetricsProvider.class); | ||
String classname = config.getProperty(METRICS_PROVIDER_CLASS, ""); | ||
|
||
MetricsProvider usedProvider = null; | ||
List<String> foundProviders = new ArrayList<>(); | ||
for (MetricsProvider provider : loader) { | ||
foundProviders.add(provider.getClass().getName()); | ||
if (!StringUtil.isNullOrEmpty(classname) && provider.getClass().getName().endsWith(classname)) { | ||
LOG.info("Using configured MetricsProvider: {}", provider.getClass().getName()); | ||
usedProvider = provider; | ||
break; | ||
} | ||
} | ||
if (usedProvider == null) { | ||
LOG.info("No MetricsProvider configured, or no matching found, using NULL provider. Available providers: {}", foundProviders); | ||
} else { | ||
metricsProvider = usedProvider; | ||
} | ||
metricsProvider.init(config); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public static void init(IConfig config) { | |
ServiceLoader<MetricsProvider> loader = ServiceLoader.load(MetricsProvider.class); | |
String classname = config.getProperty(METRICS_PROVIDER_CLASS, ""); | |
MetricsProvider usedProvider = null; | |
List<String> foundProviders = new ArrayList<>(); | |
for (MetricsProvider provider : loader) { | |
foundProviders.add(provider.getClass().getName()); | |
if (!StringUtil.isNullOrEmpty(classname) && provider.getClass().getName().endsWith(classname)) { | |
LOG.info("Using configured MetricsProvider: {}", provider.getClass().getName()); | |
usedProvider = provider; | |
break; | |
} | |
} | |
if (usedProvider == null) { | |
LOG.info("No MetricsProvider configured, or no matching found, using NULL provider. Available providers: {}", foundProviders); | |
} else { | |
metricsProvider = usedProvider; | |
} | |
metricsProvider.init(config); | |
} | |
public static void init(IConfig config) { | |
ServiceLoader<MetricsProvider> loader = ServiceLoader.load(MetricsProvider.class); | |
String classname = config.getProperty(METRICS_PROVIDER_CLASS, ""); | |
List<MetricsProvider> foundProviders = new ArrayList<>(); | |
loader.forEach(foundProviders::add); | |
Optional<MetricsProvider> usedProviderOpt = foundProviders.stream() | |
.filter(provider -> providerMatchClassname(provider, classname)) | |
.findFirst(); | |
if (usedProviderOpt.isPresent()) { | |
MetricsProvider usedProvider = usedProviderOpt.get(); | |
LOG.info("Using configured MetricsProvider: {}", usedProvider.getClass().getName()); | |
metricsProvider = usedProvider; | |
} else { | |
LOG.info("No MetricsProvider configured, or no matching found, using NULL provider. Available providers: {}", | |
foundProviders.stream().map(p -> p.getClass().getName()).collect(Collectors.toList())); | |
} | |
metricsProvider.init(config); | |
} | |
private static boolean providerMatchClassname(MetricsProvider provider, String classname) { | |
return !StringUtil.isNullOrEmpty(classname) && provider.getClass().getName().endsWith(classname); | |
} |
Try to make more explicit the intention of search for a provider with same class name defined in classname
Your "init is only called once" comment actually touches on an important detail I wanted your opinion on: Do you think this is an issue, and if it is, what is your preferred solution? |
I rewrote the initialisation, now each instance of Server gets its own MetricsProvider. |
Good! @hylkevds if you could resolve the conflicts then we are good to merge :-) |
I've also managed to add tests using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left just a note on the naming, then I think we are done with this PR :-D
/** | ||
* A metrics provider used for testing. | ||
*/ | ||
public class MetricsProviderTest extends AbstractMetricsProvider { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that <Class>Test
is a pattern used to name the test suites, to avoid confusion with this, that's a test fixture class, I would rename it MetricsProviderMock
or MetricsProviderDouble
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, while naming it my inspiration drew blank. Fixed now!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Thanks a lot @hylkevds for all this hard work! :-D |
Release notes
[feature] Added metrics framework and Prometheus implementation.
- Enable by setting
metrics_provider_class
toMetricsProviderPrometheus
.- Metrics endpoint can be found on http://localhost:9400/metrics by default.
- port can be changed with
metrics_endpoint_port
. Set to 0 to disable http endpoint.What does this PR do?
To gain insights into the state of running software, the software can expose metrics, which are then gathered by a metrics framework.
A popular framework for this is Prometheus, but there are others.
This PR adds the ability for Moquette to expose metrics for such system to gather, and provides an implementation for Prometheus specifically.
By default, metrics are not gathered, and the system is only a shim that has minimal impact on performance. The Proteus implementation is a separate project that can be added to the classpath if desired. This minimises the impact for users that do not require metrics and does not change any dependencies for those use-cases.
I have made corresponding change to the default configuration files (and/or docker env variables)How to test this PR locally
metrics_provider_class=MetricsProviderPrometheus