fix mertric.

pull/233/head
shunza 5 years ago
parent 5e75618617
commit 33da144c4a
  1. 22
      pkg/cache/metrics.go
  2. 4
      tool/kratos-gen-bts/header_template.go
  3. 8
      tool/kratos-gen-bts/multi_template.go
  4. 6
      tool/kratos-gen-bts/none_template.go
  5. 6
      tool/kratos-gen-bts/single_template.go
  6. 2
      tool/kratos-gen-mc/header_template.go
  7. 8
      tool/kratos-gen-mc/multi_template.go
  8. 4
      tool/kratos-gen-mc/none_template.go
  9. 4
      tool/kratos-gen-mc/single_template.go
  10. 4
      tool/kratos/tool_index.go

@ -0,0 +1,22 @@
package cache
import "github.com/bilibili/kratos/pkg/stat/metric"
const _metricNamespace = "cache"
var (
MetricHits = metric.NewCounterVec(&metric.CounterVecOpts{
Namespace: _metricNamespace,
Subsystem: "",
Name: "hits_total",
Help: "cache hits total.",
Labels: []string{"name"},
})
MetricMisses = metric.NewCounterVec(&metric.CounterVecOpts{
Namespace: _metricNamespace,
Subsystem: "",
Name: "misses_total",
Help: "cache misses total.",
Labels: []string{"name"},
})
)

