脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Golang - 详解prometheus监控golang服务实践记录

详解prometheus监控golang服务实践记录

2021-02-03 01:05Harvard_Fly Golang

这篇文章主要介绍了详解prometheus监控golang服务实践记录,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

 

一、prometheus基本原理介绍

 

prometheus是基于metric采样的监控,可以自定义监控指标,如:服务每秒请求数、请求失败数、请求执行时间等,每经过一个时间间隔,数据都会从运行的服务中流出,存储到一个时间序列数据库中,之后可通过PromQL语法查询。

主要特点:

多维数据模型,时间序列数据通过metric名以key、value的形式标识;

使用PromQL语法灵活地查询数据;

不需要依赖分布式存储,各服务器节点是独立自治的;

时间序列的收集,通过 HTTP 调用,基于pull 模型进行拉取;

通过push gateway推送时间序列;

通过服务发现或者静态配置,来发现目标服务对象;

多种绘图和仪表盘的可视化支持;

 

二、prometheus使用docker部署

 

查看是否有镜像

?
1
sudo docker search prometheus

新建prometheus.yaml

?
1
2
3
4
5
6
7
8
9
10
11
12
global:
scrape_interval: 10s
evaluation_interval: 60s
 
 
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['localhost:9090']
- job_name: integral
static_configs:
- targets: ['10.20.xx.xx:8001']

执行:

?
1
docker run --name prometheus -p 9090:9090 -v ~/prometheus.yaml:/etc/prometheus/prometheus.yml prom/prometheus

进入容器中可以看到配置文件已映射到容器指定目录:

详解prometheus监控golang服务实践记录

踩坑: prometheus官方镜像指定的配置文件是prometheus.yml 所以映射到容器内的文件名一定要保持一致 否则会出现指定的配置文件不生效

 

三、prometheus整体架构及各组件

 

详解prometheus监控golang服务实践记录

Prometheus Server :主程序,负责抓取和存储时序数据;

Client Libraries:客户端库,负责检测应用程序代码;

Push Gateway:Push 网关,接收短生命周期的 Job 主动推送的时序数据;

Exporters:为不同服务定制的Exporter(如:HAProxy、StatsD、Graphite等) ,从而抓取它们的Metris指标数据;

Alert Manage:告警管理器,处理不同的告警;

 

四、prometheus客户端调用示例

 

自定义prometheus的gin中间件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package ginprometheus
 
import (
  "strconv"
  "sync"
  "time"
 
  "github.com/gin-gonic/gin"
  "github.com/prometheus/client_golang/prometheus"
)
 
const (
  metricsPath = "/metrics"
  faviconPath = "/favicon.ico"
)
 
var (
  // httpHistogram prometheus 模型
  httpHistogram = prometheus.NewHistogramVec(prometheus.HistogramOpts{
    Namespace:  "http_server",
    Subsystem:  "",
    Name:    "requests_seconds",
    Help:    "Histogram of response latency (seconds) of http handlers.",
    ConstLabels: nil,
    Buckets:   nil,
  }, []string{"method", "code", "uri"})
)
 
// init 初始化prometheus模型
func init() {
  prometheus.MustRegister(httpHistogram)
}
 
// handlerPath 定义采样路由struct
type handlerPath struct {
  sync.Map
}
 
// get 获取path
func (hp *handlerPath) get(handler string) string {
  v, ok := hp.Load(handler)
  if !ok {
    return ""
  }
  return v.(string)
}
 
// set 保存path到sync.Map
func (hp *handlerPath) set(ri gin.RouteInfo) {
  hp.Store(ri.Handler, ri.Path)
}
 
// GinPrometheus gin调用Prometheus的struct
type GinPrometheus struct {
  engine *gin.Engine
  ignored map[string]bool
  pathMap *handlerPath
  updated bool
}
 
type Option func(*GinPrometheus)
 
// Ignore 添加忽略的路径
func Ignore(path ...string) Option {
  return func(gp *GinPrometheus) {
    for _, p := range path {
      gp.ignored[p] = true
    }
  }
}
 
