Documentation ¶
Overview ¶
Package sentryhttp provides Sentry integration for servers based on the net/http package.
Example ¶
For a longer and executable example, see https://github.com/getsentry/sentry-go/tree/master/_examples/http.
// Initialize the Sentry SDK once in the main function. // sentry.Init(...) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Use GetHubFromContext to get a hub associated with the current // request. Hubs provide data isolation, such that tags, breadcrumbs // and other attributes are never mixed up across requests. hub := sentry.GetHubFromContext(r.Context()) _, err := http.Get("example.com") if err != nil { hub.CaptureException(err) } }) // Wrap the default mux with Sentry to capture panics and report errors. // // Alternatively, you can also wrap individual handlers if you need to use // different options for different parts of your app. handler := sentryhttp.New(sentryhttp.Options{}).Handle(http.DefaultServeMux) server := http.Server{ Addr: ":0", ReadHeaderTimeout: 3 * time.Second, Handler: handler, } server.ListenAndServe()
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
A Handler is an HTTP middleware factory that provides integration with Sentry.
func New ¶
New returns a new Handler. Use the Handle and HandleFunc methods to wrap existing HTTP handlers.
func (*Handler) Handle ¶
Handle works as a middleware that wraps an existing http.Handler. A wrapped handler will recover from and report panics to Sentry, and provide access to a request-specific hub to report messages and errors.
func (*Handler) HandleFunc ¶
func (h *Handler) HandleFunc(handler http.HandlerFunc) http.HandlerFunc
HandleFunc is like Handle, but with a handler function parameter for cases where that is convenient. In particular, use it to wrap a handler function literal.
http.Handle(pattern, h.HandleFunc(func (w http.ResponseWriter, r *http.Request) { // handler code here }))
type Options ¶
type Options struct { // Repanic configures whether to panic again after recovering from a panic. // Use this option if you have other panic handlers or want the default // behavior from Go's http package, as documented in // https://golang.org/pkg/net/http/#Handler. Repanic bool // WaitForDelivery indicates, in case of a panic, whether to block the // current goroutine and wait until the panic event has been reported to // Sentry before repanicking or resuming normal execution. // // This option is normally not needed. Unless you need different behaviors // for different HTTP handlers, configure the SDK to use the // HTTPSyncTransport instead. // // Waiting (or using HTTPSyncTransport) is useful when the web server runs // in an environment that interrupts execution at the end of a request flow, // like modern serverless platforms. WaitForDelivery bool // Timeout for the delivery of panic events. Defaults to 2s. Only relevant // when WaitForDelivery is true. // // If the timeout is reached, the current goroutine is no longer blocked // waiting, but the delivery is not canceled. Timeout time.Duration }
Options configure a Handler.
type WrapResponseWriter ¶ added in v0.28.1
type WrapResponseWriter interface { http.ResponseWriter // Status returns the HTTP status of the request, or 200 if one has not // yet been sent. Status() int // BytesWritten returns the total number of bytes sent to the client. BytesWritten() int // Tee causes the response body to be written to the given io.Writer in // addition to proxying the writes through. Only one io.Writer can be // tee'd to at once: setting a second one will overwrite the first. // Writes will be sent to the proxy before being written to this // io.Writer. It is illegal for the tee'd writer to be modified // concurrently with writes. Tee(io.Writer) // Unwrap returns the original proxied target. Unwrap() http.ResponseWriter }
WrapResponseWriter is a proxy around an http.ResponseWriter that allows you to hook into various parts of the response process.
func NewWrapResponseWriter ¶ added in v0.28.1
func NewWrapResponseWriter(w http.ResponseWriter, protoMajor int) WrapResponseWriter