Repository navigation
Expand file tree
/
Copy patherror_tracking_slog_options.go
More file actions
121 lines (104 loc) · 4.36 KB
/
Copy patherror_tracking_slog_options.go
File metadata and controls
121 lines (104 loc) · 4.36 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
package posthog
import (
"context"
"log/slog"
)
type captureConfig struct {
// minCaptureLevel is the lowest slog.Level at which a log record
// will be sent to PostHog. Records below this level are ignored.
// Default: slog.LevelWarn.
minCaptureLevel slog.Level
// distinctID returns the user/device identifier to associate with
// this exception. If it returns an empty string, the exception will
// not be sent. Typical implementations extract the user ID from
// the provided context or log record.
distinctID func(ctx context.Context, r slog.Record) string
// fingerprint optionally computes a custom exception fingerprint
// for grouping similar errors together in PostHog. If nil, PostHog will
// assign a fingerprint during processing.
fingerprint func(ctx context.Context, r slog.Record) *string
// skip is the number of initial stack frames to omit when collecting
// the stack trace for this capture. This helps remove internal/logging
// frames from the output. Default: 5.
skip int
// stackTraceExtractor determines how stack traces are collected
// and transformed into PostHog-compatible data structures.
stackTraceExtractor StackTraceExtractor
// descriptionExtractor extracts a human-readable description to
// enrich the captured exception. By default, it searches the log
// record for fields like "err" or "error" and uses the extracted
// error message as the description.
descriptionExtractor DescriptionExtractor
// properties optionally returns custom properties to attach to the
// captured exception event. Returning nil attaches no extra properties.
// Built-in exception keys (e.g. "distinct_id") win on collision.
properties func(ctx context.Context, r slog.Record) Properties
}
func defaultCaptureConfig() captureConfig {
return captureConfig{
minCaptureLevel: slog.LevelWarn,
distinctID: func(ctx context.Context, r slog.Record) string {
return ""
},
fingerprint: func(ctx context.Context, r slog.Record) *string {
return nil
},
skip: 5,
stackTraceExtractor: DefaultStackTraceExtractor{
InAppDecider: SimpleInAppDecider,
},
descriptionExtractor: ErrorExtractor{
ErrorKeys: []string{"err", "error"},
Fallback: "<no linked error>",
},
properties: func(ctx context.Context, r slog.Record) Properties {
return nil
},
}
}
// SlogOption customizes NewSlogCaptureHandler.
type SlogOption func(*captureConfig)
// WithMinCaptureLevel sets the minimum slog level captured as a PostHog exception.
// Records below this level are still passed to the wrapped handler.
func WithMinCaptureLevel(l slog.Level) SlogOption {
return func(c *captureConfig) { c.minCaptureLevel = l }
}
// WithDistinctIDFn sets the function used to resolve the exception DistinctId.
// The fn parameter must be non-nil. If fn returns an empty string, the slog record is not captured.
func WithDistinctIDFn(fn func(ctx context.Context, r slog.Record) string) SlogOption {
return func(c *captureConfig) { c.distinctID = fn }
}
// WithFingerprintFn sets the function used to compute an optional exception fingerprint.
// The fn parameter must be non-nil.
func WithFingerprintFn(fn func(ctx context.Context, r slog.Record) *string) SlogOption {
return func(c *captureConfig) { c.fingerprint = fn }
}
// WithSkip sets how many stack frames the stack trace extractor should skip.
// Values less than or equal to zero are ignored.
func WithSkip(n int) SlogOption {
return func(c *captureConfig) {
if n > 0 {
c.skip = n
}
}
}
// WithStackTraceExtractor sets the StackTraceExtractor used for captured exceptions.
// The extractor parameter must be non-nil.
func WithStackTraceExtractor(extractor StackTraceExtractor) SlogOption {
return func(c *captureConfig) { c.stackTraceExtractor = extractor }
}
// WithDescriptionExtractor sets the DescriptionExtractor used for captured exceptions.
// The extractor parameter must be non-nil.
func WithDescriptionExtractor(extractor DescriptionExtractor) SlogOption {
return func(c *captureConfig) { c.descriptionExtractor = extractor }
}
// WithPropertiesFn sets a function that returns custom properties for captured exceptions.
// Built-in exception properties take precedence if keys collide. $lib and
// $lib_version are SDK defaults, so a value it returns wins.
func WithPropertiesFn(fn func(ctx context.Context, r slog.Record) Properties) SlogOption {
return func(c *captureConfig) {
if fn != nil {
c.properties = fn
}
}
}