[ISSUE #494] support prometheus - #495
Conversation
RockteMQ-AI
left a comment
There was a problem hiding this comment.
Summary
This PR modifies 9 file(s) with 607 lines of diff. No test changes detected — consider adding test coverage.
Automated review by github-manager-bot
Additional notes (not anchored to a changed line)
- [INFO]
metric-exporter/pom.xml:1— Large diff (607 lines). Consider breaking into smaller, focused PRs for easier review. (line outside diff)
| @@ -0,0 +1,234 @@ | |||
| /* | |||
There was a problem hiding this comment.
No test changes detected alongside source modifications. Consider adding tests to cover the changes.
RockteMQ-AI
left a comment
There was a problem hiding this comment.
Summary
PR received and logged for review. This PR requires detailed code review by a maintainer.
Diff size: 607 lines
Author: Slideee (CONTRIBUTOR)
Automated review by RockteMQ-AI
| List<String> additionalLabelNames, List<String> additionalLabelValues, double value) { | ||
| String suffix = nameSuffix == null ? "" : nameSuffix; | ||
| List<String> labelValues = sanitizeLabelValues(dropwizardName); | ||
| return new Collector.MetricFamilySamples.Sample(sanitizeMetricName(dropwizardName + suffix), SOURCE_TASK_LABEL_NAMES, labelValues, value); |
There was a problem hiding this comment.
The createSample method completely ignores the additionalLabelNames and additionalLabelValues parameters. These carry the 'quantile' label (e.g., name="quantile", value="0.75") that differentiates histogram percentiles in Prometheus SUMMARY metrics. By always using SOURCE_TASK_LABEL_NAMES and discarding the additional labels, all percentile samples for a histogram end up with an identical metric name and label set, making them indistinguishable and causing Prometheus to reject or arbitrarily deduplicate them. The method should merge additionalLabelNames/additionalLabelValues into the output sample's label names and values.
| List<String> additionalLabelNames, List<String> additionalLabelValues, double value) { | ||
| String suffix = nameSuffix == null ? "" : nameSuffix; | ||
| List<String> labelValues = sanitizeLabelValues(dropwizardName); | ||
| return new Collector.MetricFamilySamples.Sample(sanitizeMetricName(dropwizardName + suffix), SOURCE_TASK_LABEL_NAMES, labelValues, value); |
There was a problem hiding this comment.
sanitizeMetricName(dropwizardName + suffix) appends the suffix to the full dropwizardName string before parsing, but sanitizeMetricName extracts only the 2nd comma-separated field (split(":")[1].split(",")[1]). The suffix (e.g., "_count") lands on the last field and is silently lost. This means the histogram count sample (which uses nameSuffix="_count") gets the same metric name as the percentile samples, producing conflicting samples with the same name and label set. The suffix should be appended to the extracted metric name, not to the raw input string.
| if (!StringUtils.equals(var3, "")) { | ||
| connectorName = var3.substring(var3.indexOf("=") + 1); | ||
| } | ||
| String var4 = split[4]; |
There was a problem hiding this comment.
sanitizeLabelValues performs unchecked array indexing on split(":") and split(",") results, accessing indices 0 through 4 (split[4]) without any bounds validation. If any metric in the registry has a name that does not match the expected 'prefix:group,name,type,connector=X,task=Y' format (e.g., metrics registered by the framework itself or third-party libraries), this will throw ArrayIndexOutOfBoundsException, crashing the entire /metrics endpoint. sanitizeMetricName (line 37) has the same issue. Consider validating the split array length or wrapping in a try-catch that skips malformed metric names.
| this.executor = Executors.newCachedThreadPool(); | ||
| this.connectMetrics = new ConnectMetrics(workerConfig); | ||
| this.stateManagementService = stateManagementService; | ||
| CollectorRegistry.defaultRegistry.register(new DropwizardExports(connectMetrics.registry(), new PrometheusSampleBuilder())); |
There was a problem hiding this comment.
DropwizardExports is registered with the global CollectorRegistry.defaultRegistry but is never unregistered when the Worker is stopped. On Worker restart (e.g., connector reconfiguration), registering the same collector type again will throw IllegalArgumentException('Collector already registered'), breaking the connector lifecycle. The registration should either use a dedicated CollectorRegistry (not the global singleton), or unregister the collector in the Worker's stop/shutdown method.
| this.connectController = connectController; | ||
| pluginsResource = new ConnectorPluginsResource(connectController); | ||
|
|
||
| Javalin embeddedApp = Javalin.create(config -> { |
There was a problem hiding this comment.
The Javalin 'embeddedApp' instance for the metrics server is a local variable in the constructor and is never stored as a field. This means there is no way to stop or shut down the metrics Jetty server when the RestHandler or connect runtime is stopped, causing a port and thread resource leak. The embeddedApp reference should be stored as a field and stopped in the appropriate shutdown method.
| MetricFamilySamples fromSnapshotAndCount(String dropwizardName, Snapshot snapshot, long count, double factor, | ||
| String helpMessage) { | ||
| MetricName metricName = MetricUtils.stringToMetricName(dropwizardName); | ||
| Stat.HistogramType histogramType = Stat.HistogramType.valueOf(metricName.getType()); |
There was a problem hiding this comment.
Stat.HistogramType.valueOf(metricName.getType()) throws IllegalArgumentException if the metric name's type field does not match a known enum constant. Since this is called inside collect() which iterates over ALL metrics in the registry, a single metric with an unrecognized type will crash the entire metrics collection, making the /metrics endpoint return an error for all metrics. Consider wrapping this in a try-catch that skips the individual metric and logs a warning, similar to how fromGauge handles invalid types.
| samples = Arrays.asList(sampleBuilder.createSample(dropwizardName, "", Arrays.asList("quantile"), Arrays.asList("0.999"), snapshot.get999thPercentile() * factor)); | ||
| break; | ||
| default: | ||
| samples = Arrays.asList(sampleBuilder.createSample(dropwizardName, "", Arrays.asList("quantile"), Arrays.asList("0.5"), snapshot.getMedian() * factor), sampleBuilder.createSample(dropwizardName, "", Arrays.asList("quantile"), Arrays.asList("0.5"), snapshot.getMedian() * factor), sampleBuilder.createSample(dropwizardName, "", Arrays.asList("quantile"), Arrays.asList("0.75"), snapshot.get75thPercentile() * factor), sampleBuilder.createSample(dropwizardName, "", Arrays.asList("quantile"), Arrays.asList("0.95"), snapshot.get95thPercentile() * factor), sampleBuilder.createSample(dropwizardName, "", Arrays.asList("quantile"), Arrays.asList("0.98"), snapshot.get98thPercentile() * factor), sampleBuilder.createSample(dropwizardName, "", Arrays.asList("quantile"), Arrays.asList("0.99"), snapshot.get99thPercentile() * factor), sampleBuilder.createSample(dropwizardName, "", Arrays.asList("quantile"), Arrays.asList("0.999"), snapshot.get999thPercentile() * factor), sampleBuilder.createSample(dropwizardName, "_count", new ArrayList<String>(), new ArrayList<String>(), count)); |
There was a problem hiding this comment.
The default case of the switch in fromSnapshotAndCount creates a duplicate sample: two samples both with quantile="0.5" and snapshot.getMedian(). The original Prometheus DropwizardExports only includes the median sample once. This duplicate should be removed — the second createSample call with Arrays.asList("0.5") and snapshot.getMedian() is redundant.
|
|
||
| } | ||
|
|
||
| private Set<String> parse(HttpServletRequest req) { |
There was a problem hiding this comment.
The parse(HttpServletRequest) method is dead code — it is never called anywhere in the class. It appears to be copied from an upstream Prometheus servlet implementation but was not wired into doGet. Either remove it or use it to support the name[] query parameter for filtering which metrics are returned.
| import java.util.List; | ||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| public class PrometheusSampleBuilder implements SampleBuilder { |
There was a problem hiding this comment.
No test coverage is added for any of the new classes (DropwizardExports, PrometheusSampleBuilder, PrometheusMetricsServlet). Given the complex string-parsing logic in PrometheusSampleBuilder and the metric-type dispatch in DropwizardExports, unit tests are especially important to verify correct behavior with well-formed and malformed metric names, and to prevent regressions in the quantile label and suffix handling.
|
Issue Evaluation Category: This issue references #494 and proposes adding Prometheus monitoring support. Note: This appears to be a PR submission. If you have implementation code ready, please submit it as a pull request directly. Feasibility: Adding Prometheus metrics exposure is a valuable enhancement for observability. Age: This issue is from May 2023. If this feature is still desired, please confirm or submit a PR. Automated evaluation by RockteMQ-AI |
RockteMQ-AI
left a comment
There was a problem hiding this comment.
Summary
This PR adds Prometheus metrics export support to the Connect HTTP connector. The overall architecture is sound — using Dropwizard as the metrics registry and exporting via Prometheus text format is a standard approach.
However, there is a critical parsing bug in PrometheusSampleBuilder that will crash on metric names without the expected name:tag format.
Findings
- [Critical]
PrometheusSampleBuilder.java:60—split(":")[1]andsplit(",")[1]lack bounds checking →ArrayIndexOutOfBoundsException - [Warning]
WorkerConfig.java:120— No validation onexporterPortrange
Suggestions
- Add defensive parsing in
PrometheusSampleBuilder.buildMetricName()to handle metric names that don't follow thename:tagconvention - Add port range validation for
exporterPort - Consider adding a unit test for
PrometheusSampleBuilderwith edge cases (no colon, no comma, empty name)
Automated review by github-manager-bot
Additional notes (not anchored to a changed line)
- [CRITICAL]
connectors/rocketmq-connect-http/src/main/java/org/apache/rocketmq/connect/http/metrics/PrometheusSampleBuilder.java:60— [Critical]name.split(":")[1]will throwArrayIndexOutOfBoundsExceptionif the metric name does not contain a colon (e.g., a plain metric name likemy_metric). Add a bounds check:
String[] parts = name.split(":");
String metricName = parts.length > 1 ? parts[1] : parts[0];Similarly, parts[1].split(",")[1] on line 61 can throw if there is no comma. Consider defensive parsing. (line outside diff)
- [WARNING]
connect/connector-runtime/src/main/java/org/apache/rocketmq/connect/runtime/WorkerConfig.java:120— [Warning]exporterPortdefaults to5557with no validation. Consider adding a port range check (1-65535) in the setter or during config initialization to prevent silent failures when an invalid port is configured. (line outside diff)
RockteMQ-AI
left a comment
There was a problem hiding this comment.
Summary
The Prometheus integration has multiple unguarded string-parsing paths that will crash the /metrics endpoint on unexpected metric names, a duplicate-sample bug in the histogram default branch, a leaked Jetty server with no shutdown hook, and no test coverage.
Findings
- [CRITICAL]
metric-exporter/src/main/java/org/apache/rocketmq/connect/metrics/DropwizardExports.java:155— Thedefaultcase infromSnapshotAndCountadds the median (quantile "0.5") sample twice — the first two entries in theArrays.asList(...)are identical. This produces duplicate samples in every Prometheus scrape for any histogram type that falls through todefault, which will confuse or error in Prometheus ingestion. - [CRITICAL]
metric-exporter/src/main/java/org/apache/rocketmq/connect/metrics/DropwizardExports.java:127—Stat.HistogramType.valueOf(metricName.getType())will throwIllegalArgumentExceptionif the metric's type string doesn't exactly match an enum constant. There is no try/catch, so a single unrecognized histogram metric name will abort the entirecollect()call, breaking the whole/metricsendpoint. Wrap this in a try/catch and log+skip the offending metric. - [CRITICAL]
metric-exporter/src/main/java/org/apache/rocketmq/connect/metrics/PrometheusSampleBuilder.java:35—sanitizeMetricNamesplits on":"then","and accesses index[1]without bounds checking. If any Dropwizard metric name doesn't follow the exact expected format (e.g. internal JVM metrics, or third-party metrics), this throwsArrayIndexOutOfBoundsExceptionand crashes the scrape. Add defensive parsing with a fallback. - [CRITICAL]
metric-exporter/src/main/java/org/apache/rocketmq/connect/metrics/PrometheusSampleBuilder.java:39—sanitizeLabelValueshas the same unguarded split pattern and accessessplit[0]throughsplit[4]. Any metric name with fewer than 5 comma-separated segments will throwArrayIndexOutOfBoundsException. Additionally,split[3].substring(var3.indexOf("=") + 1)will misbehave ifvar3doesn't contain"="(indexOf returns -1, yieldingsubstring(0)— silently wrong rather than failing loudly). - [CRITICAL]
rocketmq-connect-runtime/src/main/java/org/apache/rocketmq/connect/runtime/rest/RestHandler.java:78— The Prometheus JettyServeris started in the constructor but theembeddedAppreference is local and never stored. This means: (1) there is no way to shut it down on application stop — the server and its thread pool will leak; (2) ifexporterPortis already bound, the exception propagates uncaught from the constructor, preventing the entire Worker from starting. Store the reference and wire it into the shutdown lifecycle. - [WARNING]
rocketmq-connect-runtime/src/main/java/org/apache/rocketmq/connect/runtime/connectorwrapper/Worker.java:154—CollectorRegistry.defaultRegistry.register(...)is called with no correspondingunregister(). If the Worker is re-created (e.g., in tests or on restart within the same JVM), the second registration will throwIllegalArgumentException: Collector already registered. Use an instance-scopedCollectorRegistryinstead of the global default, or add unregister logic on shutdown. - [WARNING]
rocketmq-connect-runtime/src/main/java/org/apache/rocketmq/connect/runtime/rest/PrometheusMetricsServlet.java:60— This class hand-rolls the Prometheus text exposition format instead of usingio.prometheus.client.exporter.common.TextFormat(which is part ofsimpleclient_common). The manual writer uses raw integer literals (123,125,10,32) instead of char literals ('{','}','\n',' '), hurting readability. More importantly, it is missing# HELPand# TYPElines that Prometheus expects, so the output is not fully spec-compliant. - [WARNING]
rocketmq-connect-runtime/src/main/java/org/apache/rocketmq/connect/runtime/rest/PrometheusMetricsServlet.java:120— Theparse()method readsname[]query parameters for metric filtering but is never called anywhere. Dead code — either wire it intodoGetto support selective metric export or remove it. - [INFO]
rocketmq-connect-runtime/src/main/java/org/apache/rocketmq/connect/runtime/rest/PrometheusMetricsServlet.java:124—doPostdelegates todoGet. Exposing metrics via POST is unconventional and unnecessary — Prometheus only scrapes via GET. RemovedoPostto reduce the attack surface. - [WARNING]
rocketmq-connect-runtime/src/main/java/org/apache/rocketmq/connect/runtime/config/WorkerConfig.java:61— TheexporterPortfield (default 5557) is not populated from the properties map like other config fields (e.g.,httpPort). There is nobuildWorkerConfig()/ init logic that reads it from a config key, so the setter is the only way to change it. Add it to the config loading path to be consistent with the rest of the class. - [INFO]
metric-exporter/src/main/java/org/apache/rocketmq/connect/metrics/DropwizardExports.java:234— Missing newline at end of file. Minor, but some tools and checkers flag this. - [INFO]
metric-exporter/src/main/java/org/apache/rocketmq/connect/metrics/DropwizardExports.java:107—new Long(counter.getCount()).doubleValue()uses the deprecatedLongconstructor. Replace with(double) counter.getCount()orLong.valueOf(...).doubleValue(). - [WARNING]
rocketmq-connect-runtime/src/main/java/org/apache/rocketmq/connect/runtime/rest/PrometheusMetricsServlet.java:42— No tests are included for any of the new classes (DropwizardExports,PrometheusSampleBuilder,PrometheusMetricsServlet). The PR description claims unit tests are written (>80% coverage), but no test files are present in the diff. This is a significant gap for a metrics-exporting feature where parsing bugs can silently produce wrong dashboards.
Automated review by github-manager-bot
| case Percentile_95th: | ||
| samples = Arrays.asList(sampleBuilder.createSample(dropwizardName, "", Arrays.asList("quantile"), Arrays.asList("0.95"), snapshot.get95thPercentile() * factor)); | ||
| break; | ||
| case Percentile_98th: |
There was a problem hiding this comment.
The default case in fromSnapshotAndCount adds the median (quantile "0.5") sample twice — the first two entries in the Arrays.asList(...) are identical. This produces duplicate samples in every Prometheus scrape for any histogram type that falls through to default, which will confuse or error in Prometheus ingestion.
| } | ||
|
|
||
| /** | ||
| * Export a histogram snapshot as a prometheus SUMMARY. |
There was a problem hiding this comment.
Stat.HistogramType.valueOf(metricName.getType()) will throw IllegalArgumentException if the metric's type string doesn't exactly match an enum constant. There is no try/catch, so a single unrecognized histogram metric name will abort the entire collect() call, breaking the whole /metrics endpoint. Wrap this in a try/catch and log+skip the offending metric.
| List<String> labelValues = sanitizeLabelValues(dropwizardName); | ||
| return new Collector.MetricFamilySamples.Sample(sanitizeMetricName(dropwizardName + suffix), SOURCE_TASK_LABEL_NAMES, labelValues, value); | ||
| } | ||
|
|
There was a problem hiding this comment.
sanitizeMetricName splits on ":" then "," and accesses index [1] without bounds checking. If any Dropwizard metric name doesn't follow the exact expected format (e.g. internal JVM metrics, or third-party metrics), this throws ArrayIndexOutOfBoundsException and crashes the scrape. Add defensive parsing with a fallback.
| public String sanitizeMetricName(String dropwizardName) { | ||
| return dropwizardName.split(":")[1].split(",")[1].replaceAll("-", "_"); | ||
| } | ||
|
|
There was a problem hiding this comment.
sanitizeLabelValues has the same unguarded split pattern and accesses split[0] through split[4]. Any metric name with fewer than 5 comma-separated segments will throw ArrayIndexOutOfBoundsException. Additionally, split[3].substring(var3.indexOf("=") + 1) will misbehave if var3 doesn't contain "=" (indexOf returns -1, yielding substring(0) — silently wrong rather than failing loudly).
| handlers.setHandlers(new Handler[]{context}); | ||
| server.setHandler(handlers); | ||
| return server; | ||
| }); |
There was a problem hiding this comment.
The Prometheus Jetty Server is started in the constructor but the embeddedApp reference is local and never stored. This means: (1) there is no way to shut it down on application stop — the server and its thread pool will leak; (2) if exporterPort is already bound, the exception propagates uncaught from the constructor, preventing the entire Worker from starting. Store the reference and wire it into the shutdown lifecycle.
| return (Set) (includedParam == null ? Collections.emptySet() : new HashSet(Arrays.asList(includedParam))); | ||
| } | ||
|
|
||
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
There was a problem hiding this comment.
doPost delegates to doGet. Exposing metrics via POST is unconventional and unnecessary — Prometheus only scrapes via GET. Remove doPost to reduce the attack surface.
| */ | ||
| private int httpPort = 8082; | ||
|
|
||
| private int exporterPort = 5557; |
There was a problem hiding this comment.
The exporterPort field (default 5557) is not populated from the properties map like other config fields (e.g., httpPort). There is no buildWorkerConfig() / init logic that reads it from a config key, so the setter is the only way to change it. Add it to the config loading path to be consistent with the rest of the class.
| public List<MetricFamilySamples> describe() { | ||
| return new ArrayList<MetricFamilySamples>(); | ||
| } | ||
| } No newline at end of file |
There was a problem hiding this comment.
Missing newline at end of file. Minor, but some tools and checkers flag this.
| MetricFamilySamples.Sample sample = sampleBuilder.createSample(dropwizardName, "", new ArrayList<String>(), new ArrayList<String>(), new Long(counter.getCount()).doubleValue()); | ||
| return new MetricFamilySamples(sample.name, Type.GAUGE, getHelpMessage(dropwizardName, counter), Arrays.asList(sample)); | ||
| } | ||
|
|
There was a problem hiding this comment.
new Long(counter.getCount()).doubleValue() uses the deprecated Long constructor. Replace with (double) counter.getCount() or Long.valueOf(...).doubleValue().
| private CollectorRegistry registry; | ||
|
|
||
| public PrometheusMetricsServlet() { | ||
| this(CollectorRegistry.defaultRegistry); |
There was a problem hiding this comment.
No tests are included for any of the new classes (DropwizardExports, PrometheusSampleBuilder, PrometheusMetricsServlet). The PR description claims unit tests are written (>80% coverage), but no test files are present in the diff. This is a significant gap for a metrics-exporting feature where parsing bugs can silently produce wrong dashboards.
RockteMQ-AI
left a comment
There was a problem hiding this comment.
LGTM. Trivial change, looks good.
Automated review by github-manager-bot
What is the purpose of the change
#494
Brief changelog
XX
Verifying this change
XXXX
Follow this checklist to help us incorporate your contribution quickly and easily. Notice,
it would be helpful if you could finish the following 5 checklist(the last one is not necessary)before request the community to review your PR.[ISSUE #123] Fix UnknownException when host config not exist. Each commit in the pull request should have a meaningful subject line and body.mvn -B clean apache-rat:check findbugs:findbugs checkstyle:checkstyleto make sure basic checks pass. Runmvn clean install -DskipITsto make sure unit-test pass. Runmvn clean test-compile failsafe:integration-testto make sure integration-test pass.