-
Notifications
You must be signed in to change notification settings - Fork 31
/
.prom-gowrap.tmpl
50 lines (44 loc) · 1.64 KB
/
.prom-gowrap.tmpl
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import (
"time"
migrate "github.com/golang-migrate/migrate/v4"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
{{ $decorator := (or .Vars.DecoratorName (printf "%sWithPrometheus" .Interface.Name)) }}
// {{$decorator}} implements {{.Interface.Type}} interface with all methods wrapped
// with Prometheus metrics
type {{$decorator}} struct {
base {{.Interface.Type}}
instanceName string
}
var {{down .Interface.Name}}DurationSummaryVec = promauto.NewSummaryVec(
prometheus.SummaryOpts{
Name: "{{down .Interface.Name}}_duration_seconds",
Help: "{{down .Interface.Name}} runtime duration and result",
MaxAge: time.Minute,
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
},
[]string{"instance_name", "method", "result"})
// New{{.Interface.Name}}WithPrometheus returns an instance of the {{.Interface.Type}} decorated with prometheus summary metric
func New{{$decorator}}(base {{.Interface.Type}}, instanceName string) {{$decorator}} {
return {{$decorator}} {
base: base,
instanceName: instanceName,
}
}
{{range $method := .Interface.Methods}}
// {{$method.Name}} implements {{$.Interface.Type}}
func (_d {{$decorator}}) {{$method.Declaration}} {
_since := time.Now()
defer func() {
result := "ok"
{{- if $method.ReturnsError}}
if err != nil {
result = "error"
}
{{end}}
{{down $.Interface.Name}}DurationSummaryVec.WithLabelValues(_d.instanceName, "{{$method.Name}}", result).Observe(time.Since(_since).Seconds())
}()
{{$method.Pass "_d.base."}}
}
{{end}}