zapsentry

package module
v1.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2021 License: MIT Imports: 6 Imported by: 0

README

Sentry client for zap logger

Integration of sentry client into zap.Logger is pretty simple:

func modifyToSentryLogger(log *zap.Logger, DSN string) *zap.Logger {
	cfg := zapsentry.Configuration{
		Level: zapcore.ErrorLevel, //when to send message to sentry
		EnableBreadcrumbs: true, // enable sending breadcrumbs to Sentry 
		BreadcrumbLevel: zapcore.InfoLevel, // at what level should we sent breadcrumbs to sentry
		Tags: map[string]string{
			"component": "system",
		},
	}
	core, err := zapsentry.NewCore(cfg, zapsentry.NewSentryClientFromDSN(DSN))
	
	// to use breadcrumbs feature - create new scope explicitly
	log = log.With(zapsentry.NewScope())
	
	//in case of err it will return noop core. so we can safely attach it
	if err != nil {
		log.Warn("failed to init zap", zap.Error(err))
	}
	return zapsentry.AttachCoreToLogger(core, log)
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AttachCoreToLogger

func AttachCoreToLogger(sentryCore zapcore.Core, l *zap.Logger) *zap.Logger
Example
package main

import (
	"fmt"
	"log"
	"time"

	"github.com/getsentry/sentry-go"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"go.uber.org/zap/zaptest/observer"

	"github.com/l2cup/zapsentry"
)

func main() {
	// Setup zap with observer (for testing), originally we use
	// 		config = zap.NewDevelopmentConfig()
	//  	logger, err := config.Build()
	// to build zap logger, here we use zap/zaptest/observer for testing
	core, recordedLogs := observer.New(zapcore.DebugLevel)
	logger := zap.New(core, zap.AddStacktrace(zap.DebugLevel))

	// Setup mock sentry client for testing, in general we use sentry.NewClient
	var recordedSentryEvent *sentry.Event
	sentryClient := mockSentryClient(func(event *sentry.Event) {
		recordedSentryEvent = event
	})

	// Setup zapsentry
	core, err := zapsentry.NewCore(
		zapsentry.NewSentryClientFromClient(sentryClient),
		zapsentry.Level(zapcore.ErrorLevel),
		zapsentry.ConvertFieldsToTags("method"),
		zapsentry.WithBreadcrumbs(zapcore.InfoLevel),
	)
	if err != nil {
		log.Fatal(err)
	}
	newLogger := zapsentry.AttachCoreToLogger(core, logger)

	// Send error log
	newLogger.
		With(zapsentry.NewScope()).
		Error("[error] something went wrong!", zap.String("method", "unknown"))

	// Check output
	fmt.Println(recordedLogs.All()[0].Message)
	fmt.Println(recordedSentryEvent.Message)
	fmt.Println(recordedSentryEvent.Extra)
}

func mockSentryClient(f func(event *sentry.Event)) *sentry.Client {
	client, _ := sentry.NewClient(sentry.ClientOptions{
		Dsn:       "",
		Transport: &transport{MockSendEvent: f},
	})
	return client
}

type transport struct {
	MockSendEvent func(event *sentry.Event)
}

// Flush waits until any buffered events are sent to the Sentry server, blocking
// for at most the given timeout. It returns false if the timeout was reached.
func (f *transport) Flush(_ time.Duration) bool { return true }

// Configure is called by the Client itself, providing it it's own ClientOptions.
func (f *transport) Configure(_ sentry.ClientOptions) {}

// SendEvent assembles a new packet out of Event and sends it to remote server.
// We use this method to capture the event for testing
func (f *transport) SendEvent(event *sentry.Event) {
	f.MockSendEvent(event)
}
Output:

[error] something went wrong!
[error] something went wrong!
map[method:unknown]

func NewCore

func NewCore(factory SentryClientFactory, opts ...Option) (zapcore.Core, error)

func NewScope added in v1.9.0

func NewScope() zapcore.Field

func WrapHub added in v1.9.0

func WrapHub(hub *sentry.Hub) zapcore.Field

func WrapScope added in v1.9.0

func WrapScope(scope *sentry.Scope) zapcore.Field

Types

type DefaultExceptionProvider added in v1.9.0

type DefaultExceptionProvider struct {
	// contains filtered or unexported fields
}

func NewExceptionProvider added in v1.9.0

func NewExceptionProvider(ff StacktraceFrameFilter) *DefaultExceptionProvider

NewExceptionProvider returns anew DefaultExceptionProvider with the passed StacktraceFrameFilter as it's frame filter.

func (*DefaultExceptionProvider) Exception added in v1.9.0

func (dep *DefaultExceptionProvider) Exception(ent zapcore.Entry) []sentry.Exception

Exception accepts a zapcore.Entry and provides a sentry exception. Sentry defines it's exception as an Exception array. This array should contain one exception if an exception exists It will return an empty array if no exception is created.

type DefaultStacktraceFrameFilter added in v1.9.0

type DefaultStacktraceFrameFilter struct{}

DefaultStacktraceFrameFilter is a default StacktraceFrameFilter implementation It uses the same login as the original TheZeroSlave/zapsentry implementation of stack trace filtering uses. It has sane defaults so it's still the default stack trace filter.

func (*DefaultStacktraceFrameFilter) FilterFrames added in v1.9.0

func (sff *DefaultStacktraceFrameFilter) FilterFrames(frames []sentry.Frame) []sentry.Frame

FilterFrames will filter out unwanted stacktrace frames

type ExceptionProvider added in v1.9.0

type ExceptionProvider interface {
	// Exception accepts a zapcore.Entry and provides a sentry exception.
	// Sentry defines it's exception as an Exception array.
	// This array should contain one exception if an exception exists
	// It will return an empty array if no exception is created.
	Exception(ent zapcore.Entry) []sentry.Exception
}

ExceptionProvider provides sentry exceptions from zapcore's entries.

type LevelEnabler added in v1.9.0

type LevelEnabler struct {
	// contains filtered or unexported fields
}

func (*LevelEnabler) Enabled added in v1.9.0

func (l *LevelEnabler) Enabled(lvl zapcore.Level) bool

type NopExceptionProvider added in v1.9.0

type NopExceptionProvider struct{}

NopExceptionProvider is a ExceptionProvider implementation which always provides an empty exception array. Used when exceptions are disabled.

func (*NopExceptionProvider) Exception added in v1.9.0

func (nep *NopExceptionProvider) Exception(_ zapcore.Entry) []sentry.Exception

Exception accepts a zapcore.Entry and provides a sentry exception. Sentry defines it's exception as an Exception array. This array should contain one exception if an exception exists It will return an empty array if no exception is created.

type Option added in v1.9.0

type Option func(c *core) error

func ConvertFieldsToTags added in v1.9.0

func ConvertFieldsToTags(keys ...string) Option

func DisableStacktrace added in v1.9.0

func DisableStacktrace() Option

func Level added in v1.9.0

func Level(lvl zapcore.Level) Option

func UseHub added in v1.9.0

func UseHub(hub *sentry.Hub) Option

func UseStacktraceFrameFilter added in v1.9.0

func UseStacktraceFrameFilter(ff StacktraceFrameFilter) Option

func WithBreadcrumbs added in v1.9.0

func WithBreadcrumbs(level zapcore.Level) Option

func WithEnvironment added in v1.9.0

func WithEnvironment(env string) Option

func WithFlushTimeout added in v1.9.0

func WithFlushTimeout(after time.Duration) Option

func WithGlobalBreadcrumbs added in v1.9.0

func WithGlobalBreadcrumbs() Option

func WithPlaform added in v1.9.0

func WithPlaform(platform string) Option

func WithTags added in v1.9.0

func WithTags(tags map[string]string) Option

type SentryClientFactory

type SentryClientFactory func() (*sentry.Client, error)

func NewSentryClientFromClient

func NewSentryClientFromClient(client *sentry.Client) SentryClientFactory

func NewSentryClientFromDSN

func NewSentryClientFromDSN(DSN string) SentryClientFactory

type StacktraceFrameFilter added in v1.9.0

type StacktraceFrameFilter interface {
	// FilterFrames will filter out unwanted stacktrace frames
	FilterFrames(frames []sentry.Frame) []sentry.Frame
}

StacktraceFrameFilter filters stacktrace frames. Used to skip unnecesarry stack trace frames.

type Tagger added in v1.9.0

type Tagger interface {
	// Tags returns custom defined tags for the type
	Tags() map[string]string
}

Tagger allows adding custom tags on datatypes

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL