摘要:前言實際項目中,提供接口,對接公司的監(jiān)控系統(tǒng),增加服務(wù)的可觀察性,是一個基本的要求。在中集成,非常簡單。由上可知,我們可以訪問獲取格式的。在層,增加然后具體的邏輯中,直接使用。最后重新啟動項目,再次訪問可看到自定義的已經(jīng)存在了。
前言
實際項目中,提供metrics接口,對接公司的監(jiān)控系統(tǒng),增加服務(wù)的可觀察性,是一個基本的要求。在spring boot 1.X 中集成prometheus metrics,非常簡單。但是spring boot 2.X 頗費周折。因為prometheus官方提供的prometheus-client-java不兼容spring boot 2.X 。需要借助micrometer。
步驟1:引入所需的包
在pom.xml文件中增加如下:
org.springframework.boot spring-boot-starter-actuator io.micrometer micrometer-core io.micrometer micrometer-registry-prometheus
2: 增加相關(guān)配置
在 application.yml中增加如下設(shè)置:
management: endpoints: web: exposure: include: ["metrics","prometheus"] endpoint: metrics: enabled: true prometheus: enabled: true metrics: export: prometheus: enabled: true
PS: 如果想獲取其他的metrics,可以設(shè)置include: ["*"]
3:運行查看metrics
運行項目,訪問 http://localhost:8090/actuator,可看到如下:
{"_links":{"self":{"href":"http://localhost:8090/actuator","templated":false},"prometheus":{"href":"http://localhost:8090/actuator/prometheus","templated":false},"metrics-requiredMetricName":{"href":"http://localhost:8090/actuator/metrics/{requiredMetricName}","templated":true},"metrics":{"href":"http://localhost:8090/actuator/metrics","templated":false}}}
PS:注意我的項目端口是8090。
由上可知,我們可以訪問 http://localhost:8090/actuator/prometheus 獲取prometheus格式的metrics。
具體如下:
# HELP system_load_average_1m The sum of the number of runnable entities queued to available processors and the number of runnable entities running on the available processors averaged over a period of time # TYPE system_load_average_1m gauge system_load_average_1m 3.12939453125 # HELP system_cpu_count The number of processors available to the Java virtual machine # TYPE system_cpu_count gauge system_cpu_count 8.0 # HELP system_cpu_usage The "recent cpu usage" for the whole system # TYPE system_cpu_usage gauge system_cpu_usage 0.11287867482465304 # HELP jvm_gc_pause_seconds Time spent in GC pause # TYPE jvm_gc_pause_seconds summary jvm_gc_pause_seconds_count{action="end of minor GC",cause="Allocation Failure",} 1.0 jvm_gc_pause_seconds_sum{action="end of minor GC",cause="Allocation Failure",} 0.014 # HELP jvm_gc_pause_seconds_max Time spent in GC pause # TYPE jvm_gc_pause_seconds_max gauge jvm_gc_pause_seconds_max{action="end of minor GC",cause="Allocation Failure",} 0.014 # HELP process_cpu_usage The "recent cpu usage" for the Java Virtual Machine process # TYPE process_cpu_usage gauge process_cpu_usage 2.803742769828689E-4 # HELP jvm_gc_memory_allocated_bytes_total Incremented for an increase in the size of the young generation memory pool after one GC to before the next # TYPE jvm_gc_memory_allocated_bytes_total counter jvm_gc_memory_allocated_bytes_total 1.73539328E8 # HELP process_uptime_seconds The uptime of the Java virtual machine # TYPE process_uptime_seconds gauge process_uptime_seconds 175.835 # HELP tomcat_sessions_active_current_sessions # TYPE tomcat_sessions_active_current_sessions gauge tomcat_sessions_active_current_sessions 0.0 # HELP tomcat_global_received_bytes_total # TYPE tomcat_global_received_bytes_total counter tomcat_global_received_bytes_total{name="http-nio-8090",} 0.0 # HELP tomcat_global_error_total # TYPE tomcat_global_error_total counter tomcat_global_error_total{name="http-nio-8090",} 0.0 # HELP tomcat_threads_current_threads # TYPE tomcat_threads_current_threads gauge tomcat_threads_current_threads{name="http-nio-8090",} 10.0 # HELP jvm_memory_committed_bytes The amount of memory in bytes that is committed for the Java virtual machine to use # TYPE jvm_memory_committed_bytes gauge jvm_memory_committed_bytes{area="heap",id="PS Survivor Space",} 1.8874368E7 jvm_memory_committed_bytes{area="heap",id="PS Old Gen",} 1.63053568E8 jvm_memory_committed_bytes{area="heap",id="PS Eden Space",} 1.73539328E8 jvm_memory_committed_bytes{area="nonheap",id="Metaspace",} 5.505024E7 jvm_memory_committed_bytes{area="nonheap",id="Code Cache",} 1.114112E7 jvm_memory_committed_bytes{area="nonheap",id="Compressed Class Space",} 7602176.0 # HELP tomcat_sessions_expired_sessions_total # TYPE tomcat_sessions_expired_sessions_total counter tomcat_sessions_expired_sessions_total 0.0 # HELP tomcat_sessions_rejected_sessions_total # TYPE tomcat_sessions_rejected_sessions_total counter tomcat_sessions_rejected_sessions_total 0.0 # HELP jvm_threads_states_threads The current number of threads having NEW state # TYPE jvm_threads_states_threads gauge jvm_threads_states_threads{state="runnable",} 11.0 jvm_threads_states_threads{state="blocked",} 0.0 jvm_threads_states_threads{state="waiting",} 13.0 jvm_threads_states_threads{state="timed-waiting",} 5.0 jvm_threads_states_threads{state="new",} 0.0 jvm_threads_states_threads{state="terminated",} 0.0 # HELP tomcat_sessions_alive_max_seconds # TYPE tomcat_sessions_alive_max_seconds gauge tomcat_sessions_alive_max_seconds 0.0 # HELP jvm_threads_live_threads The current number of live threads including both daemon and non-daemon threads # TYPE jvm_threads_live_threads gauge jvm_threads_live_threads 29.0 # HELP tomcat_global_sent_bytes_total # TYPE tomcat_global_sent_bytes_total counter tomcat_global_sent_bytes_total{name="http-nio-8090",} 9114.0 # HELP jvm_gc_max_data_size_bytes Max size of old generation memory pool # TYPE jvm_gc_max_data_size_bytes gauge jvm_gc_max_data_size_bytes 0.0 # HELP jvm_memory_max_bytes The maximum amount of memory in bytes that can be used for memory management # TYPE jvm_memory_max_bytes gauge jvm_memory_max_bytes{area="heap",id="PS Survivor Space",} 1.8874368E7 jvm_memory_max_bytes{area="heap",id="PS Old Gen",} 2.863661056E9 jvm_memory_max_bytes{area="heap",id="PS Eden Space",} 1.392508928E9 jvm_memory_max_bytes{area="nonheap",id="Metaspace",} -1.0 jvm_memory_max_bytes{area="nonheap",id="Code Cache",} 2.5165824E8 jvm_memory_max_bytes{area="nonheap",id="Compressed Class Space",} 1.073741824E9 # HELP process_files_open_files The open file descriptor count # TYPE process_files_open_files gauge process_files_open_files 142.0 # HELP tomcat_sessions_active_max_sessions # TYPE tomcat_sessions_active_max_sessions gauge tomcat_sessions_active_max_sessions 0.0 # HELP jvm_threads_daemon_threads The current number of live daemon threads # TYPE jvm_threads_daemon_threads gauge jvm_threads_daemon_threads 25.0 # HELP tomcat_threads_config_max_threads # TYPE tomcat_threads_config_max_threads gauge tomcat_threads_config_max_threads{name="http-nio-8090",} 200.0 # HELP jvm_buffer_count_buffers An estimate of the number of buffers in the pool # TYPE jvm_buffer_count_buffers gauge jvm_buffer_count_buffers{id="direct",} 5.0 jvm_buffer_count_buffers{id="mapped",} 0.0 # HELP jvm_gc_memory_promoted_bytes_total Count of positive increases in the size of the old generation memory pool before GC to after GC # TYPE jvm_gc_memory_promoted_bytes_total counter jvm_gc_memory_promoted_bytes_total 8192.0 # HELP logback_events_total Number of error level events that made it to the logs # TYPE logback_events_total counter logback_events_total{level="warn",} 0.0 logback_events_total{level="debug",} 0.0 logback_events_total{level="error",} 0.0 logback_events_total{level="trace",} 0.0 logback_events_total{level="info",} 77.0 # HELP jvm_gc_live_data_size_bytes Size of old generation memory pool after a full GC # TYPE jvm_gc_live_data_size_bytes gauge jvm_gc_live_data_size_bytes 0.0 # HELP tomcat_global_request_seconds # TYPE tomcat_global_request_seconds summary tomcat_global_request_seconds_count{name="http-nio-8090",} 2.0 tomcat_global_request_seconds_sum{name="http-nio-8090",} 0.131 # HELP jvm_memory_used_bytes The amount of used memory # TYPE jvm_memory_used_bytes gauge jvm_memory_used_bytes{area="heap",id="PS Survivor Space",} 1.4836336E7 jvm_memory_used_bytes{area="heap",id="PS Old Gen",} 2.4336488E7 jvm_memory_used_bytes{area="heap",id="PS Eden Space",} 1.60499616E8 jvm_memory_used_bytes{area="nonheap",id="Metaspace",} 5.22724E7 jvm_memory_used_bytes{area="nonheap",id="Code Cache",} 1.0880512E7 jvm_memory_used_bytes{area="nonheap",id="Compressed Class Space",} 7006832.0 # HELP http_server_requests_seconds # TYPE http_server_requests_seconds summary http_server_requests_seconds_count{exception="None",method="GET",outcome="SUCCESS",status="200",uri="/actuator/prometheus",} 1.0 http_server_requests_seconds_sum{exception="None",method="GET",outcome="SUCCESS",status="200",uri="/actuator/prometheus",} 0.071661037 http_server_requests_seconds_count{exception="None",method="GET",outcome="SUCCESS",status="200",uri="/actuator",} 1.0 http_server_requests_seconds_sum{exception="None",method="GET",outcome="SUCCESS",status="200",uri="/actuator",} 0.026864224 # HELP http_server_requests_seconds_max # TYPE http_server_requests_seconds_max gauge http_server_requests_seconds_max{exception="None",method="GET",outcome="SUCCESS",status="200",uri="/actuator/prometheus",} 0.071661037 http_server_requests_seconds_max{exception="None",method="GET",outcome="SUCCESS",status="200",uri="/actuator",} 0.026864224 # HELP jvm_buffer_memory_used_bytes An estimate of the memory that the Java virtual machine is using for this buffer pool # TYPE jvm_buffer_memory_used_bytes gauge jvm_buffer_memory_used_bytes{id="direct",} 40960.0 jvm_buffer_memory_used_bytes{id="mapped",} 0.0 # HELP process_start_time_seconds Start time of the process since unix epoch. # TYPE process_start_time_seconds gauge process_start_time_seconds 1.556615678449E9 # HELP tomcat_threads_busy_threads # TYPE tomcat_threads_busy_threads gauge tomcat_threads_busy_threads{name="http-nio-8090",} 1.0 # HELP jvm_threads_peak_threads The peak live thread count since the Java virtual machine started or peak was reset # TYPE jvm_threads_peak_threads gauge jvm_threads_peak_threads 36.0 # HELP jvm_classes_loaded_classes The number of classes that are currently loaded in the Java virtual machine # TYPE jvm_classes_loaded_classes gauge jvm_classes_loaded_classes 10376.0 # HELP tomcat_sessions_created_sessions_total # TYPE tomcat_sessions_created_sessions_total counter tomcat_sessions_created_sessions_total 0.0 # HELP jvm_buffer_total_capacity_bytes An estimate of the total capacity of the buffers in this pool # TYPE jvm_buffer_total_capacity_bytes gauge jvm_buffer_total_capacity_bytes{id="direct",} 40960.0 jvm_buffer_total_capacity_bytes{id="mapped",} 0.0 # HELP tomcat_global_request_max_seconds # TYPE tomcat_global_request_max_seconds gauge tomcat_global_request_max_seconds{name="http-nio-8090",} 0.103 # HELP jvm_classes_unloaded_classes_total The total number of classes unloaded since the Java virtual machine has started execution # TYPE jvm_classes_unloaded_classes_total counter jvm_classes_unloaded_classes_total 1.0 # HELP process_files_max_files The maximum file descriptor count # TYPE process_files_max_files gauge process_files_max_files 10240.0
4: 自定義自己的metrics
在service層,編寫具體的MetricsService,如下:
import com.scmp.scmpnotify.service.MetricsService; import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.stereotype.Service; @Service public class MetricsService { private final Counter sendSuccessCounter; private final Counter sendFaileCounter; MetricsServiceImpl(MeterRegistry registry) { this.sendFaileCounter = Counter.builder("send_faile") .description("send faile email total").register(registry); this.sendSuccessCounter = Counter.builder("send_success") .description("send success email total").register(registry); } public void sendSuccessIncrement(){ sendSuccessCounter.increment(); } public void sendFaileIncrement(){ sendFaileCounter.increment(); } }
這里我們定義了兩個counter 變量,分別統(tǒng)計發(fā)送成功和失敗的消息數(shù)。
在controller層,
增加
@Autowired private MetricsService metricsService;
然后具體的邏輯中,直接使用 metricsService.sendSuccessIncrement()。
最后重新啟動項目,再次訪問:http://localhost:8090/actuator/prometheus 可看到自定義的metrics已經(jīng)存在了。
如下:
... # HELP send_success_total send success email total # TYPE send_success_total counter send_success_total 0.0 # HELP send_faile_total send faile email total # TYPE send_faile_total counter send_faile_total 0.0 ...
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/74365.html
摘要:簡介中文名稱為普羅米修斯,受啟發(fā)于的監(jiān)控系統(tǒng),從年開始由前工程師在以開源軟件的形式進(jìn)行研發(fā),年月發(fā)布版本。拉的代表,主要代表就是,讓我們不用擔(dān)心監(jiān)控應(yīng)用本身的狀態(tài)。這樣的一個程序稱為,的實例稱為一個。 Prometheus 簡介 Prometheus 中文名稱為普羅米修斯,受啟發(fā)于Google的Brogmon監(jiān)控系統(tǒng),從2012年開始由前Google工程師在Soundcloud以開源軟...
摘要:可簡單地認(rèn)為它是的擴(kuò)展,負(fù)載均衡自然成為不可或缺的特性。是基于開發(fā)的服務(wù)代理組件,在使用場景中,它與和整合,打造具備服務(wù)動態(tài)更新和負(fù)載均衡能力的服務(wù)網(wǎng)關(guān)。類似的特性在項目也有體現(xiàn),它是另一種高性能代理的方案,提供服務(wù)發(fā)現(xiàn)健康和負(fù)載均衡。 摘要: Cloud Native 應(yīng)用架構(gòu)隨著云技術(shù)的發(fā)展受到業(yè)界特別重視和關(guān)注,尤其是 CNCF(Cloud Native Computing Fo...
摘要:可簡單地認(rèn)為它是的擴(kuò)展,負(fù)載均衡自然成為不可或缺的特性。類似的特性在項目也有體現(xiàn),它是另一種高性能代理的方案,提供服務(wù)發(fā)現(xiàn)健康和負(fù)載均衡。 Dubbo Cloud Native 實踐與思考 分享簡介 Cloud Native 應(yīng)用架構(gòu)隨著云技術(shù)的發(fā)展受到業(yè)界特別重視和關(guān)注,尤其是 CNCF(Cloud Native Computing Foundation)項目蓬勃發(fā)展之際。Dubbo...
摘要:比如定義了基礎(chǔ)的數(shù)據(jù)類型以及對應(yīng)的方法收集事件次數(shù)等單調(diào)遞增的數(shù)據(jù)收集當(dāng)前的狀態(tài),比如數(shù)據(jù)庫連接數(shù)收集隨機(jī)正態(tài)分布數(shù)據(jù),比如響應(yīng)延遲收集隨機(jī)正態(tài)分布數(shù)據(jù),和是類似的庫的詳細(xì)解析可以參考本文為容器監(jiān)控實踐系列文章,完整內(nèi)容見 概述 Prometheus從2016年加入CNCF,到2018年8月畢業(yè),現(xiàn)在已經(jīng)成為Kubernetes的官方監(jiān)控方案,接下來的幾篇文章將詳細(xì)解讀Promethu...
摘要:比如定義了基礎(chǔ)的數(shù)據(jù)類型以及對應(yīng)的方法收集事件次數(shù)等單調(diào)遞增的數(shù)據(jù)收集當(dāng)前的狀態(tài),比如數(shù)據(jù)庫連接數(shù)收集隨機(jī)正態(tài)分布數(shù)據(jù),比如響應(yīng)延遲收集隨機(jī)正態(tài)分布數(shù)據(jù),和是類似的庫的詳細(xì)解析可以參考本文為容器監(jiān)控實踐系列文章,完整內(nèi)容見 概述 Prometheus從2016年加入CNCF,到2018年8月畢業(yè),現(xiàn)在已經(jīng)成為Kubernetes的官方監(jiān)控方案,接下來的幾篇文章將詳細(xì)解讀Promethu...
閱讀 3328·2023-04-26 00:07
閱讀 3922·2021-11-23 10:08
閱讀 2940·2021-11-22 09:34
閱讀 859·2021-09-22 15:27
閱讀 1749·2019-08-30 15:54
閱讀 3743·2019-08-30 14:07
閱讀 913·2019-08-30 11:12
閱讀 678·2019-08-29 18:44