github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/log/sentry.go (about)

     1  package log
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/getsentry/sentry-go"
     7  	"go.uber.org/zap/zapcore"
     8  )
     9  
    10  /*
    11     Creation Time: 2019 - Apr - 24
    12     Created by:  (ehsan)
    13     Maintainers:
    14        1.  Ehsan N. Moosa (E2)
    15     Auditor: Ehsan N. Moosa (E2)
    16     Copyright Ronak Software Group 2020
    17  */
    18  
    19  type sentryCore struct {
    20  	zapcore.LevelEnabler
    21  	hub  *sentry.Hub
    22  	tags map[string]string
    23  }
    24  
    25  func NewSentryCore(sentryDSN, release, environment string, level zapcore.Level, tags map[string]string) zapcore.Core {
    26  	if len(sentryDSN) == 0 {
    27  		return nil
    28  	}
    29  	client, err := sentry.NewClient(sentry.ClientOptions{
    30  		Dsn:         sentryDSN,
    31  		Release:     release,
    32  		Environment: environment,
    33  	})
    34  	if err != nil {
    35  		return zapcore.NewNopCore()
    36  	}
    37  
    38  	sentryScope := sentry.NewScope()
    39  	sentryHub := sentry.NewHub(client, sentryScope)
    40  
    41  	return &sentryCore{
    42  		hub:          sentryHub,
    43  		tags:         tags,
    44  		LevelEnabler: level,
    45  	}
    46  }
    47  
    48  func (c *sentryCore) With(fs []zapcore.Field) zapcore.Core {
    49  	return &sentryCore{
    50  		hub:          c.hub,
    51  		tags:         c.tags,
    52  		LevelEnabler: c.LevelEnabler,
    53  	}
    54  }
    55  
    56  func (c *sentryCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
    57  	if c.LevelEnabler.Enabled(ent.Level) {
    58  		return ce.AddCore(ent, c)
    59  	}
    60  
    61  	return ce
    62  }
    63  
    64  func (c *sentryCore) Write(ent zapcore.Entry, fs []zapcore.Field) error {
    65  	m := make(map[string]interface{}, len(fs))
    66  	enc := zapcore.NewMapObjectEncoder()
    67  	for _, f := range fs {
    68  		f.AddTo(enc)
    69  	}
    70  	for k, v := range enc.Fields {
    71  		m[k] = v
    72  	}
    73  
    74  	event := sentry.NewEvent()
    75  	event.Message = ent.Message
    76  	event.Timestamp = ent.Time
    77  	event.Level = sentryLevel(ent.Level)
    78  	event.Extra = m
    79  	event.Tags = c.tags
    80  	c.hub.CaptureEvent(event)
    81  
    82  	// We may be crashing the program, so should flush any buffered events.
    83  	if ent.Level > zapcore.ErrorLevel {
    84  		c.hub.Flush(time.Second)
    85  	}
    86  
    87  	return nil
    88  }
    89  
    90  func (c *sentryCore) Sync() error {
    91  	c.hub.Flush(time.Second * 3)
    92  
    93  	return nil
    94  }
    95  
    96  func sentryLevel(lvl zapcore.Level) sentry.Level {
    97  	switch lvl {
    98  	case zapcore.DebugLevel:
    99  		return sentry.LevelDebug
   100  	case zapcore.InfoLevel:
   101  		return sentry.LevelInfo
   102  	case zapcore.WarnLevel:
   103  		return sentry.LevelWarning
   104  	case zapcore.ErrorLevel:
   105  		return sentry.LevelError
   106  	case zapcore.DPanicLevel:
   107  		return sentry.LevelFatal
   108  	case zapcore.PanicLevel:
   109  		return sentry.LevelFatal
   110  	case zapcore.FatalLevel:
   111  		return sentry.LevelFatal
   112  	default:
   113  		// Unrecognized levels are fatal.
   114  		return sentry.LevelFatal
   115  	}
   116  }