abhishekagarwal87 commented on code in PR #15659:
URL: https://github.com/apache/druid/pull/15659#discussion_r1489306210


##########
extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/metrics/Metric.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * 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.druid.emitter.prometheus.metrics;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonSubTypes;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import io.prometheus.client.SimpleCollector;
+import org.apache.druid.emitter.prometheus.PrometheusEmitterConfig;
+import org.apache.druid.java.util.common.StringUtils;
+
+import java.util.SortedSet;
+import java.util.regex.Pattern;
+
+@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
+@JsonSubTypes({
+    @JsonSubTypes.Type(value = Gauge.class, name = 
MetricType.DimensionMapNames.GAUGE),
+    @JsonSubTypes.Type(value = Histogram.class, name = 
MetricType.DimensionMapNames.HISTOGRAM),
+    @JsonSubTypes.Type(value = Timer.class, name = 
MetricType.DimensionMapNames.TIMER),
+    @JsonSubTypes.Type(value = Counter.class, name = 
MetricType.DimensionMapNames.COUNTER),
+    @JsonSubTypes.Type(value = Summary.class, name = 
MetricType.DimensionMapNames.SUMMARY)
+})
+public abstract class Metric<T extends SimpleCollector<?>>
+{
+  public static final Pattern PATTERN = 
Pattern.compile("[^a-zA-Z_:][^a-zA-Z0-9_:]*");
+  private static final String TAG_HOSTNAME = "host_name";
+  private static final String TAG_SERVICE = "druid_service";
+
+  public final SortedSet<String> dimensions;
+  public final MetricType type;
+  public final String help;
+
+  private String namespace;
+  private String formattedName;
+
+  @JsonIgnore
+  private T collector;
+
+  public Metric(
+      SortedSet<String> dimensions,
+      MetricType type,
+      String help
+  )
+  {
+    this.dimensions = dimensions;
+    this.type = type;
+    this.help = help;
+  }
+
+  public String getNamespace()
+  {
+    return namespace;
+  }
+
+  public T getCollector()
+  {
+    return collector;
+  }
+
+  public void setCollector(T collector)
+  {
+    this.collector = collector;
+  }
+
+  public String getFormattedName()
+  {
+    return formattedName;
+  }
+
+  public abstract void record(String[] labelValues, double value);
+
+  public abstract void createCollector(String name, PrometheusEmitterConfig 
emitterConfig);
+
+  void configure(String name, PrometheusEmitterConfig emitterConfig)
+  {
+    if (emitterConfig.isAddHostAsLabel()) {
+      this.dimensions.add(TAG_HOSTNAME);
+    }
+    if (emitterConfig.isAddServiceAsLabel()) {
+      this.dimensions.add(TAG_SERVICE);
+    }
+    this.dimensions.addAll(emitterConfig.getExtraLabels().keySet());
+    this.formattedName = 
PATTERN.matcher(StringUtils.toLowerCase(name)).replaceAll("_");
+    this.namespace = emitterConfig.getNamespace();
+  }

Review Comment:
   can this be called in the constructor itself? 



##########
extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/metrics/MetricType.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.druid.emitter.prometheus.metrics;
+
+public enum MetricType
+{
+  count(DimensionMapNames.COUNTER),
+  gauge(DimensionMapNames.GAUGE),
+  timer(DimensionMapNames.TIMER),
+  histogram(DimensionMapNames.HISTOGRAM),
+  summary(DimensionMapNames.SUMMARY);
+
+  private final String dimensionMapName;
+
+  MetricType(String dimensionMapName)
+  {
+    this.dimensionMapName = dimensionMapName;
+  }
+
+  public String getDimensionMapName()
+  {
+    return dimensionMapName;
+  }
+
+  // TODO: get rid of this inner class after upgrading jackson in parent
+  //       ref: https://github.com/FasterXML/jackson-databind/issues/2739

Review Comment:
   We generally don't leave TODOs in the code. You could just leave a comment 
here



##########
extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/metrics/Metric.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * 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.druid.emitter.prometheus.metrics;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonSubTypes;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import io.prometheus.client.SimpleCollector;
+import org.apache.druid.emitter.prometheus.PrometheusEmitterConfig;
+import org.apache.druid.java.util.common.StringUtils;
+
+import java.util.SortedSet;
+import java.util.regex.Pattern;
+
+@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
+@JsonSubTypes({
+    @JsonSubTypes.Type(value = Gauge.class, name = 
MetricType.DimensionMapNames.GAUGE),
+    @JsonSubTypes.Type(value = Histogram.class, name = 
MetricType.DimensionMapNames.HISTOGRAM),
+    @JsonSubTypes.Type(value = Timer.class, name = 
MetricType.DimensionMapNames.TIMER),
+    @JsonSubTypes.Type(value = Counter.class, name = 
MetricType.DimensionMapNames.COUNTER),
+    @JsonSubTypes.Type(value = Summary.class, name = 
MetricType.DimensionMapNames.SUMMARY)
+})
+public abstract class Metric<T extends SimpleCollector<?>>
+{
+  public static final Pattern PATTERN = 
Pattern.compile("[^a-zA-Z_:][^a-zA-Z0-9_:]*");
+  private static final String TAG_HOSTNAME = "host_name";
+  private static final String TAG_SERVICE = "druid_service";
+
+  public final SortedSet<String> dimensions;
+  public final MetricType type;
+  public final String help;
+
+  private String namespace;
+  private String formattedName;
+
+  @JsonIgnore
+  private T collector;
+
+  public Metric(
+      SortedSet<String> dimensions,
+      MetricType type,
+      String help
+  )
+  {
+    this.dimensions = dimensions;
+    this.type = type;
+    this.help = help;
+  }
+
+  public String getNamespace()
+  {
+    return namespace;
+  }
+
+  public T getCollector()
+  {
+    return collector;
+  }
+
+  public void setCollector(T collector)
+  {
+    this.collector = collector;
+  }
+
+  public String getFormattedName()
+  {
+    return formattedName;
+  }
+
+  public abstract void record(String[] labelValues, double value);
+
+  public abstract void createCollector(String name, PrometheusEmitterConfig 
emitterConfig);
+
+  void configure(String name, PrometheusEmitterConfig emitterConfig)
+  {
+    if (emitterConfig.isAddHostAsLabel()) {
+      this.dimensions.add(TAG_HOSTNAME);
+    }
+    if (emitterConfig.isAddServiceAsLabel()) {
+      this.dimensions.add(TAG_SERVICE);
+    }
+    this.dimensions.addAll(emitterConfig.getExtraLabels().keySet());

Review Comment:
   getExtraLabels can return null, isn't it? 



##########
extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/metrics/Summary.java:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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.druid.emitter.prometheus.metrics;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonTypeName;
+import org.apache.druid.emitter.prometheus.PrometheusEmitterConfig;
+
+import javax.annotation.Nullable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Objects;
+import java.util.SortedSet;
+
+@JsonTypeName(MetricType.DimensionMapNames.SUMMARY)
+public class Summary extends Metric<io.prometheus.client.Summary>
+{
+  private final List<Double> quantiles;
+  private final List<Double> errors;
+  private final Long ageSeconds;
+  private final Integer ageBuckets;
+
+  public Summary(
+      @JsonProperty("dimensions") SortedSet<String> dimensions,
+      @JsonProperty("type") MetricType type,
+      @JsonProperty("help") String help,
+      @JsonProperty("quantiles") List<Double> quantiles,
+      @JsonProperty("errors") List<Double> errors,
+      @JsonProperty("ageSeconds") @Nullable Long ageSeconds,
+      @JsonProperty("ageBuckets") @Nullable Integer ageBuckets
+  )
+  {
+    super(dimensions, type, help);
+    this.quantiles = quantiles;
+    this.errors = errors;
+    this.ageBuckets = ageBuckets;
+    this.ageSeconds = ageSeconds;
+  }
+
+  @Override
+  public void record(String[] labelValues, double value)
+  {
+    this.getCollector().labels(labelValues).observe(value);
+  }
+
+  @Override
+  public void createCollector(String name, PrometheusEmitterConfig 
emitterConfig)
+  {
+    super.configure(name, emitterConfig);
+    io.prometheus.client.Summary.Builder builder = 
io.prometheus.client.Summary.build(getFormattedName(), help);

Review Comment:
   should there be a validation that size of quantiles and errors is same? 



##########
extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/metrics/Metric.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * 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.druid.emitter.prometheus.metrics;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonSubTypes;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import io.prometheus.client.SimpleCollector;
+import org.apache.druid.emitter.prometheus.PrometheusEmitterConfig;
+import org.apache.druid.java.util.common.StringUtils;
+
+import java.util.SortedSet;
+import java.util.regex.Pattern;
+
+@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
+@JsonSubTypes({
+    @JsonSubTypes.Type(value = Gauge.class, name = 
MetricType.DimensionMapNames.GAUGE),
+    @JsonSubTypes.Type(value = Histogram.class, name = 
MetricType.DimensionMapNames.HISTOGRAM),
+    @JsonSubTypes.Type(value = Timer.class, name = 
MetricType.DimensionMapNames.TIMER),
+    @JsonSubTypes.Type(value = Counter.class, name = 
MetricType.DimensionMapNames.COUNTER),
+    @JsonSubTypes.Type(value = Summary.class, name = 
MetricType.DimensionMapNames.SUMMARY)
+})
+public abstract class Metric<T extends SimpleCollector<?>>

Review Comment:
   Please add some javadocs 



##########
extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/metrics/Metric.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * 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.druid.emitter.prometheus.metrics;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonSubTypes;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import io.prometheus.client.SimpleCollector;
+import org.apache.druid.emitter.prometheus.PrometheusEmitterConfig;
+import org.apache.druid.java.util.common.StringUtils;
+
+import java.util.SortedSet;
+import java.util.regex.Pattern;
+
+@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
+@JsonSubTypes({
+    @JsonSubTypes.Type(value = Gauge.class, name = 
MetricType.DimensionMapNames.GAUGE),
+    @JsonSubTypes.Type(value = Histogram.class, name = 
MetricType.DimensionMapNames.HISTOGRAM),
+    @JsonSubTypes.Type(value = Timer.class, name = 
MetricType.DimensionMapNames.TIMER),
+    @JsonSubTypes.Type(value = Counter.class, name = 
MetricType.DimensionMapNames.COUNTER),
+    @JsonSubTypes.Type(value = Summary.class, name = 
MetricType.DimensionMapNames.SUMMARY)
+})
+public abstract class Metric<T extends SimpleCollector<?>>
+{
+  public static final Pattern PATTERN = 
Pattern.compile("[^a-zA-Z_:][^a-zA-Z0-9_:]*");
+  private static final String TAG_HOSTNAME = "host_name";
+  private static final String TAG_SERVICE = "druid_service";
+
+  public final SortedSet<String> dimensions;
+  public final MetricType type;
+  public final String help;
+
+  private String namespace;
+  private String formattedName;
+
+  @JsonIgnore
+  private T collector;
+
+  public Metric(
+      SortedSet<String> dimensions,
+      MetricType type,
+      String help
+  )
+  {
+    this.dimensions = dimensions;
+    this.type = type;
+    this.help = help;
+  }
+
+  public String getNamespace()
+  {
+    return namespace;
+  }
+
+  public T getCollector()
+  {
+    return collector;
+  }
+
+  public void setCollector(T collector)

Review Comment:
   This could be protected or just removed if `collector` is made protected. 



##########
extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/metrics/Metric.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * 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.druid.emitter.prometheus.metrics;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonSubTypes;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import io.prometheus.client.SimpleCollector;
+import org.apache.druid.emitter.prometheus.PrometheusEmitterConfig;
+import org.apache.druid.java.util.common.StringUtils;
+
+import java.util.SortedSet;
+import java.util.regex.Pattern;
+
+@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
+@JsonSubTypes({
+    @JsonSubTypes.Type(value = Gauge.class, name = 
MetricType.DimensionMapNames.GAUGE),
+    @JsonSubTypes.Type(value = Histogram.class, name = 
MetricType.DimensionMapNames.HISTOGRAM),
+    @JsonSubTypes.Type(value = Timer.class, name = 
MetricType.DimensionMapNames.TIMER),
+    @JsonSubTypes.Type(value = Counter.class, name = 
MetricType.DimensionMapNames.COUNTER),
+    @JsonSubTypes.Type(value = Summary.class, name = 
MetricType.DimensionMapNames.SUMMARY)
+})
+public abstract class Metric<T extends SimpleCollector<?>>
+{
+  public static final Pattern PATTERN = 
Pattern.compile("[^a-zA-Z_:][^a-zA-Z0-9_:]*");
+  private static final String TAG_HOSTNAME = "host_name";
+  private static final String TAG_SERVICE = "druid_service";

Review Comment:
   These fields are in PrometheusEmitter as well. Why do they need to be 
duplicated? 



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

Reply via email to