madrob commented on a change in pull request #2165: URL: https://github.com/apache/lucene-solr/pull/2165#discussion_r548076192
########## File path: solr/contrib/prometheus-exporter/src/java/org/apache/solr/prometheus/exporter/MetricsConfiguration.java ########## @@ -105,17 +108,23 @@ public static MetricsConfiguration from(String resource) throws Exception { public static MetricsConfiguration from(Document config) throws Exception { Node settings = getNode(config, "/config/settings"); + Map<String,MetricsQueryTemplate> jqTemplatesMap = null; + NodeList jqTemplates = (NodeList)(xpathFactory.newXPath()).evaluate("/config/jq-templates/template", config, XPathConstants.NODESET); Review comment: Noble just spent a bunch of effort getting rid of XPath in other places, is this a good direction to be going now? ########## File path: solr/contrib/prometheus-exporter/src/java/org/apache/solr/prometheus/exporter/MetricsQueryTemplate.java ########## @@ -0,0 +1,128 @@ +/* + * 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.solr.prometheus.exporter; + +import java.util.Objects; +import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class MetricsQueryTemplate { + private static final Pattern matchJqTemplate = + Pattern.compile("^\\$jq:(?<TEMPLATE>.*?)\\(\\s?(?<UNIQUE>[^,]*),\\s?(?<KEYSELECTOR>[^,]*)(,\\s?(?<METRIC>[^,]*)\\s?)?(,\\s?(?<TYPE>[^,]*)\\s?)?\\)$"); Review comment: Please add a human readable comment describing the intent ########## File path: solr/contrib/prometheus-exporter/src/java/org/apache/solr/prometheus/exporter/MetricsQueryTemplate.java ########## @@ -0,0 +1,128 @@ +/* + * 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.solr.prometheus.exporter; + +import java.util.Objects; +import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class MetricsQueryTemplate { + private static final Pattern matchJqTemplate = + Pattern.compile("^\\$jq:(?<TEMPLATE>.*?)\\(\\s?(?<UNIQUE>[^,]*),\\s?(?<KEYSELECTOR>[^,]*)(,\\s?(?<METRIC>[^,]*)\\s?)?(,\\s?(?<TYPE>[^,]*)\\s?)?\\)$"); + + public static Optional<Matcher> matches(String jsonQuery) { + Optional<Matcher> maybe = Optional.empty(); + if (jsonQuery != null) { + String toMatch = jsonQuery.replaceAll("\\s+", " ").trim(); + Matcher m = matchJqTemplate.matcher(toMatch); + if (m.matches()) { + maybe = Optional.of(m); + } + } + return maybe; + } + + private final String name; + private final String defaultType; + private final String template; + + public MetricsQueryTemplate(String name, String template, String defaultType) { + Objects.requireNonNull(name, "jq template must have a name"); + Objects.requireNonNull(template, "jq template is required"); + + this.name = name; + this.template = template.replaceAll("\\s+", " ").trim(); + if (this.template.isEmpty()) { + throw new IllegalArgumentException("jq template must not be empty"); + } + this.defaultType = defaultType != null ? defaultType : "GAUGE"; + } + + public String getName() { + return name; + } + + public String getDefaultType() { + return defaultType; + } + + public String getTemplate() { + return template; + } + + public String applyTemplate(final Matcher matched) { + String keySelector = matched.group("KEYSELECTOR"); + if (keySelector != null) { + if (!keySelector.contains("select(") && !keySelector.contains(".key")) { + if (keySelector.contains("(") && keySelector.contains(")")) { + // some kind of function here ... + keySelector = ".key | " + keySelector.trim(); + } else { + keySelector = ".key == " + keySelector.trim(); + } + } + } + String unique = matched.group("UNIQUE").trim(); + String type = matched.group("TYPE"); + if (type == null) { + type = defaultType; + } + + String metric = matched.group("METRIC"); + if (metric == null) { + metric = unique; + } + + // could be a simple field name or some kind of function here + if (!metric.contains("$")) { + if ("object.value".equals(metric)) { + metric = "$object.value"; // don't require the user to supply the $ Review comment: Are we trying to be too helpful here? Or is this matching some existing spec? ########## File path: solr/contrib/prometheus-exporter/src/java/org/apache/solr/prometheus/exporter/MetricsQueryTemplate.java ########## @@ -0,0 +1,128 @@ +/* + * 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.solr.prometheus.exporter; + +import java.util.Objects; +import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class MetricsQueryTemplate { + private static final Pattern matchJqTemplate = + Pattern.compile("^\\$jq:(?<TEMPLATE>.*?)\\(\\s?(?<UNIQUE>[^,]*),\\s?(?<KEYSELECTOR>[^,]*)(,\\s?(?<METRIC>[^,]*)\\s?)?(,\\s?(?<TYPE>[^,]*)\\s?)?\\)$"); + + public static Optional<Matcher> matches(String jsonQuery) { + Optional<Matcher> maybe = Optional.empty(); + if (jsonQuery != null) { + String toMatch = jsonQuery.replaceAll("\\s+", " ").trim(); + Matcher m = matchJqTemplate.matcher(toMatch); + if (m.matches()) { + maybe = Optional.of(m); + } + } + return maybe; + } + + private final String name; + private final String defaultType; + private final String template; + + public MetricsQueryTemplate(String name, String template, String defaultType) { + Objects.requireNonNull(name, "jq template must have a name"); + Objects.requireNonNull(template, "jq template is required"); + + this.name = name; + this.template = template.replaceAll("\\s+", " ").trim(); + if (this.template.isEmpty()) { + throw new IllegalArgumentException("jq template must not be empty"); + } + this.defaultType = defaultType != null ? defaultType : "GAUGE"; + } + + public String getName() { + return name; + } + + public String getDefaultType() { + return defaultType; + } + + public String getTemplate() { Review comment: unused ########## File path: solr/contrib/prometheus-exporter/src/java/org/apache/solr/prometheus/exporter/MetricsConfiguration.java ########## @@ -105,17 +108,23 @@ public static MetricsConfiguration from(String resource) throws Exception { public static MetricsConfiguration from(Document config) throws Exception { Node settings = getNode(config, "/config/settings"); + Map<String,MetricsQueryTemplate> jqTemplatesMap = null; + NodeList jqTemplates = (NodeList)(xpathFactory.newXPath()).evaluate("/config/jq-templates/template", config, XPathConstants.NODESET); + if (jqTemplates.getLength() > 0) { Review comment: Did we need a null check first? ########## File path: solr/contrib/prometheus-exporter/src/java/org/apache/solr/prometheus/exporter/MetricsConfiguration.java ########## @@ -141,12 +150,29 @@ private static Node getNode(Document doc, String path) { } } - private static List<MetricsQuery> toMetricQueries(Node node) throws JsonQueryException { + private static List<MetricsQuery> toMetricQueries(Node node, Map<String,MetricsQueryTemplate> jqTemplatesMap) throws JsonQueryException { if (node == null) { return Collections.emptyList(); } - return MetricsQuery.from(node); + return MetricsQuery.from(node, jqTemplatesMap); } + private static Map<String,MetricsQueryTemplate> loadJqTemplates(NodeList jqTemplates) { + Map<String,MetricsQueryTemplate> map = new HashMap<>(); + for (int t=0; t < jqTemplates.getLength(); t++) { + Node template = jqTemplates.item(t); + if (template.getNodeType() == Node.ELEMENT_NODE && template.hasAttributes()) { + Node nameAttr = template.getAttributes().getNamedItem("name"); + String tmpl = template.getTextContent(); + if (nameAttr != null && tmpl != null && !tmpl.trim().isEmpty()) { Review comment: Check for name.trim.isEmpty also? Isn't there a StringUtils method that does this for us safely? ########## File path: solr/contrib/prometheus-exporter/src/java/org/apache/solr/prometheus/exporter/MetricsQuery.java ########## @@ -116,6 +117,23 @@ public String getPath() { List<JsonQuery> compiledQueries = new ArrayList<>(); if (jsonQueries != null) { for (String jsonQuery : jsonQueries) { + + // does this query refer to a reusable jq template to reduce boilerplate in the config? + String stripWs = jsonQuery.replaceAll("\\s+", " ").trim(); + if (stripWs.startsWith("$jq:")) { + Optional<Matcher> maybeMatcher = MetricsQueryTemplate.matches(stripWs); + if (maybeMatcher.isPresent()) { + Matcher matcher = maybeMatcher.get(); + String templateName = matcher.group("TEMPLATE"); + MetricsQueryTemplate template = jqTemplates.get(templateName); Review comment: `jqTemplates` could be null ########## File path: solr/contrib/prometheus-exporter/src/java/org/apache/solr/prometheus/exporter/MetricsQueryTemplate.java ########## @@ -0,0 +1,128 @@ +/* + * 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.solr.prometheus.exporter; + +import java.util.Objects; +import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class MetricsQueryTemplate { + private static final Pattern matchJqTemplate = + Pattern.compile("^\\$jq:(?<TEMPLATE>.*?)\\(\\s?(?<UNIQUE>[^,]*),\\s?(?<KEYSELECTOR>[^,]*)(,\\s?(?<METRIC>[^,]*)\\s?)?(,\\s?(?<TYPE>[^,]*)\\s?)?\\)$"); + + public static Optional<Matcher> matches(String jsonQuery) { + Optional<Matcher> maybe = Optional.empty(); + if (jsonQuery != null) { + String toMatch = jsonQuery.replaceAll("\\s+", " ").trim(); + Matcher m = matchJqTemplate.matcher(toMatch); + if (m.matches()) { + maybe = Optional.of(m); + } + } + return maybe; + } + + private final String name; + private final String defaultType; + private final String template; + + public MetricsQueryTemplate(String name, String template, String defaultType) { + Objects.requireNonNull(name, "jq template must have a name"); + Objects.requireNonNull(template, "jq template is required"); + + this.name = name; + this.template = template.replaceAll("\\s+", " ").trim(); + if (this.template.isEmpty()) { + throw new IllegalArgumentException("jq template must not be empty"); + } + this.defaultType = defaultType != null ? defaultType : "GAUGE"; + } + + public String getName() { + return name; + } + + public String getDefaultType() { Review comment: unused? ########## File path: solr/contrib/prometheus-exporter/src/java/org/apache/solr/prometheus/exporter/MetricsConfiguration.java ########## @@ -141,12 +150,29 @@ private static Node getNode(Document doc, String path) { } } - private static List<MetricsQuery> toMetricQueries(Node node) throws JsonQueryException { + private static List<MetricsQuery> toMetricQueries(Node node, Map<String,MetricsQueryTemplate> jqTemplatesMap) throws JsonQueryException { if (node == null) { return Collections.emptyList(); } - return MetricsQuery.from(node); + return MetricsQuery.from(node, jqTemplatesMap); } + private static Map<String,MetricsQueryTemplate> loadJqTemplates(NodeList jqTemplates) { + Map<String,MetricsQueryTemplate> map = new HashMap<>(); + for (int t=0; t < jqTemplates.getLength(); t++) { + Node template = jqTemplates.item(t); Review comment: Is this going to be slooooooow? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org