You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by ma...@apache.org on 2022/03/22 18:22:15 UTC

[druid] branch master updated: Added support in urls, and grouped metrics (#12296)

This is an automated email from the ASF dual-hosted git repository.

maytasm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/druid.git


The following commit(s) were added to refs/heads/master by this push:
     new d7308e9  Added support in urls, and grouped metrics (#12296)
d7308e9 is described below

commit d7308e929076b2170ba34fd1b731aa0309e09e90
Author: syacobovitz <98...@users.noreply.github.com>
AuthorDate: Tue Mar 22 20:22:05 2022 +0200

    Added support in urls, and grouped metrics (#12296)
---
 .../emitter/prometheus/PrometheusEmitter.java      | 36 +++++++++++++++++-----
 .../prometheus/PrometheusEmitterConfig.java        |  3 ++
 .../emitter/prometheus/PrometheusEmitterTest.java  | 28 +++++++++++++++--
 3 files changed, 57 insertions(+), 10 deletions(-)

diff --git a/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/PrometheusEmitter.java b/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/PrometheusEmitter.java
index 19d047f..080800d 100644
--- a/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/PrometheusEmitter.java
+++ b/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/PrometheusEmitter.java
@@ -21,6 +21,7 @@ package org.apache.druid.emitter.prometheus;
 
 
 import com.google.common.collect.ImmutableMap;
+import io.prometheus.client.CollectorRegistry;
 import io.prometheus.client.Counter;
 import io.prometheus.client.Gauge;
 import io.prometheus.client.Histogram;
@@ -32,6 +33,8 @@ import org.apache.druid.java.util.emitter.core.Event;
 import org.apache.druid.java.util.emitter.service.ServiceMetricEvent;
 
 import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.Map;
 import java.util.regex.Pattern;
 
@@ -78,9 +81,24 @@ public class PrometheusEmitter implements Emitter
         log.error("HTTPServer is already started");
       }
     } else if (strategy.equals(PrometheusEmitterConfig.Strategy.pushgateway)) {
-      pushGateway = new PushGateway(config.getPushGatewayAddress());
+      String address = config.getPushGatewayAddress();
+      if (address.startsWith("https") || address.startsWith("http")) {
+        URL myURL = createURLSneakily(address);
+        pushGateway = new PushGateway(myURL);
+      } else {
+        pushGateway = new PushGateway(address);
+      }
     }
+  }
 
+  private static URL createURLSneakily(final String urlString)
+  {
+    try {
+      return new URL(urlString);
+    }
+    catch (MalformedURLException e) {
+      throw new RuntimeException(e);
+    }
   }
 
   @Override
@@ -127,15 +145,17 @@ public class PrometheusEmitter implements Emitter
   private void pushMetric()
   {
     Map<String, DimensionsAndCollector> map = metrics.getRegisteredMetrics();
-    try {
-      for (DimensionsAndCollector collector : map.values()) {
-        if (config.getNamespace() != null) {
-          pushGateway.push(collector.getCollector(), config.getNamespace(), ImmutableMap.of(config.getNamespace(), identifier));
+    CollectorRegistry metrics = new CollectorRegistry();
+    if (config.getNamespace() != null) {
+      try {
+        for (DimensionsAndCollector collector : map.values()) {
+          metrics.register(collector.getCollector());
         }
+        pushGateway.push(metrics, config.getNamespace(), ImmutableMap.of(config.getNamespace(), identifier));
+      }
+      catch (IOException e) {
+        log.error(e, "Unable to push prometheus metrics to pushGateway");
       }
-    }
-    catch (IOException e) {
-      log.error(e, "Unable to push prometheus metrics to pushGateway");
     }
   }
 
diff --git a/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/PrometheusEmitterConfig.java b/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/PrometheusEmitterConfig.java
index 8a6e7a7..475f370 100644
--- a/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/PrometheusEmitterConfig.java
+++ b/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/PrometheusEmitterConfig.java
@@ -68,6 +68,9 @@ public class PrometheusEmitterConfig
     Preconditions.checkArgument(PATTERN.matcher(this.namespace).matches(), "Invalid namespace " + this.namespace);
     this.dimensionMapPath = dimensionMapPath;
     this.port = port;
+    if (this.strategy == Strategy.pushgateway) {
+      Preconditions.checkNotNull(pushGatewayAddress, "Invalid pushGateway address");
+    }
     this.pushGatewayAddress = pushGatewayAddress;
   }
 
diff --git a/extensions-contrib/prometheus-emitter/src/test/java/org/apache/druid/emitter/prometheus/PrometheusEmitterTest.java b/extensions-contrib/prometheus-emitter/src/test/java/org/apache/druid/emitter/prometheus/PrometheusEmitterTest.java
index c9ca139..b4979df 100644
--- a/extensions-contrib/prometheus-emitter/src/test/java/org/apache/druid/emitter/prometheus/PrometheusEmitterTest.java
+++ b/extensions-contrib/prometheus-emitter/src/test/java/org/apache/druid/emitter/prometheus/PrometheusEmitterTest.java
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
- 
+
 package org.apache.druid.emitter.prometheus;
 
 import com.google.common.collect.ImmutableMap;
@@ -38,7 +38,7 @@ import static org.easymock.EasyMock.mock;
 public class PrometheusEmitterTest
 {
   @Test
-  public void testEmitter() 
+  public void testEmitter()
   {
     PrometheusEmitterConfig config = new PrometheusEmitterConfig(PrometheusEmitterConfig.Strategy.exporter, null, null, 0, null);
     PrometheusEmitterModule prometheusEmitterModule = new PrometheusEmitterModule();
@@ -109,4 +109,28 @@ public class PrometheusEmitterTest
     emitter.emit(build);
     emitter.flush();
   }
+
+  @Test
+  public void testEmitterConfigCreationWithNullAsAddress()
+  {
+    Assert.assertThrows(NullPointerException.class, () -> new PrometheusEmitterConfig(PrometheusEmitterConfig.Strategy.pushgateway, "namespace4", null, 0, null));
+  }
+
+  @Test
+  public void testEmitterStartWithHttpUrl()
+  {
+    PrometheusEmitterConfig pushEmitterConfig = new PrometheusEmitterConfig(PrometheusEmitterConfig.Strategy.pushgateway, "namespace4", null, 0, "http://pushgateway");
+    PrometheusEmitter pushEmitter = new PrometheusEmitter(pushEmitterConfig);
+    pushEmitter.start();
+    Assert.assertNotNull(pushEmitter.getPushGateway());
+  }
+
+  @Test
+  public void testEmitterStartWithHttpsUrl()
+  {
+    PrometheusEmitterConfig pushEmitterConfig = new PrometheusEmitterConfig(PrometheusEmitterConfig.Strategy.pushgateway, "namespace5", null, 0, "https://pushgateway");
+    PrometheusEmitter pushEmitter = new PrometheusEmitter(pushEmitterConfig);
+    pushEmitter.start();
+    Assert.assertNotNull(pushEmitter.getPushGateway());
+  }
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org