fakesentry

package module
v0.0.0-...-401321f Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2020 License: MIT Imports: 13 Imported by: 0

README

fakesentry GoDoc

import "github.com/VerticalOps/fakesentry"

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 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.

Index

Examples
Package files

fakesentry.go options.go server.go

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")
)

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.

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
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

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
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
func WithErrorHandler(eh ErrorHandler) Option

WithErrorHandler returns an Option for an ErrorHandler

func WithLogger
func WithLogger(logger Logger) Option

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

type Server

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
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
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

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 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
//Development or build flag
var devmode bool
//custom opts
var opts sentry.ClientOptions

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.
	opts.Dsn = `http://thisis:myfakeauth@localhost/1`
	opts.Transport = sentry.NewHTTPSyncTransport()
	opts.HTTPTransport = srv.Transport()
}

//All sentry requests now go to the internal server.
//fakesentry also provides a Handler type that can be used on its own, independent of the Server.
//look to tests for more example usage.
if err := sentry.Init(opts); err != nil {
	//handle error
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
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

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

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

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

func WithErrorHandler(eh ErrorHandler) Option

WithErrorHandler returns an Option for an ErrorHandler

func WithLogger

func WithLogger(logger Logger) Option

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

type Server

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

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

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.

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.

Jump to

Keyboard shortcuts

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