@ -17,7 +17,7 @@ import (
"context" "context"
{{if .EnableBatch }}"sync"{{end}} {{if .EnableBatch }}"sync"{{end}}
NEWLINE NEWLINE
"github.com/bilibili/kratos/pkg/stat/metric" "github.com/bilibili/kratos/pkg/cache"
{{if .EnableBatch }}"github.com/bilibili/kratos/pkg/sync/errgroup"{{end}} {{if .EnableBatch }}"github.com/bilibili/kratos/pkg/sync/errgroup"{{end}}
{{.ImportPackage}} {{.ImportPackage}}
NEWLINE NEWLINE
@ -26,8 +26,6 @@ NEWLINE
var ( var (
_ _bts _ _bts
_metricHits = metric.NewBusinessMetricCount("hits_total", "name")
_metricMisses = metric.NewBusinessMetricCount("misses_total", "name")
) )
{{if .EnableSingleFlight}} {{if .EnableSingleFlight}}
var cacheSingleFlights = [SFCOUNT]*singleflight.Group{SFINIT} var cacheSingleFlights = [SFCOUNT]*singleflight.Group{SFINIT}

@ -26,7 +26,7 @@ func (d *Dao) NAME(c context.Context, {{.IDName}} []KEY{{.ExtraArgsType}}) (res
miss = append(miss, key) miss = append(miss, key)
} }
} }
_metricHits.Add(float64(len({{.IDName}}) - len(miss)), "NAME") cache.MetricHits.Add(float64(len({{.IDName}}) - len(miss)), "bts:NAME")
{{if .EnableNullCache}} {{if .EnableNullCache}}
for k, v := range res { for k, v := range res {
{{if .SimpleValue}} if v == {{.NullCache}} { {{else}} if {{.CheckNullCode}} { {{end}} {{if .SimpleValue}} if v == {{.NullCache}} { {{else}} if {{.CheckNullCode}} { {{end}}
@ -47,14 +47,14 @@ func (d *Dao) NAME(c context.Context, {{.IDName}} []KEY{{.ExtraArgsType}}) (res
var rr interface{} var rr interface{}
sf := d.cacheSFNAME({{.IDName}} {{.ExtraArgs}}) sf := d.cacheSFNAME({{.IDName}} {{.ExtraArgs}})
rr, err, _ = cacheSingleFlights[SFNUM].Do(sf, func() (r interface{}, e error) { rr, err, _ = cacheSingleFlights[SFNUM].Do(sf, func() (r interface{}, e error) {
_metricMisses.Add(float64(len(miss)), "NAME") cache.MetricMisses.Add(float64(len(miss)), "bts:NAME")
r, e = RAWFUNC(c, miss {{.ExtraRawArgs}}) r, e = RAWFUNC(c, miss {{.ExtraRawArgs}})
return return
}) })
missData = rr.(map[KEY]VALUE) missData = rr.(map[KEY]VALUE)
{{else}} {{else}}
{{if .EnableBatch}} {{if .EnableBatch}}
_metricMisses.Add(int64(missLen), "NAME") cache.MetricMisses.Add(float64(missLen), "bts:NAME")
var mutex sync.Mutex var mutex sync.Mutex
{{if .BatchErrBreak}} {{if .BatchErrBreak}}
group := errgroup.WithCancel(c) group := errgroup.WithCancel(c)
@ -87,7 +87,7 @@ func (d *Dao) NAME(c context.Context, {{.IDName}} []KEY{{.ExtraArgsType}}) (res
} }
err = group.Wait() err = group.Wait()
{{else}} {{else}}
_metricMisses.Add(int64(len(miss)), "NAME") cache.MetricMisses.Add(float64(len(miss)), "bts:NAME")
missData, err = RAWFUNC(c, miss {{.ExtraRawArgs}}) missData, err = RAWFUNC(c, miss {{.ExtraRawArgs}})
{{end}} {{end}}
{{end}} {{end}}

@ -21,20 +21,20 @@ func (d *Dao) NAME(c context.Context) (res VALUE, err error) {
{{else}} {{else}}
if res != {{.ZeroValue}} { if res != {{.ZeroValue}} {
{{end}} {{end}}
_metricHits.Incr("NAME") cache.MetricHits.Inc("bts:NAME")
return return
} }
{{if .EnableSingleFlight}} {{if .EnableSingleFlight}}
var rr interface{} var rr interface{}
sf := d.cacheSFNAME() sf := d.cacheSFNAME()
rr, err, _ = cacheSingleFlights[SFNUM].Do(sf, func() (r interface{}, e error) { rr, err, _ = cacheSingleFlights[SFNUM].Do(sf, func() (r interface{}, e error) {
_metricMisses.Incr("NAME") cache.MetricMisses.Inc("bts:NAME")
r, e = RAWFUNC(c) r, e = RAWFUNC(c)
return return
}) })
res = rr.(VALUE) res = rr.(VALUE)
{{else}} {{else}}
_metricMisses.Incr("NAME") cache.MetricMisses.Inc("bts:NAME")
res, err = RAWFUNC(c) res, err = RAWFUNC(c)
{{end}} {{end}}
if err != nil { if err != nil {

@ -21,7 +21,7 @@ func (d *Dao) NAME(c context.Context, {{.IDName}} KEY{{.ExtraArgsType}}) (res VA
{{else}} {{else}}
if res != {{.ZeroValue}} { if res != {{.ZeroValue}} {
{{end}} {{end}}
prom.CacheHit.Incr("NAME") cache.MetricHits.Inc("bts:NAME")
return return
} }
{{if .EnablePaging}} {{if .EnablePaging}}
@ -31,7 +31,7 @@ func (d *Dao) NAME(c context.Context, {{.IDName}} KEY{{.ExtraArgsType}}) (res VA
var rr interface{} var rr interface{}
sf := d.cacheSFNAME({{.IDName}} {{.ExtraArgs}}) sf := d.cacheSFNAME({{.IDName}} {{.ExtraArgs}})
rr, err, _ = cacheSingleFlights[SFNUM].Do(sf, func() (r interface{}, e error) { rr, err, _ = cacheSingleFlights[SFNUM].Do(sf, func() (r interface{}, e error) {
prom.CacheMiss.Incr("NAME") cache.MetricMisses.Inc("bts:NAME")
{{if .EnablePaging}} {{if .EnablePaging}}
var rrs [2]interface{} var rrs [2]interface{}
rrs[0], rrs[1], e = RAWFUNC(c, {{.IDName}} {{.ExtraRawArgs}}) rrs[0], rrs[1], e = RAWFUNC(c, {{.IDName}} {{.ExtraRawArgs}})
@ -48,7 +48,7 @@ func (d *Dao) NAME(c context.Context, {{.IDName}} KEY{{.ExtraArgsType}}) (res VA
res = rr.(VALUE) res = rr.(VALUE)
{{end}} {{end}}
{{else}} {{else}}
prom.CacheMiss.Incr("NAME") cache.MetricMisses.Inc("bts:NAME")
{{if .EnablePaging}} {{if .EnablePaging}}
res, miss, err = RAWFUNC(c, {{.IDName}} {{.ExtraRawArgs}}) res, miss, err = RAWFUNC(c, {{.IDName}} {{.ExtraRawArgs}})
{{else}} {{else}}

@ -19,7 +19,6 @@ import (
{{if .UseStrConv}}"strconv"{{end}} {{if .UseStrConv}}"strconv"{{end}}
{{if .EnableBatch }}"sync"{{end}} {{if .EnableBatch }}"sync"{{end}}
NEWLINE NEWLINE
"github.com/bilibili/kratos/pkg/stat/metric"
{{if .UseMemcached }}"github.com/bilibili/kratos/pkg/cache/memcache"{{end}} {{if .UseMemcached }}"github.com/bilibili/kratos/pkg/cache/memcache"{{end}}
{{if .EnableBatch }}"github.com/bilibili/kratos/pkg/sync/errgroup"{{end}} {{if .EnableBatch }}"github.com/bilibili/kratos/pkg/sync/errgroup"{{end}}
"github.com/bilibili/kratos/pkg/log" "github.com/bilibili/kratos/pkg/log"
@ -28,6 +27,5 @@ NEWLINE
var ( var (
_ _mc _ _mc
_metricErrCount = metric.NewBusinessMetricCount("mc_error_total", "NAME")
) )
` `

@ -44,7 +44,6 @@ func (d *{{.StructName}}) NAME(c context.Context, ids []KEY {{.ExtraArgsType}})
} }
replies, err := d.mc.GetMulti(c, keys) replies, err := d.mc.GetMulti(c, keys)
if err != nil { if err != nil {
_metricErrCount.Inc("NAME")
log.Errorv(ctx, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("keys", keys)) log.Errorv(ctx, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("keys", keys))
return return
} }
@ -67,14 +66,12 @@ func (d *{{.StructName}}) NAME(c context.Context, ids []KEY {{.ExtraArgsType}})
{{end}} {{end}}
{{end}} {{end}}
if err != nil { if err != nil {
_metricErrCount.Inc("NAME")
log.Errorv(ctx, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key)) log.Errorv(ctx, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
return return
} }
{{if .GetSimpleValue}} {{if .GetSimpleValue}}
r, err := {{.ConvertBytes2Value}} r, err := {{.ConvertBytes2Value}}
if err != nil { if err != nil {
_metricErrCount.Inc("NAME")
log.Errorv(ctx, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key)) log.Errorv(ctx, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
return res, err return res, err
} }
@ -114,7 +111,6 @@ func (d *{{.StructName}}) NAME(c context.Context, ids []KEY {{.ExtraArgsType}})
} }
replies, err := d.mc.GetMulti(c, keys) replies, err := d.mc.GetMulti(c, keys)
if err != nil { if err != nil {
_metricErrCount.Inc("NAME")
log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("keys", keys)) log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("keys", keys))
return return
} }
@ -132,14 +128,12 @@ func (d *{{.StructName}}) NAME(c context.Context, ids []KEY {{.ExtraArgsType}})
{{end}} {{end}}
{{end}} {{end}}
if err != nil { if err != nil {
_metricErrCount.Inc("NAME")
log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key)) log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
return return
} }
{{if .GetSimpleValue}} {{if .GetSimpleValue}}
r, err := {{.ConvertBytes2Value}} r, err := {{.ConvertBytes2Value}}
if err != nil { if err != nil {
_metricErrCount.Inc("NAME")
log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key)) log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
return res, err return res, err
} }
@ -174,7 +168,6 @@ func (d *{{.StructName}}) NAME(c context.Context, values map[KEY]VALUE {{.ExtraA
item := &memcache.Item{Key: key, Object: val, Expiration: {{.ExpireCode}}, Flags: {{.Encode}}} item := &memcache.Item{Key: key, Object: val, Expiration: {{.ExpireCode}}, Flags: {{.Encode}}}
{{end}} {{end}}
if err = d.mc.Set(c, item); err != nil { if err = d.mc.Set(c, item); err != nil {
_metricErrCount.Inc("NAME")
log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key)) log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
return return
} }
@ -198,7 +191,6 @@ func (d *{{.StructName}}) NAME(c context.Context, ids []KEY {{.ExtraArgsType}})
err = nil err = nil
continue continue
} }
_metricErrCount.Inc("NAME")
log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key)) log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
return return
} }

@ -37,14 +37,12 @@ func (d *{{.StructName}}) NAME(c context.Context) (res VALUE, err error) {
return return
} }
{{end}} {{end}}
_metricErrCount.Inc("NAME")
log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key)) log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
return return
} }
{{if .GetSimpleValue}} {{if .GetSimpleValue}}
r, err := {{.ConvertBytes2Value}} r, err := {{.ConvertBytes2Value}}
if err != nil { if err != nil {
_metricErrCount.Inc("NAME")
log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key)) log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
return return
} }
@ -75,7 +73,6 @@ func (d *{{.StructName}}) NAME(c context.Context, val VALUE) (err error) {
item := &memcache.Item{Key: key, Object: val, Expiration: {{.ExpireCode}}, Flags: {{.Encode}}} item := &memcache.Item{Key: key, Object: val, Expiration: {{.ExpireCode}}, Flags: {{.Encode}}}
{{end}} {{end}}
if err = d.mc.Set(c, item); err != nil { if err = d.mc.Set(c, item); err != nil {
_metricErrCount.Inc("NAME")
log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key)) log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
return return
} }
@ -94,7 +91,6 @@ func (d *{{.StructName}}) NAME(c context.Context) (err error) {
err = nil err = nil
return return
} }
_metricErrCount.Inc("NAME")
log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key)) log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
return return
} }

