prometheus自定义metrics

aside section ._1OhGeD · · 3020 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

项目地址 个人博客

日常项目开发过程中为了观察项目的线上运行指标通常需要项目提供一系列指标信息,我们目前用的prometheus,而项目当然要提供一系列prometheus metrics指标信息,但是默认官方golang的仓库不是很好用。

  • 所有指标全部保存在内存重启后会全部清零
  • 如果指标labels数量变更,无法重新注册,unregister貌似无效
  • 无法定时清除数据,比如我想观察某些指标每一天的数据
  • web服务很多通用的http接口指标需要重复定义并自己定义中间件收集

于是乎找到github上一个很好的项目,可以满足上面所有需求可以大多数一般项目开发过程中的metrics需求,使用起来也比较简单。下面来自项目READEME:

lib-metrics

image.png

GoDoc

Dependency

github.com/AlecAivazis/survey/v2 v2.0.1 // indirect
github.com/atotto/clipboard v0.1.2 // indirect
github.com/go-redis/redis v6.15.2+incompatible
github.com/golang/protobuf v1.3.1
github.com/onsi/ginkgo v1.8.0 // indirect
github.com/onsi/gomega v1.5.0 // indirect
github.com/prometheus/client_golang v1.0.0
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90
github.com/prometheus/common v0.6.0
gopkg.in/robfig/cron.v2 v2.0.0-20150107220207-be2e0b0deed5
gopkg.in/yaml.v2 v2.2.2 // indirect

Initialization

init global collector gather

subsystem := "test"
metricsCollector = NewAutumnCollector(subsystem)

create local metric and registry

localMetrics = metricsCollector.NewAutumnLocalMetric("local_test", "local_type")
// Reset data at zero every day
localMetrics.ResetByCron("0 0 * * *")
metricsCollector.Register(localMetrics)

create metric base on redis and registry

redisMetric = metricsCollector.NewAutumnRedisMetric(client, "redis_test", "type")
// reset redis key
redisMetric.ResetMetricRedisKey("reset_redis_key_test")
// get now redis key
redisMetric.GetRedisKey()
// Reset data at zero every day
redisMetric.ResetByCron("0 0 * * *")
// registry metric
metricsCollector.Register(redisMetric)

Register HTTP handler middleware to customize metrics for collecting HTTP requests

// Collect interface responses
var standardRequestDuration = prometheus.NewHistogramVec(
    prometheus.HistogramOpts{
        Name:    "server_request_duration_seconds",
        Help:    "How much time costs(s)",
        Buckets: []float64{0.0005, 0.001, 0.002, 0.005, 0.010, 0.020, 0.050, 0.1, 0.5, 1, 5},
    }, []string{"code", "router"})

metricsCollector.RegisterPrometheus(standardRequestDuration)

// Pre-interceptor record start time
metricsCollector.UseBeforeHandlerHook(func(ctx *Context) {
    fmt.Println("this is before 1 start")
    start := time.Now()
    ctx.Set("start", start)
    fmt.Println("this is before 1 end")
})

// the post-interceptor calculates the response time and obtains information such as code, which is set by Observer.
metricsCollector.UseAfterHandlerHook(func(ctx *Context) {
    fmt.Println("this is after 1 start")
    start := ctx.GetTime("start")
    elapsed := time.Since(start).Seconds()
    standardRequestDuration.WithLabelValues(fmt.Sprint(ctx.ResponseWriter.Status()), ctx.Request.URL.Path).Observe(elapsed)
    fmt.Println("this is after 1 end")
})

Usage

// local metrics
localMetrics.Set(1,"1")
localMetrics.Incr("2")
localMetrics.Add(1,"3")

// redis metrics
redisMetric.Set(1,"1")
redisMetric.Incr("2")
redisMetric.Add(1,"3")

// Using native metric
standardRequestDuration.WithLabelValues(200, "/test").Observe(10)

Registry Handler

Register gin middleware and add metrics interface to collect HTTP metrics information

engine = gin.New()
// add gin middleware
engine.Use(func(ctx *gin.Context) {
    metricsCollector.Use(http.HandlerFunc(func(w http.ResponseWriter,r *http.Request) {
        ctx.Next()
    })).ServeHTTP(ctx.Writer,ctx.Request)
})
// Register gin interface to provide metrics interface
engine.Get(DefaultMetricPath, gin.WrapF(metricsCollector.Handler()))

Register native HTTP Middleware

engine = new(http.ServeMux)
h:=metricsCollector.Use(engine)
engine.Handle(DefaultMetricPath, metricsCollector.Handler())
http.ListenAndServe(":8080", h)

Built-in default HTTP service startup metrics interface

if err := metricsCollector.ListenAndServe(":8080",metricsCollector.Handler); err != nil && err != http.ErrServerClosed {
     panic(err)
}

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:aside section ._1OhGeD

查看原文:prometheus自定义metrics

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

3020 次点击  
加入收藏 微博
被以下专栏收入,发现更多相似内容
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传