Documentation ¶
Overview ¶
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/Raven 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.
Example ¶
package main import ( "net/http" "github.com/VerticalOps/fakesentry" raven "github.com/getsentry/raven-go" ) func main() { //Development or build flag var devmode bool //NewClient, DefaultClient var client *raven.Client if devmode { //Server with defaults, look at package documentation for more that can be changed. srv := fakesentry.NewServer() //Close Server on program end though it isn't needed with the default settings. defer srv.Close() //Override Sentry client Transport or make sure the underlying *http.Transport has its //DialX functions provided by srv.Dialer. client.Transport = &raven.HTTPTransport{ Client: &http.Client{Transport: srv.Transport()}, } /* Setting a fake DSN is optional, though it does need to be http and not https unless you've changed to using (or something similar). srv := fakesentry.NewUnstartedServer() go srv.ServeTLS(srv.Listener(), "cert.pem", "key.pem") Either way, if you don't change the underlying net.Listener completely, the Server does not actually use the network stack. Take a look at fakesentry docs/tests as well as fakesentry/ipc for more information. */ client.SetDSN(`http://thisis:myfakeauth@localhost/1`) } //All sentry/raven requests now go to the internal server. //fakesentry also provides a Handler type that can be used on its own, independent of the Server. }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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") )
Functions ¶
func FromContext ¶
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 ¶
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.
Types ¶
type ErrorHandler ¶
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 ¶
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 ¶
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.
type Logger ¶
type Logger interface {
Printf(format string, arg ...interface{})
}
Logger is the basic logging interface used by this package.
type Option ¶
type Option func(*Handler)
Option is a functional option to help configure a Handler.
func AsMiddleware ¶
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 ¶
func WithErrorHandler(eh ErrorHandler) Option
WithErrorHandler returns an Option for an ErrorHandler
func WithLogger ¶
WithLogger returns an Option that sets a Logger on a Handler.
type Server ¶
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 ¶
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 ¶
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 ¶
Dialer returns the intra-process Dialer from this repos ipc package. Look to that packages documentation for more information.
Directories ¶
Path | Synopsis |
---|---|
Package ipc implements a simple set of intra-process-communication net.Conn types, specifically a net.Listener and an associated Dialer that can be used in place of http Servers/Clients or anything that needs a net.Conn to work or test within the same process.
|
Package ipc implements a simple set of intra-process-communication net.Conn types, specifically a net.Listener and an associated Dialer that can be used in place of http Servers/Clients or anything that needs a net.Conn to work or test within the same process. |