Skip to content

Fake yourself some Sentry by running a simple fake Sentry server inside your Go program

License

Notifications You must be signed in to change notification settings

VerticalOps/fakesentry

Repository files navigation

fakesentry GoDoc

import "github.com/VerticalOps/fakesentry"

Package fakesentry provides various functionality to run a 'fake sentry' server internal to a Go process off or on the network. The primary motivation of this package was to provide an easy way of viewing data your Go program may send to Sentry during run, but in a development mode without sending data to a production DSN or otherwise setting up an entire Sentry server instance with its dependencies. As such, this should be guarded under a development mode or build flag unless care is taken and the user has read this packages documentation.

By default, the Server type in this package will start an internal HTTP server without using the network stack and provides a Dialer and *http.Transport that can be configured with your Sentry client to make requests to. The Server will simply pretty-print the JSON sent by the client to STDERR. Much of this behavior can be added to or changed. It is recommended to look at the package example and read each types documentation for more configuration.

fakesentry.go options.go server.go

var (
    //ErrBadMethod is passed into an ErrorHandler if the client uses an unexpected HTTP method.
    ErrBadMethod = errors.New("fakesentry: Bad HTTP method from client")

    //ErrBadContentLength is passed into an ErrorHandler if the client sends an invalid content length.
    ErrBadContentLength = errors.New("fakesentry: Bad ContentLength from client")

    //ErrBadContentType is passed into an ErrorHandler if the clients sends an unexpected content type.
    ErrBadContentType = errors.New("fakesentry: Bad ContentType from client")
)
func FromContext(ctx context.Context) (jb json.RawMessage, ok bool)

FromContext returns a json.RawMessage that was saved if AsMiddleware is used with the Handler type. Look to FromRequest for more information.

func FromRequest(r *http.Request) (json.RawMessage, bool)

FromRequest retrieves the saved json.RawMessage Handler.ServeHTTP saves into the request context if AsMiddleware is used. Other http.Handlers can use this to retrieve the value and do with it as they will.

type ErrorHandler func(http.ResponseWriter, *http.Request, error)

ErrorHandler is called when an error occurs within Handler.ServeHTTP. The error is guaranteed to be non-nil, the handler may use the passed http types to communicate to the client what happened.

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

Handler implements a basic ServeHTTP method that can be used to accept and parse HTTP requests from Sentry clients. The Handler type is meant for testing, usually guarded by a development or build flag. It is generally unsafe to use in production unless guarded by other HTTP Handlers, such as something that checks the requests auth state.

func NewHandler(opts ...Option) Handler

NewHandler returns a new Handler with defaults unless Options are set. Unless otherwise changed, the default logger is a stdlib 'log' that prints to STDERR. The default ErrorHandler prints the error to the log and writes a 400 or 500 code to the client. If the Handler is not used in middleware then the default action is to dump the HTTP request as well as pretty print the Sentry JSON into the logger.

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler. Please refer to the Handler type definiton NewHandler function for more information.

type Logger interface {
    Printf(format string, arg ...interface{})
}

Logger is the basic logging interface used by this package.

type Option func(*Handler)

Option is a functional option to help configure a Handler.

func AsMiddleware(next http.Handler) Option

AsMiddleware takes the next http.Handler and changes the behavior of Handler.ServeHTTP, in that if a http.Handler is set, the parsed Sentry JSON value will be saved into the http.Requests context for use by the next http.Handler.

func WithErrorHandler(eh ErrorHandler) Option

WithErrorHandler returns an Option for an ErrorHandler

func WithLogger(logger Logger) Option

WithLogger returns an Option that sets a Logger on a Handler.

type Server struct {
    *http.Server
    // contains filtered or unexported fields
}

Server is a convenience type that uses the default configurations of a *http.Server, this packages Handler type, this repos ipc package Listener/Dialer types, and *http.Transport, to start an HTTP server that only listens for intra-process connections and does not use the network. For more configuration use this Server's methods, or use each of the aforementioned types individually. Refer to the package example and each types documentation for more information.

func NewServer() Server

NewServer is like NewUnstartedServer but it does start a goroutine to service connections from the available Dialer and Transport methods. Server.Close should be called when done.

func NewUnstartedServer() Server

NewUnstartedServer returns a new Server with defaults mentioned on the Server types definition. It does not start a goroutine serving connections with the internal intra-process listener.

func (Server) Dialer

func (s Server) Dialer() ipc.Dialer

Dialer returns the intra-process Dialer from this repos ipc package. Look to that packages documentation for more information.

func (Server) Listener

func (s Server) Listener() ipc.Listener

Listener returns the intra-process connection Listener from this repos ipc package. Look to that packages documentation for more information.

func (Server) Transport

func (s Server) Transport() *http.Transport

Transport returns an *http.Transport that can be used with *http.Clients to speak to the Server.


Generated by godoc2md

About

Fake yourself some Sentry by running a simple fake Sentry server inside your Go program

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages