基於Grafana和Prometheus的監視系統(3):java客戶端使用
基於Grafana和Prometheus的監視系統(3):java客戶端使用
0.基本說明
- 如何在程式碼中進行指標埋點: prometheus java client的使用
- 如何生成jvm 指標資料: hotspot expoter的使用
- 在spring boot 中進行指標採集
- 非指標資料的採集: Elasticsearch的使用
1. java client的使用
[https://github.com/prometheus/client_java]
使用方式和log4j的使用很相似
<!-- The client -->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.4.0</version>
</dependency>
<!-- Hotspot JVM metrics-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>0.4.0</version>
</dependency>
<!-- Exposition HTTPServer-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_httpserver</artifactId>
<version>0.4.0</version>
</dependency>
<!-- Pushgateway exposition-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_pushgateway</artifactId>
<version>0.4.0</version>
</dependency>
class A {
static CollectorRegistry registry = new CollectorRegistry();
// 請求總數統計
static final Counter counterRequest = Counter.build().name("TsHistoryData_RequestTotal").help("xx").
labelNames("request").register(registry);
// 請求時間
static final Gauge costTime = Gauge.build().name("TsHistoryData_CostTime").help("xx").
labelNames("id").register(registry);
// 直方圖, 統計在某個bucket時間的請求的個數.(linearBuckets | exponentialBuckets)
static final Histogram requestLatency_hgm = Histogram.build()
.name("TsHistoryData_RequestLatency_hgm").exponentialBuckets(0.5,2,10).help("Request latency in seconds.").register(registry);
static final Summary requestLatency_suy = Summary.build()
.name("TsHistoryData_RequestLatency_suy").
quantile(0.1, 0.01).
quantile(0.3, 0.01).
quantile(0.5,0.01).
quantile(0.7, 0.01).
quantile(0.9, 0.01).
quantile(0.95, 0.01).help("Request latency in seconds.").register(registry);
void function1() {
String requestId = System.currentTimeMillis() + "";
counterRequest.labels("request").inc();
Gauge.Timer costTimeTimer = costTime.labels(requestId).startTimer();
Histogram.Timer requestTimer = requestLatency_hgm.startTimer();
Summary.Timer requestTimer_suy = requestLatency_suy.startTimer();
......
costTimeTimer.setDuration();
requestTimer.observeDuration();
requestTimer_suy.observeDuration();
Monitor.pushMetricToPushGateway(registry, "TS_HISTORY_DATA");
}
}
2. hotspot expoter
對於程式執行時的jvm指標進行監控
public static void jvmExport() throws Exception {
DefaultExports.initialize();
InetSocketAddress address = new InetSocketAddress("192.168.1.222", 9201);
Server server = new Server(address);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
server.setHandler(context);
context.addServlet(new ServletHolder(new MetricsServlet()), "/metrics");
server.start();
// server.join();
}
3.在spring boot中的使用
1.新增Maven的依賴
<!-- Hotspot JVM metrics-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>0.4.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.4.0</version>
</dependency>
<!-- Exposition servlet -->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_servlet</artifactId>
<version>0.4.0</version>
</dependency>
- 啟用Prometheus Metrics Endpoint
新增註解@EnablePrometheusEndpoint啟用Prometheus Endpoint,這裡同時使用了simpleclient_hotspot中提供的DefaultExporter,該Exporter展示當前應用JVM的相關資訊
@SpringBootApplication
@EnablePrometheusEndpoint
public class CoreApplication extends WebMvcConfigurerAdapter implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(CoreApplication.class);
springApplication.run(args);
}
@Override
public void run(String... strings) throws Exception {
DefaultExports.initialize();
}
}
向外暴露指標的介面
@Configuration
public class MonitoringConfig {
@Bean
ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new MetricsServlet(), "/metrics");
}
}
這樣訪問 localhost:8080/metrics 可以看到jvm相關的指標.
3.新增攔截器,為監控埋點做準備
除了獲取應用JVM相關的狀態以外,我們還可能需要新增一些自定義的監控Metrics實現對系統效能,以及業務狀態進行採集,以提供日後優化的相關支撐資料。首先我們使用攔截器處理對應用的所有請求。
繼承WebMvcConfigurerAdapter類,複寫addInterceptors方法,對所有請求/**新增攔截器
@SpringBootApplication
@EnablePrometheusEndpoint
public class CoreApplication extends WebMvcConfigurerAdapter implements CommandLineRunner {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new PrometheusMetricsInterceptor()).addPathPatterns("/**");
}
}
PrometheusMetricsInterceptor整合HandlerInterceptorAdapter,通過複寫父方法,實現對請求處理前/處理完成的處理。
@Component
public class PrometheusMetricsInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return super.preHandle(request, response, handler);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
super.afterCompletion(request, response, handler, ex);
}
}
4.自定義指標
@Component
public class PrometheusMetricsInterceptor extends HandlerInterceptorAdapter {
static final Counter requestCounter = Counter.build()
.name("module_core_http_requests_total").labelNames("path", "method", "code")
.help("Total requests.").register();
static final Gauge inprogressRequests = Gauge.build()
.name("module_core_http_inprogress_requests").labelNames("path", "method", "code")
.help("Inprogress requests.").register();
static final Gauge requestTime = Gauge.build()
.name("module_core_http_requests_costTime").labelNames("path", "method", "code")
.help("requests cost time.").register();
static final Histogram requestLatencyHistogram = Histogram.build().labelNames("path", "method", "code")
.name("module_core_http_requests_latency_seconds_histogram").help("Request latency in seconds.")
.register();
static final Summary requestLatency = Summary.build()
.name("module_core_http_requests_latency_seconds_summary")
.quantile(0.5, 0.05)
.quantile(0.9, 0.01)
.labelNames("path", "method", "code")
.help("Request latency in seconds.").register();
private Histogram.Timer histogramRequestTimer;
private Summary.Timer summaryTimer;
private Gauge.Timer gaugeTimer;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String requestURI = request.getRequestURI();
String method = request.getMethod();
int status = response.getStatus();
inprogressRequests.labels(requestURI, method, String.valueOf(status)).inc();
histogramRequestTimer = requestLatencyHistogram.labels(requestURI, method, String.valueOf(status)).startTimer();
summaryTimer = requestLatency.labels(requestURI, method, String.valueOf(status)).startTimer();
gaugeTimer = requestTime.labels(requestURI, method, String.valueOf(status)).startTimer();
return super.preHandle(request, response, handler);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
String requestURI = request.getRequestURI();
String method = request.getMethod();
int status = response.getStatus();
requestCounter.labels(requestURI, method, String.valueOf(status)).inc();
inprogressRequests.labels(requestURI, method, String.valueOf(status)).dec();
histogramRequestTimer.observeDuration();
summaryTimer.observeDuration();
gaugeTimer.setDuration();
super.afterCompletion(request, response, handler, ex);
}
}
5.使用Collector暴露業務指標
除了在攔截器中使用Prometheus提供的Counter,Summary,Gauage等構造監控指標以外,我們還可以通過自定義的Collector實現對相關業務指標的暴露
@SpringBootApplication
@EnablePrometheusEndpoint
public class CoreApplication extends WebMvcConfigurerAdapter implements CommandLineRunner {
@Autowired
private CustomExporter customExporter;
...省略的程式碼
@Override
public void run(String... args) throws Exception {
...省略的程式碼
customExporter.register();
}
}
CustomExporter整合自io.prometheus.client.Collector,在呼叫Collector的register()方法後,當訪問/metrics時,則會自動從Collector的collection()方法中獲取採集到的監控指標。
由於這裡CustomExporter存在於Spring的IOC容器當中,這裡可以直接訪問業務程式碼,返回需要的業務相關的指標。
@Component
public class CustomExporter extends Collector {
@Override
public List<MetricFamilySamples> collect() {
List<MetricFamilySamples> mfs = new ArrayList<>();
GaugeMetricFamily labeledGauge =
new GaugeMetricFamily("module_core_custom_metrics", "custom metrics", Collections.singletonList("labelname"));
labeledGauge.addMetric(Collections.singletonList("labelvalue"), 1);
mfs.add(labeledGauge);
return mfs;
}
}
4. ES的使用
docker compose [https://www.elastic.co/guide/en/elasticsearch/reference/5.6/docker.html]
java client
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.0.0</version>
</dependency>
public class EsConfig {
private static RestHighLevelClient client;
public static RestHighLevelClient init() throws Exception{
client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("192.168.1.223", 9200, "http")
));
return client;
}
public static void close() throws Exception{
client.close();
}
}
try {
client = EsConfig.init();
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("Id", requestId);
jsonMap.put("Date", new Date());
jsonMap.put("Value", tt);
IndexRequest indexRequest = new IndexRequest("ts_data_history", "doc", requestId)
.source(jsonMap);
IndexResponse indexResponse = client.index(indexRequest);
client.close();
} catch (Exception e) {
e.printStackTrace();
}
相關文章
- Java服務端監控:Prometheus與Grafana的整合Java服務端PrometheusGrafana
- 使用Prometheus和Grafana進行系統監控和預測 - flightawarePrometheusGrafana
- SSH Exporter:基於Prometheus的遠端系統效能監控神器ExportPrometheus
- 基於java的客戶關係管理系統Java
- 基於 Prometheus 的監控系統實踐Prometheus
- 基於Prometheus+Grafana監控Laravel+Swoole應用PrometheusGrafanaLaravel
- 使用Prometheus和Grafana監控Spring Boot應用PrometheusGrafanaSpring Boot
- Prometheus+Grafana+Alertmanager搭建全方位的監控告警系統PrometheusGrafana
- 使用Prometheus、Grafana監控Artifactory實踐PrometheusGrafana
- grafana+prometheus快速搭建MySql監控系統實踐GrafanaPrometheusMySql
- docker-compose 搭建 Prometheus+Grafana監控系統DockerPrometheusGrafana
- 3 個很酷的基於文字的郵件客戶端客戶端
- docker-compose快速搭建 Prometheus+Grafana監控系統DockerPrometheusGrafana
- Prometheus MySQL監控+grafana展示PrometheusMySqlGrafana
- prometheus+grafana 監控nginxPrometheusGrafanaNginx
- 配置 Prometheus 伺服器監控和 Grafana 看板Prometheus伺服器Grafana
- 容器編排系統K8s之Prometheus監控系統+Grafana部署K8SPrometheusGrafana
- 基於CMPP2.0的Socket客戶端(Java) - JAVA程式語言客戶端Java
- SpringCloud使用Prometheus監控(基於Eureka)SpringGCCloudPrometheus
- docker部署監控Prometheus+GrafanaDockerPrometheusGrafana
- Prometheus + Grafana 監控平臺搭建PrometheusGrafana
- Prometheus 監控Mysql伺服器及Grafana視覺化PrometheusMySql伺服器Grafana視覺化
- linux視覺化監控:Grafana+Prometheus+node_exporterLinux視覺化GrafanaPrometheusExport
- 使用java語言基於SMTP協議手寫郵件客戶端Java協議客戶端
- [Hyperf] 在 Hyperf 框架中使用 prometheus + grafana 部署基本的監控框架PrometheusGrafana
- Prometheus+Grafana視覺化監控SpringBoot專案PrometheusGrafana視覺化Spring Boot
- Linux系統中KafKa安裝和使用方法 java客戶端連線kafkaLinuxKafkaJava客戶端
- Grafana、Prometheus、mtail-日誌監控GrafanaPrometheusAI
- Grafana+Prometheus 監控 MySql服務GrafanaPrometheusMySql
- [Prometheus+Grafana系列]基於docker-compose搭建PrometheusGrafanaDocker
- 使用 Grafana、collectd 和 InfluxDB 打造現代監控系統GrafanaUX
- [jaeger] 二、客戶端使用 (Java版本)客戶端Java
- Elasticsearch及java客戶端jest使用ElasticsearchJava客戶端
- 基於WebSocket的modbus通訊(二)- 客戶端Web客戶端
- 在k8s中快速搭建基於Prometheus監控系統K8SPrometheus
- powerVM客戶端系統盤克隆客戶端
- [轉載] 使用Redis的Java客戶端JedisRedisJava客戶端
- zookeeper Java客戶端API的使用方法Java客戶端API