Repository navigation
Expand file tree
/
Copy pathreport.go
More file actions
130 lines (121 loc) · 3.57 KB
/
Copy pathreport.go
File metadata and controls
130 lines (121 loc) · 3.57 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
package errx
import (
"context"
"sync"
)
// Report is the neutral, serializable projection of an *Error. Every
// contrib adapter (sentry, otel, http, grpc, prometheus, ...) consumes a
// Report and nothing else — adapters never touch *Error internals and the
// core never imports an adapter. This is the single seam (ERR-ADR-001).
//
// Unsafe fields are redacted here, so any sink that crosses a trust
// boundary is safe by construction.
type Report struct {
Domain string
Code string
Class Class
Severity Severity
Message string // operator message
Public string // end-user-safe message
Hint string
Owner string
DocURL string
Localized map[string]string // BCP-47 locale -> message
Fingerprint string
Retryable bool
RetryAfter string // human duration, "" if none
TraceID string
SpanID string
Fields []Field // unsafe values already replaced with redacted marker
Stack []Frame // origin stack
Trace []Frame // return-trace: where the error traveled (per-wrap)
Suppressed []string
Cause string
}
// RedactedMarker replaces the value of any field not marked Safe when the
// error is projected to a Report.
const RedactedMarker = "‹redacted›"
// Snapshot projects the nearest *Error in err into a Report, resolving the
// lazy stack and redacting unsafe fields. ctx is accepted for future
// trace-extraction hooks; nil is fine.
func Snapshot(ctx context.Context, err error) (Report, bool) {
_ = ctx
e := Get(err)
if e == nil {
return Report{}, false
}
r := Report{
Domain: e.domain,
Code: e.code,
Class: e.class,
Severity: e.severity,
Message: e.Error(),
Public: e.publicMsg,
Hint: e.Remediation(),
Owner: e.owner,
DocURL: e.URL(),
Localized: e.LocaleMessages(),
Fingerprint: e.Fingerprint(),
Retryable: e.retryable,
TraceID: e.traceID,
SpanID: e.spanID,
Stack: e.Frames(),
Trace: e.Trace(),
}
if e.retryAfter > 0 {
r.RetryAfter = e.retryAfter.String()
}
if e.cause != nil {
r.Cause = e.cause.Error()
}
for _, f := range e.fields {
if f.Safe {
r.Fields = append(r.Fields, f)
} else {
r.Fields = append(r.Fields, Field{Key: f.Key, Value: RedactedMarker, Safe: false})
}
}
for _, s := range e.suppressed {
r.Suppressed = append(r.Suppressed, s.Error())
}
return r, true
}
// Sink receives projected error Reports. Implementations live in contrib/*.
type Sink interface {
Name() string
Emit(ctx context.Context, r *Report) error
}
// Registry fans a Report out to registered sinks. Registration is explicit
// (caller wires it) — there is no init() magic. Per-sink Emit errors are
// swallowed so one bad sink cannot break reporting; SinkError records them
// for observability of the observability.
type Registry struct {
mu sync.RWMutex
sinks []Sink
OnSinkError func(name string, err error)
}
// NewRegistry returns an empty registry.
func NewRegistry() *Registry { return &Registry{} }
// Add registers a sink. Returns the registry for chaining.
func (r *Registry) Add(s Sink) *Registry {
r.mu.Lock()
r.sinks = append(r.sinks, s)
r.mu.Unlock()
return r
}
// Report projects err and emits it to every sink. No-op if err carries no
// *Error. Safe for concurrent use.
func (r *Registry) Report(ctx context.Context, err error) {
rep, ok := Snapshot(ctx, err)
if !ok {
return
}
r.mu.RLock()
sinks := r.sinks
r.mu.RUnlock()
for _, s := range sinks {
if e := s.Emit(ctx, &rep); e != nil && r.OnSinkError != nil {
r.OnSinkError(s.Name(), e)
}
}
}