-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[Testing] Add unit tests to verify Prometheus JMX template regexp patterns #18414
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
Merged
noob-se7en
merged 3 commits into
apache:master
from
Akanksha-kedia:test/prometheus-template-regexp-13588
May 8, 2026
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
d421779
[Testing] Add unit tests to verify Prometheus JMX template regexp pat…
Akanksha-kedia 7e75603
test: load YAML patterns in targeted regexp tests instead of inlining
Akanksha-kedia 33f3ec3
test: key YAML pattern lookup off rule name instead of numeric index
Akanksha-kedia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
293 changes: 293 additions & 0 deletions
293
...rc/test/java/org/apache/pinot/common/metrics/prometheus/PrometheusTemplateRegexpTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,293 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.pinot.common.metrics.prometheus; | ||
|
|
||
| import java.io.FileReader; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import java.util.regex.PatternSyntaxException; | ||
| import java.util.stream.Collectors; | ||
| import org.testng.Assert; | ||
| import org.testng.annotations.DataProvider; | ||
| import org.testng.annotations.Test; | ||
| import org.yaml.snakeyaml.Yaml; | ||
|
|
||
|
|
||
| /** | ||
| * Verifies that the Prometheus JMX template regexp patterns defined in the docker config YAML files | ||
| * are valid Java regexps and match expected JMX metric name strings with correct capture groups. | ||
| * | ||
| * Config files under test: docker/images/pinot/etc/jmx_prometheus_javaagent/configs/ | ||
| * | ||
| * @see <a href="https://github.com/apache/pinot/issues/13588">Issue #13588</a> | ||
| */ | ||
| public class PrometheusTemplateRegexpTest { | ||
|
|
||
| private static final String CONFIG_BASE_PATH = | ||
| "../docker/images/pinot/etc/jmx_prometheus_javaagent/configs"; | ||
|
|
||
| @DataProvider(name = "configFiles") | ||
| public Object[][] configFiles() { | ||
| return new Object[][]{ | ||
| {"broker.yml"}, | ||
| {"server.yml"}, | ||
| {"controller.yml"}, | ||
| {"minion.yml"}, | ||
| {"pinot.yml"} | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Verifies every pattern in each YAML config file compiles as a valid Java regexp. | ||
| */ | ||
| @Test(dataProvider = "configFiles") | ||
| public void testAllPatternsAreValidRegexp(String configFile) | ||
| throws Exception { | ||
| List<String> patterns = extractPatterns(CONFIG_BASE_PATH + "/" + configFile); | ||
| Assert.assertFalse(patterns.isEmpty(), | ||
| "Expected at least one rule pattern in " + configFile); | ||
| for (String patternStr : patterns) { | ||
| try { | ||
| Pattern.compile(patternStr); | ||
| } catch (PatternSyntaxException e) { | ||
| Assert.fail( | ||
| "Invalid regexp in " + configFile + ": [" + patternStr + "] - " + e.getDescription()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // ---- Broker patterns ---- | ||
|
|
||
| /** | ||
| * broker.yml rule 0: meters/timers scoped to tableNameWithType. | ||
| * e.g. pinot.broker.myTable_REALTIME.queries | ||
| */ | ||
| @Test | ||
| public void testBrokerTableWithTypeMeterPattern() | ||
| throws Exception { | ||
| String pattern = loadPattern("broker.yml", 0); | ||
| Matcher m = Pattern.compile(pattern).matcher( | ||
| "\"org.apache.pinot.common.metrics\"<type=\"BrokerMetrics\", " | ||
| + "name=\"pinot.broker.myTable_REALTIME.queries\"><>Count"); | ||
| Assert.assertTrue(m.matches(), "Pattern should match broker table-scoped meter"); | ||
| Assert.assertEquals(m.group(1), "broker"); | ||
| Assert.assertEquals(m.group(4), "myTable"); | ||
| Assert.assertEquals(m.group(5), "REALTIME"); | ||
| Assert.assertEquals(m.group(6), "queries"); | ||
| Assert.assertEquals(m.group(7), "Count"); | ||
| } | ||
|
|
||
| /** | ||
| * broker.yml rule 0: meters/timers scoped to tableNameWithType with database prefix. | ||
| * e.g. pinot.broker.myDb.myTable_OFFLINE.queries | ||
| */ | ||
| @Test | ||
| public void testBrokerTableWithTypeMeterPatternWithDatabase() | ||
| throws Exception { | ||
| String pattern = loadPattern("broker.yml", 0); | ||
| Matcher m = Pattern.compile(pattern).matcher( | ||
| "\"org.apache.pinot.common.metrics\"<type=\"BrokerMetrics\", " | ||
| + "name=\"pinot.broker.myDb.myTable_OFFLINE.queries\"><>Count"); | ||
| Assert.assertTrue(m.matches(), "Pattern should match broker table-scoped meter with database prefix"); | ||
| Assert.assertEquals(m.group(1), "broker"); | ||
| Assert.assertEquals(m.group(3), "myDb"); | ||
| Assert.assertEquals(m.group(4), "myTable"); | ||
| Assert.assertEquals(m.group(5), "OFFLINE"); | ||
| Assert.assertEquals(m.group(6), "queries"); | ||
| Assert.assertEquals(m.group(7), "Count"); | ||
| } | ||
|
|
||
| /** | ||
| * broker.yml rule 3: meters/timers scoped to rawTableName. | ||
| * e.g. pinot.broker.myTable.queries | ||
| */ | ||
| @Test | ||
| public void testBrokerRawTableNameMeterPattern() | ||
| throws Exception { | ||
| String pattern = loadPattern("broker.yml", 3); | ||
| Matcher m = Pattern.compile(pattern).matcher( | ||
| "\"org.apache.pinot.common.metrics\"<type=\"BrokerMetrics\", " | ||
| + "name=\"pinot.broker.myTable.queries\"><>Count"); | ||
| Assert.assertTrue(m.matches(), "Pattern should match broker raw-table-name meter"); | ||
| Assert.assertEquals(m.group(1), "broker"); | ||
| Assert.assertEquals(m.group(4), "myTable"); | ||
| Assert.assertEquals(m.group(5), "queries"); | ||
| Assert.assertEquals(m.group(6), "Count"); | ||
| } | ||
|
|
||
| /** | ||
| * broker.yml rule 5: global gauge/meter/timer (no table scope). | ||
| * e.g. pinot.broker.totalDocuments | ||
| */ | ||
| @Test | ||
| public void testBrokerGlobalMeterPattern() | ||
| throws Exception { | ||
| String pattern = loadPattern("broker.yml", 5); | ||
| Matcher m = Pattern.compile(pattern).matcher( | ||
| "\"org.apache.pinot.common.metrics\"<type=\"BrokerMetrics\", " | ||
| + "name=\"pinot.broker.totalDocuments\"><>Value"); | ||
| Assert.assertTrue(m.matches(), "Pattern should match global broker gauge"); | ||
| Assert.assertEquals(m.group(1), "totalDocuments"); | ||
| Assert.assertEquals(m.group(2), "Value"); | ||
| } | ||
|
|
||
| // ---- Server patterns ---- | ||
|
|
||
| /** | ||
| * server.yml rule 7: meters/timers scoped to tableNameWithType. | ||
| * e.g. pinot.server.myTable_OFFLINE.segmentUploadFailure | ||
| */ | ||
| @Test | ||
| public void testServerTableWithTypeMeterPattern() | ||
| throws Exception { | ||
| String pattern = loadPattern("server.yml", 7); | ||
| Matcher m = Pattern.compile(pattern).matcher( | ||
| "\"org.apache.pinot.common.metrics\"<type=\"ServerMetrics\", " | ||
| + "name=\"pinot.server.myTable_OFFLINE.segmentUploadFailure\"><>Count"); | ||
| Assert.assertTrue(m.matches(), "Pattern should match server table-scoped meter"); | ||
| Assert.assertEquals(m.group(3), "myTable"); | ||
| Assert.assertEquals(m.group(4), "OFFLINE"); | ||
| Assert.assertEquals(m.group(5), "segmentUploadFailure"); | ||
| Assert.assertEquals(m.group(6), "Count"); | ||
| } | ||
|
|
||
| /** | ||
| * server.yml rule 2: gauge scoped to tableNameWithType with partition. | ||
| * e.g. pinot.server.queries.myTable_REALTIME.3 | ||
| */ | ||
| @Test | ||
| public void testServerTableWithTypeAndPartitionGaugePattern() | ||
| throws Exception { | ||
| String pattern = loadPattern("server.yml", 2); | ||
| Matcher m = Pattern.compile(pattern).matcher( | ||
| "\"org.apache.pinot.common.metrics\"<type=\"ServerMetrics\", " | ||
| + "name=\"pinot.server.queries.myTable_REALTIME.3\"><>Value"); | ||
| Assert.assertTrue(m.matches(), "Pattern should match server table-scoped gauge with partition"); | ||
| Assert.assertEquals(m.group(1), "queries"); | ||
| Assert.assertEquals(m.group(4), "myTable"); | ||
| Assert.assertEquals(m.group(5), "REALTIME"); | ||
| Assert.assertEquals(m.group(6), "3"); | ||
| Assert.assertEquals(m.group(7), "Value"); | ||
| } | ||
|
|
||
| // ---- Controller patterns ---- | ||
|
|
||
| /** | ||
| * controller.yml rule 2: minion task-type gauge. | ||
| * e.g. pinot.controller.numMinionTasksInProgress.SegmentGenerationAndPush | ||
| */ | ||
| @Test | ||
| public void testControllerTaskTypeGaugePattern() | ||
| throws Exception { | ||
| String pattern = loadPattern("controller.yml", 2); | ||
| Matcher m = Pattern.compile(pattern).matcher( | ||
| "\"org.apache.pinot.common.metrics\"<type=\"ControllerMetrics\", " | ||
| + "name=\"pinot.controller.numMinionTasksInProgress.SegmentGenerationAndPush\"><>Value"); | ||
| Assert.assertTrue(m.matches(), "Pattern should match controller task-type gauge"); | ||
| Assert.assertEquals(m.group(1), "numMinionTasksInProgress"); | ||
| Assert.assertEquals(m.group(2), "SegmentGenerationAndPush"); | ||
| Assert.assertEquals(m.group(3), "Value"); | ||
| } | ||
|
|
||
| /** | ||
| * controller.yml rule 10: meters/timers scoped to tableNameWithType. | ||
| * e.g. pinot.controller.myTable_OFFLINE.segmentUploadFailure | ||
| */ | ||
| @Test | ||
| public void testControllerTableWithTypeMeterPattern() | ||
| throws Exception { | ||
| String pattern = loadPattern("controller.yml", 10); | ||
| Matcher m = Pattern.compile(pattern).matcher( | ||
| "\"org.apache.pinot.common.metrics\"<type=\"ControllerMetrics\", " | ||
| + "name=\"pinot.controller.myTable_OFFLINE.segmentUploadFailure\"><>Count"); | ||
| Assert.assertTrue(m.matches(), "Pattern should match controller table-scoped meter"); | ||
| Assert.assertEquals(m.group(1), "controller"); | ||
| Assert.assertEquals(m.group(4), "myTable"); | ||
| Assert.assertEquals(m.group(5), "OFFLINE"); | ||
| Assert.assertEquals(m.group(6), "segmentUploadFailure"); | ||
| Assert.assertEquals(m.group(7), "Count"); | ||
| } | ||
|
|
||
| // ---- Minion patterns ---- | ||
|
|
||
| /** | ||
| * minion.yml rule 0: meters/timers scoped to tableNameWithType and taskType. | ||
| * e.g. pinot.minion.myTable_REALTIME.SegmentGenerationAndPush.segmentUploadFailure | ||
| */ | ||
| @Test | ||
| public void testMinionTableWithTypeAndTaskTypeMeterPattern() | ||
| throws Exception { | ||
| String pattern = loadPattern("minion.yml", 0); | ||
| Matcher m = Pattern.compile(pattern).matcher( | ||
| "\"org.apache.pinot.common.metrics\"<type=\"MinionMetrics\", " | ||
| + "name=\"pinot.minion.myTable_REALTIME.SegmentGenerationAndPush.segmentUploadFailure\"><>Count"); | ||
| Assert.assertTrue(m.matches(), "Pattern should match minion table + taskType scoped meter"); | ||
| Assert.assertEquals(m.group(3), "myTable"); | ||
| Assert.assertEquals(m.group(4), "REALTIME"); | ||
| Assert.assertEquals(m.group(5), "SegmentGenerationAndPush"); | ||
| Assert.assertEquals(m.group(6), "segmentUploadFailure"); | ||
| Assert.assertEquals(m.group(7), "Count"); | ||
| } | ||
|
|
||
| /** | ||
| * minion.yml rule 1: meters/timers accepting either rawTableName or tableNameWithType. | ||
| * e.g. pinot.minion.myTable.queries | ||
| */ | ||
| @Test | ||
| public void testMinionTableOrIdScopedMeterPattern() | ||
| throws Exception { | ||
| String pattern = loadPattern("minion.yml", 1); | ||
| Matcher m = Pattern.compile(pattern).matcher( | ||
| "\"org.apache.pinot.common.metrics\"<type=\"MinionMetrics\", " | ||
| + "name=\"pinot.minion.myTable.numberOfSegmentsQueued\"><>Value"); | ||
| Assert.assertTrue(m.matches(), "Pattern should match minion table/id scoped meter"); | ||
| Assert.assertEquals(m.group(1), "myTable"); | ||
| Assert.assertEquals(m.group(2), "numberOfSegmentsQueued"); | ||
| Assert.assertEquals(m.group(3), "Value"); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the pattern string at the given index from the named config file. | ||
| * Only rules that contain a {@code pattern} field are counted. | ||
| */ | ||
| private String loadPattern(String configFile, int ruleIndex) | ||
| throws Exception { | ||
| List<String> patterns = extractPatterns(CONFIG_BASE_PATH + "/" + configFile); | ||
| Assert.assertTrue(ruleIndex < patterns.size(), | ||
| "Rule index " + ruleIndex + " out of bounds for " + configFile | ||
| + " (has " + patterns.size() + " pattern rules)"); | ||
| return patterns.get(ruleIndex); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private List<String> extractPatterns(String filePath) | ||
| throws Exception { | ||
| Yaml yaml = new Yaml(); | ||
| try (FileReader reader = new FileReader(filePath)) { | ||
| Map<String, Object> config = yaml.load(reader); | ||
| List<Map<String, Object>> rules = (List<Map<String, Object>>) config.get("rules"); | ||
| return rules.stream() | ||
| .filter(rule -> rule.containsKey("pattern")) | ||
| .map(rule -> (String) rule.get("pattern")) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.