forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstrument.go
70 lines (56 loc) · 1.53 KB
/
instrument.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
package datastore
import (
"os"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"www.velocidex.com/golang/velociraptor/file_store/api"
)
var (
DatastoreHistorgram = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "datastore_latency",
Help: "Latency to access datastore.",
Buckets: prometheus.LinearBuckets(0.01, 0.05, 10),
},
[]string{"tag", "action", "datastore"},
)
// Simulate running on a very slow filesystem (EFS)
inject_time = 0
)
func Instrument(access_type, datastore string,
path_spec api.DSPathSpec) func() time.Duration {
tag := path_spec.Tag()
if tag == "" {
tag = "Generic"
}
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
DatastoreHistorgram.WithLabelValues(tag, access_type, datastore).Observe(v)
}))
return timer.ObserveDuration
}
func InstrumentWithDelay(
access_type, datastore string, path_spec api.DSPathSpec) func() time.Duration {
tag := path_spec.Tag()
if tag == "" {
tag = "Generic"
}
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
DatastoreHistorgram.WithLabelValues(tag, access_type, datastore).Observe(v)
}))
// Instrument a delay in API calls.
if inject_time > 0 {
time.Sleep(time.Duration(inject_time) * time.Millisecond)
}
return timer.ObserveDuration
}
func init() {
delay_str, pres := os.LookupEnv("VELOCIRAPTOR_SLOW_FILESYSTEM")
if pres {
delay, err := strconv.Atoi(delay_str)
if err == nil {
inject_time = delay
}
}
}