forked from open-lambda/open-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdepTracer.go
More file actions
80 lines (68 loc) · 1.27 KB
/
Copy pathdepTracer.go
File metadata and controls
80 lines (68 loc) · 1.27 KB
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
package lambda
import (
"bufio"
"encoding/json"
"os"
)
type DepTracer struct {
file *os.File
writer *bufio.Writer
events chan map[string]any
done chan bool
}
func NewDepTracer(logPath string) (*DepTracer, error) {
file, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return nil, err
}
t := &DepTracer{
file: file,
writer: bufio.NewWriter(file),
events: make(chan map[string]any, 128),
done: make(chan bool),
}
go t.run()
return t, nil
}
func (t *DepTracer) run() {
for {
ev, ok := <-t.events
if !ok {
t.writer.Flush()
t.file.Close()
t.done <- true
return
}
b, err := json.Marshal(ev)
if err != nil {
panic(err)
}
t.writer.Write(b)
t.writer.WriteString("\n")
}
}
func (t *DepTracer) Cleanup() {
close(t.events)
<-t.done
}
func (t *DepTracer) TracePackage(p *Package) {
t.events <- map[string]any{
"type": "package",
"name": p.name,
"deps": p.meta.Deps,
"top": p.meta.TopLevel,
}
}
func (t *DepTracer) TraceFunction(codeDir string, directDeps []string) {
t.events <- map[string]any{
"type": "function",
"name": codeDir,
"deps": directDeps,
}
}
func (t *DepTracer) TraceInvocation(codeDir string) {
t.events <- map[string]any{
"type": "invocation",
"name": codeDir,
}
}