You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by GitBox <gi...@apache.org> on 2021/01/05 22:06:07 UTC

[GitHub] [lucene-solr] muse-dev[bot] commented on a change in pull request #2165: SOLR-15059: Improve query performance monitoring

muse-dev[bot] commented on a change in pull request #2165:
URL: https://github.com/apache/lucene-solr/pull/2165#discussion_r552227035



##########
File path: solr/contrib/prometheus-exporter/src/java/org/apache/solr/prometheus/exporter/MetricsQueryTemplate.java
##########
@@ -0,0 +1,135 @@
+/*
+ * 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 {
+  /*
+  A regex with named groups is used to match template references to template + vars using the basic pattern:
+
+      $jq:<TEMPLATE>( <UNIQUE>, <KEYSELECTOR>, <METRIC>, <TYPE> )
+
+  For instance,
+
+      $jq:core(requests_total, endswith(".requestTimes"), count, COUNTER)
+
+  TEMPLATE = core
+  UNIQUE = requests_total (unique suffix for this metric, results in a metric named "solr_metrics_core_requests_total")
+  KEYSELECTOR = endswith(".requestTimes") (filter to select the specific key for this metric)
+  METRIC = count
+  TYPE = COUNTER
+  */
+  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 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();

Review comment:
       *NULL_DEREFERENCE:*  object returned by `matched.group("UNIQUE")` could be null and is dereferenced at line 88.




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org