doudeng2057 2015-10-04 12:24
浏览 25

Golang中的方法级执行时间指标?

I am quite new to Go and I was wondering if there are any nice (like AOP in Java) ways to gather method-level execution time metrics in Go?

It would be best if such code would not be placed inside regular business logic code.

I don't want to profile an app. I mean real production ready metrics that could be exported to Graphite etc. so I could monitor response time histograms and such.

  • 写回答

1条回答 默认 最新

  • duan7007 2015-10-06 12:20
    关注

    You could rewrite source code using the go parser and add the instructions. This is how godebug and the coverage tool work: https://github.com/mailgun/godebug.

    Obviously that would be a lot of work, but I think your methodology is flawed anyway. Measuring everything means your program will spend far more time measuring than actually doing work. This is why profiling only samples.

    It sounds like perhaps you're working on an HTTP project? You could easily instrument your code by wrapping all your http.Handlers or using a framework like Negroni. Here's an example of someone doing something similar. Go also has the expvar package which is sometimes useful for counters and the like.

    Also worth considering is using a statsd client (which can get your data into Graphite). Here's one package which can do that: godspeed*. Calls are pretty easy:

    package main
    
    import (
        "fmt"
        "net/http"
        "time"
    
        "github.com/PagerDuty/godspeed"
        "github.com/codegangsta/negroni"
    )
    
    func statsdMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
        start := time.Now()
        next(w, r)
        elapsed := float64(time.Now().Sub(start)) / float64(time.Millisecond)
    
        g, err := godspeed.NewDefault()
        if err != nil {
            return
        }
        defer g.Conn.Close()
    
        g.Histogram("http.response.time_ms", elapsed, []string{"path:" + r.URL.Path})
    }
    
    func main() {
        mux := http.NewServeMux()
        mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
            fmt.Fprintf(w, "Welcome to the home page!")
        })
    
        n := negroni.Classic()
        n.Use(negroni.HandlerFunc(statsdMiddleware))
        n.UseHandler(mux)
        n.Run(":3000")
    }
    

    This approach works for standard TCP servers as well. For pipelines reading / writing queues I usually measure how fast I'm reading in data and writing it out the other end and then use back-pressure gauges for the intermediate steps. (which helps me find out which step is causing the problem)

    Disclaimer: I work at DataDog

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c