首页新闻博问专区闪存班级 我的博客我的园子账号设置退出登录注册登录Harvard_Fly 博客园首页新随笔联系管理订阅 随笔- 64 文章- 0 评论- 26 prometheus监控golang服务实践 一、prometheus基本原理介绍prometheus是基于metric采样的监控,可以自定义监控指标,如:服务每秒请求数、请求失败数、请求执行时间等,每经过一个时间间隔,数据都会从运行的服务中流出,存储到一个时间序列数据库中,之后可通过PromQL语法查询。主要特点:多维数据模型,时间序列数据通过metric名以key、value的形式标识;使用PromQL语法灵活地查询数据;不需要依赖分布式存储,各服务器节点是独立自治的;时间序列的收集,通过 HTTP 调用,基于pull 模型进行拉取;通过push gateway推送时间序列;通过服务发现或者静态配置,来发现目标服务对象;多种绘图和仪表盘的可视化支持; 二、prometheus使用docker部署查看是否有镜像sudo docker search prometheus 新建prometheus.yamlglobal:
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’] 执行:docker run --name prometheus -p 9090:9090 -v ~/prometheus.yaml:/etc/prometheus/prometheus.yml prom/prometheus进入容器中可以看到配置文件已映射到容器指定目录: 踩坑: prometheus官方镜像指定的配置文件是prometheus.yml 所以映射到容器内的文件名一定要保持一致 否则会出现指定的配置文件不生效 三、prometheus整体架构及各组件Prometheus Server :主程序,负责抓取和存储时序数据;Client Libraries:客户端库,负责检测应用程序代码;Push Gateway:Push 网关,接收短生命周期的 Job 主动推送的时序数据;Exporters:为不同服务定制的Exporter(如:HAProxy、StatsD、Graphite等) ,从而抓取它们的Metris指标数据;Alert Manage:告警管理器,处理不同的告警; 四、prometheus客户端调用示例自定义prometheus的gin中间件?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122package 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 定义采样路由structtype handlerPath struct { sync.Map} // get 获取pathfunc (hp *handlerPath) get(handler string) string { v, ok := hp.Load(handler) if !ok { return “” } return v.(string)} // set 保存path到sync.Mapfunc (hp *handlerPath) set(ri gin.RouteInfo) { hp.Store(ri.Handler, ri.Path)} // GinPrometheus gin调用Prometheus的structtype 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 prometheusfunc 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 更新pathfunc (gp *GinPrometheus) updatePath() { gp.updated = true for _, ri := range gp.engine.Routes() { gp.pathMap.set(ri) }} // Middleware set gin middlewarefunc (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,使用中间件采样?1234gp := ginprometheus.New®r.Use(gp.Middleware())// metrics采样r.GET("/metrics", gin.WrapH(promhttp.Handler()))
查看target 选取指标对应的graph,这里以gc采样的时间为例:如果需要展示更为丰富的可视化看板,可以将prometheus与grafana结合,将prometheus数据接入到grafana中,此处不再过多阐述 分类: Golang好文要顶 关注我 收藏该文 Harvard_Fly
关注 - 2
粉丝 - 31 +加关注 0 0
« 上一篇: ETCD核心机制解析 posted @ 2020-11-17 15:01 Harvard_Fly 阅读(71) 评论(0) 编辑 收藏
刷新评论刷新页面返回顶部 登录后才能发表评论,立即 登录 或 注册, 访问 网站首页。 首页 新闻 博问 专区 闪存 班级 【推荐】News: 大型组态、工控、仿真、CADGIS 50万行VC++源码免费下载
【推荐】博客园 & 陌上花开HIMMR 给单身的程序员小哥哥助力脱单啦~
【推荐】了不起的开发者,挡不住的华为,园子里的品牌专区
【推荐】未知数的距离,毫秒间的传递,声网与你实时互动
【福利】AWS携手博客园为开发者送免费套餐与抵扣券
【推荐】 阿里云折扣价格返场,错过再等一年
相关博文:
· prometheusexporter简介
· prometheus监控tomcat
· Prometheus+Grafana监控
· prometheus安装(docker)
· Prometheus笔记(一)metrictype
» 更多推荐…最新 IT 新闻:
· iPhone 12:比绿更绿
· 负债90亿、万人维权,“蛋壳公寓”会成为下一个ofo吗?
· 谷歌微软腾讯网易吹起的云游戏风口,又一个旧金山卖水人的故事
· 微软新提案:让Windows端Chrome/Edge支持计量连接模式
· 百度财报电话会议实录:直播将成为百度变现的一个很好渠道
» 更多新闻…
公告 昵称: Harvard_Fly
园龄: 5年7个月
粉丝: 31
关注: 2 +加关注
< 2020年11月> 日一二三四五六 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 1 2 3 4 5 6 7 8 9 10 11 12
搜索 常用链接 我的随笔我的评论我的参与最新评论我的标签更多链接积分与排名 积分 - 86795 排名 - 10114 随笔分类 C/C++(2) Docker(2) Elasticsearch(1) Github(1) Golang(7) Linux(1) Python后端(23) Redis(1) 大数据处理(11) 机器学习(5) 深度学习(1) 微服务(3) 消息中间件(6) 随笔档案 2020年11月(2) 2020年10月(1) 2020年9月(1) 2020年6月(1) 2020年5月(4) 2019年6月(1) 2019年5月(5) 2019年4月(1) 2019年2月(1) 2019年1月(3) 2018年12月(6) 2018年11月(4) 2018年10月(13) 2018年9月(3) 2018年8月(7) 2017年11月(1) 2017年2月(3) 2016年3月(2) 2016年1月(1) 2015年12月(3) 2015年5月(1) 最新评论1. Re:Python之抽象类、抽象方法python的抽象类可不是只能有抽象方法,自己试试就知道了
–养只猫叫二狗2. Re:使用Tornado异步接入第三方(支付宝)支付@刘_love_田 根据订单号、订单金额、订单标题,调用支付宝API生成相应的支付链接,通过支付链接支付完成后会调用回调url…–Harvard_Fly3. Re:Kafka生产者发送消息的三种方式@峰言风语 语言实现差别都不大,思想都是一致的。这里用py简单实现的,java、golang实现差别也不大…–Harvard_Fly4. Re:Kafka生产者发送消息的三种方式这是用什么语言实现的
–峰言风语5. Re:Spark中groupByKey、reduceByKey与sortByKey好文
–ac23阅读排行榜 1. Kafka提交offset机制(29187) 2. python线程池ThreadPoolExecutor与进程池ProcessPoolExecutor(19688) 3. Kqueue与epoll机制(16061) 4. Kafka生产者发送消息的三种方式(14389) 5. c++设计模式总结(11711) 评论排行榜 1. Kqueue与epoll机制(4) 2. c++设计模式总结(4) 3. 浅谈Django的Q查询以及AngularJS的Datatables分页插件(3) 4. 赶集网二手商品信息爬取(3) 5. 使用Tornado异步接入第三方(支付宝)支付(2) 推荐排行榜 1. Kqueue与epoll机制(6) 2. Kafka生产者发送消息的三种方式(5) 3. c++设计模式总结(3) 4. 使用Kazoo操作ZooKeeper服务治理(2) 5. RabbitMQ中RPC的实现及其通信机制(2)
Copyright © 2020 Harvard_Fly
Powered by .NET Core on Kubernetes