-
Notifications
You must be signed in to change notification settings - Fork 1
/
telemetry.go
138 lines (107 loc) · 4.64 KB
/
telemetry.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package observability
import (
"context"
"github.com/google/uuid"
"github.com/hephbuild/heph/artifacts"
"github.com/hephbuild/heph/graph"
"io"
"time"
)
type Observability struct {
hooks []Hook
}
func NewTelemetry() *Observability {
t := &Observability{}
return t
}
func (t *Observability) RegisterHook(h Hook) {
t.hooks = append(t.hooks, h)
}
func newSpan[S spanInternal](ctx context.Context, hooks []Hook, span S, fun func(Hook, context.Context, S) (context.Context, SpanHook)) (context.Context, S) {
span.setId(nextID())
span.SetStartTime(time.Now())
for _, hook := range hooks {
var shook SpanHook
ctx, shook = fun(hook, ctx, span)
span.registerHook(shook)
}
return ctx, span
}
type spanInternalWithId interface {
spanInternal
spanDedupId() interface{}
}
func newSpanWithDedup[S spanInternalWithId](hookName string, ctx context.Context, hooks []Hook, span S, fun func(Hook, context.Context, S) (context.Context, SpanHook)) (context.Context, S) {
keyCtx := struct {
hookname string
sid interface{}
}{
hookname: hookName,
sid: span.spanDedupId(),
}
if span, ok := ctx.Value(keyCtx).(S); ok {
return ctx, span
}
ctx, nspan := newSpan[S](ctx, hooks, span, fun)
ctx = context.WithValue(ctx, keyCtx, nspan)
return ctx, nspan
}
func (t *Observability) SpanRoot(ctx context.Context) (context.Context, *BaseSpan) {
span := &BaseSpan{}
return newSpan(ctx, t.hooks, span, Hook.OnRoot)
}
func (t *Observability) SpanRun(ctx context.Context, target *graph.Target) (context.Context, *TargetSpan) {
span := &TargetSpan{target: target}
return newSpanWithDedup("Run", ctx, t.hooks, span, Hook.OnRun)
}
func (t *Observability) SpanGenPass(ctx context.Context) (context.Context, *BaseSpan) {
span := &BaseSpan{}
ctx, span = newSpan(ctx, t.hooks, span, Hook.OnGenPass)
ctx = context.WithValue(ctx, duringGenKey{}, true)
return ctx, span
}
func (t *Observability) SpanCacheDownload(ctx context.Context, target *graph.Target, cache string, artifact artifacts.Artifact) (context.Context, *TargetArtifactCacheSpan) {
span := &TargetArtifactCacheSpan{TargetArtifactSpan: TargetArtifactSpan{TargetSpan: TargetSpan{target: target}, artifact: artifact}, Cache: cache}
return newSpanWithDedup("CacheDownload", ctx, t.hooks, span, Hook.OnCacheDownload)
}
func (t *Observability) SpanCacheUpload(ctx context.Context, target *graph.Target, cache string, artifact artifacts.Artifact) (context.Context, *TargetArtifactCacheSpan) {
span := &TargetArtifactCacheSpan{TargetArtifactSpan: TargetArtifactSpan{TargetSpan: TargetSpan{target: target}, artifact: artifact}, Cache: cache}
return newSpanWithDedup("CacheUpload", ctx, t.hooks, span, Hook.OnCacheUpload)
}
func (t *Observability) SpanRunPrepare(ctx context.Context, target *graph.Target) (context.Context, *TargetSpan) {
span := &TargetSpan{target: target}
return newSpanWithDedup("RunPrepare", ctx, t.hooks, span, Hook.OnRunPrepare)
}
func (t *Observability) SpanRunExec(ctx context.Context, target *graph.Target) (context.Context, *TargetExecSpan) {
span := &TargetExecSpan{TargetSpan: TargetSpan{target: target}, ExecId: uuid.New()}
return newSpanWithDedup("RunExec", ctx, t.hooks, span, Hook.OnRunExec)
}
func (t *Observability) SpanCollectOutput(ctx context.Context, target *graph.Target) (context.Context, *TargetSpan) {
span := &TargetSpan{target: target}
return newSpanWithDedup("CollectOutput", ctx, t.hooks, span, Hook.OnCollectOutput)
}
func (t *Observability) SpanLocalCacheStore(ctx context.Context, target *graph.Target) (context.Context, *TargetSpan) {
span := &TargetSpan{target: target}
return newSpanWithDedup("LocalCacheStore", ctx, t.hooks, span, Hook.OnLocalCacheStore)
}
func (t *Observability) SpanLocalCacheCheck(ctx context.Context, target *graph.Target, artifact artifacts.Artifact) (context.Context, *TargetArtifactCacheSpan) {
span := &TargetArtifactCacheSpan{TargetArtifactSpan: TargetArtifactSpan{TargetSpan: TargetSpan{target: target}, artifact: artifact}}
return newSpanWithDedup("LocalCacheCheck", ctx, t.hooks, span, Hook.OnLocalCacheCheck)
}
func (t *Observability) SpanExternalCacheGet(ctx context.Context, target *graph.Target, cache string, outputs []string, onlyMeta bool) (context.Context, *ExternalCacheGetSpan) {
span := &ExternalCacheGetSpan{TargetSpan: TargetSpan{target: target}, Cache: cache, Outputs: outputs, OnlyMeta: onlyMeta}
return newSpanWithDedup("ExternalCacheGet", ctx, t.hooks, span, Hook.OnExternalCacheGet)
}
func (t *Observability) LogsWriter(ctx context.Context) io.Writer {
var ws []io.Writer
for _, hook := range t.hooks {
w := hook.OnLogs(ctx)
if w != nil {
ws = append(ws, w)
}
}
if len(ws) == 0 {
return nil
}
return io.MultiWriter(ws...)
}