rocketmq icon indicating copy to clipboard operation
rocketmq copied to clipboard

[Bug] Prometheus exporter throws DuplicateLabelsException causing all Master broker metrics to be lost

Open shendongsd opened this issue 2 months ago • 2 comments

Before Creating the Bug Report

  • [x] I found a bug, not just asking a question, which should be created in GitHub Discussions.

  • [x] I have searched the GitHub Issues and GitHub Discussions of this repository and believe that this is not a duplicate.

  • [x] I have confirmed that this bug belongs to the current repository, not other repositories of RocketMQ.

Runtime platform environment

When using metricsExporterType=PROM (PrometheusHttpServer), if there are Pop consumers connected to the broker, the /metrics endpoint crashes with DuplicateLabelsException, causing all Master broker metrics to be lost. Slave brokers are not affected because Pop consumers only connect to Master.

RocketMQ version

Environment RocketMQ Version: V5_3_3 (issue also present in develop branch as of 2026-10-01) metricsExporterType: PROM Consume mode: Pop consumption

JDK Version

No response

Describe the Bug

Root Cause In BrokerMetricsManager.initLagAndDlqMetrics(), three gauges (rocketmq_consumer_lag_messages, rocketmq_consumer_lag_latency, rocketmq_consumer_ready_messages) use a single measurement object shared between two calculators: `consumerLagLatency = brokerMeter.gaugeBuilder(GAUGE_CONSUMER_LAG_LATENCY) .buildWithCallback(measurement -> { consumerLagCalculator.calculateLag(lagResult -> measurement.record(lagResult.getLagLatency(), buildLagAttributes(lagResult))); // ①

    liteConsumerLagCalculator.calculateLiteLagLatency(lagResult ->
        measurement.record(lagResult.getLagLatency(), buildLagAttributes(lagResult)));  // ②
});`

For Pop consumers, ConsumerLagCalculator.calculate() records two entries per ProcessGroupInfo — one with isRetry=false and one with isRetry=true. When the same (consumer_group, topic, is_retry, is_system) label combination is produced across ① and ②, MetricSnapshot.validateLabels() throws DuplicateLabelsException. Once triggered, the entire /metrics scrape fails — all metrics from the Master broker disappear.

Steps to Reproduce

Environment RocketMQ Version: V5_3_3 (issue also present in develop branch as of 2026-10-01) metricsExporterType: PROM Consume mode: Pop consumption

What Did You Expect to See?

