-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.go
More file actions
369 lines (312 loc) · 10 KB
/
Copy pathstack.go
File metadata and controls
369 lines (312 loc) · 10 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
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
package stack
import (
"context"
"errors"
"fmt"
"reflect"
"runtime/debug"
"sync/atomic"
"time"
"github.com/themakers/stack/stack_backend"
"github.com/themakers/stack/stack_backend/stack_backend_text"
)
type A = stack_backend.Attr
// Attr and F build an attribute. Generic on purpose: the value is captured
// with its static type and dispatched via a pointer type-switch, so common
// kinds (string, ints, bool, float, duration, time) are packed into
// stack_backend.Value with zero heap allocations — a plain `any` parameter
// would box every escaping non-pointer value on the caller side.
func Attr[T any](name string, value T) A {
return F(name, value)
}
func F[T any](name string, value T) A {
var val stack_backend.Value
switch p := any(&value).(type) {
case *string:
val = stack_backend.StringValue(*p)
case *int:
val = stack_backend.Int64Value(int64(*p))
case *int8:
val = stack_backend.Int64Value(int64(*p))
case *int16:
val = stack_backend.Int64Value(int64(*p))
case *int32:
val = stack_backend.Int64Value(int64(*p))
case *int64:
val = stack_backend.Int64Value(*p)
case *uint:
val = stack_backend.Uint64Value(uint64(*p))
case *uint8:
val = stack_backend.Uint64Value(uint64(*p))
case *uint16:
val = stack_backend.Uint64Value(uint64(*p))
case *uint32:
val = stack_backend.Uint64Value(uint64(*p))
case *uint64:
val = stack_backend.Uint64Value(*p)
case *bool:
val = stack_backend.BoolValue(*p)
case *float32:
val = stack_backend.Float64Value(float64(*p))
case *float64:
val = stack_backend.Float64Value(*p)
case *time.Duration:
val = stack_backend.DurationValue(*p)
case *time.Time:
val = stack_backend.TimeValue(*p)
case *stack_backend.RawAttrValue:
val = stack_backend.RawValue(*p)
case *stack_backend.Value:
val = *p
default:
// Interfaces (incl. error), maps, structs, slices: boxed as-is.
val = stack_backend.AnyValue(value)
}
return A{Name: name, Value: val}
}
// COMMON
// ▗▄▖ ▗▄▄▖▗▄▄▄▖▗▄▄▄▖ ▗▄▖ ▗▖ ▗▖ ▗▄▄▖
// ▐▌ ▐▌▐▌ ▐▌ █ █ ▐▌ ▐▌▐▛▚▖▐▌▐▌
// ▐▌ ▐▌▐▛▀▘ █ █ ▐▌ ▐▌▐▌ ▝▜▌ ▝▀▚▖
// ▝▚▄▞▘▐▌ █ ▗▄█▄▖▝▚▄▞▘▐▌ ▐▌▗▄▄▞▘
//
func Name(name string) stack_backend.Option {
return stack_backend.OptionFunc(func(s *stack_backend.Stack) {
s.Span.Name = name
})
}
func Op() op {
var name, _, _ = stack_backend.Operation(0)
return op(name)
}
var _ stack_backend.Option = op("")
type op string
func (o op) ApplyToStack(s *stack_backend.Stack) {
s.Span.Name = string(o)
}
func With() stack_backend.Options {
return stack_backend.Options{}
}
func Cancel() stack_backend.Options {
return With().Cancel()
}
func Default(ctx context.Context) context.Context {
return With().Backend(stack_backend_text.New()).Apply(ctx)
}
func WithVCSFields() stack_backend.Option {
info, ok := debug.ReadBuildInfo()
return stack_backend.OptionFunc(func(sb *stack_backend.Stack) {
if ok {
for _, s := range info.Settings {
switch s.Key {
case "vcs":
//s.Value
case "vcs.revision":
sb.Options.ScopeAttrs = append(sb.Options.ScopeAttrs, F("vcs.revision", s.Value))
case "vcs.time":
// RFC3339 (e.g., "2024-11-01T12:34:56Z")
if t, err := time.Parse(time.RFC3339, s.Value); err == nil {
sb.Options.ScopeAttrs = append(sb.Options.ScopeAttrs, F("vcs.time", t))
}
case "vcs.modified":
//sb.Span.Attrs = append(sb.Span.Attrs, ...)
sb.Options.ScopeAttrs = append(sb.Options.ScopeAttrs, F("vcs.modified", s.Value))
}
}
}
})
}
//
// ▗▄▄▖▗▄▄▖ ▗▄▖ ▗▖ ▗▖
// ▐▌ ▐▌ ▐▌▐▌ ▐▌▐▛▚▖▐▌
// ▝▀▚▖▐▛▀▘ ▐▛▀▜▌▐▌ ▝▜▌
// ▗▄▄▞▘▐▌ ▐▌ ▐▌▐▌ ▐▌
//
type endFunc func(cause ...error)
func Span(ctx context.Context, opts ...stack_backend.Option) (context.Context, endFunc) {
var s = stack_backend.Get(ctx).Clone()
s.Span.Time = time.Now()
// A new span must not inherit parent state specific to a particular span:
// logs, the error, and its stack trace. Clone makes a shallow copy, and
// these fields belong to this span, not to the chain. OwnLogs = nil (not
// an empty slice) — the first append allocates lazily.
s.Span.OwnLogs = nil
s.Span.Error = nil
s.Span.ErrorStackTrace = nil
if s.Span.TraceID.IsZero() {
s.Span.TraceID = stack_backend.NewTraceID()
}
s.Span.ParentSpanID = s.Span.ID
s.Span.ID = stack_backend.NewID()
// The function name is stored "raw" (without the "()" suffix): the string
// shares memory with pclntab, no copies or concatenations. Backends append
// the "()" suffix at render time (see stack_backend_text). This removes an
// allocation from every span.
s.Span.Name, s.Span.File, s.Span.Line = stack_backend.Operation(0)
//> Apply options from arguments
stack_backend.Options(opts).ApplyToStack(s)
var cancel context.CancelCauseFunc
if s.CloseContextWithSpan {
ctx, cancel = context.WithCancelCause(ctx)
}
ctx = stack_backend.Put(ctx, s)
s.Backend.Handle(stack_backend.Event{
Kind: stack_backend.KindSpan,
State: s,
})
var ended atomic.Bool
return ctx, func(cause ...error) {
// Idempotency: a repeated done() is a no-op, no second KindSpanEnd.
if !ended.CompareAndSwap(false, true) {
return
}
var cause0 error
if len(cause) > 0 {
cause0 = cause[0]
}
if cancel != nil {
cancel(cause0)
}
// Mutations go under the lock — a concurrent log() may be reading the
// Stack in Clone(). done(err) marks the span as failed unless an error
// has already been set (via stack.Error inside the span).
s.LockState()
if cause0 != nil && s.Span.Error == nil {
s.Span.Error = cause0
}
s.Span.EndTime = time.Now()
s.UnlockState()
// The backend gets a snapshot (Clone copies under the lock): if another
// goroutine keeps logging into this ctx during done(), the backend must
// not read OwnLogs/Error concurrently with their mutation.
s.Backend.Handle(stack_backend.Event{
Kind: stack_backend.KindSpanEnd,
State: s.Clone(),
})
}
}
//
// ▗▖ ▗▄▖ ▗▄▄▖ ▗▄▄▖▗▄▄▄▖▗▖ ▗▖ ▗▄▄▖
// ▐▌ ▐▌ ▐▌▐▌ ▐▌ █ ▐▛▚▖▐▌▐▌
// ▐▌ ▐▌ ▐▌▐▌▝▜▌▐▌▝▜▌ █ ▐▌ ▝▜▌▐▌▝▜▌
// ▐▙▄▄▖▝▚▄▞▘▝▚▄▞▘▝▚▄▞▘▗▄█▄▖▐▌ ▐▌▝▚▄▞▘
//
func log(ctx context.Context, level, name string, err error, st stack_backend.StackTrace, attrs ...A) {
var (
t = time.Now()
s = stack_backend.Get(ctx)
_, file, line = stack_backend.Operation(1)
)
if level == stack_backend.LevelError && err == nil {
err = errors.New(name)
}
// Serialize mutations of the shared *Stack (OwnLogs, Error): the same ctx
// may be used from multiple goroutines. Level/Error are stored as SpanLog
// fields rather than appended to attrs — this removes the variadic attrs
// slice reallocation on every log.
if s.Options.AddLogsToSpan || (err != nil) {
s.LockState()
if s.Options.AddLogsToSpan {
s.Span.OwnLogs = append(s.Span.OwnLogs, stack_backend.SpanLog{
Time: t,
Name: name,
Level: level,
Error: err,
Attrs: attrs,
})
}
if err != nil && s.Span.Error == nil {
s.Span.Error = err
s.Span.ErrorStackTrace = st
}
s.UnlockState()
}
var e = stack_backend.Event{
Kind: stack_backend.KindLog,
State: s.Clone(),
LogEvent: stack_backend.LogEvent{
ID: stack_backend.NewID(),
Time: t,
Name: name,
Level: level,
OwnAttrs: attrs,
Error: err,
StackTrace: st,
File: file,
Line: line,
},
}
if err != nil {
e.Kind |= stack_backend.KindError
}
s.Backend.Handle(e)
}
func Log(ctx context.Context, level, name string, attrs ...A) {
log(ctx, level, name, nil, nil, attrs...)
}
func Debug(ctx context.Context, name string, attrs ...A) {
log(ctx, stack_backend.LevelDebug, name, nil, nil, attrs...)
}
func Info(ctx context.Context, name string, attrs ...A) {
log(ctx, stack_backend.LevelInfo, name, nil, nil, attrs...)
}
func Warn(ctx context.Context, name string, attrs ...A) {
log(ctx, stack_backend.LevelWarn, name, nil, nil, attrs...)
}
func Error(ctx context.Context, name string, err error, attrs ...A) error {
log(ctx, stack_backend.LevelError, name, err, stack_backend.Stacktrace(0), attrs...)
return err
}
func TLog(ctx context.Context, typed any) {
// The logger must not panic: report invalid input with a Warn log and return.
val := reflect.ValueOf(typed)
if !val.IsValid() {
Warn(ctx, "stack.TLog: input must be a struct or pointer to a struct", F("type", "nil"))
return
}
typ := val.Type()
for typ.Kind() == reflect.Ptr {
if val.IsNil() {
Warn(ctx, "stack.TLog: input must be a struct or pointer to a struct", F("type", typ.String()))
return
}
val = val.Elem()
typ = val.Type()
}
if typ.Kind() != reflect.Struct {
Warn(ctx, "stack.TLog: input must be a struct or pointer to a struct", F("type", typ.String()))
return
}
fullName := fmt.Sprintf("%s.%s", typ.PkgPath(), typ.Name())
var attrs []A
for i := 0; i < val.NumField(); i++ {
var (
field = typ.Field(i)
fieldName = field.Name
fieldValue = val.Field(i)
)
if !fieldValue.CanInterface() {
continue
}
if name, ok := field.Tag.Lookup("name"); ok {
fieldName = name
}
attrs = append(attrs, A{
Name: fieldName,
Value: stack_backend.AnyValue(fieldValue.Interface()),
})
}
log(ctx, stack_backend.LevelInfo, fullName, nil, nil, attrs...)
}
//
// ▗▄▄▄▖▗▄▄▖ ▗▄▄▖ ▗▄▖ ▗▄▄▖ ▗▄▄▖
// ▐▌ ▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌▐▌
// ▐▛▀▀▘▐▛▀▚▖▐▛▀▚▖▐▌ ▐▌▐▛▀▚▖ ▝▀▚▖
// ▐▙▄▄▖▐▌ ▐▌▐▌ ▐▌▝▚▄▞▘▐▌ ▐▌▗▄▄▞▘
//
// TODO
func Recover(ctx context.Context, rFn func(rec any)) {
if rec := recover(); rec != nil {
rFn(rec)
}
}