forked from go-rel/rel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instrumentation.go
40 lines (33 loc) · 1.04 KB
/
instrumentation.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
package rel
import (
"context"
"log"
"strings"
"time"
)
// Instrumenter defines function type that can be used for instrumetation.
// This function should return a function with no argument as a callback for finished execution.
type Instrumenter func(ctx context.Context, op string, message string, args ...any) func(err error)
// Observe operation.
func (i Instrumenter) Observe(ctx context.Context, op string, message string, args ...any) func(err error) {
if i != nil {
return i(ctx, op, message, args)
}
return func(err error) {}
}
// DefaultLogger instrumentation to log queries and rel operation.
func DefaultLogger(ctx context.Context, op string, message string, args ...any) func(err error) {
// no op for rel functions.
if strings.HasPrefix(op, "rel-") {
return func(error) {}
}
t := time.Now()
return func(err error) {
duration := time.Since(t)
if err != nil {
log.Print("[duration: ", duration, " op: ", op, "] ", message, " - ", err)
} else {
log.Print("[duration: ", duration, " op: ", op, "] ", message)
}
}
}