@ -36,14 +36,12 @@ func (d *{{.StructName}}) NAME(c context.Context, id KEY {{.ExtraArgsType}}) (re
return return
} }
{{end}} {{end}}
_metricErrCount.Inc("NAME")
log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key)) log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
return return
} }
{{if .GetSimpleValue}} {{if .GetSimpleValue}}
r, err := {{.ConvertBytes2Value}} r, err := {{.ConvertBytes2Value}}
if err != nil { if err != nil {
_metricErrCount.Incr("NAME")
log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key)) log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
return return
} }
@ -74,7 +72,6 @@ func (d *{{.StructName}}) NAME(c context.Context, id KEY, val VALUE {{.ExtraArgs
item := &memcache.Item{Key: key, Object: val, Expiration: {{.ExpireCode}}, Flags: {{.Encode}}} item := &memcache.Item{Key: key, Object: val, Expiration: {{.ExpireCode}}, Flags: {{.Encode}}}
{{end}} {{end}}
if err = d.mc.Set(c, item); err != nil { if err = d.mc.Set(c, item); err != nil {
_metricErrCount.Incr("NAME")
log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key)) log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
return return
} }
@ -93,7 +90,6 @@ func (d *{{.StructName}}) NAME(c context.Context, id KEY {{.ExtraArgsType}}) (er
err = nil err = nil
return return
} }
_metricErrCount.Incr("NAME")
log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key)) log.Errorv(c, log.KV("NAME", fmt.Sprintf("%+v", err)), log.KV("key", key))
return return
} }

