-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
ping.go
62 lines (48 loc) · 1.2 KB
/
ping.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
package otelsql
import (
"context"
)
const (
metricMethodPing = "go.sql.ping"
traceMethodPing = "ping"
)
// pingFuncMiddleware is a type for pingFunc middleware.
type pingFuncMiddleware = middleware[pingFunc]
// pingFunc is a callback for pingFunc.
type pingFunc func(ctx context.Context) error
// nopPing pings nothing.
func nopPing(_ context.Context) error {
return nil
}
// pingStats records ping stats.
func pingStats(r methodRecorder) pingFuncMiddleware {
return func(next pingFunc) pingFunc {
return func(ctx context.Context) (err error) {
end := r.Record(ctx, metricMethodPing)
defer func() {
end(err)
}()
return next(ctx)
}
}
}
// pingTrace traces ping.
func pingTrace(t methodTracer) pingFuncMiddleware {
return func(next pingFunc) pingFunc {
return func(ctx context.Context) (err error) {
ctx, end := t.Trace(ctx, traceMethodPing)
defer func() {
end(err)
}()
return next(ctx)
}
}
}
func makePingFuncMiddlewares(r methodRecorder, t methodTracer) []pingFuncMiddleware {
middlewares := make([]pingFuncMiddleware, 0, 2)
middlewares = append(middlewares, pingStats(r))
if t != nil {
middlewares = append(middlewares, pingTrace(t))
}
return middlewares
}