第28关 k8s监控实战之Prometheus(八)

大家好,我是博哥爱运维。从这节课开始,博哥计划引入golang(简称go)语言开发的一些内容,没有接触过go语言的同学也不用慌,我会尽量以一个新人的角度,去把这些go开发的内容讲得通俗一些。这节课还是继续 prometheus监控相关的内容,博哥带大家用go语言开发一个简单的http服务并暴露相应的prometheus指标。

首先电脑上需要安装好go语言,下载链接(选择相应的系统安装包):

https://golang.google.cn/dl/

然后安装好vscode这个编程IDE工具:

https://code.visualstudio.com/

配置好vscode里面go的相关插件,可以参考下这个文档:

https://zhuanlan.zhihu.com/p/320343679

我来先准备写一个简单的http服务扮演我们的业务服务角色

翻译自:https://prometheus.io/docs/tutorials/instrumenting_http_server_in_go/

package main

import (
   "fmt"
   "net/http"
)

func ping(w http.ResponseWriter, req *http.Request){
   
   fmt.Fprintf(w,"pong")
}

func main() {
   
   http.HandleFunc("/ping",ping)

   http.ListenAndServe(":8090", nil)
}

运行测试下,确保服务访问正常

然后我们准备创建一个 Prometheus counter计数器,来记录请求数

var pingCounter = prometheus.NewCounter(
   prometheus.CounterOpts{
   
       Name: "ping_request_count",
       Help: "No of request handled by Ping handler",
   },
)

接着我们在ping函数里面加入 pingCounter.Inc()来引用这个计数器

func ping(w http.ResponseWriter, req *http.Request) {
   
   pingCounter.Inc()
   fmt.Fprintf(w, "pong")
}

然后将计数器注册到 Default Register 并公开指标

func main() {
   
   prometheus.MustRegister(pingCounter)
   http.HandleFunc("/ping", ping)
   http.Handle("/metrics", promhttp.Handler())
   http.ListenAndServe(":8090", nil)
}

最终完整代码如下:

package main

import (
   "fmt"
   "net/http"

   "github.com/prometheus/client_golang/prometheus"
   "github.com/prometheus/client_golang/prometheus/promhttp"
)

var pingCounter = prometheus.NewCounter(
   prometheus.CounterOpts{
   
       Name: "ping_request_count",
       Help: "No of request handled by Ping handler",
   },
)

func ping(w http.ResponseWriter, req *http.Request) {
   
   pingCounter.Inc()
   fmt.Fprintf(w, "pong")
}

func main() {
   
   prometheus.MustRegister(pingCounter)

   http.HandleFunc("/ping", ping)
   http.Handle("/metrics", promhttp.Handler())
   http.ListenAndServe(":8090", nil)
}

我们准备来运行它

# 初始化包管理
go mod init prometheus
# 用国内加速代理下载包
export GOPROXY=https://goproxy.cn
# 更新依赖包
go mod tidy
# 运行服务
go run server.go

查看暴露的指标

http://127.0.0.1:8090/metrics

增加一些请求数

http://127.0.0.1:8090/ping

这里我们可以修改prometheus的配置,来监控我们自定义的这个服务,或者也可以参照博哥之前的课程,用serviceMonitor来暴露指标

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ["localhost:9090"]
  - job_name: simple_server
    static_configs:
      - targets: ["localhost:8090"]

相关推荐

  1. 28 k8s监控实战Prometheus()

    2024-01-17 18:08:01       39 阅读
  2. 28 k8s监控实战Prometheus(三)

    2024-01-17 18:08:01       40 阅读
  3. 28 k8s监控实战Prometheus(二)

    2024-01-17 18:08:01       42 阅读
  4. 28 k8s监控实战Prometheus(四)

    2024-01-17 18:08:01       42 阅读
  5. 28 k8s监控实战Prometheus(五)

    2024-01-17 18:08:01       43 阅读
  6. 28 k8s监控实战Prometheus(六)

    2024-01-17 18:08:01       39 阅读
  7. 28 k8s监控实战Prometheus(七)

    2024-01-17 18:08:01       38 阅读
  8. 28 k8s监控实战Prometheus(九)

    2024-01-17 18:08:01       43 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-17 18:08:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-17 18:08:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-17 18:08:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-17 18:08:01       20 阅读

热门阅读

  1. 音频筑基:算法时延分析

    2024-01-17 18:08:01       40 阅读
  2. 第一章 计算机网络概述——教案(附PPT)

    2024-01-17 18:08:01       35 阅读
  3. Python从入门到网络爬虫(正则表达详解)

    2024-01-17 18:08:01       34 阅读
  4. 美国服务器网络延迟过高的原因及解决方案

    2024-01-17 18:08:01       41 阅读
  5. nginx location 与 proxy的配置

    2024-01-17 18:08:01       40 阅读
  6. c++ 指针的安全问题

    2024-01-17 18:08:01       30 阅读