noob-se7en commented on code in PR #18414: URL: https://github.com/apache/pinot/pull/18414#discussion_r3189168894
########## pinot-common/src/test/java/org/apache/pinot/common/metrics/prometheus/PrometheusTemplateRegexpTest.java: ########## @@ -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); Review Comment: nice fix on the inline-duplication issue. minor follow-up: looking up by numeric index couples this to YAML rule order — inserting or reordering a rule in `broker.yml` will silently shift every index, so these tests will either match a different rule (and quietly assert wrong groups) or fail with a confusing "doesn't match" error. keying off the rule's `name` field (e.g. `"pinot_$1_$6_$7"` for this one) would survive reorderings. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
