github.com/blend/go-sdk@v1.20220411.3/sentry/add_listeners.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package sentry 9 10 import ( 11 "github.com/blend/go-sdk/configmeta" 12 "github.com/blend/go-sdk/logger" 13 "github.com/blend/go-sdk/webutil" 14 ) 15 16 // AddListeners adds error listeners. 17 func AddListeners(log logger.Listenable, meta configmeta.Meta, cfg Config, opts ...AddListenersOption) error { 18 if log == nil || cfg.IsZero() { 19 return nil 20 } 21 if cfg.Environment == "" { 22 cfg.Environment = meta.ServiceEnv 23 } 24 if cfg.ServerName == "" { 25 cfg.ServerName = meta.ServiceName 26 } 27 if cfg.Release == "" { 28 cfg.Release = meta.Version 29 } 30 31 if typed, ok := log.(logger.InfofReceiver); ok { 32 typed.Infof("using sentry host: %s and environment: %s", webutil.MustParseURL(cfg.DSN).Hostname(), cfg.Environment) 33 } 34 client, err := New(cfg) 35 if err != nil { 36 return err 37 } 38 39 options := AddListenersOptions{ 40 EnabledFlags: DefaultListenerFlags, 41 Scopes: logger.ScopesAll(), 42 } 43 for _, opt := range opts { 44 opt(&options) 45 } 46 47 listener := logger.NewScopedErrorEventListener(client.Notify, options.Scopes) 48 for _, flag := range options.EnabledFlags { 49 log.Listen(flag, ListenerName, listener) 50 } 51 return nil 52 } 53 54 // AddListenersOptions are all the options we can set when 55 // adding Sentry error listeners 56 type AddListenersOptions struct { 57 EnabledFlags []string 58 Scopes *logger.Scopes 59 } 60 61 // AddListenersOption mutates AddListeners options 62 type AddListenersOption func(options *AddListenersOptions) 63 64 // AddListenersOptionFlags sets the logger flags to send Sentry 65 // notifications for 66 func AddListenersOptionFlags(flags ...string) AddListenersOption { 67 return func(options *AddListenersOptions) { 68 options.EnabledFlags = flags 69 } 70 } 71 72 // AddListenersOptionScopes sets the logger scopes to send Sentry 73 // notifications for 74 func AddListenersOptionScopes(scopes ...string) AddListenersOption { 75 return func(options *AddListenersOptions) { 76 options.Scopes = logger.NewScopes(scopes...) 77 } 78 }