Added support in urls, and grouped metrics (#12296)

This commit is contained in:
syacobovitz 2022-03-22 20:22:05 +02:00 committed by GitHub
parent 0867ca75e1
commit d7308e9290
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 10 deletions

View File

@ -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");
}
}

View File

@ -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;
}

View File

@ -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());
}
}