generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
span.go
210 lines (175 loc) 路 4.52 KB
/
span.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
package telemetry
import (
"context"
"errors"
"log/slog"
"reflect"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
)
// Span represents a single named and timed operation of a workflow.
type Span struct {
ctx context.Context
recorder *Recorder
name string
span trace.Span
attrs []Attr
logger *slog.Logger
groupedLogAttrs []any
ungroupedLogAttrs []any
errors int64
}
// StartSpan starts a new span.
func (r *Recorder) StartSpan(
ctx context.Context,
name string,
attrs ...Attr,
) (context.Context, *Span) {
ctx, underlying := r.tracer.Start(
ctx,
name,
)
span := &Span{
ctx: ctx,
recorder: r,
name: name,
span: underlying,
logger: r.logger,
}
span.SetAttributes(r.attrs...)
span.SetAttributes(attrs...)
var spanLogAttrs []any
if sctx := underlying.SpanContext(); sctx.HasSpanID() {
spanLogAttrs = append(
spanLogAttrs,
slog.String("id", sctx.SpanID().String()),
)
}
spanLogAttrs = append(
spanLogAttrs,
slog.String("name", name),
)
span.groupedLogAttrs = append(
span.groupedLogAttrs,
slog.Group("span", spanLogAttrs...),
)
return ctx, span
}
// End completes the span.
func (s *Span) End() {
if s.errors == 0 {
s.span.SetStatus(codes.Ok, "")
} else {
s.recorder.errors.Add(
s.ctx,
s.errors,
metric.WithAttributes(
attribute.String("span.name", s.name),
),
)
}
s.span.End()
}
// SetAttributes adds the given attributes to the underlying OpenTelemetry span
// and any future log messages.
func (s *Span) SetAttributes(attrs ...Attr) {
tel, log := s.resolveAttrs(attrs)
s.span.SetAttributes(tel...)
s.ungroupedLogAttrs = append(s.ungroupedLogAttrs, log...)
}
func (s *Span) resolveAttrs(attrs []Attr) ([]attribute.KeyValue, []any) {
tel := make([]attribute.KeyValue, 0, len(attrs))
log := make([]any, 0, len(attrs))
prefix := attribute.Key(s.recorder.name + ".")
for _, attr := range attrs {
if attr, ok := attr.otel(); ok {
attr.Key = prefix + attr.Key
tel = append(tel, attr)
}
if attr, ok := attr.slog(); ok {
log = append(log, attr)
}
}
return tel, log
}
// Info logs an informational message to the log and as a span event.
func (s *Span) Info(message string, attrs ...Attr) {
s.recordEvent(slog.LevelInfo, message, nil, attrs)
}
// Warn logs a warning message to the log and as a span event.
func (s *Span) Warn(message string, attrs ...Attr) {
s.recordEvent(slog.LevelWarn, message, nil, attrs)
}
// Debug logs a debug message to the log and as a span event.
func (s *Span) Debug(message string, attrs ...Attr) {
s.recordEvent(slog.LevelDebug, message, nil, attrs)
}
// Error logs an error message to the log and as a span event.
//
// It marks the span as an error and emits increments the "errors" metric.
func (s *Span) Error(message string, err error, attrs ...Attr) {
s.recordEvent(slog.LevelError, message, err, attrs)
}
func (s *Span) recordEvent(
level slog.Level,
message string,
err error,
attrs []Attr,
) {
if err != nil {
s.span.SetStatus(codes.Error, err.Error())
s.span.RecordError(err)
s.errors++
}
if !s.logger.Enabled(s.ctx, level) {
return
}
eventAttrs, ungroupedLogAttrs := s.resolveAttrs(attrs)
s.span.AddEvent(
message,
trace.WithAttributes(eventAttrs...),
)
ungroupedLogAttrs = append(ungroupedLogAttrs, s.ungroupedLogAttrs...)
groupedLogAttrs := s.groupedLogAttrs
if err != nil {
exceptionAttrs := []any{
slog.String("message", err.Error()),
}
if err := unwrapError(err); err != nil {
exceptionAttrs = append(
exceptionAttrs,
slog.String("type", reflect.TypeOf(err).String()),
)
}
groupedLogAttrs = append(
groupedLogAttrs,
slog.Group("exception", exceptionAttrs...),
)
}
groupedLogAttrs = append(
groupedLogAttrs,
slog.Group(s.recorder.name, ungroupedLogAttrs...),
)
s.logger.Log(
s.ctx,
level,
message,
groupedLogAttrs...,
)
}
var errorsStringType = reflect.TypeOf(errors.New(""))
// isStringError returns true if err is an error that was created using
// errors.New() or fmt.Errorf(), and therefore has no meaningful type.
func isStringError(err error) bool {
return reflect.TypeOf(err) == errorsStringType
}
// unwrapError unwraps err until an error with a meaningful type is found. If
// all errors in the chain are "string errors", it returns nil.
func unwrapError(err error) error {
for err != nil && isStringError(err) {
err = errors.Unwrap(err)
}
return err
}