sry

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package sry provides a convenient way to send events and start transactions for Sentry.

Example (Exception)

This function demonstrates how to use the sry package to record an error.

_ = sry.InitSentryDebug("https://abc@123.ingest.sentry.io/456")
defer sry.Flush()

sry.SendException(
	context.Background(),
	fmt.Errorf("failed to make a cup of joe: %w", errors.New("no coffee beans")),
	sry.ScopeLevel(sentry.LevelFatal),
	sry.ScopeUser(sentry.User{IPAddress: "{{auto}}"}),
	sry.ScopeTag("drink", "coffee"),
	sry.ScopeContextData("profile", map[string]interface{}{
		"flavor":  "vanilla",
		"content": []string{"espresso"},
	}),
)
Output:

Example (Message)

This function demonstrates how to use the sry package to record a message.

_ = sry.InitSentryDebug("https://abc@123.ingest.sentry.io/456")
defer sry.Flush()

sry.SendMessage(
	context.Background(),
	"made a cup of joe",
	sry.ScopeLevel(sentry.LevelDebug),
	sry.ScopeTag("drink", "coffee"),
	sry.ScopeExtra("foo", "bar"),
	sry.ScopeContextData("profile", map[string]interface{}{
		"flavor":  "vanilla",
		"content": []string{"ice_cream", "espresso"},
	}),
)
Output:

Example (Transaction)

This function demonstrates how to use the sry package to record a transaction.

_ = sry.InitSentryDebug("https://abc@123.ingest.sentry.io/456")
defer sry.Flush()

// set a global scope setter
sry.GlobalScopePush(func(s *sentry.Scope) {
	s.SetTag("example", "test")
})

// start a transaction
tx := sry.StartTransaction(
	context.Background(),
	"make_affogato",
	sry.SpanUser(sentry.User{IPAddress: "{{auto}}"}),
)
// just in case
defer tx.Complete()

// start a span
s1 := tx.StartChild("chill_glass",
	sry.SpanTag("temperature", "cold"),
	sry.SpanExtra("tool", []string{"fridge"}),
)
// doing ...
s1.Success()

// start another span
s2 := tx.StartChild("add_gelato")
s2.Apply(sry.SpanTag("flavor", "vanilla"))
// doing ...
s2.Success()

// start the last span
s3 := tx.StartChild("add_espresso", sry.SpanTag("strength", "strong"))
// doing ...
s3.Failure(sentry.SpanStatusResourceExhausted)
tx.Stop()

// final result
tx.Apply(
	sry.SpanLevel(sentry.LevelError),
	sry.SpanExtra("shots", 2),
	sry.SpanContextData("profile", map[string]interface{}{
		"flavor":  "vanilla",
		"content": []string{"ice_cream", "espresso"},
	}),
	sry.SpanScopeSet(func(s *sentry.Scope) {
		s.SetTag("progress", "blocked")
	}),
)
tx.Failure(sentry.SpanStatusInternalError)
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// FlushTimeout is the timeout for flushing events and transactions to Sentry.
	FlushTimeout = 5 * time.Second
)

Functions

func Flush

func Flush()

Flush flushes all events and transactions to Sentry.

func GlobalScopeCount added in v0.0.3

func GlobalScopeCount() int

GlobalScopeCount returns the number of global scope setters.

func GlobalScopePush added in v0.0.3

func GlobalScopePush(setter ScopeSetter)

GlobalScopePush pushes a scope setter to global scope setters.

func InitSentry

func InitSentry(dsn string) error

InitSentry initializes sentry-go with given DSN.

func InitSentryDebug

func InitSentryDebug(dsn string) error

InitSentryDebug initializes sentry-go with given DSN and debug mode.

func InitSentryRelease

func InitSentryRelease(dsn, release string) error

InitSentryRelease initializes sentry-go with given release name.

func NewSpanID added in v0.0.3

func NewSpanID(s string) sentry.SpanID

NewSpanID creates a new SpanID from a string, it attempts to convert as Hex string first, and then ASCII.

func NewTraceID

func NewTraceID(s string) sentry.TraceID

NewTraceID creates a new TraceID from a string, it attempts to convert as Hex string first, and then ASCII.

func SendException

func SendException(ctx context.Context, err error, options ...ScopeSetter) *sentry.EventID

SendException sends an error event to sentry.

func SendExceptionFromHub

func SendExceptionFromHub(hub *sentry.Hub, err error, options ...ScopeSetter) *sentry.EventID

SendExceptionFromHub sends an error event to sentry with given hub.

func SendMessage

func SendMessage(ctx context.Context, msg string, options ...ScopeSetter) *sentry.EventID

SendMessage sends a message event to sentry.

func SendMessageFromHub

func SendMessageFromHub(hub *sentry.Hub, msg string, options ...ScopeSetter) *sentry.EventID

SendMessageFromHub sends a message event to sentry with given hub.

Types

type ScopeSetter

type ScopeSetter func(s *sentry.Scope)

ScopeSetter is a function that sets a property of scope.

func GlobalScopeClone added in v0.0.3

func GlobalScopeClone() []ScopeSetter

