Repository navigation
Expand file tree
/
Copy pathsnapshot.go
More file actions
160 lines (146 loc) · 3.69 KB
/
Copy pathsnapshot.go
File metadata and controls
160 lines (146 loc) · 3.69 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
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
package phos
import (
"encoding/json"
"log/slog"
"time"
)
type Snapshot struct {
ID string `json:"id"`
Name string `json:"name"`
ParentID string `json:"parent_id"`
TraceID string `json:"trace_id"`
TimeStart time.Time `json:"time_start"`
TimeEnd time.Time `json:"time_end"`
Kind SpanKind `json:"kind"`
Failed bool `json:"failed"`
Attrs []slog.Attr `json:"attrs"`
Links []SnapshotLink `json:"links"`
Events []SnapshotEvent `json:"events"`
Errors []SnapshotError `json:"errors"`
}
func (s Snapshot) MarshalJSON() ([]byte, error) {
type snapshotJSON struct {
ID string `json:"id"`
Name string `json:"name"`
ParentID string `json:"parent_id"`
TraceID string `json:"trace_id"`
TimeStart time.Time `json:"time_start"`
TimeEnd time.Time `json:"time_end"`
Kind SpanKind `json:"kind"`
Failed bool `json:"failed"`
Attrs []attrJSON `json:"attrs"`
Links []SnapshotLink `json:"links"`
Events []SnapshotEvent `json:"events"`
Errors []SnapshotError `json:"errors"`
}
return json.Marshal(snapshotJSON{
ID: s.ID,
Name: s.Name,
ParentID: s.ParentID,
TraceID: s.TraceID,
TimeStart: s.TimeStart,
TimeEnd: s.TimeEnd,
Kind: s.Kind,
Failed: s.Failed,
Attrs: marshalAttrs(s.Attrs),
Links: s.Links,
Events: s.Events,
Errors: s.Errors,
})
}
type SnapshotLink struct {
TraceID string `json:"trace_id"`
SpanID string `json:"span_id"`
Attrs []slog.Attr `json:"attrs"`
}
func (l SnapshotLink) MarshalJSON() ([]byte, error) {
type linkJSON struct {
TraceID string `json:"trace_id"`
SpanID string `json:"span_id"`
Attrs []attrJSON `json:"attrs"`
}
return json.Marshal(linkJSON{
TraceID: l.TraceID,
SpanID: l.SpanID,
Attrs: marshalAttrs(l.Attrs),
})
}
type SnapshotEvent struct {
Time time.Time `json:"time"`
Name string `json:"name"`
Attrs []slog.Attr `json:"attrs"`
}
func (e SnapshotEvent) MarshalJSON() ([]byte, error) {
type eventJSON struct {
Time time.Time `json:"time"`
Name string `json:"name"`
Attrs []attrJSON `json:"attrs"`
}
return json.Marshal(eventJSON{
Time: e.Time,
Name: e.Name,
Attrs: marshalAttrs(e.Attrs),
})
}
type SnapshotError struct {
Err error `json:"-"`
Attrs []slog.Attr `json:"attrs"`
}
func (e SnapshotError) MarshalJSON() ([]byte, error) {
type errorJSON struct {
Message string `json:"message"`
Attrs []attrJSON `json:"attrs"`
}
return json.Marshal(errorJSON{
Message: errorMessage(e.Err),
Attrs: marshalAttrs(e.Attrs),
})
}
type attrJSON struct {
Key string `json:"Key"`
Value any `json:"Value"`
}
func marshalAttrs(attrs []slog.Attr) []attrJSON {
if len(attrs) == 0 {
return nil
}
out := make([]attrJSON, len(attrs))
for i, attr := range attrs {
resolved := attr.Value.Resolve()
out[i] = attrJSON{
Key: attr.Key,
Value: slogValueToAny(resolved),
}
}
return out
}
func slogValueToAny(value slog.Value) any {
switch value.Kind() {
case slog.KindString:
return value.String()
case slog.KindInt64:
return value.Int64()
case slog.KindUint64:
return value.Uint64()
case slog.KindFloat64:
return value.Float64()
case slog.KindBool:
return value.Bool()
case slog.KindDuration:
return value.Duration().String()
case slog.KindTime:
return value.Time()
case slog.KindGroup:
return marshalAttrs(value.Group())
case slog.KindAny:
return value.Any()
default:
return value.Any()
}
}
func errorMessage(err error) string {
if err == nil {
return "<nil>"
}
return err.Error()
}