-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtracer.go
73 lines (61 loc) · 1.66 KB
/
tracer.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
package tracer
import (
"context"
"fmt"
"github.com/genuinetools/bpfd/api/grpc"
"github.com/sirupsen/logrus"
)
var (
// All registered tracers.
tracers map[string]InitFunc
)
// InitFunc initializes the tracer.
type InitFunc func() (Tracer, error)
// Tracer defines the basic capabilities of a tracer.
type Tracer interface {
// Load creates the bpf module and starts collecting the data for the tracer.
Load() error
// Unload closes the bpf module and all the probes that all attached to it.
Unload()
// WatchEvent defines the function to watch the events for the tracer.
WatchEvent(context.Context) (*grpc.Event, error)
// Start starts the map for the tracer.
Start()
// String returns a string representation of this tracer.
String() string
}
// Init initialized the tracer map.
func init() {
tracers = make(map[string]InitFunc)
}
// Register registers an InitFunc for the tracer.
func Register(name string, initFunc InitFunc) error {
if _, exists := tracers[name]; exists {
return fmt.Errorf("tracer name already registered %s", name)
}
tracers[name] = initFunc
return nil
}
// Get initializes and returns the registered tracer.
func Get(name string) (Tracer, error) {
if initFunc, exists := tracers[name]; exists {
return initFunc()
}
return nil, fmt.Errorf("tracer %q does not exist as a supported tracer", name)
}
// List all the registered tracers.
func List() []string {
keys := []string{}
for k := range tracers {
keys = append(keys, k)
}
return keys
}
// UnloadAll unloads all the registered tracers.
func UnloadAll() {
for p := range tracers {
prog, _ := Get(p)
prog.Unload()
logrus.Infof("Successfully unloaded tracer: %s", p)
}
}