forked from dagger/dagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
543 lines (465 loc) · 14.1 KB
/
db.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package dagui
import (
"context"
"fmt"
"sort"
"time"
"go.opentelemetry.io/otel/attribute"
sdklog "go.opentelemetry.io/otel/sdk/log"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"dagger.io/dagger/telemetry"
"github.com/dagger/dagger/dagql/call/callpbv1"
"github.com/dagger/dagger/engine/slog"
)
type DB struct {
PrimarySpan trace.SpanID
PrimaryLogs map[trace.SpanID][]sdklog.Record
Traces map[trace.TraceID]*Trace
Spans map[trace.SpanID]*Span
SpanOrder []*Span
Children map[trace.SpanID]map[trace.SpanID]struct{}
ChildrenOrder map[trace.SpanID][]trace.SpanID
Calls map[string]*callpbv1.Call
Outputs map[string]map[string]struct{}
OutputOf map[string]map[string]struct{}
Intervals map[string]map[time.Time]*Span
Effects map[string]*Span
EffectSite map[string]*Span
}
func NewDB() *DB {
return &DB{
PrimaryLogs: make(map[trace.SpanID][]sdklog.Record),
Traces: make(map[trace.TraceID]*Trace),
Spans: make(map[trace.SpanID]*Span),
SpanOrder: make([]*Span, 0),
Children: make(map[trace.SpanID]map[trace.SpanID]struct{}),
ChildrenOrder: make(map[trace.SpanID][]trace.SpanID),
Calls: make(map[string]*callpbv1.Call),
OutputOf: make(map[string]map[string]struct{}),
Outputs: make(map[string]map[string]struct{}),
Intervals: make(map[string]map[time.Time]*Span),
Effects: make(map[string]*Span),
EffectSite: make(map[string]*Span),
}
}
func (db *DB) AllTraces() []*Trace {
traces := make([]*Trace, 0, len(db.Traces))
for _, traceData := range db.Traces {
traces = append(traces, traceData)
}
sort.Slice(traces, func(i, j int) bool {
return traces[i].Epoch.After(traces[j].Epoch)
})
return traces
}
var _ sdktrace.SpanExporter = (*DB)(nil)
func (db *DB) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpan) error {
for _, span := range spans {
traceID := span.SpanContext().TraceID()
traceData, found := db.Traces[traceID]
if !found {
traceData = &Trace{
ID: traceID,
Epoch: span.StartTime(),
End: span.EndTime(),
db: db,
}
db.Traces[traceID] = traceData
}
if span.StartTime().Before(traceData.Epoch) {
slog.Debug("new epoch", "old", traceData.Epoch, "new", span.StartTime())
traceData.Epoch = span.StartTime()
}
if span.EndTime().Before(span.StartTime()) {
traceData.IsRunning = true
}
if span.EndTime().After(traceData.End) {
slog.Debug("new end", "old", traceData.End, "new", span.EndTime())
traceData.End = span.EndTime()
}
db.maybeRecordSpan(traceData, span)
}
return nil
}
func (db *DB) LogExporter() sdklog.Exporter {
return DBLogExporter{db}
}
type DBLogExporter struct {
*DB
}
func (db DBLogExporter) Export(ctx context.Context, logs []sdklog.Record) error {
for _, log := range logs {
if log.Body().AsString() == "" {
// eof; ignore
continue
}
if log.SpanID() == db.PrimarySpan {
// buffer raw logs so we can replay them later
db.PrimaryLogs[log.SpanID()] = append(db.PrimaryLogs[log.SpanID()], log)
}
}
return nil
}
func (db *DB) Shutdown(ctx context.Context) error {
return nil // noop
}
func (db *DB) ForceFlush(ctx context.Context) error {
return nil // noop
}
func (db *DB) MetricExporter() sdkmetric.Exporter {
return DBMetricExporter{db}
}
func (db *DB) Temporality(sdkmetric.InstrumentKind) metricdata.Temporality {
return metricdata.DeltaTemporality
}
func (db *DB) Aggregation(sdkmetric.InstrumentKind) sdkmetric.Aggregation {
return sdkmetric.AggregationDefault{}
}
type DBMetricExporter struct {
*DB
}
func (db DBMetricExporter) Export(ctx context.Context, resourceMetrics *metricdata.ResourceMetrics) error {
for _, scopeMetric := range resourceMetrics.ScopeMetrics {
for _, metric := range scopeMetric.Metrics {
metricData, ok := metric.Data.(metricdata.Gauge[int64])
if !ok {
continue
}
for _, point := range metricData.DataPoints {
spanIDStr, ok := point.Attributes.Value(telemetry.MetricsSpanID)
if !ok {
continue
}
spanID, err := trace.SpanIDFromHex(spanIDStr.AsString())
if err != nil {
continue
}
span, ok := db.Spans[spanID]
if !ok {
continue
}
if span.MetricsByName == nil {
span.MetricsByName = make(map[string][]metricdata.DataPoint[int64])
}
span.MetricsByName[metric.Name] = append(span.MetricsByName[metric.Name], point)
}
}
}
return nil
}
// SetPrimarySpan allows the primary span to be explicitly set to a particular
// span. normally we assume the root span is the primary span, but in a nested
// scenario we never actually see the root span, so the CLI explicitly sets it
// to the span it created.
func (db *DB) SetPrimarySpan(span trace.SpanID) {
db.PrimarySpan = span
}
func (db *DB) maybeRecordSpan(traceData *Trace, span sdktrace.ReadOnlySpan) { //nolint: gocyclo
spanID := span.SpanContext().SpanID()
spanData, found := db.Spans[spanID]
if !found {
if !span.Parent().IsValid() && !db.PrimarySpan.IsValid() {
// Default the 'primary' span to the root span.
db.PrimarySpan = spanID
}
spanData = &Span{
ID: spanID,
FailedEffects: map[string]*Span{},
RunningEffects: map[string]*Span{},
db: db,
trace: traceData,
}
db.Spans[spanID] = spanData
db.SpanOrder = append(db.SpanOrder, spanData)
// collect any children that were received before the parent
for _, childID := range db.ChildrenOrder[spanID] {
child := db.Spans[childID]
if child == nil {
// defensive
slog.Warn("child span not found", "child", childID)
continue
}
spanData.ChildSpans = append(spanData.ChildSpans, child)
child.ParentSpan = spanData
}
}
spanData.ReadOnlySpan = span
spanData.IsSelfRunning = span.EndTime().Before(span.StartTime())
slog.Debug("recording span", "span", span.Name(), "id", spanID)
// track parent/child relationships
if parent := span.Parent(); parent.IsValid() {
if db.Children[parent.SpanID()] == nil {
db.Children[parent.SpanID()] = make(map[trace.SpanID]struct{})
}
slog.Debug("recording span child", "span", span.Name(), "parent", parent.SpanID(), "child", spanID)
if _, found := db.Children[parent.SpanID()][spanID]; !found {
db.Children[parent.SpanID()][spanID] = struct{}{}
db.ChildrenOrder[parent.SpanID()] = append(db.ChildrenOrder[parent.SpanID()], spanID)
if parent, exists := db.Spans[span.Parent().SpanID()]; exists {
spanData.ParentSpan = parent
parent.ChildSpans = append(parent.ChildSpans, spanData)
}
}
} else if !db.PrimarySpan.IsValid() {
// default primary to "root" span, but we might never see it in a nested
// scenario.
db.PrimarySpan = spanID
}
attrs := span.Attributes()
var digest string
if digestAttr, ok := getAttr(attrs, telemetry.DagDigestAttr); ok {
digest = digestAttr.AsString()
spanData.Digest = digest
// keep track of intervals seen for a digest
if db.Intervals[digest] == nil {
db.Intervals[digest] = make(map[time.Time]*Span)
}
db.Intervals[digest][span.StartTime()] = spanData
}
for _, attr := range attrs {
switch attr.Key {
case telemetry.DagCallAttr:
var call callpbv1.Call
if err := call.Decode(attr.Value.AsString()); err != nil {
slog.Warn("failed to decode id", "err", err)
continue
}
spanData.Call = &call
// Seeing loadFooFromID is only really interesting if it actually
// resulted in evaluating the ID, so we set Passthrough, which will only
// show its children.
if call.Field == fmt.Sprintf("load%sFromID", call.Type.ToAST().Name()) {
spanData.Passthrough = true
}
// We also don't care about seeing the id field selection itself, since
// it's more noisy and confusing than helpful. We'll still show all the
// spans leadning up to it, just not the ID selection.
if call.Field == "id" {
spanData.Ignore = true
}
if digest != "" {
db.Calls[digest] = &call
}
case telemetry.LLBOpAttr:
// TODO
case telemetry.CachedAttr:
spanData.Cached = attr.Value.AsBool()
case telemetry.CanceledAttr:
spanData.Canceled = attr.Value.AsBool()
case telemetry.UIEncapsulateAttr:
spanData.Encapsulate = attr.Value.AsBool()
case telemetry.UIEncapsulatedAttr:
spanData.Encapsulated = attr.Value.AsBool()
case telemetry.UIInternalAttr:
spanData.Internal = attr.Value.AsBool()
case telemetry.UIPassthroughAttr:
spanData.Passthrough = attr.Value.AsBool()
case telemetry.DagInputsAttr:
spanData.Inputs = attr.Value.AsStringSlice()
case telemetry.EffectIDsAttr:
spanData.Effects = attr.Value.AsStringSlice()
for _, digest := range spanData.Effects {
if db.EffectSite[digest] == nil {
db.EffectSite[digest] = spanData
}
}
case telemetry.DagOutputAttr:
output := attr.Value.AsString()
if digest == "" {
slog.Warn("output attribute is set, but a digest is not?")
} else {
slog.Debug("recording output", "digest", digest, "output", output)
// parent -> child
if db.Outputs[digest] == nil {
db.Outputs[digest] = make(map[string]struct{})
}
db.Outputs[digest][output] = struct{}{}
// child -> parent
if db.OutputOf[output] == nil {
db.OutputOf[output] = make(map[string]struct{})
}
db.OutputOf[output][digest] = struct{}{}
}
case telemetry.EffectIDAttr:
spanData.EffectID = attr.Value.AsString()
db.Effects[spanData.EffectID] = spanData
if dependentSpan := db.EffectSite[spanData.EffectID]; dependentSpan != nil {
if spanData.IsRunning() {
dependentSpan.RunningEffects[spanData.EffectID] = spanData
} else {
delete(dependentSpan.RunningEffects, spanData.EffectID)
}
if spanData.Failed() {
dependentSpan.FailedEffects[spanData.EffectID] = spanData
}
}
case "rpc.service":
// TODO: rather than special-casing this, we should just switch
// the telemetry pipeline over to HTTP.
// I tried adding attributes like 'internal' to the spans we care about
// but the OTel API is broken and stuck in bikeshedding:
// https://github.com/open-telemetry/opentelemetry-go-contrib/pull/5431#pullrequestreview-2024891968
spanData.Passthrough = true
}
}
if spanData.Call != nil && spanData.Call.ReceiverDigest != "" {
parentCall, ok := db.Calls[spanData.Call.ReceiverDigest]
if ok {
spanData.Base = db.Simplify(parentCall, spanData.Internal)
}
}
}
func (db *DB) HighLevelSpan(call *callpbv1.Call) *Span {
return db.MostInterestingSpan(db.Simplify(call, false).Digest)
}
func (db *DB) MostInterestingSpan(dig string) *Span {
var earliest *Span
var earliestCached bool
vs := make([]sdktrace.ReadOnlySpan, 0, len(db.Intervals[dig]))
for _, span := range db.Intervals[dig] {
vs = append(vs, span)
}
sort.Slice(vs, func(i, j int) bool {
return vs[i].StartTime().Before(vs[j].StartTime())
})
for _, span := range db.Intervals[dig] {
// a running vertex is always most interesting, and these are already in
// order
if span.IsRunning() {
return span
}
switch {
case earliest == nil:
// always show _something_
earliest = span
earliestCached = span.Cached
case span.Cached:
// don't allow a cached vertex to override a non-cached one
case earliestCached:
// unclear how this would happen, but non-cached versions are always more
// interesting
earliest = span
case span.StartTime().Before(earliest.StartTime()):
// prefer the earliest active interval
earliest = span
}
}
return earliest
}
// func (db *DB) IsTransitiveDependency(dig, depDig string) bool {
// for _, v := range db.Intervals[dig] {
// for _, dig := range v.Inputs {
// if dig == depDig {
// return true
// }
// if db.IsTransitiveDependency(dig, depDig) {
// return true
// }
// }
// // assume they all have the same inputs
// return false
// }
// return false
// }
func (*DB) Close() error {
return nil
}
func (db *DB) MustCall(dig string) *callpbv1.Call {
call, ok := db.Calls[dig]
if !ok {
// Sometimes may see a call's digest before the call itself.
//
// The loadFooFromID APIs for example will emit their call via their span
// before loading the ID, and its ID argument will just be a digest like
// anything else.
return &callpbv1.Call{
Field: "unknown",
Type: &callpbv1.Type{
NamedType: "Void",
},
Digest: dig,
}
}
return call
}
func (db *DB) litSize(lit *callpbv1.Literal) int {
switch x := lit.GetValue().(type) {
case *callpbv1.Literal_CallDigest:
return db.idSize(db.MustCall(x.CallDigest))
case *callpbv1.Literal_List:
size := 0
for _, lit := range x.List.GetValues() {
size += db.litSize(lit)
}
return size
case *callpbv1.Literal_Object:
size := 0
for _, lit := range x.Object.GetValues() {
size += db.litSize(lit.GetValue())
}
return size
}
return 1
}
func (db *DB) idSize(id *callpbv1.Call) int {
size := 0
for id := id; id != nil; id = db.Calls[id.ReceiverDigest] {
size++
size += len(id.Args)
for _, arg := range id.Args {
size += db.litSize(arg.GetValue())
}
}
return size
}
func (db *DB) Simplify(call *callpbv1.Call, force bool) (smallest *callpbv1.Call) {
smallest = call
smallestSize := -1
if !force {
smallestSize = db.idSize(smallest)
}
creators, ok := db.OutputOf[call.Digest]
if !ok {
return smallest
}
simplified := false
loop:
for creatorDig := range creators {
if creatorDig == call.Digest {
// can't be simplified to itself
continue
}
creator, ok := db.Calls[creatorDig]
if ok {
for _, creatorArg := range creator.Args {
if creatorArg, ok := creatorArg.Value.Value.(*callpbv1.Literal_CallDigest); ok {
if creatorArg.CallDigest == call.Digest {
// can't be simplified to a call that references itself
// in it's argument - which would loop endlessly
continue loop
}
}
}
if size := db.idSize(creator); smallestSize == -1 || size < smallestSize {
smallest = creator
smallestSize = size
simplified = true
}
}
}
if simplified {
return db.Simplify(smallest, false)
}
return smallest
}
func getAttr(attrs []attribute.KeyValue, key attribute.Key) (attribute.Value, bool) {
for _, attr := range attrs {
if attr.Key == key {
return attr.Value, true
}
}
return attribute.Value{}, false
}