您好,登錄后才能下訂單哦!
本篇文章為大家展示了如何在在Prometheus 中利SpringBoot 實(shí)現(xiàn)一個(gè)指標(biāo)監(jiān)控功能,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
添加業(yè)務(wù)監(jiān)控指標(biāo)
在 spring-web-prometheus-demo 項(xiàng)目的基礎(chǔ)上,我們添加一個(gè) PrometheusCustomMonitor 類。在這里面我們定義了三個(gè)業(yè)務(wù)指標(biāo):
order_request_count:下單總次數(shù)
order_amount_sum:下單總金額
@Component public class PrometheusCustomMonitor { /** * 訂單發(fā)起次數(shù) */ private Counter orderCount; /** * 金額統(tǒng)計(jì) */ private DistributionSummary amountSum; private final MeterRegistry registry; @Autowired public PrometheusCustomMonitor(MeterRegistry registry) { this.registry = registry; } @PostConstruct private void init() { orderCount = registry.counter("order_request_count", "order", "test-svc"); amountSum = registry.summary("order_amount_sum", "orderAmount", "test-svc"); } public Counter getOrderCount() { return orderCount; } public DistributionSummary getAmountSum() { return amountSum; } }
這里我們新增一個(gè) TestController 類,去模擬現(xiàn)實(shí)的訂單數(shù)據(jù)。
后續(xù)應(yīng)用啟動(dòng)后,我們可以通過(guò) localhost:8080/order 去模擬用戶下單操作。
package com.chenshuyi.springwebprometheusdemo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.Random; @RestController public class TestController { @Resource private PrometheusCustomMonitor monitor; @RequestMapping("/order") public String order() throws Exception { // 統(tǒng)計(jì)下單次數(shù) monitor.getOrderCount().increment(); Random random = new Random(); int amount = random.nextInt(100); // 統(tǒng)計(jì)金額 monitor.getAmountSum().record(amount); return "下單成功, 金額: " + amount; } }
實(shí)際項(xiàng)目中,我們一般使用 AOP 的方式去實(shí)現(xiàn)業(yè)務(wù)指標(biāo)上報(bào)。這里為了簡(jiǎn)單,直接寫在代碼里了。
現(xiàn)在我們啟動(dòng)應(yīng)用,訪問(wèn) localhost:8080/order 可以成功模擬下單,每次都會(huì)有一個(gè)隨機(jī)的訂單金額產(chǎn)生。
此時(shí)我們?cè)L問(wèn) localhost:8080/actuator/prometheus 就可以看到對(duì)應(yīng)的指標(biāo)已經(jīng)存在。
后續(xù)我們?cè)?Grafana 中配置好相應(yīng)的圖表就可以看到對(duì)應(yīng)的業(yè)務(wù)指標(biāo)變化了。
這里我們一共配置四個(gè)圖表,分別是:
訂單總數(shù)
訂單支付總額
訂單數(shù)增長(zhǎng)率
訂單支付金額增長(zhǎng)率
我們?cè)谠忻姘迳闲陆ㄒ粋€(gè)圖表(Panel),名稱命名為「訂單個(gè)數(shù)」,來(lái)統(tǒng)計(jì)所有的訂單數(shù)量。
在「數(shù)據(jù)配置區(qū)」中數(shù)據(jù)源選擇「Prometheus」,Metrics 填入「order_amount_sum_count」。
接著在「圖表設(shè)置區(qū)」的「Visualization」中選擇「Stat」類別,表示這是一個(gè)統(tǒng)計(jì)數(shù)值。
接著在「圖表設(shè)置區(qū)」的「Display」中的 Value 設(shè)置為「Last」,表示其值是取最后一個(gè)數(shù)值(因?yàn)檫@個(gè)數(shù)值是已經(jīng)統(tǒng)計(jì)好了的)。Fields 設(shè)置為「Numeric Fields」,表示其是一個(gè)數(shù)值字段。
我們同樣在原有面板上新建一個(gè)圖表(Panel),名稱命名為「訂單金額」,來(lái)統(tǒng)計(jì)所有訂單的支付總金額。
在「數(shù)據(jù)配置區(qū)」中數(shù)據(jù)源選擇「Prometheus」,Metrics 填入「order_amount_sum_sum」。
接著在「圖表設(shè)置區(qū)」的「Visualization」中選擇「Stat」類別,表示這是一個(gè)統(tǒng)計(jì)數(shù)值。
接著在「圖表設(shè)置區(qū)」的「Display」中的 Value 設(shè)置為「Last」,表示其值是取最后一個(gè)數(shù)值(因?yàn)檫@個(gè)數(shù)值是已經(jīng)統(tǒng)計(jì)好了的)。Fields 設(shè)置為「Numeric Fields」,表示其是一個(gè)數(shù)值字段。
這里我們配置一個(gè)訂單數(shù)的增長(zhǎng)率,同樣在原有面板上新建一個(gè)圖表(Panel),名稱命名為「訂單增長(zhǎng)率」,來(lái)統(tǒng)計(jì)訂單數(shù)的增長(zhǎng)率。
在「數(shù)據(jù)配置區(qū)」中數(shù)據(jù)源選擇「Prometheus」,Metrics 填入「rate (order_amount_sum_count [1m])」,Legend 填入「{{instance}}」。
接著在「圖表設(shè)置區(qū)」的「Visualization」中選擇「Graph」類別,表示這是一個(gè)圖形。
在「圖表設(shè)置區(qū)」的「Axes」中設(shè)置「Left Y」的「Unit」設(shè)置其單位為:percent (0.0-1.0)。
與配置訂單增長(zhǎng)率相似,只不過(guò)這里的 Metrics 需要填入「rate (order_amount_sum_sum [1m])」。
在「圖表設(shè)置區(qū)」的「Axes」中設(shè)置「Left Y」的「Unit」設(shè)置其單位為:percent (0-100)。
設(shè)置完之后的監(jiān)控界面如下圖所示:
接下來(lái)我們模擬一下訂單的增長(zhǎng),訪問(wèn)下 localhost:8080/order 模擬下單。多訪問(wèn)幾次,以便看到更明顯的增長(zhǎng)效果。
我們可以看到各項(xiàng)指標(biāo)都有明顯的變化,這說(shuō)明我們的監(jiān)控生效了!
上述內(nèi)容就是如何在在Prometheus 中利SpringBoot 實(shí)現(xiàn)一個(gè)指標(biāo)監(jiān)控功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。