Repository navigation
Expand file tree
/
Copy pathoption.go
More file actions
122 lines (107 loc) · 2.05 KB
/
Copy pathoption.go
File metadata and controls
122 lines (107 loc) · 2.05 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
package phos
import (
"encoding/json"
"log/slog"
)
type SpanKind int
const (
Internal SpanKind = iota
Server
Client
Producer
Consumer
)
func (k SpanKind) String() string {
switch k {
case Server:
return "server"
case Client:
return "client"
case Producer:
return "producer"
case Consumer:
return "consumer"
default:
return "internal"
}
}
func (k SpanKind) MarshalJSON() ([]byte, error) {
return json.Marshal(k.String())
}
type link struct {
traceID string
spanID string
attrs []slog.Attr
}
type spanConfig struct {
attrs []slog.Attr
kind SpanKind
links []link
}
type SpanOption func(*spanConfig)
func WithAttrs(attrs ...slog.Attr) SpanOption {
cloned := cloneAttrs(attrs)
return func(cfg *spanConfig) {
cfg.attrs = append(cfg.attrs, cloned...)
}
}
func WithKind(kind SpanKind) SpanOption {
return func(cfg *spanConfig) {
cfg.kind = kind
}
}
func WithLink(traceID, spanID string, attrs ...slog.Attr) SpanOption {
cloned := cloneAttrs(attrs)
return func(cfg *spanConfig) {
cfg.links = append(cfg.links, link{
traceID: traceID,
spanID: spanID,
attrs: cloned,
})
}
}
func newSpanConfig(opts []SpanOption) spanConfig {
cfg := spanConfig{kind: Internal}
for _, opt := range opts {
if opt != nil {
opt(&cfg)
}
}
return cfg
}
func cloneLinks(links []link) []link {
if len(links) == 0 {
return nil
}
cloned := make([]link, len(links))
copy(cloned, links)
for i := range cloned {
cloned[i].attrs = cloneAttrs(cloned[i].attrs)
}
return cloned
}
func snapshotLinks(links []link) []SnapshotLink {
if len(links) == 0 {
return nil
}
out := make([]SnapshotLink, len(links))
for i, existing := range links {
out[i] = SnapshotLink{
TraceID: existing.traceID,
SpanID: existing.spanID,
Attrs: cloneAttrs(existing.attrs),
}
}
return out
}
func cloneSnapshotLinks(links []SnapshotLink) []SnapshotLink {
if len(links) == 0 {
return nil
}
cloned := make([]SnapshotLink, len(links))
copy(cloned, links)
for i := range cloned {
cloned[i].Attrs = cloneAttrs(cloned[i].Attrs)
}
return cloned
}