Skip to content

Commit

Permalink
将Prometheus以http方式暴露
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyijq committed Dec 30, 2018
1 parent 1ae9365 commit d223170
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class LocalDynamicConfig implements DynamicConfig {
LocalDynamicConfig(String name, boolean failOnNotExist) {
this.name = name;
this.listeners = new CopyOnWriteArrayList<>();
this.config = new HashMap<>();
this.file = getFileByName(name);

if (failOnNotExist && (file == null || !file.exists())) {
Expand Down
43 changes: 41 additions & 2 deletions qmq-metrics-prometheus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,54 @@
</parent>

<artifactId>qmq-metrics-prometheus</artifactId>
<name>qmq metrics prometheus</name>
<description>qmq metrics to prometheus</description>

<properties>
<java_source_version>1.7</java_source_version>
<java_target_version>1.7</java_target_version>
</properties>

<dependencies>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>qmq-common</artifactId>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_httpserver</artifactId>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_graphite_bridge</artifactId>
</dependency>
<dependency>
<groupId>com.qunar.qmq</groupId>
<artifactId>qmq</artifactId>
<version>1.1.0</version>
<optional>true</optional>
</dependency>
</dependencies>

<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
import io.prometheus.client.GaugeMetricFamily;
import io.prometheus.client.SimpleCollector;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;

public class PrometheusQmqGauge extends SimpleCollector<PrometheusQmqGauge.Child> implements Collector.Describable {

Expand Down Expand Up @@ -52,7 +49,9 @@ public List<MetricFamilySamples> collect() {

@Override
public List<MetricFamilySamples> describe() {
return Collections.singletonList(new GaugeMetricFamily(fullname, help, labelNames));
List<MetricFamilySamples> list = new ArrayList<>();
list.add(new GaugeMetricFamily(fullname, help, labelNames));
return list;
}

public static class Builder extends SimpleCollector.Builder<Builder, PrometheusQmqGauge> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import io.prometheus.client.Collector;
import io.prometheus.client.Gauge;
import io.prometheus.client.SimpleCollector;
import io.prometheus.client.Summary;
import io.prometheus.client.*;
import io.prometheus.client.bridge.Graphite;
import io.prometheus.client.exporter.HTTPServer;
import qunar.tc.qmq.configuration.DynamicConfig;
import qunar.tc.qmq.configuration.DynamicConfigLoader;
import qunar.tc.qmq.metrics.QmqCounter;
import qunar.tc.qmq.metrics.QmqMeter;
import qunar.tc.qmq.metrics.QmqMetricRegistry;
import qunar.tc.qmq.metrics.QmqTimer;

import java.io.IOException;
import java.util.Arrays;

/**
Expand All @@ -44,6 +46,27 @@ public Collector load(Key key) {
}
});

public PrometheusQmqMetricRegistry() {
DynamicConfig config = DynamicConfigLoader.load("qmq.prometheus.properties", false);
String type = config.getString("monitor.type", "prometheus");
if ("prometheus".equals(type)) {
String action = config.getString("monitor.action", "pull");
if ("pull".equals(action)) {
try {
HTTPServer server = new HTTPServer(config.getInt("monitor.port", 3333));
} catch (IOException e) {
e.printStackTrace();
}
}
} else if ("graphite".equals(type)) {
String host = config.getString("graphite.host");
int port = config.getInt("graphite.port");
Graphite graphite = new Graphite(host, port);
graphite.start(CollectorRegistry.defaultRegistry, 60);
}

}

@SuppressWarnings("unchecked")
private static <M extends Collector> M cacheFor(Key<M> key) {
return (M) CACHE.getUnchecked(key);
Expand Down

0 comments on commit d223170

Please sign in to comment.