forked from recht/gowrap
/
interface_with_prometheus.go
executable file
·74 lines (63 loc) · 2.34 KB
/
interface_with_prometheus.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package templatestests
// DO NOT EDIT!
// This code is generated with http://github.com/gabstv/gowrap tool
// using ../templates/prometheus template
//go:generate gowrap gen -p github.com/gabstv/gowrap/templates_tests -i TestInterface -t ../templates/prometheus -o interface_with_prometheus.go
import (
"context"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
// TestInterfaceWithPrometheus implements TestInterface interface with all methods wrapped
// with Prometheus metrics
type TestInterfaceWithPrometheus struct {
base TestInterface
instanceName string
}
var testinterfaceDurationSummaryVec = promauto.NewSummaryVec(
prometheus.SummaryOpts{
Name: "testinterface_duration_seconds",
Help: "testinterface 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"})
// NewTestInterfaceWithPrometheus returns an instance of the TestInterface decorated with prometheus summary metric
func NewTestInterfaceWithPrometheus(base TestInterface, instanceName string) TestInterfaceWithPrometheus {
return TestInterfaceWithPrometheus{
base: base,
instanceName: instanceName,
}
}
// F implements TestInterface
func (_d TestInterfaceWithPrometheus) F(ctx context.Context, a1 string, a2 ...string) (result1 string, result2 string, err error) {
_since := time.Now()
defer func() {
result := "ok"
if err != nil {
result = "error"
}
testinterfaceDurationSummaryVec.WithLabelValues(_d.instanceName, "F", result).Observe(time.Since(_since).Seconds())
}()
return _d.base.F(ctx, a1, a2...)
}
// NoError implements TestInterface
func (_d TestInterfaceWithPrometheus) NoError(s1 string) (s2 string) {
_since := time.Now()
defer func() {
result := "ok"
testinterfaceDurationSummaryVec.WithLabelValues(_d.instanceName, "NoError", result).Observe(time.Since(_since).Seconds())
}()
return _d.base.NoError(s1)
}
// NoParamsOrResults implements TestInterface
func (_d TestInterfaceWithPrometheus) NoParamsOrResults() {
_since := time.Now()
defer func() {
result := "ok"
testinterfaceDurationSummaryVec.WithLabelValues(_d.instanceName, "NoParamsOrResults", result).Observe(time.Since(_since).Seconds())
}()
_d.base.NoParamsOrResults()
return
}