-
Notifications
You must be signed in to change notification settings - Fork 29
feat: add support for custom properties in error tracking #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
7ea73da
244818a
0702aaa
a9395b7
0cf6ec8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ type Exception struct { | |
|
|
||
| DistinctId string | ||
| Timestamp time.Time | ||
| Properties Properties | ||
| DisableGeoIP bool | ||
|
|
||
| // Typed properties that end up in the API "properties" object: | ||
|
|
@@ -51,21 +52,12 @@ type StackFrame struct { | |
| } | ||
|
|
||
| type ExceptionInApi struct { | ||
| Type string `json:"type"` | ||
| Library string `json:"library"` | ||
| LibraryVersion string `json:"library_version"` | ||
| Timestamp time.Time `json:"timestamp"` | ||
| Event string `json:"event"` | ||
| Properties ExceptionInApiProperties `json:"properties"` | ||
| } | ||
|
|
||
| type ExceptionInApiProperties struct { | ||
| Lib string `json:"$lib"` | ||
| LibVersion string `json:"$lib_version"` | ||
| DistinctId string `json:"distinct_id"` | ||
| DisableGeoIP bool `json:"$geoip_disable,omitempty"` | ||
| ExceptionList []ExceptionItem `json:"$exception_list"` | ||
| ExceptionFingerprint *string `json:"$exception_fingerprint,omitempty"` | ||
| Type string `json:"type"` | ||
| Library string `json:"library"` | ||
| LibraryVersion string `json:"library_version"` | ||
| Timestamp time.Time `json:"timestamp"` | ||
| Event string `json:"event"` | ||
| Properties Properties `json:"properties"` | ||
| } | ||
|
|
||
| func (msg Exception) internal() { panic(unimplementedError) } | ||
|
|
@@ -116,25 +108,39 @@ func (msg ExceptionItem) Validate() error { | |
| func (msg Exception) APIfy() APIMessage { | ||
| libVersion := getVersion() | ||
|
|
||
| properties := Properties{}. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, wouldn't it be better to re-use
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't do that unfortunately, because that'd mutate the caller's |
||
| Set("$lib", SDKName). | ||
| Set("$lib_version", libVersion). | ||
| Set("distinct_id", msg.DistinctId). | ||
| Set("$exception_list", msg.ExceptionList) | ||
|
|
||
| if msg.DisableGeoIP { | ||
| properties.Set("$geoip_disable", true) | ||
| } | ||
|
|
||
| if msg.ExceptionFingerprint != nil { | ||
| properties.Set("$exception_fingerprint", msg.ExceptionFingerprint) | ||
| } | ||
|
|
||
| if msg.Properties != nil { | ||
| for k, v := range msg.Properties { | ||
| properties[k] = v | ||
| } | ||
| } | ||
|
|
||
| return ExceptionInApi{ | ||
| Type: msg.Type, // set to "exception" by Enqueue switch | ||
| Type: msg.Type, | ||
| Event: "$exception", | ||
| Library: SDKName, | ||
| LibraryVersion: libVersion, | ||
| Timestamp: msg.Timestamp, | ||
| Properties: ExceptionInApiProperties{ | ||
| Lib: SDKName, | ||
| LibVersion: libVersion, | ||
| DistinctId: msg.DistinctId, | ||
| DisableGeoIP: msg.DisableGeoIP, | ||
| ExceptionList: msg.ExceptionList, | ||
| ExceptionFingerprint: msg.ExceptionFingerprint, | ||
| }, | ||
| Properties: properties, | ||
| } | ||
| } | ||
|
|
||
| // NewDefaultException is a convenience function to build an Exception object (usable for `client.Enqueue`) | ||
| // with sane defaults. If you want more control, please manually build the Exception object. | ||
| // Use .WithProperties() to add custom properties to the exception. | ||
| func NewDefaultException( | ||
| timestamp time.Time, | ||
| distinctID, title, description string, | ||
|
|
@@ -153,3 +159,8 @@ func NewDefaultException( | |
| }, | ||
| } | ||
| } | ||
|
|
||
| func (e Exception) WithProperties(properties Properties) Exception { | ||
| e.Properties = properties | ||
| return e | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -36,6 +36,9 @@ type captureConfig struct { | |||||||||||||||
| // record for fields like "err" or "error" and uses the extracted | ||||||||||||||||
| // error message as the description. | ||||||||||||||||
| descriptionExtractor DescriptionExtractor | ||||||||||||||||
|
|
||||||||||||||||
| // event properties to attach to the captured exception event. | ||||||||||||||||
| properties func(ctx context.Context, r slog.Record) Properties | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func defaultCaptureConfig() captureConfig { | ||||||||||||||||
|
|
@@ -55,6 +58,9 @@ func defaultCaptureConfig() captureConfig { | |||||||||||||||
| ErrorKeys: []string{"err", "error"}, | ||||||||||||||||
| Fallback: "<no linked error>", | ||||||||||||||||
| }, | ||||||||||||||||
| properties: func(ctx context.Context, r slog.Record) Properties { | ||||||||||||||||
| return NewProperties() | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor, but is it worth considering having something like this (shamelessly taken from the unit test) as default behavior?
Suggested change
I figure that most users will likely do something like this as their default behaviour, and if they don't they can override it through |
||||||||||||||||
| }, | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -87,3 +93,9 @@ func WithStackTraceExtractor(extractor StackTraceExtractor) SlogOption { | |||||||||||||||
| func WithDescriptionExtractor(extractor DescriptionExtractor) SlogOption { | ||||||||||||||||
| return func(c *captureConfig) { c.descriptionExtractor = extractor } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // WithPropertiesFn sets a custom function to extract properties from slog records. | ||||||||||||||||
| // This allows you to attach custom metadata from log records to exception events. | ||||||||||||||||
| func WithPropertiesFn(fn func(ctx context.Context, r slog.Record) Properties) SlogOption { | ||||||||||||||||
| return func(c *captureConfig) { c.properties = fn } | ||||||||||||||||
| } | ||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we follow semver (Do we here?) this should be a major since it'll break people's apps compilation until they change the type
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like in
CHANGELOG.mdwe previously used a minor version for a breaking changes release, so not sure if we're strictly following semver. Happy to bump to2.0.0if we want to start being more strict about it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, the versioning in Go got messed up at some point and people often release breaking changes as 1.X change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imo, until the sdk reaches feature parity with the other more mature ones (e.g. js sdk), breaking changes are okay without major version bump. The APIs in go sdk still leaves a lot to be desired (compared to the other ones). I'd assume a few other breaking changes like this might be necessary going forward.