rocketmq_broker_permission{cluster="...",node_id="...",node_type="broker",otel_scope_name="broker-meter"} 6.0 rocketmq_consumer_connections{cluster="...",consume_mode="push",consumer_group="...",is_system="false",language="java",node_id="...",node_type="broker",otel_scope_name="broker-meter",protocol_type="remoting",version="v5_3_3"} 2.0 rocketmq_consumer_group_number{cluster="...",node_id="...",node_type="broker",otel_scope_name="broker-meter"} 113.0 rocketmq_consumer_inflight_messages{cluster="...",consumer_group="...",is_retry="false",is_system="true",node_id="...",node_type="broker",otel_scope_name="broker-meter",topic="..."} 0.0 rocketmq_consumer_lag_latency_milliseconds{cluster="...",consumer_group="...",is_retry="false",is_system="true",node_id="...",node_type="broker",otel_scope_name="broker-meter",topic="..."} 0.0 rocketmq_consumer_lag_messages{cluster="...",consumer_group="...",is_retry="false",is_system="true",node_id="...",node_type="broker",otel_scope_name="broker-meter",topic="..."} 0.0 rocketmq_consumer_queueing_latency_milliseconds{cluster="...",consumer_group="...",is_retry="false",is_system="true",node_id="...",node_type="broker",otel_scope_name="broker-meter",topic="..."} 9.7563873E7 rocketmq_consumer_ready_messages{cluster="...",consumer_group="...",is_retry="false",is_system="true",node_id="...",node_type="broker",otel_scope_name="broker-meter",topic="..."} 7741731.0 rocketmq_delay_message_latency_seconds_bucket{cluster="...",node_id="...",node_type="broker",otel_scope_name="broker-meter",storage_medium="disk

What Did You See Instead?

An Exception occurred while scraping metrics: io.prometheus.metrics.model.snapshots.DuplicateLabelsException: Duplicate labels for metric "rocketmq_consumer_lag_latency_milliseconds": {cluster="...", consumer_group="...", is_retry="true", is_system="false", node_id="...", node_type="broker", otel_scope_name="broker-meter", topic="..."} at io.prometheus.metrics.model.snapshots.MetricSnapshot.validateLabels(MetricSnapshot.java:42) at io.prometheus.metrics.model.snapshots.MetricSnapshot.(MetricSnapshot.java:27) at io.prometheus.metrics.model.snapshots.GaugeSnapshot.(GaugeSnapshot.java:18) at io.opentelemetry.exporter.prometheus.Otel2PrometheusConverter.convertLongGauge(...) at io.opentelemetry.exporter.prometheus.Otel2PrometheusConverter.convert(...) at io.opentelemetry.exporter.prometheus.PrometheusMetricReader.collect(...) at io.prometheus.metrics.model.registry.PrometheusRegistry.scrape(...)

Additional Context

No response

shendongsd avatar Oct 01 '26 03:10 shendongsd

Hi, I looked into this and I think the root cause may differ from the one described.

LiteConsumerLagCalculator always emits isRetry=false, so it can't produce a duplicate is_retry="true" series. Also, 5.3.3 doesn't have the Lite calculator at all.

In 5.3.3, when enableNotifyBeforePopCalculateLag is on and a pop request is pending, calculateLag passes lagRecorder into an async PopCommandCallback. That callback records into the OTel measurement later, from another thread, outside the collection callback. Those late writes can collide with the synchronous ones and produce duplicate label sets. apache/rocketmq#9873 (released in 5.4.0) changed this to a CompletableFuture-based approach, so could you check whether the exception still reproduces on 5.4.0+?

While reading the current develop code, I also noticed a related issue. The async path calls calculate(info, future::complete), but for Pop groups calculate() invokes the recorder twice (normal lag, then retry lag). CompletableFuture.complete only keeps the first value, so the is_retry=true lag result is silently dropped whenever a pop request is pending.

I'd like to work on a fix: collect all results from calculate() in the async path, not just the first, plus a unit test covering Pop groups. Could this be assigned to me?

Ayush0612005 avatar Oct 02 '26 15:10 Ayush0612005

Hi maintainers,

I investigated issue #11304 on the current develop branch and isolated a related correctness problem in the asynchronous POP lag calculation path.

The reproduction path is:

  1. enableNotifyBeforePopCalculateLag is enabled.
  2. A POP consumer group has a pending long-polling request.
  3. PopLongPollingService invokes PopCommandCallback.
  4. ConsumerLagCalculator.calculate() emits two results for the same POP group: one for the normal topic and one for the POP retry topic.
  5. The asynchronous adapter currently uses future::complete, while a CompletableFuture can retain only one value. The retry lag result is therefore silently discarded.

I prepared a focused fix that:

  • changes the asynchronous result container to collect all CalculateLagResult values;
  • preserves both normal and retry lag results;
  • records the results only after the future completes, avoiding metric recording from the asynchronous callback thread;
  • leaves message semantics, metric labels, and the synchronous calculation path unchanged;
  • adds a deterministic regression test covering both result types and lag values.

The focused broker tests pass with Checkstyle and SpotBugs. The change does not claim that the original 5.3.3 Prometheus DuplicateLabelsException still reproduces on current 5.4+ code; it addresses the independently reproducible POP retry-lag result loss in the current asynchronous path.

Would this scope be acceptable for issue #11304? If so, may I open a pull request for this fix? I can rebase the change onto the latest develop, squash it into one commit, and update the tests according to review feedback.

Yeti11H avatar Oct 16 '26 06:10 Yeti11H