diff --git a/errortracking/capture.go b/errortracking/capture.go index e1135594719e83037e336abbea47f5e368a2903c..536540828196520585ed9b3dbed25f7a0246fdc2 100644 --- a/errortracking/capture.go +++ b/errortracking/capture.go @@ -8,14 +8,8 @@ import ( // Capture will report an error to the error reporting service func Capture(err error, opts ...CaptureOption) { - event := sentry.NewEvent() - event.Level = sentry.LevelError - - for _, v := range opts { - v(event) - } - - event.Exception = []sentry.Exception{ + config := applyCaptureOptions(opts) + config.event.Exception = []sentry.Exception{ { Type: reflect.TypeOf(err).String(), Value: err.Error(), @@ -23,5 +17,5 @@ func Capture(err error, opts ...CaptureOption) { }, } - sentry.CaptureEvent(event) + config.hub.CaptureEvent(config.event) } diff --git a/errortracking/capture_context.go b/errortracking/capture_context.go index fda98ed6e70d476326c664dcc1336689b7e9d151..11454cd1c40f09b40facd8c77080e709703a5d85 100644 --- a/errortracking/capture_context.go +++ b/errortracking/capture_context.go @@ -3,8 +3,6 @@ package errortracking import ( "context" - "github.com/getsentry/sentry-go" - "gitlab.com/gitlab-org/labkit/correlation" ) @@ -12,10 +10,10 @@ const sentryExtraKey = "gitlab.CorrelationID" // WithContext will extract information from the context to add to the error func WithContext(ctx context.Context) CaptureOption { - return func(event *sentry.Event) { + return func(config *captureConfig) { correlationID := correlation.ExtractFromContext(ctx) if correlationID != "" { - event.Tags[sentryExtraKey] = correlationID + config.event.Tags[sentryExtraKey] = correlationID } } } diff --git a/errortracking/capture_field.go b/errortracking/capture_field.go index 59ca7ec4e1ab35cb09921e46f9fab42f8658b018..9cf31417d153624e23e77ad4bfd9de856673fedc 100644 --- a/errortracking/capture_field.go +++ b/errortracking/capture_field.go @@ -1,12 +1,8 @@ package errortracking -import ( - "github.com/getsentry/sentry-go" -) - // WithField allows to add a custom field to the error func WithField(key string, value string) CaptureOption { - return func(event *sentry.Event) { - event.Tags[key] = value + return func(config *captureConfig) { + config.event.Tags[key] = value } } diff --git a/errortracking/capture_field_test.go b/errortracking/capture_field_test.go index 5a1eb2685c54d270987bc429b76098ad466b1457..7c69de72995d21d5ccad380149fcf0a2658faad2 100644 --- a/errortracking/capture_field_test.go +++ b/errortracking/capture_field_test.go @@ -11,7 +11,7 @@ func TestWithField(t *testing.T) { event := sentry.NewEvent() domain := "http://example.com" - WithField("domain", domain)(event) + WithField("domain", domain)(&captureConfig{event: event}) require.True(t, event.Tags["domain"] == domain) } diff --git a/errortracking/capture_options.go b/errortracking/capture_options.go index 7adabc3a589768bd6d14bbf22f67e95c01d1ce55..a8f50d01c2da9df4cab2d7a63be7a6d19d8a4405 100644 --- a/errortracking/capture_options.go +++ b/errortracking/capture_options.go @@ -5,4 +5,24 @@ import ( ) // CaptureOption will configure how an error is captured -type CaptureOption func(*sentry.Event) +type CaptureOption func(*captureConfig) + +// The configuration for Capture. +type captureConfig struct { + hub *sentry.Hub + event *sentry.Event +} + +func applyCaptureOptions(opts []CaptureOption) captureConfig { + event := sentry.NewEvent() + event.Level = sentry.LevelError + config := captureConfig{ + hub: sentry.CurrentHub(), + event: event, + } + for _, v := range opts { + v(&config) + } + + return config +} diff --git a/errortracking/capture_request.go b/errortracking/capture_request.go index 3db06569acfd90a7d54ac365fcc1e2fe6d19a93c..ac4c52a64d0fb085b97b0e5bf4b892de5d5154e2 100644 --- a/errortracking/capture_request.go +++ b/errortracking/capture_request.go @@ -10,7 +10,8 @@ import ( // WithRequest will capture details of the request along with the error func WithRequest(r *http.Request) CaptureOption { - return func(event *sentry.Event) { + return func(config *captureConfig) { + event := config.event event.Request = redactRequestInfo(r) event.Request.URL = r.URL.String() event.Request.Headers["host"] = r.URL.Hostname() diff --git a/errortracking/initialization.go b/errortracking/initialization.go index 9b03b62139e84edfa14f00c9a50b9085b7df7807..daee78b825066e577408cf711696a0a1bd741b42 100644 --- a/errortracking/initialization.go +++ b/errortracking/initialization.go @@ -6,16 +6,25 @@ import ( // Initialize will initialize error reporting func Initialize(opts ...InitializationOption) error { - config := applyInitializationOptions(opts) + return sentry.Init(optionsToClientOptions(opts...)) +} + +func NewClient(opts ...InitializationOption) (*sentry.Client, error) { + return sentry.NewClient(optionsToClientOptions(opts...)) +} - sentryClientOptions := sentry.ClientOptions{ +func optionsToClientOptions(opts ...InitializationOption) sentry.ClientOptions { + config := applyInitializationOptions(opts) + return sentry.ClientOptions{ Dsn: config.sentryDSN, + Release: config.version, Environment: config.sentryEnvironment, } +} - if config.version != "" { - sentryClientOptions.Release = config.version +// WithHub allows to set a sentry.Hub to use. +func WithHub(hub *sentry.Hub) CaptureOption { + return func(config *captureConfig) { + config.hub = hub } - - return sentry.Init(sentryClientOptions) }