GlobalScopeClone returns a copy of global scope setters.

func GlobalScopePop added in v0.0.3

func GlobalScopePop() ScopeSetter

GlobalScopePop pops the last scope setter from global scope setters.

func ScopeContextData

func ScopeContextData(section string, data sentry.Context) ScopeSetter

ScopeContextData is a ScopeSetter that sets a list of context of scope.

func ScopeExtra

func ScopeExtra(key string, data interface{}) ScopeSetter

ScopeExtra is a ScopeSetter that sets additional data of scope.

func ScopeFingerprint

func ScopeFingerprint(fingerprint []string) ScopeSetter

ScopeFingerprint is a ScopeSetter that sets a fingerprint of scope.

func ScopeLevel

func ScopeLevel(lvl sentry.Level) ScopeSetter

ScopeLevel is a ScopeSetter that sets a level of scope.

func ScopeTag

func ScopeTag(key, value string) ScopeSetter

ScopeTag is a ScopeSetter that sets a tag of scope.

func ScopeTags

func ScopeTags(tags map[string]string) ScopeSetter

ScopeTags is a ScopeSetter that sets tags of scope.

func ScopeTransaction

func ScopeTransaction(transaction string) ScopeSetter

ScopeTransaction is a ScopeSetter that sets a transaction of scope.

func ScopeUser

func ScopeUser(user sentry.User) ScopeSetter

ScopeUser is a ScopeSetter that sets a user of scope.

type SpanSetter

type SpanSetter func(s *SpanWrapper)

SpanSetter is a function that sets a property of Span and its Scope.

func SpanContextData

func SpanContextData(section string, data sentry.Context) SpanSetter

SpanContextData is a SpanSetter that sets a section of context data of Span, existing section will be overwritten. It works only on transaction/root span, since it's based on scope setter.

func SpanDescription

func SpanDescription(description string) SpanSetter

SpanDescription is a SpanSetter that sets the description of Span.

func SpanExtra

func SpanExtra(key string, data interface{}) SpanSetter

SpanExtra is a SpanSetter that sets a key-value as additional data of Span, existing key will be overwritten.

func SpanLevel

func SpanLevel(level sentry.Level) SpanSetter

SpanLevel is a SpanSetter that sets the level of Span. It works only on transaction/root span, since it's based on scope setter.

func SpanOptionSet added in v0.0.3

func SpanOptionSet(option sentry.SpanOption) SpanSetter

SpanOptionSet is a SpanSetter that apply a SpanOption to Span.

func SpanScopeSet

func SpanScopeSet(setter ScopeSetter) SpanSetter

SpanScopeSet is a SpanSetter that append a ScopeSetter to Span. It works only on transaction/root span, since it's a scope setter.

func SpanStatus

func SpanStatus(status sentry.SpanStatus) SpanSetter

SpanStatus is a SpanSetter that sets the status of Span.

func SpanTag

func SpanTag(key, value string) SpanSetter

SpanTag is a SpanSetter that sets a searchable tag of Span.

func SpanTraceID

func SpanTraceID(t sentry.TraceID) SpanSetter

SpanTraceID is a SpanSetter that sets the trace ID of Span.

func SpanTraceIDString

func SpanTraceIDString(ts string) SpanSetter

SpanTraceIDString is a SpanSetter that sets the trace ID of Span from raw string.

func SpanUser

func SpanUser(user sentry.User) SpanSetter

SpanUser is a SpanSetter that sets the user of Span. It works only on transaction/root span, since it's based on scope setter.

type SpanWrapper

type SpanWrapper struct {
	*sentry.Span
	// contains filtered or unexported fields
}

SpanWrapper wraps sentry.Span to provide a convenient way to start and finish a span.

func StartTransaction

func StartTransaction(ctx context.Context, name string, options ...SpanSetter) *SpanWrapper

StartTransaction starts a transaction with given name and options, if there's no active transaction in context. Otherwise, it starts a child span of the existing transaction. Note that the function with the same name in sentry-go package returns the existing transaction if there's one, which is not the behavior we want.

func (*SpanWrapper) Apply

func (s *SpanWrapper) Apply(options ...SpanSetter)

Apply applies all SpanSetter to the SpanWrapper.

func (*SpanWrapper) Complete

func (s *SpanWrapper) Complete()

Complete marks the transaction completed and as successful if it has not been marked.

func (*SpanWrapper) Failure

func (s *SpanWrapper) Failure(status sentry.SpanStatus)

Failure marks the transaction completed as failed with given status.

func (*SpanWrapper) Finish

func (s *SpanWrapper) Finish()

Finish is an alias of Complete() to shadow the original function.

func (*SpanWrapper) StartChild

func (s *SpanWrapper) StartChild(opName string, options ...SpanSetter) *SpanWrapper

StartChild starts a child span with given name and options.

func (*SpanWrapper) Stop

func (s *SpanWrapper) Stop()

Stop sets the end time of Span.

func (*SpanWrapper) Success

func (s *SpanWrapper) Success()

Success marks the transaction completed as successful.

Jump to

Keyboard shortcuts

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