@ -33,7 +33,7 @@ var toolIndexs = []*Tool{
&Tool{ &Tool{
Name: "genbts", Name: "genbts",
Alias: "kratos-gen-bts", Alias: "kratos-gen-bts",
BuildTime: time.Date(2019, 7, 20, 0, 0, 0, 0, time.Local), BuildTime: time.Date(2019, 7, 23, 0, 0, 0, 0, time.Local),
Install: "go get -u github.com/bilibili/kratos/tool/kratos-gen-bts", Install: "go get -u github.com/bilibili/kratos/tool/kratos-gen-bts",
Summary: "缓存回源逻辑代码生成器", Summary: "缓存回源逻辑代码生成器",
Platform: []string{"darwin", "linux", "windows"}, Platform: []string{"darwin", "linux", "windows"},
@ -42,7 +42,7 @@ var toolIndexs = []*Tool{
&Tool{ &Tool{
Name: "genmc", Name: "genmc",
Alias: "kratos-gen-mc", Alias: "kratos-gen-mc",
BuildTime: time.Date(2019, 7, 20, 0, 0, 0, 0, time.Local), BuildTime: time.Date(2019, 7, 23, 0, 0, 0, 0, time.Local),
Install: "go get -u github.com/bilibili/kratos/tool/kratos-gen-mc", Install: "go get -u github.com/bilibili/kratos/tool/kratos-gen-mc",
Summary: "mc缓存代码生成", Summary: "mc缓存代码生成",
Platform: []string{"darwin", "linux", "windows"}, Platform: []string{"darwin", "linux", "windows"},

Loading…
Cancel
Save