spring-boot-micrometer.html 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <meta name="generator" content="Asciidoctor 2.0.15">
  8. <meta name="author" content="pxzxj, pudge.zxj@gmail.com, 2021/10/29">
  9. <title>使用Micrometer记录Java应用指标</title>
  10. <link rel="stylesheet" href="css/site.css">
  11. <link href="css/custom.css" rel="stylesheet">
  12. <script src="js/setup.js"></script><script defer src="js/site.js"></script>
  13. </head>
  14. <body class="article toc2 toc-left"><div id="banner-container" class="container" role="banner">
  15. <div id="banner" class="contained" role="banner">
  16. <div id="switch-theme">
  17. <input type="checkbox" id="switch-theme-checkbox" />
  18. <label for="switch-theme-checkbox">Dark Theme</label>
  19. </div>
  20. </div>
  21. </div>
  22. <div id="tocbar-container" class="container" role="navigation">
  23. <div id="tocbar" class="contained" role="navigation">
  24. <button id="toggle-toc"></button>
  25. </div>
  26. </div>
  27. <div id="main-container" class="container">
  28. <div id="main" class="contained">
  29. <div id="doc" class="doc">
  30. <div id="header">
  31. <h1>使用Micrometer记录Java应用指标</h1>
  32. <div class="details">
  33. <span id="author" class="author">pxzxj</span><br>
  34. <span id="author2" class="author">pudge.zxj@gmail.com</span><br>
  35. <span id="author3" class="author">2021/10/29</span><br>
  36. </div>
  37. <div id="toc" class="toc2">
  38. <div id="toctitle">Table of Contents</div>
  39. <span id="back-to-index"><a href="index.html">Back to index</a></span><ul class="sectlevel1">
  40. <li><a href="#_observability">1. Observability</a></li>
  41. <li><a href="#_micrometer">2. Micrometer</a>
  42. <ul class="sectlevel2">
  43. <li><a href="#_支持的监控软件">2.1. 支持的监控软件</a>
  44. <ul class="sectlevel3">
  45. <li><a href="#_基于指标格式分类">2.1.1. 基于指标格式分类</a></li>
  46. <li><a href="#_基于采集方式分类">2.1.2. 基于采集方式分类</a></li>
  47. </ul>
  48. </li>
  49. <li><a href="#_术语">2.2. 术语</a>
  50. <ul class="sectlevel3">
  51. <li><a href="#_meter">2.2.1. Meter</a></li>
  52. <li><a href="#_meterregistry">2.2.2. MeterRegistry</a></li>
  53. </ul>
  54. </li>
  55. <li><a href="#_examples">2.3. Examples</a>
  56. <ul class="sectlevel3">
  57. <li><a href="#_counter_timer">2.3.1. Counter &amp; Timer</a></li>
  58. <li><a href="#_compositeregistry_loggingregistry">2.3.2. CompositeRegistry &amp; LoggingRegistry</a></li>
  59. <li><a href="#_tags_commonstags">2.3.3. Tags &amp; CommonsTags</a></li>
  60. <li><a href="#_gauge">2.3.4. Gauge</a></li>
  61. </ul>
  62. </li>
  63. <li><a href="#_最佳实践">2.4. 最佳实践</a>
  64. <ul class="sectlevel3">
  65. <li><a href="#_避免指标数量过多">2.4.1. 避免指标数量过多</a></li>
  66. <li><a href="#_使用meterfilter降噪">2.4.2. 使用MeterFilter降噪</a></li>
  67. </ul>
  68. </li>
  69. </ul>
  70. </li>
  71. <li><a href="#_spring_boot_micrometer">3. Spring Boot <span class="image"><img src="images/heart.png" alt="25" width="25"></span> Micrometer</a>
  72. <ul class="sectlevel2">
  73. <li><a href="#autowired-mr">3.1. Autowired MeterRegistry</a></li>
  74. <li><a href="#_metrics_endpoint">3.2. Metrics Endpoint</a></li>
  75. <li><a href="#_resttemplate">3.3. RestTemplate</a></li>
  76. <li><a href="#_meterbinder">3.4. MeterBinder</a></li>
  77. <li><a href="#_meterfilter">3.5. MeterFilter</a></li>
  78. <li><a href="#_common_tags">3.6. Common Tags</a></li>
  79. <li><a href="#_healthinfo">3.7. HealthInfo</a></li>
  80. </ul>
  81. </li>
  82. <li><a href="#_prometheus_grafana">4. Prometheus &amp; Grafana</a></li>
  83. </ul>
  84. </div>
  85. </div>
  86. <div id="content">
  87. <div id="preamble">
  88. <div class="sectionbody">
  89. <div class="paragraph">
  90. <p>本文根据SpringOne 2019的演讲Performance Monitoring Backend and Frontend using Micrometer整理而成,英语能力不错的建议直接观看下面的原始视频</p>
  91. </div>
  92. <div class="videoblock"><div class="content">
  93. <iframe width="640" height="480" src="https://player.bilibili.com/player.html?bvid=BV1jQ4y1q7uC&high_quality=1&page=1" border="0" frameborder="no" framespacing="0" scrolling="no" allowfullscreen="true"></iframe>
  94. </div></div>
  95. <div class="paragraph">
  96. <p><a href="slides/SpringOne2019-ClintChecketts-PerformanceMonitoringBackendandFrontendUsingMicrometer.pdf">PPT</a> <a href="https://github.com/checketts/micrometer-springone-2019">代码</a></p>
  97. </div>
  98. </div>
  99. </div>
  100. <div class="sect1">
  101. <h2 id="_observability"><a class="anchor" href="#_observability"></a>1. Observability</h2>
  102. <div class="sectionbody">
  103. <div class="paragraph">
  104. <p>监控告警是软件系统尤其是对可用性要求高的软件系统的重要组成部分,通过监控告警可以防患于未然,将故障对业务系统的影响降到最低<br>
  105. 通常监控需要包含三部分内容日志、指标、跟踪</p>
  106. </div>
  107. <div class="dlist">
  108. <dl>
  109. <dt class="hdlist1"><strong>日志(Logging)</strong> </dt>
  110. <dd>
  111. <p>日志记录了所有业务操作的详细信息,对日常问题定位起着至关重要的左右。日志记录通常使用日志框架Slf4j、Log4j、Logback实现,小应用直接使用日志文件即可无需考虑其它存储方式,而对中大型的应用或者微服务场景中一般使用ELFK的方案存储日志</p>
  112. </dd>
  113. <dt class="hdlist1"><strong>指标(Metrics)</strong> </dt>
  114. <dd>
  115. <p>指标是对某一业务数据的统计或聚合例如常见的CPU利用率、接口访问量等,相比日志指标是更直观的数据,基于指标可以快速发现系统存在在的问题,指标一般也会与告警系统一起使用使运维人员能在问题出现时立刻收到通知<br>
  116. 指标统计可以使用Micrometer实现,Micrometer的使用也是本文的主要内容,指标的结果一般使用时序数据库进行存储常见的如InfluxDB、Prometheus等</p>
  117. </dd>
  118. <dt class="hdlist1"><strong>跟踪(Traceing)</strong> </dt>
  119. <dd>
  120. <p>跟踪通常只在比较复杂的业务系统例如一个业务操作需要调用不同的应用程序完成的场景中使用,通过traceId可以将这些不同的调用关联起来进行分析<br>
  121. Zipkin可以用来实现应用跟踪</p>
  122. </dd>
  123. </dl>
  124. </div>
  125. </div>
  126. </div>
  127. <div class="sect1">
  128. <h2 id="_micrometer"><a class="anchor" href="#_micrometer"></a>2. Micrometer</h2>
  129. <div class="sectionbody">
  130. <div class="paragraph">
  131. <p>Micrometer用于在JVM应用中实现指标统计功能,它的最大特点是使用了类似Slf4j门面模式的设计,使开发者无需关注指标的存储实现,直接使用统一的API记录即可,开发完成后可以选择Micrometer支持的任意一种或多种存储系统,正如使用Slf4j记录的日志既可以使用Log4j的实现也可以使用Logback的实现</p>
  132. </div>
  133. <div class="sect2">
  134. <h3 id="_支持的监控软件"><a class="anchor" href="#_支持的监控软件"></a>2.1. 支持的监控软件</h3>
  135. <div class="paragraph">
  136. <p>Micrometer支持众多监控软件,这些软件一般会通过下面两种方式进行分类</p>
  137. </div>
  138. <div class="sect3">
  139. <h4 id="_基于指标格式分类"><a class="anchor" href="#_基于指标格式分类"></a>2.1.1. 基于指标格式分类</h4>
  140. <div class="paragraph">
  141. <p>指标格式有基于维度的(Dimensional)和基于层级的(Hierarchical)两种,Dimensional指标是由一个名称和多个Tag组成,每个Tag是一个键值对,Hierarchical指标则只有一个名称,所有信息都压扁保存在名称中<br></p>
  142. </div>
  143. <div class="exampleblock">
  144. <div class="title">Hierarchical</div>
  145. <div class="content">
  146. <div class="literalblock">
  147. <div class="content">
  148. <pre>server1.http.requests = 10
  149. us-east.blue.server1.http.requests.200.users = 10</pre>
  150. </div>
  151. </div>
  152. </div>
  153. </div>
  154. <div class="exampleblock">
  155. <div class="title">Dimensional</div>
  156. <div class="content">
  157. <div class="literalblock">
  158. <div class="content">
  159. <pre>http_requests{server="server1"} 10
  160. http_requests{server="server1", region="us-east", cluster="blue", status="200", uri="users"} 10</pre>
  161. </div>
  162. </div>
  163. </div>
  164. </div>
  165. <div class="paragraph">
  166. <p>从上面的示例可以看出基于维度的指标有两个优点,首先是意义更清晰,它的每个维度都是一个key-value格式的数据,通过维度信息可以很明确看出指标的意义,而基于层级的只有value而没有key,所以不容易理解;另一个优点是它更灵活便于修改,指标维度变化时可以直接修改而不破坏原来的结构</p>
  167. </div>
  168. <table class="tableblock frame-all grid-all stretch">
  169. <colgroup>
  170. <col style="width: 50%;">
  171. <col style="width: 50%;">
  172. </colgroup>
  173. <thead>
  174. <tr>
  175. <th class="tableblock halign-left valign-top">Dimensional</th>
  176. <th class="tableblock halign-left valign-top">Hierarchical</th>
  177. </tr>
  178. </thead>
  179. <tbody>
  180. <tr>
  181. <td class="tableblock halign-left valign-top"><p class="tableblock">AppOptics, Atlas, Azure Monitor, Cloudwatch, Datadog, Datadog StatsD, Dynatrace, Elastic, Humio, Influx, KairosDB, New Relic, Prometheus, SignalFx, Sysdig StatsD, Telegraf StatsD, Wavefront</p></td>
  182. <td class="tableblock halign-left valign-top"><p class="tableblock">Graphite, Ganglia, JMX, Etsy StatsD</p></td>
  183. </tr>
  184. </tbody>
  185. </table>
  186. </div>
  187. <div class="sect3">
  188. <h4 id="_基于采集方式分类"><a class="anchor" href="#_基于采集方式分类"></a>2.1.2. 基于采集方式分类</h4>
  189. <div class="paragraph">
  190. <p>采集方式有Client push和Server poll两种方式,不管哪种方式都是周期执行</p>
  191. </div>
  192. <table class="tableblock frame-all grid-all stretch">
  193. <colgroup>
  194. <col style="width: 50%;">
  195. <col style="width: 50%;">
  196. </colgroup>
  197. <thead>
  198. <tr>
  199. <th class="tableblock halign-left valign-top">Client pushes</th>
  200. <th class="tableblock halign-left valign-top">Server polls</th>
  201. </tr>
  202. </thead>
  203. <tbody>
  204. <tr>
  205. <td class="tableblock halign-left valign-top"><p class="tableblock">AppOptics, Atlas, Azure Monitor, Datadog, Elastic, Graphite, Ganglia, Humio, Influx, JMX, Kairos, New Relic, SignalFx, Wavefront</p></td>
  206. <td class="tableblock halign-left valign-top"><p class="tableblock">Prometheus, all StatsD flavors</p></td>
  207. </tr>
  208. </tbody>
  209. </table>
  210. </div>
  211. </div>
  212. <div class="sect2">
  213. <h3 id="_术语"><a class="anchor" href="#_术语"></a>2.2. 术语</h3>
  214. <div class="sect3">
  215. <h4 id="_meter"><a class="anchor" href="#_meter"></a>2.2.1. Meter</h4>
  216. <div class="paragraph">
  217. <p>Meter表示一个指标,新增业务指标时首先需要确定它的类型,Micrometer支持下面几种类型</p>
  218. </div>
  219. <div class="ulist">
  220. <ul>
  221. <li>
  222. <p>Counter:计数器,用于保存单调递增型的数据,例如站点的访问次数,JVM的GC次数等;不能为负值,也不支持减少,但可以重置为0</p>
  223. </li>
  224. <li>
  225. <p>Gauge:仪表盘,用于存储有着起伏特征的数据,例如堆内存的大小,注意能用Counter记录的指标不要用Guage</p>
  226. </li>
  227. <li>
  228. <p>Timer:计时器,记录事件的次数和总时间,例如HTTP请求消耗的时间,Timer同时也会包含次数统计,不需要再使用Counter</p>
  229. </li>
  230. <li>
  231. <p>Distribution Summaries: 用于跟踪事件的分布,与Timer结构类似,但值的单位可以是自定义的任意单位</p>
  232. </li>
  233. </ul>
  234. </div>
  235. <div class="paragraph">
  236. <p>确定类型后要为指标取一个合适的名称并添加标签(Tag),名称最好由小写字母和点组成例如http.request.count,标签是key-value格式的数据,key-value都是字符串,key最好也只包含小写字母和点,一个指标可以包含多个Tag,最终的指标形式如下</p>
  237. </div>
  238. <div class="exampleblock">
  239. <div class="content">
  240. <div class="paragraph">
  241. <p>cpu.usage {"host"="192.168.3.1"}<br>
  242. cpu.usage {"host"="192.168.3.2"}</p>
  243. </div>
  244. </div>
  245. </div>
  246. <div class="paragraph">
  247. <p>名称和标签唯一确定了一个指标,上面的示例表示示两台主机的cpu利用率,host值不同就是两个不同的指标</p>
  248. </div>
  249. <div class="paragraph">
  250. <p>其它指标相关内容 <a href="https://micrometer.io/docs/concepts">官方文档</a></p>
  251. </div>
  252. </div>
  253. <div class="sect3">
  254. <h4 id="_meterregistry"><a class="anchor" href="#_meterregistry"></a>2.2.2. MeterRegistry</h4>
  255. <div class="paragraph">
  256. <p>MeterRegistry代表指标的存储,每种监控软件都有对应的MeterRegistry实现</p>
  257. </div>
  258. </div>
  259. </div>
  260. <div class="sect2">
  261. <h3 id="_examples"><a class="anchor" href="#_examples"></a>2.3. Examples</h3>
  262. <div class="sect3">
  263. <h4 id="_counter_timer"><a class="anchor" href="#_counter_timer"></a>2.3.1. Counter &amp; Timer</h4>
  264. <div class="exampleblock">
  265. <div class="content">
  266. <div class="listingblock">
  267. <div class="content">
  268. <pre class="highlight"><code class="language-java" data-lang="java"><span class="fold-block">package io.github;
  269. </span><span class="fold-block hide-when-folded">import io.micrometer.core.instrument.*;
  270. import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
  271. import io.micrometer.core.instrument.config.MeterFilter;
  272. import io.micrometer.core.instrument.logging.LoggingMeterRegistry;
  273. import io.micrometer.core.instrument.logging.LoggingRegistryConfig;
  274. import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
  275. import org.junit.jupiter.api.Test;
  276. import java.time.Duration;
  277. import java.util.ArrayList;
  278. import java.util.Arrays;
  279. import java.util.List;
  280. import java.util.concurrent.TimeUnit;
  281. import java.util.stream.Collectors;
  282. </span><span class="fold-block">public class MicrometerTest {
  283. private List&lt;Chore&gt; chores = Arrays.asList(
  284. new Chore("Mow front lawn", Duration.ofMinutes(20), "yard"),
  285. new Chore("Mow back lawn", Duration.ofMinutes(10), "yard"),
  286. new Chore("Gather the laundry", Duration.ofMinutes(7), "laundry"),
  287. new Chore("Wash the laundry", Duration.ofMinutes(3), "laundry"),
  288. new Chore("Sort/Fold the laundry", Duration.ofMinutes(50), "laundry"),
  289. new Chore("Was the dishes", Duration.ofMinutes(10), "kitchen"),
  290. new Chore("Find my phone charger", Duration.ofMinutes(5))
  291. );
  292. @Test
  293. void testCounterAndTimer() {
  294. MeterRegistry meterRegistry = new SimpleMeterRegistry(); <i class="conum" data-value="1"></i><b>(1)</b>
  295. for(Chore chore : chores) {
  296. System.out.println("Doing " + chore.getName());
  297. meterRegistry.counter("chore.completed").increment(); <i class="conum" data-value="2"></i><b>(2)</b>
  298. meterRegistry.timer("chore.duration").record(chore.getDuration()); <i class="conum" data-value="3"></i><b>(3)</b>
  299. }
  300. for(Meter meter : meterRegistry.getMeters()) {
  301. System.out.println(meter.getId() + " " + meter.measure());
  302. }
  303. }
  304. static class Chore {
  305. private String name;
  306. private Duration duration;
  307. private String group;
  308. public Chore(String name, Duration duration, String group) {
  309. this.name = name;
  310. this.duration = duration;
  311. this.group = group;
  312. }
  313. public Chore(String name, Duration duration) {
  314. this.name = name;
  315. this.duration = duration;
  316. this.group = "home";
  317. }
  318. //getter, setter
  319. }
  320. }
  321. </span></code></pre>
  322. </div>
  323. </div>
  324. <div class="colist arabic">
  325. <table>
  326. <tr>
  327. <td><i class="conum" data-value="1"></i><b>1</b></td>
  328. <td><code>SimpleMeterRegistry</code> 可以用来测试Micrometer的功能,</td>
  329. </tr>
  330. <tr>
  331. <td><i class="conum" data-value="2"></i><b>2</b></td>
  332. <td><code>MeterRegistry</code> 的 <code>counter()</code> 方法用来创建Counter类型指标,<code>Counter.increment()</code> 方法表示该指标值加1</td>
  333. </tr>
  334. <tr>
  335. <td><i class="conum" data-value="3"></i><b>3</b></td>
  336. <td><code>MeterRegistry</code> 的 <code>timer()</code> 方法用来创建Counter类型指标,<code>Timer.record()</code> 方法记录事件耗时</td>
  337. </tr>
  338. </table>
  339. </div>
  340. </div>
  341. </div>
  342. <div class="admonitionblock tip">
  343. <table>
  344. <tr>
  345. <td class="icon">
  346. <i class="fa icon-tip" title="Tip"></i>
  347. </td>
  348. <td class="content">
  349. 可以在 <a href="https://github.com/pxzxj/micrometer-demo">GitHub</a> 下载示例源码
  350. </td>
  351. </tr>
  352. </table>
  353. </div>
  354. </div>
  355. <div class="sect3">
  356. <h4 id="_compositeregistry_loggingregistry"><a class="anchor" href="#_compositeregistry_loggingregistry"></a>2.3.2. CompositeRegistry &amp; LoggingRegistry</h4>
  357. <div class="exampleblock">
  358. <div class="content">
  359. <div class="listingblock">
  360. <div class="content">
  361. <pre class="highlight"><code class="language-java" data-lang="java">public class MicrometerTest {
  362. @Test
  363. void testCompositeMeterRegistryAndLoggingMeterRegistry() throws InterruptedException {
  364. CompositeMeterRegistry meterRegistry = Metrics.globalRegistry; // <i class="conum" data-value="1"></i><b>(1)</b>
  365. LoggingRegistryConfig loggingRegistryConfig = new LoggingRegistryConfig() {
  366. @Override
  367. public String get(String s) {
  368. return null;
  369. }
  370. @Override
  371. public boolean logInactive() {
  372. return true;
  373. }
  374. @Override
  375. public Duration step() {
  376. return Duration.ofSeconds(5);
  377. }
  378. }; <i class="conum" data-value="2"></i><b>(2)</b>
  379. MeterRegistry loggingRegistry = new LoggingMeterRegistry(loggingRegistryConfig, Clock.SYSTEM);
  380. meterRegistry.add(loggingRegistry);
  381. meterRegistry.add(new SimpleMeterRegistry());
  382. for(Chore chore : chores) {
  383. System.out.println("Doing " + chore.getName());
  384. meterRegistry.counter("chore.completed").increment();
  385. meterRegistry.timer("chore.duration").record(chore.getDuration());
  386. }
  387. for(Meter meter : meterRegistry.getMeters()) {
  388. System.out.println(meter.getId() + " " + meter.measure());
  389. }
  390. for(int i = 1; i &lt; 100; i++) { <i class="conum" data-value="3"></i><b>(3)</b>
  391. TimeUnit.SECONDS.sleep(1);
  392. System.out.println("Waiting " + i);
  393. }
  394. }
  395. }
  396. </code></pre>
  397. </div>
  398. </div>
  399. <div class="colist arabic">
  400. <table>
  401. <tr>
  402. <td><i class="conum" data-value="1"></i><b>1</b></td>
  403. <td>可以使用 <code>Metrics.globalRegistry</code> 也可以使用 <code>new CompositeMeterRegistry()</code></td>
  404. </tr>
  405. <tr>
  406. <td><i class="conum" data-value="2"></i><b>2</b></td>
  407. <td>设置日志每5秒推送一次</td>
  408. </tr>
  409. <tr>
  410. <td><i class="conum" data-value="3"></i><b>3</b></td>
  411. <td>等100s为了观察 `LoggingMeterRegistry`的效果</td>
  412. </tr>
  413. </table>
  414. </div>
  415. </div>
  416. </div>
  417. </div>
  418. <div class="sect3">
  419. <h4 id="_tags_commonstags"><a class="anchor" href="#_tags_commonstags"></a>2.3.3. Tags &amp; CommonsTags</h4>
  420. <div class="exampleblock">
  421. <div class="content">
  422. <div class="listingblock">
  423. <div class="content">
  424. <pre class="highlight"><code class="language-java" data-lang="java">public class MicrometerTest {
  425. @Test
  426. void testTagsAndCommonTags() throws InterruptedException {
  427. MeterRegistry meterRegistry = new SimpleMeterRegistry();
  428. meterRegistry.config().commonTags("team", "spring"); // <i class="conum" data-value="1"></i><b>(1)</b>
  429. for(Chore chore : chores) {
  430. System.out.println("Doing " + chore.getName());
  431. meterRegistry.counter("chore.completed").increment();
  432. meterRegistry.timer("chore.duration", Tags.of("group", chore.getGroup())).record(chore.getDuration()); <i class="conum" data-value="2"></i><b>(2)</b>
  433. }
  434. for(Meter meter : meterRegistry.getMeters()) {
  435. System.out.println(meter.getId() + " " + meter.measure());
  436. }
  437. }
  438. }
  439. </code></pre>
  440. </div>
  441. </div>
  442. <div class="colist arabic">
  443. <table>
  444. <tr>
  445. <td><i class="conum" data-value="1"></i><b>1</b></td>
  446. <td>添加commonsTags,commonsTag就是对所有指标都生效的Tag</td>
  447. </tr>
  448. <tr>
  449. <td><i class="conum" data-value="2"></i><b>2</b></td>
  450. <td>使用 两个参数的 <code>timer()</code> 方法为Timer指标添加Tag</td>
  451. </tr>
  452. </table>
  453. </div>
  454. </div>
  455. </div>
  456. </div>
  457. <div class="sect3">
  458. <h4 id="_gauge"><a class="anchor" href="#_gauge"></a>2.3.4. Gauge</h4>
  459. <div class="exampleblock">
  460. <div class="content">
  461. <div class="listingblock">
  462. <div class="content">
  463. <pre class="highlight"><code class="language-java" data-lang="java">public class MicrometerTest {
  464. @Test
  465. void testGauge() throws InterruptedException {
  466. CompositeMeterRegistry meterRegistry = Metrics.globalRegistry;
  467. LoggingRegistryConfig loggingRegistryConfig = new LoggingRegistryConfig() {
  468. @Override
  469. public String get(String s) {
  470. return null;
  471. }
  472. @Override
  473. public boolean logInactive() {
  474. return true;
  475. }
  476. @Override
  477. public Duration step() {
  478. return Duration.ofSeconds(5);
  479. }
  480. };
  481. MeterRegistry loggingRegistry = new LoggingMeterRegistry(loggingRegistryConfig, Clock.SYSTEM);
  482. meterRegistry.add(loggingRegistry);
  483. meterRegistry.add(new SimpleMeterRegistry());
  484. meterRegistry.config().commonTags("team", "spring");
  485. addGauge(meterRegistry);
  486. for(Chore chore : chores) {
  487. System.out.println("Doing " + chore.getName());
  488. meterRegistry.counter("chore.completed").increment();
  489. meterRegistry.timer("chore.duration", Tags.of("group", chore.getGroup())).record(chore.getDuration());
  490. }
  491. for(Meter meter : meterRegistry.getMeters()) {
  492. System.out.println(meter.getId() + " " + meter.measure());
  493. }
  494. System.gc();
  495. for(int i = 1; i &lt; 100; i++) {
  496. TimeUnit.SECONDS.sleep(1);
  497. System.out.println("Waiting " + i);
  498. }
  499. }
  500. void addGauge(MeterRegistry meterRegistry) {
  501. List&lt;Chore&gt; choresList = new ArrayList&lt;&gt;(chores);
  502. meterRegistry.gauge("chore.size.weak", choresList, List::size); // <i class="conum" data-value="1"></i><b>(1)</b>
  503. meterRegistry.gauge("chore.size.lambda", "", o -&gt; choresList.size()); // <i class="conum" data-value="2"></i><b>(2)</b>
  504. Gauge.builder("chore.size.strong", choresList, List::size).strongReference(true).register(meterRegistry); // <i class="conum" data-value="3"></i><b>(3)</b>
  505. }
  506. }
  507. </code></pre>
  508. </div>
  509. </div>
  510. <div class="colist arabic">
  511. <table>
  512. <tr>
  513. <td><i class="conum" data-value="1"></i><b>1</b></td>
  514. <td>Gauge默认使用弱引用,可能出现值为NaN,演示演示效果时需要注释掉下面两行</td>
  515. </tr>
  516. <tr>
  517. <td><i class="conum" data-value="2"></i><b>2</b></td>
  518. <td>使用Lambda表达式解决弱引用问题</td>
  519. </tr>
  520. <tr>
  521. <td><i class="conum" data-value="3"></i><b>3</b></td>
  522. <td>使用强引用</td>
  523. </tr>
  524. </table>
  525. </div>
  526. </div>
  527. </div>
  528. </div>
  529. </div>
  530. <div class="sect2">
  531. <h3 id="_最佳实践"><a class="anchor" href="#_最佳实践"></a>2.4. 最佳实践</h3>
  532. <div class="sect3">
  533. <h4 id="_避免指标数量过多"><a class="anchor" href="#_避免指标数量过多"></a>2.4.1. 避免指标数量过多</h4>
  534. <div class="paragraph">
  535. <p>在使用Micrometer时要注意指标数量,不要出现数量爆炸(Cardinality Explosion)</p>
  536. </div>
  537. <div class="paragraph">
  538. <p>下面是一个典型的示例,有个查询用户的接口 <code>/user/{id}</code> ,新增了一个指标 <code>http_request</code> 记录接口调用量,如果把每次用户请求的url作为一个Tag去记录指标那么最终该接口会出现无数个指标,合理的方式是用 <code>/user/{id}</code> 作为Tag</p>
  539. </div>
  540. <div class="imageblock">
  541. <div class="content">
  542. <img src="images/cardinality-explosion.png" alt="cardinality explosion">
  543. </div>
  544. </div>
  545. </div>
  546. <div class="sect3">
  547. <h4 id="_使用meterfilter降噪"><a class="anchor" href="#_使用meterfilter降噪"></a>2.4.2. 使用MeterFilter降噪</h4>
  548. <div class="paragraph">
  549. <p>解决指标数量爆炸的另一种方式是MeterFilter,它能够重写指标的Tag甚至是直接忽略指标</p>
  550. </div>
  551. <div class="exampleblock">
  552. <div class="content">
  553. <div class="listingblock">
  554. <div class="content">
  555. <pre class="highlight"><code class="language-java" data-lang="java">public class MicrometerTest {
  556. @Test
  557. void testMeterFilter() throws InterruptedException {
  558. MeterRegistry meterRegistry = new SimpleMeterRegistry();
  559. meterRegistry.config().meterFilter(MeterFilter.deny(id -&gt; id.getName().equals("chore.completed"))); // <i class="conum" data-value="1"></i><b>(1)</b>
  560. meterRegistry.config().meterFilter(MeterFilter.maximumAllowableMetrics(2)); // <i class="conum" data-value="2"></i><b>(2)</b>
  561. meterRegistry.config().meterFilter(new MeterFilter() { // <i class="conum" data-value="3"></i><b>(3)</b>
  562. @Override
  563. public Meter.Id map(Meter.Id id) {
  564. if(id.getName().equals("chore.duration")) {
  565. return id.replaceTags(id.getTags().stream().map(tag -&gt; {
  566. if(tag.getKey().equals("group") &amp;&amp; tag.getValue().equals("laundry")) {
  567. return tag;
  568. } else {
  569. return Tag.of("group", "other");
  570. }
  571. }).collect(Collectors.toList()));
  572. } else {
  573. return id;
  574. }
  575. }
  576. });
  577. meterRegistry.config().commonTags("team", "spring");
  578. for(Chore chore : chores) {
  579. System.out.println("Doing " + chore.getName());
  580. meterRegistry.counter("chore.completed").increment();
  581. meterRegistry.timer("chore.duration", Tags.of("group", chore.getGroup())).record(chore.getDuration());
  582. }
  583. for(Meter meter : meterRegistry.getMeters()) {
  584. System.out.println(meter.getId() + " " + meter.measure());
  585. }
  586. }
  587. }
  588. </code></pre>
  589. </div>
  590. </div>
  591. <div class="colist arabic">
  592. <table>
  593. <tr>
  594. <td><i class="conum" data-value="1"></i><b>1</b></td>
  595. <td>deny()方法用于屏蔽部分指标</td>
  596. </tr>
  597. <tr>
  598. <td><i class="conum" data-value="2"></i><b>2</b></td>
  599. <td>maximumAllowableMetrics()方法设置最大指标数量,超出此数量的指标会直接忽略</td>
  600. </tr>
  601. <tr>
  602. <td><i class="conum" data-value="3"></i><b>3</b></td>
  603. <td>map()方法可以转换指标的Tag</td>
  604. </tr>
  605. </table>
  606. </div>
  607. </div>
  608. </div>
  609. <div class="paragraph">
  610. <p>MeterFilter还有更多用法可以自行查看其API</p>
  611. </div>
  612. </div>
  613. </div>
  614. </div>
  615. </div>
  616. <div class="sect1">
  617. <h2 id="_spring_boot_micrometer"><a class="anchor" href="#_spring_boot_micrometer"></a>3. Spring Boot <span class="image"><img src="images/heart.png" alt="25" width="25"></span> Micrometer</h2>
  618. <div class="sectionbody">
  619. <div class="paragraph">
  620. <p>Spring Boot的Actuator模块提供了与Micrometer的整合,因此在Spring Boot中使用Micrometer会更简单</p>
  621. </div>
  622. <div class="listingblock">
  623. <div class="content">
  624. <pre class="highlight"><code class="language-xml" data-lang="xml"> &lt;dependency&gt;
  625. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  626. &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
  627. &lt;/dependency&gt;</code></pre>
  628. </div>
  629. </div>
  630. <div class="sect2">
  631. <h3 id="autowired-mr"><a class="anchor" href="#autowired-mr"></a>3.1. Autowired MeterRegistry</h3>
  632. <div class="paragraph">
  633. <p>Spring Boot自动配置了一个 <code>CompositeMeterRegistry</code> ,因此应用代码中无需再创建,可以直接使用依赖注入,下面是一个构造器注入的示例</p>
  634. </div>
  635. <div class="exampleblock">
  636. <div class="content">
  637. <div class="listingblock">
  638. <div class="content">
  639. <pre class="highlight"><code class="language-java" data-lang="java"><span class="fold-block">package io.github.controller;
  640. </span><span class="fold-block hide-when-folded">import io.micrometer.core.instrument.Counter;
  641. import io.micrometer.core.instrument.Meter;
  642. import io.micrometer.core.instrument.MeterRegistry;
  643. import io.micrometer.core.instrument.Tags;
  644. import org.springframework.web.bind.annotation.GetMapping;
  645. import org.springframework.web.bind.annotation.RestController;
  646. </span><span class="fold-block">@RestController
  647. public class HelloController {
  648. private Counter counter;
  649. public HelloController(MeterRegistry meterRegistry) {
  650. this.counter = meterRegistry.counter("demo.http.requests.total", Tags.of("uri", "/hello"));
  651. }
  652. @GetMapping("/hello")
  653. public String hello() {
  654. counter.increment();
  655. return "Hello Micrometer!";
  656. }
  657. }
  658. </span></code></pre>
  659. </div>
  660. </div>
  661. </div>
  662. </div>
  663. <div class="paragraph">
  664. <p>还可以使用 <code>MeterRegistryCustomizer</code> 对Spring自动配置的 <code>MeterRegistry</code> 做更多配置</p>
  665. </div>
  666. <div class="exampleblock">
  667. <div class="content">
  668. <div class="listingblock">
  669. <div class="content">
  670. <pre class="highlight"><code class="language-java" data-lang="java">@Configuration
  671. public class MicrometerConfig {
  672. @Bean
  673. public MeterRegistryCustomizer&lt;MeterRegistry&gt; meterRegistryCustomizer() {
  674. return registry -&gt; registry.config().commonTags("team", "spring");
  675. }
  676. }
  677. </code></pre>
  678. </div>
  679. </div>
  680. </div>
  681. </div>
  682. </div>
  683. <div class="sect2">
  684. <h3 id="_metrics_endpoint"><a class="anchor" href="#_metrics_endpoint"></a>3.2. Metrics Endpoint</h3>
  685. <div class="paragraph">
  686. <p>Actuator提供了/metrics端点用于查看指标的值,首先需要暴露此端点</p>
  687. </div>
  688. <div class="listingblock primary">
  689. <div class="title">Properties</div>
  690. <div class="content">
  691. <pre class="highlight"><code class="language-properties" data-lang="properties">management.endpoints.web.exposure.include=health,metrics,prometheus</code></pre>
  692. </div>
  693. </div>
  694. <div class="listingblock secondary">
  695. <div class="title">Yaml</div>
  696. <div class="content">
  697. <pre class="highlight"><code class="language-yaml" data-lang="yaml">management:
  698. endpoints:
  699. web:
  700. exposure:
  701. include: health,metrics,prometheus</code></pre>
  702. </div>
  703. </div>
  704. <div class="paragraph">
  705. <p>浏览器访问/actuator/metrics就可以看到所有的指标</p>
  706. </div>
  707. <div class="imageblock">
  708. <div class="content">
  709. <img src="images/meters-endpoint.jpg" alt="meters endpoint">
  710. </div>
  711. </div>
  712. <div class="paragraph">
  713. <p>可以看到除了上一步添加的 <code>demo.http.requests.total</code> 指标外还有许多其它指标,这些都是Spring Boot默认提供的,实际上这里只是一部分默认指标,完整的可以参考 <a href="https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#actuator.metrics.supported">官方文档</a> 进行查看</p>
  714. </div>
  715. <div class="paragraph">
  716. <p>/metrics后还可以添加特定指标名称查看此指标的值,还可以使用tag参数做进一步过滤,tag参数格式为 <code>tag={key}:{value}</code></p>
  717. </div>
  718. <div class="imageblock">
  719. <div class="content">
  720. <img src="images/specific-metrics.jpg" alt="specific metrics">
  721. </div>
  722. </div>
  723. </div>
  724. <div class="sect2">
  725. <h3 id="_resttemplate"><a class="anchor" href="#_resttemplate"></a>3.3. RestTemplate</h3>
  726. <div class="paragraph">
  727. <p>Spring Boot自动配置 <code>RestTemplateBuilder</code> 已经添加了指标统计的功能,使用它创建的 <code>RestTemplate</code> 会使用一个名称为 <code>http.client.requests</code> 的Timer指标记录请求的时延,但要注意接口调用时要使用UriTemplate的形式,否则会出现上文提到的数量爆炸问题</p>
  728. </div>
  729. <div class="exampleblock">
  730. <div class="content">
  731. <div class="listingblock">
  732. <div class="content">
  733. <pre class="highlight"><code class="language-java" data-lang="java">@RestController
  734. public class HelloController {
  735. private RestTemplate restTemplate;
  736. public HelloController(RestTemplateBuilder builder) {
  737. this.restTemplate = builder.build();
  738. }
  739. @GetMapping("/restwithuritemplate")
  740. public Map&lt;String, String&gt; restWithUriTemplate(String suffix) {
  741. return Collections.singletonMap("html", restTemplate.getForObject("https://tieba.baidu.com/{suffix}", String.class, suffix));
  742. }
  743. @GetMapping("/restwithouturitemplate")
  744. public Map&lt;String, String&gt; restWithoutUriTemplate(String suffix) {
  745. return Collections.singletonMap("html", restTemplate.getForObject("https://tieba.baidu.com/" + suffix, String.class));
  746. }
  747. }
  748. </code></pre>
  749. </div>
  750. </div>
  751. </div>
  752. </div>
  753. </div>
  754. <div class="sect2">
  755. <h3 id="_meterbinder"><a class="anchor" href="#_meterbinder"></a>3.4. MeterBinder</h3>
  756. <div class="paragraph">
  757. <p><a href="#autowired-mr">上文</a>的示例直接向Bean中注入 <code>MeterRegistry</code> 用来记录指标,这样对原代表有很强的侵入性,直接影响了原本的依赖关系,一种更好的方法是使用 <code>MeterBinder</code></p>
  758. </div>
  759. <div class="exampleblock">
  760. <div class="content">
  761. <div class="listingblock">
  762. <div class="content">
  763. <pre class="highlight"><code class="language-java" data-lang="java"><span class="fold-block hide-when-folded">import io.micrometer.core.instrument.Gauge;
  764. import io.micrometer.core.instrument.binder.MeterBinder;
  765. import org.springframework.context.annotation.Bean;
  766. </span><span class="fold-block">public class MyMeterBinderConfiguration {
  767. @Bean
  768. public MeterBinder queueSize(Queue queue) {
  769. return (registry) -&gt; Gauge.builder("queueSize", queue::size).register(registry);
  770. }
  771. }
  772. </span></code></pre>
  773. </div>
  774. </div>
  775. </div>
  776. </div>
  777. </div>
  778. <div class="sect2">
  779. <h3 id="_meterfilter"><a class="anchor" href="#_meterfilter"></a>3.5. MeterFilter</h3>
  780. <div class="paragraph">
  781. <p>Spring Boot应用中声明为Bean的MeterFilter会自动配置在MeterRegistry上</p>
  782. </div>
  783. <div class="listingblock">
  784. <div class="content">
  785. <pre class="highlight"><code class="language-java" data-lang="java"><span class="fold-block hide-when-folded">import io.micrometer.core.instrument.config.MeterFilter;
  786. import org.springframework.context.annotation.Bean;
  787. import org.springframework.context.annotation.Configuration;
  788. </span><span class="fold-block">@Configuration(proxyBeanMethods = false)
  789. public class MyMetricsFilterConfiguration {
  790. @Bean
  791. public MeterFilter renameRegionTagMeterFilter() {
  792. return MeterFilter.renameTag("com.example", "mytag.region", "mytag.area");
  793. }
  794. }
  795. </span></code></pre>
  796. </div>
  797. </div>
  798. </div>
  799. <div class="sect2">
  800. <h3 id="_common_tags"><a class="anchor" href="#_common_tags"></a>3.6. Common Tags</h3>
  801. <div class="paragraph">
  802. <p>Spring Boot应用可以在application.yml中配置CommonTag</p>
  803. </div>
  804. <div class="listingblock primary">
  805. <div class="title">Properties</div>
  806. <div class="content">
  807. <pre class="highlight"><code class="language-properties" data-lang="properties">management.metrics.tags.application=${spring.application.name}
  808. management.metrics.tags.country=cn</code></pre>
  809. </div>
  810. </div>
  811. <div class="listingblock secondary">
  812. <div class="title">Yaml</div>
  813. <div class="content">
  814. <pre class="highlight"><code class="language-yaml" data-lang="yaml">management:
  815. metrics:
  816. tags:
  817. application: ${spring.application.name}
  818. country: cn</code></pre>
  819. </div>
  820. </div>
  821. </div>
  822. <div class="sect2">
  823. <h3 id="_healthinfo"><a class="anchor" href="#_healthinfo"></a>3.7. HealthInfo</h3>
  824. <div class="paragraph">
  825. <p>Spring Boot 能够对应用本身及依赖的其它外部组件做简单的健康检查,例如Redis是否正常、磁盘空间是否正常等, <a href="https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#actuator.endpoints.health.auto-configured-health-indicators">所有</a>这些检查项都需要实现 <code>HealthIndicator</code> 接口,健康检查的结果通常只是简单的服务是否存活,不包含特别详细的指标信息</p>
  826. </div>
  827. <div class="listingblock">
  828. <div class="content">
  829. <pre class="highlight"><code class="language-java" data-lang="java">public interface HealthIndicator extends HealthContributor {
  830. /**
  831. * Return an indication of health.
  832. * @return the health
  833. */
  834. Health health();
  835. }
  836. </code></pre>
  837. </div>
  838. </div>
  839. <div class="paragraph">
  840. <p>监控检查的结果可以通过 <code>/health</code> 端点查看</p>
  841. </div>
  842. <div class="imageblock">
  843. <div class="content">
  844. <img src="images/health-endpoint.jpg" alt="health endpoint">
  845. </div>
  846. </div>
  847. <div class="paragraph">
  848. <p>在生产环境中监控检查的结果需要接入真实的监控系统从而实现服务故障时的告警通知,因此可以将健康检查的结果也转换为指标输出</p>
  849. </div>
  850. <div class="exampleblock">
  851. <div class="content">
  852. <div class="listingblock">
  853. <div class="content">
  854. <pre class="highlight"><code class="language-java" data-lang="java"><span class="fold-block">package io.github.controller;
  855. </span><span class="fold-block hide-when-folded">import io.micrometer.core.instrument.MeterRegistry;
  856. import io.micrometer.core.instrument.Tags;
  857. import io.micrometer.core.instrument.binder.MeterBinder;
  858. import org.springframework.beans.factory.InitializingBean;
  859. import org.springframework.boot.actuate.health.Health;
  860. import org.springframework.boot.actuate.health.HealthIndicator;
  861. import org.springframework.boot.actuate.health.Status;
  862. import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
  863. import org.springframework.stereotype.Component;
  864. import java.time.Duration;
  865. import java.util.Map;
  866. import java.util.concurrent.ConcurrentHashMap;
  867. </span><span class="fold-block">@Component
  868. public class HealthToMetricsConverter implements InitializingBean, MeterBinder {
  869. private Map&lt;String, HealthIndicator&gt; map;
  870. private ThreadPoolTaskScheduler scheduler;
  871. private final ConcurrentHashMap&lt;String, Health&gt; latestHealth = new ConcurrentHashMap&lt;&gt;();
  872. public HealthToMetricsConverter(Map&lt;String, HealthIndicator&gt; map) {
  873. this.map = map;
  874. this.scheduler = new ThreadPoolTaskScheduler();
  875. scheduler.setPoolSize(5);
  876. scheduler.setThreadNamePrefix("ThreadPoolTaskScheduler");
  877. scheduler.initialize();
  878. }
  879. @Override
  880. public void afterPropertiesSet() throws Exception {
  881. for(Map.Entry&lt;String, HealthIndicator&gt; entry : map.entrySet()) {
  882. scheduler.scheduleWithFixedDelay(() -&gt; latestHealth.put(entry.getKey(), entry.getValue().health()), Duration.ofSeconds(10)); <i class="conum" data-value="1"></i><b>(1)</b>
  883. }
  884. }
  885. @Override
  886. public void bindTo(MeterRegistry registry) {
  887. for(Map.Entry&lt;String, Health&gt; entry : latestHealth.entrySet()) {
  888. registry.gauge("health.indicator", Tags.of("name", entry.getKey()), entry.getValue(), health -&gt; {
  889. Status status = health.getStatus();
  890. double v = 3.0;
  891. if(status.equals(Status.UP)) { <i class="conum" data-value="2"></i><b>(2)</b>
  892. v = 1.0;
  893. } else if(status.equals(Status.DOWN)) {
  894. v = -1.0;
  895. } else if(status.equals(Status.OUT_OF_SERVICE)) {
  896. v = -2.0;
  897. }
  898. return v;
  899. });
  900. }
  901. }
  902. }
  903. </span></code></pre>
  904. </div>
  905. </div>
  906. <div class="colist arabic">
  907. <table>
  908. <tr>
  909. <td><i class="conum" data-value="1"></i><b>1</b></td>
  910. <td>健康检查可能是很慢的过程,而指标采集需要快速,因此使用线程池定期保存监控检查的结果</td>
  911. </tr>
  912. <tr>
  913. <td><i class="conum" data-value="2"></i><b>2</b></td>
  914. <td>指标的值必须是数字,因此将Status转为数字</td>
  915. </tr>
  916. </table>
  917. </div>
  918. </div>
  919. </div>
  920. </div>
  921. </div>
  922. </div>
  923. <div class="sect1">
  924. <h2 id="_prometheus_grafana"><a class="anchor" href="#_prometheus_grafana"></a>4. Prometheus &amp; Grafana</h2>
  925. <div class="sectionbody">
  926. <div class="paragraph">
  927. <p>Micrometer使用了门面模式,使用不同的监控系统只需要添加对应的依赖 <code>micrometer-registry-{system}</code> 即可,Prometheus对应如下依赖</p>
  928. </div>
  929. <div class="listingblock">
  930. <div class="content">
  931. <pre class="highlight"><code class="language-xml" data-lang="xml"> &lt;dependency&gt;
  932. &lt;groupId&gt;io.micrometer&lt;/groupId&gt;
  933. &lt;artifactId&gt;micrometer-registry-prometheus&lt;/artifactId&gt;
  934. &lt;/dependency&gt;</code></pre>
  935. </div>
  936. </div>
  937. <div class="admonitionblock tip">
  938. <table>
  939. <tr>
  940. <td class="icon">
  941. <i class="fa icon-tip" title="Tip"></i>
  942. </td>
  943. <td class="content">
  944. Prometheus的安装很简单,在官网下载安装包解压运行即可
  945. </td>
  946. </tr>
  947. </table>
  948. </div>
  949. <div class="paragraph">
  950. <p>Prometheus是使用pull的方式采集数据,Actuator模块会使用 <code>/prometheus</code> 端点暴露所有指标数据,因此在Prometheus的配置文件 <code>prometheus.yml</code> 中配置采集的目标和接口如下</p>
  951. </div>
  952. <div class="listingblock">
  953. <div class="content">
  954. <pre class="highlight"><code class="language-yaml" data-lang="yaml">scrape_configs:
  955. - job_name: "myapp"
  956. metrics_path: "/actuator/prometheus"
  957. static_configs:
  958. - targets: ["HOST:PORT"]</code></pre>
  959. </div>
  960. </div>
  961. <div class="admonitionblock note">
  962. <table>
  963. <tr>
  964. <td class="icon">
  965. <i class="fa icon-note" title="Note"></i>
  966. </td>
  967. <td class="content">
  968. 静态配置的方式实际上并不推荐,Prometheus支持使用服务发现的方式如Eureka、Zookeeper添加target,具体配置方式参考 <a href="https://prometheus.io/docs/prometheus/latest/configuration/configuration/">官方网站</a>
  969. </td>
  970. </tr>
  971. </table>
  972. </div>
  973. <div class="paragraph">
  974. <p>最后在Grafana中添加Prometheus作为数据源并添加Spring Boot仪表盘,就可以非常直观地查看所有指标数据了</p>
  975. </div>
  976. <div class="imageblock">
  977. <div class="content">
  978. <img src="images/grafana.jpg" alt="grafana">
  979. </div>
  980. </div>
  981. </div>
  982. </div>
  983. </div>
  984. <div id="footer">
  985. <div id="footer-text">
  986. Last updated 2024-03-18 05:44:42 UTC
  987. </div>
  988. </div>
  989. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.3/highlight.min.js"></script>
  990. <script>
  991. if (!hljs.initHighlighting.called) {
  992. hljs.initHighlighting.called = true
  993. ;[].slice.call(document.querySelectorAll('pre.highlight > code')).forEach(function (el) { hljs.highlightBlock(el) })
  994. }
  995. </script>
  996. <script src="https://utteranc.es/client.js"
  997. repo="pxzxj/articles"
  998. issue-term="title"
  999. label="utteranc"
  1000. theme="github-light"
  1001. crossorigin="anonymous"
  1002. async>
  1003. </script>
  1004. </div>
  1005. </div>
  1006. </div>
  1007. </body>
  1008. </html>