Repository navigation
Expand file tree
/
Copy pathexporterInMem.go
More file actions
79 lines (70 loc) · 1.77 KB
/
Copy pathexporterInMem.go
File metadata and controls
79 lines (70 loc) · 1.77 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
package phos
import (
"log/slog"
"sync"
)
type InMemExportImporter struct {
mu sync.Mutex
spans map[string]Snapshot
}
func NewInMemExportImporter() *InMemExportImporter {
return &InMemExportImporter{spans: make(map[string]Snapshot)}
}
func (e *InMemExportImporter) Export(data Snapshot) {
data.Attrs = cloneAttrs(data.Attrs)
data.Links = cloneSnapshotLinks(data.Links)
data.Events = cloneEvents(data.Events)
data.Errors = cloneErrorData(data.Errors)
e.mu.Lock()
defer e.mu.Unlock()
e.spans[data.ID] = data
}
// Spans returns a deep copy of all exported spans keyed by span ID.
func (e *InMemExportImporter) Spans() map[string]Snapshot {
e.mu.Lock()
defer e.mu.Unlock()
out := make(map[string]Snapshot, len(e.spans))
for id, data := range e.spans {
data.Attrs = cloneAttrs(data.Attrs)
data.Links = cloneSnapshotLinks(data.Links)
data.Events = cloneEvents(data.Events)
data.Errors = cloneErrorData(data.Errors)
out[id] = data
}
return out
}
func cloneAttrs(attrs []slog.Attr) []slog.Attr {
if len(attrs) == 0 {
return nil
}
cloned := make([]slog.Attr, len(attrs))
copy(cloned, attrs)
for i := range cloned {
if cloned[i].Value.Kind() == slog.KindGroup {
cloned[i].Value = slog.GroupValue(cloneAttrs(cloned[i].Value.Group())...)
}
}
return cloned
}
func cloneEvents(events []SnapshotEvent) []SnapshotEvent {
if len(events) == 0 {
return nil
}
cloned := make([]SnapshotEvent, len(events))
copy(cloned, events)
for i := range cloned {
cloned[i].Attrs = cloneAttrs(cloned[i].Attrs)
}
return cloned
}
func cloneErrorData(errs []SnapshotError) []SnapshotError {
if len(errs) == 0 {
return nil
}
cloned := make([]SnapshotError, len(errs))
copy(cloned, errs)
for i := range cloned {
cloned[i].Attrs = cloneAttrs(cloned[i].Attrs)
}
return cloned
}