go-kit 上手之example stringsvc2 添加日志和监控

本文介绍了如何在Go-kit的stringsvc2服务中添加日志和监控功能。通过logMiddleware和instrumentingMiddleware封装服务,记录requests次数、执行时间和goroutine数量。同时探讨了Histograms与Summary的区别,并分析了metrics结果,包括/go_threads、/count接口的返回统计、以及/uppercase和/count接口的请求次数和时间统计。
摘要由CSDN通过智能技术生成

//StringService的结构体定义、以及输入输出参数结构定义和stringsvc1中是一样的

日志

//loggingMiddleware 这个方法的缺点在于无论是uppercase还是count参数都是uppercase,通用的就没有办法针对每个功能进行定制日志。
//由于StringService是一个接口,所以我们只需要定义一个新的类型包装之前的StringService,实现StringService的接口,在实现过程中加入log。

type logMiddleware struct {
    logger log.Logger
    next   StringService
}

func (mw logMiddleware) Uppercase(ctx context.Context, s string) (output string, err error) {
    defer func(begin time.Time) {
        mw.logger.Log(
            "logmethod", "uppercase",
            "input", s,
            "output", output,
            "err", err,
            "took", time.Since(begin),
        )
    }(time.Now())

    output, err = mw.next.Uppercase(ctx, s)
    return
}

func (mw logMiddleware) Count(ctx context.Context, s string) (n int) {
    defer func(begin time.Time) {
        mw.logger.Log(
            "logmethod", "uppercase",
            "input", s,
            "output", n,
            // "err", err,
            "took", time.Since(begin),
        )
    }(time.Now())

    n = mw.next.Count(ctx, s)
    return
}

添加instrument

instrumentation means using package metrics to record statistics about your service’s runtime behavior. Counting the number of jobs processed, recording the duration of requests after they’ve finished, and tracking the number of in-flight operations would all be considered instrumentation.

说白了就是对服务运行情况的统计

type instrumentingMiddleware struct {
    requestCount   metrics.Counter
    requestLatency metrics.Histogram //官网文档这里似乎错了
    countResult    metrics.Histogram
    next           StringService
}

func (mw instrumentingMiddleware) Uppercase(ctx context.Context, s string) (output string, err error) {
    defer func(begin time.Time) {
        //官网文档是 metrics.Field 似乎是错的
        //methodFiled 要和 main中的fieldKeys保持一致
        methodField := []string{
  "method", "uppercase", "error", fmt.Sprintf("%T", err != nil)}
        mw.requestCount.With(methodField...).Add(1)
        mw.requestLatency.With(methodField...).Observe(time.Since(begin).Seconds())

    }(time.Now())

    output, err = mw.next.Uppercase(ctx, s)
    return
}

func (mw instrumentingMiddleware) Count(ctx context.Context, s string) (n int) {
    defer func(begin time.Time) {
        //官网文档里面是 metrics.Field 似乎是错的
        methodField := []string{
  "method", "count", "error", "false"}
        mw.requestCount.With(methodField...).Add(1)
        mw.requestLatency.With(methodField...).Observe(time.Since(begin).Seconds())
        //文档里面是int64是错的,
        mw.countResult.Observe(float64(n))

    }(time.Now())

    n = mw.next.Count(ctx, s)
    return
}

对于Histograms和Summary的区别


// Note that Histograms, in contrast to Summaries, can be aggregated with the
// Prometheus query language (see the documentation for detailed
// procedures). However, Histograms require the user to pre-define suitable
// buckets, and they are in general less accurate. The Observe method of a
// Histogram has a very low performance overhead in comparison with the Observe
// method of a Summary.


// A Summary captures individual observations from an event or sample stream and
// summarizes them in a manner similar to traditional summary statistics: 1. sum
// of observations, 2. observation count, 3. rank estimations.
//
// A typical use-case is the observation of request latencies. By default, a
// Summary provides the median, the 90th and the 99th percentile of the latency
// as rank estimations. However, the default behavior will change in the
// upcoming v0.10 of the library. There will be no rank estiamtions at all by
// default. For a sane transition, it is recommended to set the desired rank
// estimations explicitly.
//
// Note 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值