From 74a5e6fbb20cd6650e29749365654abbba105676 Mon Sep 17 00:00:00 2001 From: Mikhail Mazurskiy Date: Mon, 12 Oct 2020 13:57:40 +1100 Subject: [PATCH] Support non-global Sentry --- errortracking/capture.go | 12 +++--------- errortracking/capture_context.go | 6 ++---- errortracking/capture_field.go | 8 ++------ errortracking/capture_field_test.go | 2 +- errortracking/capture_options.go | 22 +++++++++++++++++++++- errortracking/capture_request.go | 3 ++- errortracking/initialization.go | 21 +++++++++++++++------ 7 files changed, 46 insertions(+), 28 deletions(-) diff --git a/errortracking/capture.go b/errortracking/capture.go index e113559..5365408 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 fda98ed..11454cd 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 59ca7ec..9cf3141 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 5a1eb26..7c69de7 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 7adabc3..a8f50d0 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 3db0656..ac4c52a 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 9b03b62..daee78b 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) } -- GitLab