-
Notifications
You must be signed in to change notification settings - Fork 568
/
metrics.go
190 lines (172 loc) · 4.93 KB
/
metrics.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package metrics
import (
"path"
"runtime"
"strings"
"sync"
"time"
units "github.com/docker/go-units"
"github.com/prometheus/client_golang/prometheus"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
)
// TODO The metrics code should probably be reorganized at some point.
// The current setup provides an easy way to collect metrics for both external and internal PFS/Storage APIs.
type metrics struct {
requestCounter *prometheus.CounterVec
requestSummary, requestSummaryThroughput *prometheus.SummaryVec
}
var (
subsystems = make(map[string]*metrics)
mu sync.Mutex
)
const (
trimPrefix = "github.com/pachyderm/pachyderm/v2/src/"
)
// ReportRequest reports a request to Prometheus.
// This function automatically registers a metric (if one does not already
// exist) with the default register.
// The calling function's package name is used as the subsystem name and the
// function name is used for the operation label.
// This function also labels the request as successful or not, and records
// the time spent in a separate metric.
func ReportRequest(f func() error, skip ...int) (retErr error) {
start := time.Now()
defer func() {
ci, err := retrieveCallInfo(skip...)
if err != nil {
return
}
ms, err := maybeRegisterSubsystem(ci.packageName)
if err != nil {
return
}
operation := ci.funcName
result := "success"
if retErr != nil {
result = retErr.Error()
}
ms.requestCounter.WithLabelValues(operation, result).Inc()
ms.requestSummary.WithLabelValues(operation).Observe(time.Since(start).Seconds())
}()
return f()
}
// ReportRequestWithThroughput functions the same as ReportRequest, but also
// reports the throughput in a separate metric.
func ReportRequestWithThroughput(f func() (int64, error)) error {
start := time.Now()
return ReportRequest(func() error {
bytesProcessed, err := f()
defer func() {
ci, err := retrieveCallInfo()
if err != nil {
return
}
ms, err := maybeRegisterSubsystem(ci.packageName)
if err != nil {
return
}
operation := ci.funcName
throughput := float64(bytesProcessed) / units.MB / time.Since(start).Seconds()
ms.requestSummaryThroughput.WithLabelValues(operation).Observe(throughput)
}()
return err
}, 1)
}
type callInfo struct {
packageName string
fileName string
funcName string
line int
}
func retrieveCallInfo(skip ...int) (*callInfo, error) {
skipFrames := 2
if len(skip) > 0 {
skipFrames += skip[0]
}
pc, file, line, ok := runtime.Caller(skipFrames)
if !ok {
return nil, errors.Errorf("could not retrieve caller info")
}
_, fileName := path.Split(file)
fn := runtime.FuncForPC(pc)
if fn == nil {
return nil, errors.Errorf("could not retrieve function for program counter")
}
parts := strings.Split(fn.Name(), ".")
pl := len(parts)
packageName := ""
funcName := parts[pl-1]
if parts[pl-2][0] == '(' {
funcName = parts[pl-2] + "." + funcName
packageName = strings.Join(parts[0:pl-2], ".")
} else {
packageName = strings.Join(parts[0:pl-1], ".")
}
return &callInfo{
packageName: packageName,
fileName: fileName,
funcName: funcName,
line: line,
}, nil
}
func maybeRegisterSubsystem(packageName string) (*metrics, error) {
subsystem := strings.ReplaceAll(strings.TrimPrefix(packageName, trimPrefix), "/", "_")
mu.Lock()
defer mu.Unlock()
if ms, ok := subsystems[subsystem]; ok {
return ms, nil
}
err := register(subsystem)
return subsystems[subsystem], err
}
func register(subsystem string) error {
ms := &metrics{
requestCounter: newRequestCounter(subsystem),
requestSummary: newRequestSummary(subsystem),
requestSummaryThroughput: newRequestSummaryThroughput(subsystem),
}
for _, m := range []prometheus.Collector{
ms.requestCounter,
ms.requestSummary,
ms.requestSummaryThroughput,
} {
if err := prometheus.Register(m); err != nil {
return errors.EnsureStack(err)
}
}
subsystems[subsystem] = ms
return nil
}
func newRequestCounter(subsystem string) *prometheus.CounterVec {
return prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "pachyderm",
Subsystem: subsystem,
Name: "request_results",
Help: subsystem + " operations, count by operation and result type",
},
[]string{"operation", "result"},
)
}
func newRequestSummary(subsystem string) *prometheus.SummaryVec {
return prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: "pachyderm",
Subsystem: subsystem,
Name: "request_time",
Help: "time spent on " + subsystem + " operations, histogram by duration (seconds)",
},
[]string{"operation"},
)
}
func newRequestSummaryThroughput(subsystem string) *prometheus.SummaryVec {
return prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: "pachyderm",
Subsystem: subsystem,
Name: "request_throughput",
Help: "throughput of " + subsystem + " operations, histogram by throughput (MB/s)",
},
[]string{"operation"},
)
}