thelabdude commented on a change in pull request #2165:
URL: https://github.com/apache/lucene-solr/pull/2165#discussion_r548128158



##########
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:
       just trying to be helpful to keep the template syntax simpler ... e.g. I 
like {{count}} vs. {{$object.value.count}}




----------------------------------------------------------------
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

Reply via email to