Documentation ¶
Overview ¶
Example (BreadcrumbTransport) ¶
// This will not work without SENTRY_DSN environment variable _ = sentry.Init(sentry.ClientOptions{ Transport: sentry.NewHTTPSyncTransport(), }) // Create non-default http-client with client := http.Client{ Transport: NewBreadcrumbTransport(sentry.LevelDebug, nil), } // Create context with sentry.Hub // This is not required: if hub is not available from context sentry.CurrentHub() will be used instead ctx := WithHub(context.Background(), sentry.CurrentHub()) req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "https://go.pr0ger.dev/", nil) resp, err := client.Do(req) if err != nil { // Either send error to sentry sentry.CaptureException(err) return } defer resp.Body.Close() // Or just log response sentry.CaptureMessage(fmt.Sprintf("Response status: %s", resp.Status)) // Either way it will contain full info about request in breadcrumb
Output:
Example (Simple) ¶
// This will not work without SENTRY_DSN environment variable _ = sentry.Init(sentry.ClientOptions{ Transport: sentry.NewHTTPSyncTransport(), }) // Create logger for logging directly to sentry (without local output) log := zap.New(NewSentryCore(sentry.CurrentHub())) log.Debug("this message will be logged as breadcrumb", zap.Int("key", 1337)) log.Error("and this will create event in sentry") log.Error("and this message will attach stacktrace", zap.Error(errors.New("error from pkg/errors")))
Output:
Example (WebServer) ¶
// This will not work without SENTRY_DSN environment variable _ = sentry.Init(sentry.ClientOptions{ Transport: sentry.NewHTTPSyncTransport(), TracesSampleRate: 1, }) // Create core for logging to stdout/stderr localCore := NewCore(true) // Create core splitter to logging both to local and sentry // zapcore.NewTee also can be used, but is not recommended if you want to use RequestLogger middleware core := NewSentryCoreWrapper(localCore, sentry.CurrentHub()) // And create logger logger := zap.New(core) logger.Debug("this event will be logged to stdout but will not appear in request breadcrumbs") // Create handler for network requests handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log := Ctx(r.Context()) log.Debug("some debug logs from request") // Create an HTTP client with our transport client := http.Client{ Transport: NewBreadcrumbTransport(sentry.LevelInfo, http.DefaultTransport), } // We need to pass current context to HTTP request so transport will know where to log req, _ := http.NewRequestWithContext(r.Context(), http.MethodGet, "https://go.pr0ger.dev/logger", nil) resp, err := client.Do(req) if err != nil { log.Warn("request failed", zap.Error(err)) } else { log.Info(fmt.Sprintf("Response status: %s", resp.Status)) resp.Body.Close() } _, _ = w.Write([]byte("ok")) log.Error("let's assume we have an error here") }) // And use it with our middleware server := &http.Server{ Addr: ":8080", Handler: RequestLogger(logger)(handler), } _ = server.ListenAndServe()
Output:
Index ¶
- Constants
- func Ctx(ctx context.Context) *zap.Logger
- func ForkedLogger(logger *zap.Logger) *zap.Logger
- func Hub(ctx context.Context) *sentry.Hub
- func NewBreadcrumbTransport(level sentry.Level, transport http.RoundTripper) http.RoundTripper
- func NewCore(debug bool) zapcore.Core
- func NewSentryCore(hub *sentry.Hub, options ...SentryCoreOption) zapcore.Core
- func NewSentryCoreWrapper(localCore zapcore.Core, hub *sentry.Hub, options ...SentryCoreOption) zapcore.Core
- func RequestID(ctx context.Context) string
- func RequestLogger(logger *zap.Logger) func(next http.Handler) http.Handler
- func SentryLevel(level zapcore.Level) sentry.Level
- func SpanStatus(httpCode int) sentry.SpanStatus
- func WithExtraFields(fieldsGenerator func(r *http.Request) []zap.Field) func(next http.Handler) http.Handler
- func WithHub(ctx context.Context, hub *sentry.Hub) context.Context
- func WithLogger(ctx context.Context, logger *zap.Logger) context.Context
- func WithRequestID(ctx context.Context, requestID string) context.Context
- type SentryCore
- type SentryCoreOption
- type WrapResponseWriter
Examples ¶
Constants ¶
const ( // BreadcrumbTypeDefault describes a generic breadcrumb. BreadcrumbTypeDefault = "default" // BreadcrumbTypeHTTP describes an HTTP request breadcrumb. BreadcrumbTypeHTTP = "http" )
https://docs.sentry.io/development/sdk-dev/event-payloads/breadcrumbs/#breadcrumb-types
const ( BreadcrumbDataURL = "url" BreadcrumbDataMethod = "method" BreadcrumbDataStatusCode = "status_code" BreadcrumbDataReason = "reason" )
Describes data for an HTTP request breadcrumb https://docs.sentry.io/development/sdk-dev/event-payloads/breadcrumbs/#http
Variables ¶
This section is empty.
Functions ¶
func Ctx ¶
Ctx returns the in-context Logger for a request. If no logger is associated returns no-op logger.
func ForkedLogger ¶ added in v1.3.0
ForkedLogger will return a new logger with isolated sentry.Hub. No-op if logger is not using SentryCore.
func Hub ¶
Hub returns the sentry.Hub associated with the context. If no hub is associated returns CurrentHub().
func NewBreadcrumbTransport ¶
func NewBreadcrumbTransport(level sentry.Level, transport http.RoundTripper) http.RoundTripper
func NewCore ¶
NewCore will create handy Core with sensible defaults: - messages with error level and higher will go to stderr, everything else to stdout - use json encoder for production and console for development.
func NewSentryCore ¶
func NewSentryCore(hub *sentry.Hub, options ...SentryCoreOption) zapcore.Core
func NewSentryCoreWrapper ¶
func NewSentryCoreWrapper(localCore zapcore.Core, hub *sentry.Hub, options ...SentryCoreOption) zapcore.Core
NewSentryCoreWrapper creates a Core that duplicates log entries into provided local Core and implicitly created Sentry core.
func RequestLogger ¶
RequestLogger is a middleware for injecting sentry.Hub and zap.Logger into request context. If provided logger has sentryCoreWrapper as core injected logger will have core with same local core and sentry core based on an empty Hub for each request so breadcrumbs list will be empty each time. In other case logger.Core() will be used as a local core and sentry core will be created if sentry is initialized.
func SentryLevel ¶
func SpanStatus ¶ added in v1.3.0
func SpanStatus(httpCode int) sentry.SpanStatus
func WithExtraFields ¶ added in v1.4.0
func WithExtraFields(fieldsGenerator func(r *http.Request) []zap.Field) func(next http.Handler) http.Handler
WithExtraFields is a middleware for injecting extra field to the logger injected by RequestLogger middleware.
func WithLogger ¶
WithLogger returns a copy of provided context with added logger field.
Types ¶
type SentryCore ¶
type SentryCore struct { zapcore.LevelEnabler BreadcrumbLevel zapcore.Level EventLevel zapcore.Level // contains filtered or unexported fields }
func (*SentryCore) Check ¶
func (s *SentryCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry
func (*SentryCore) Sync ¶
func (s *SentryCore) Sync() error
type SentryCoreOption ¶
type SentryCoreOption func(*SentryCore)
func BreadcrumbLevel ¶
func BreadcrumbLevel(level zapcore.Level) SentryCoreOption
BreadcrumbLevel will set a minimum level of messages should be stored as breadcrumbs.
func EventLevel ¶
func EventLevel(level zapcore.Level) SentryCoreOption
EventLevel will set a minimum level of messages should be sent as events.
type WrapResponseWriter ¶ added in v1.2.0
type WrapResponseWriter interface { http.ResponseWriter Status() int BytesWritten() int }
func NewWrapResponseWriter ¶ added in v1.2.0
func NewWrapResponseWriter(w http.ResponseWriter, protoMajor int) WrapResponseWriter