JAvaとprometheusを組み合わせたカスタムデータモニタリング
22944 ワード
一、prometheusの構成
prometheus.yml
二、監視応用
構想相関依存 を導入監視指標露出endpoint を配置するカスタムモニタリング指標 キーコード
1.依存関係の導入
pom.xml
2.監視指標を’/metrics’に暴露する
PrometheusConfig.java
3.カスタム監視指標
MyMetrics.java
三、監視応用
構想相関依存 を導入 prometheus APIを呼び出してデータを取得する データを整理して に戻る
キーコード
1.依存関係の導入
pom.xml
2.prometheus APIを呼び出す
ある時点のデータを取得
期間のデータの取得
詳細については、Prometheus-クエリーを参照してください.
3.処理データ
prometheusは乱順の結果を返す場合があります.以下のコードはタイムスタンプでソートできます.
prometheus.yml
...
- job_name: 'my-service'
metrics_path: /metrics
static_configs:
- targets: ['xxx.xxx.xxx.xxx:yyyy'] // url
...
二、監視応用
構想
1.依存関係の導入
pom.xml
<dependency>
<groupId>io.prometheusgroupId>
<artifactId>simpleclientartifactId>
<version>0.3.0version>
dependency>
<dependency>
<groupId>io.prometheusgroupId>
<artifactId>simpleclient_hotspotartifactId>
<version>0.3.0version>
dependency>
<dependency>
<groupId>io.prometheusgroupId>
<artifactId>simpleclient_servletartifactId>
<version>0.3.0version>
dependency>
2.監視指標を’/metrics’に暴露する
PrometheusConfig.java
import io.prometheus.client.exporter.MetricsServlet;
import io.prometheus.client.hotspot.DefaultExports;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
...
@Configuration
public class PrometheusConfig {
// ...
@Bean
public ServletRegistrationBean servletRegistrationBean(){
DefaultExports.initialize();
return new ServletRegistrationBean(new MetricsServlet(), "/metrics");
}
}
3.カスタム監視指標
MyMetrics.java
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
...
// counter
static final Counter customizeCounter = Counter.build()
.name("customize_counter") // ,
.help("customize counter") //
.register();
customizeCounter.inc(); // 1
// gauge
static final Gauge customizeGauge = Gauge.build()
.name("customize_gauge")
.help("customize gauge")
.labelNames("label1", "label2", "label3") // ,
.register();
customizeGauge.inc(); // 1
customizeGauge.dec(); // 1
customizeGauge.labels("value1","value2","value3").set(1100); // ,
// histogram summary
三、監視応用
構想
キーコード
1.依存関係の導入
pom.xml
<dependency>
<groupId>io.prometheusgroupId>
<artifactId>simpleclientartifactId>
<version>0.3.0version>
dependency>
<dependency>
<groupId>io.prometheusgroupId>
<artifactId>simpleclient_hotspotartifactId>
<version>0.3.0version>
dependency>
<dependency>
<groupId>io.prometheusgroupId>
<artifactId>simpleclient_servletartifactId>
<version>0.3.0version>
dependency>
2.prometheus APIを呼び出す
ある時点のデータを取得
String query = "customize_counter";
String url = "http://[ip]:[port]/api/v1/query?query=" + query + "&time=2019-05-01T20:10:51.781Z";
HttpGet get = new HttpGet(url);
CloseableHttpClient httpClient = HttpClients.custom().build();
CloseableHttpResponse response = httpClient.execute(get);
期間のデータの取得
String query = "rate(customize_counter{label1 = value1}[30s])";
String url = "http://[ip]:[port]/api/v1/query_range?query=" + query + "&start=2019-05-01T20:10:51.781Z&end=2019-05-02T20:10:51.781Z";
HttpGet get = new HttpGet(url);
CloseableHttpClient httpClient = HttpClients.custom().build();
CloseableHttpResponse response = httpClient.execute(get);
詳細については、Prometheus-クエリーを参照してください.
3.処理データ
prometheusは乱順の結果を返す場合があります.以下のコードはタイムスタンプでソートできます.
public class ComparatorPromData implements Comparator {
@Override
public int compare(Object o1, Object o2) {
List list1 = (List)o1;
List list2 = (List)o2;
return ((Integer) list1.get(0)).compareTo(((Integer) list2.get(0)));
}
}
public class SortUtil {
public static List sortPromData(List<List> list) {
ComparatorPromData comparator = new ComparatorPromData();
Collections.sort(list, comparator);
return list;
}
}