[feat][monitor] Add custom topic metric labels to OpenTelemetry metrics attributes#25306
[feat][monitor] Add custom topic metric labels to OpenTelemetry metrics attributes#25306coderzc wants to merge 4 commits intoapache:masterfrom
Conversation
|
/pulsarbot rerun-failure-checks |
| var persistentTopicAttributes = persistentTopic.getTopicAttributes(); | ||
| // For persistent topics, get custom attributes once and reuse | ||
| var customLabels = persistentTopicAttributes.getCustomAttributes(); | ||
| var attributesWithCustomLabels = persistentTopicAttributes |
There was a problem hiding this comment.
Custom labels are only merged into the persistent-only metrics below. The AbstractTopic topic counters above (subscription, producer, consumer, message, bytes, publish rate limit) still record with attributes, so this change does not actually expose custom labels across all OpenTelemetry topic metrics.
There was a problem hiding this comment.
| @Test(groups = "broker") | ||
| public class OpenTelemetryCustomLabelsTest extends BrokerTestBase { | ||
|
|
||
| private static final Set<String> ALLOWED_CUSTOM_METRIC_LABEL_KEYS = Set.of("__SLA_TIER", "APP_OWNER"); |
There was a problem hiding this comment.
The allow-list key here is __SLA_TIER, but the test later writes SLA_TIER. getCustomMetricLabelsMap() filters by exact property key before lowercasing for OpenTelemetry output, so this label is not actually whitelisted.
| .build(); | ||
|
|
||
| // Verify metrics contain custom labels for topic1 | ||
| assertMetricLongSumValue(metrics, OpenTelemetryTopicStats.MESSAGE_IN_COUNTER, attributesTopic1, 5); |
There was a problem hiding this comment.
This test is asserting MESSAGE_IN_COUNTER / PRODUCER_COUNTER, but the patch only changes the persistent-topic metrics in the lower block (storage*, backlog, transaction, compaction, delayed subscription). Even if this passes, it does not verify the metrics that were actually modified here.
| storageOffloadedCounter.record(managedLedger.getOffloadedSize(), attributes); | ||
| storageInCounter.record(managedLedgerStats.getReadEntriesSucceededTotal(), attributes); | ||
| storageOutCounter.record(managedLedgerStats.getAddEntrySucceedTotal(), attributes); | ||
| storageCounter.record(managedLedgerStats.getStoredMessagesSize(), attributesWithCustomLabels); |
There was a problem hiding this comment.
Could we push this concern down into PersistentTopicAttributes instead of threading attributesWithCustomLabels through each recorder here? TopicAttributes already exposes getCommonAttributes(), so overriding that in PersistentTopicAttributes to merge in getCustomAttributes() would let both the shared topic metrics and the persistent-only metrics pick up the custom labels automatically. That would also reduce the amount of mechanical churn in OpenTelemetryTopicStats and make it harder to miss one metric when new recorders are added later.
There was a problem hiding this comment.
Thanks, I agree the merge logic should be centralized in PersistentTopicAttributes.
My main concern is overriding getCommonAttributes(): today it is a cheap accessor over pre-built static attributes, while custom labels are dynamic and require reading topic properties. Hiding that work in a common getter makes the behavior less obvious.
I kept getCommonAttributes() unchanged, but moved the custom-label merge closer to PersistentTopicAttributes by introducing an explicit per-scrape attribute snapshot there. This keeps the dynamic work out of the common getter while also removing most of the repetitive merging from OpenTelemetryTopicStats.
Main Issue: N/A
PIP: https://github.com/apache/pulsar/blob/master/pip/pip-447.md
Motivation
This PR is part 2 of PIP-447.
Part 1 for Prometheus labels has already been merged in #24991. This follow-up extends the same custom topic metric label support to OpenTelemetry topic metrics so both metrics pipelines expose the same topic property based labels.
Modifications
PersistentTopicAttributesto build OpenTelemetry attributes with custom labelsOpenTelemetryTopicStatsto attach custom labels across topic metricsmasterVerifying this change
This change added tests and can be verified as follows:
mvn clean install -DskipTestsOpenTelemetryCustomLabelsTestto verify custom labels are exposed in OpenTelemetry metrics and omitted when the feature is disabledDoes this pull request potentially affect one of the following parts:
Documentation
docdoc-requireddoc-not-neededdoc-completeMatching PR in forked repository
PR in forked repository: coderzc#51