// New new gin prometheus
func New(e *gin.Engine, options ...Option) *GinPrometheus {
  if e == nil {
    return nil
  }
 
  gp := &GinPrometheus{
    engine: e,
    ignored: map[string]bool{
      metricsPath: true,
      faviconPath: true,
    },
    pathMap: &handlerPath{},
  }
 
  for _, o := range options {
    o(gp)
  }
  return gp
}
 
// updatePath 更新path
func (gp *GinPrometheus) updatePath() {
  gp.updated = true
  for _, ri := range gp.engine.Routes() {
    gp.pathMap.set(ri)
  }
}
 
// Middleware set gin middleware
func (gp *GinPrometheus) Middleware() gin.HandlerFunc {
  return func(c *gin.Context) {
    if !gp.updated {
      gp.updatePath()
    }
    // 过滤请求
    if gp.ignored[c.Request.URL.String()] {
      c.Next()
      return
    }
 
    start := time.Now()
    c.Next()
 
    httpHistogram.WithLabelValues(
      c.Request.Method,
      strconv.Itoa(c.Writer.Status()),
      gp.pathMap.get(c.HandlerName()),
    ).Observe(time.Since(start).Seconds())
  }
}

gin路由初始化prometheus,使用中间件采样

?
1
2
3
4
gp := ginprometheus.New(r)
r.Use(gp.Middleware())
// metrics采样
r.GET("/metrics", gin.WrapH(promhttp.Handler()))

详解prometheus监控golang服务实践记录

查看target

详解prometheus监控golang服务实践记录

选取指标对应的graph,这里以gc采样的时间为例:

详解prometheus监控golang服务实践记录

如果需要展示更为丰富的可视化看板,可以将prometheus与grafana结合,将prometheus数据接入到grafana中,此处不再过多阐述

到此这篇关于详解prometheus监控golang服务实践记录的文章就介绍到这了,更多相关prometheus监控golang服务内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/FG123/p/13689649.html

延伸 · 阅读

精彩推荐
  • GolangGolang中Bit数组的实现方式

    Golang中Bit数组的实现方式

    这篇文章主要介绍了Golang中Bit数组的实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    天易独尊11682021-06-09
  • GolangGolang通脉之数据类型详情

    Golang通脉之数据类型详情

    这篇文章主要介绍了Golang通脉之数据类型,在编程语言中标识符就是定义的具有某种意义的词,比如变量名、常量名、函数名等等,Go语言中标识符允许由...

    4272021-11-24
  • Golanggolang的httpserver优雅重启方法详解

    golang的httpserver优雅重启方法详解

    这篇文章主要给大家介绍了关于golang的httpserver优雅重启的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,...

    helight2992020-05-14
  • Golanggolang 通过ssh代理连接mysql的操作

    golang 通过ssh代理连接mysql的操作

    这篇文章主要介绍了golang 通过ssh代理连接mysql的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    a165861639710342021-03-08
  • Golanggo日志系统logrus显示文件和行号的操作

    go日志系统logrus显示文件和行号的操作

    这篇文章主要介绍了go日志系统logrus显示文件和行号的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    SmallQinYan12302021-02-02
  • Golanggo语言制作端口扫描器

    go语言制作端口扫描器

    本文给大家分享的是使用go语言编写的TCP端口扫描器,可以选择IP范围,扫描的端口,以及多线程,有需要的小伙伴可以参考下。 ...

    脚本之家3642020-04-25
  • Golanggolang json.Marshal 特殊html字符被转义的解决方法

    golang json.Marshal 特殊html字符被转义的解决方法

    今天小编就为大家分享一篇golang json.Marshal 特殊html字符被转义的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    李浩的life12792020-05-27
  • Golanggolang如何使用struct的tag属性的详细介绍

    golang如何使用struct的tag属性的详细介绍

    这篇文章主要介绍了golang如何使用struct的tag属性的详细介绍,从例子说起,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看...

    Go语言中文网11352020-05-21