Documentation ¶
Overview ¶
Package testbackend contains helpers to make it easier to test the tracing behavior of code using veneur's trace API.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewBackend ¶
func NewBackend(ch chan<- *ssf.SSFSpan, opts ...BackendOption) trace.ClientBackend
NewBackend returns a new trace.ClientBackend that sends spans down a channel.
Example ¶
package main import ( "context" "fmt" "github.com/stripe/veneur/ssf" "github.com/stripe/veneur/trace" "github.com/stripe/veneur/trace/testbackend" ) func main() { ctx := context.Background() // The channel is buffered so we don't have to do async // receives: ch := make(chan *ssf.SSFSpan, 1) client, _ := trace.NewBackendClient(testbackend.NewBackend(ch)) span, ctx := trace.StartSpanFromContext(ctx, "hi_there") span.ClientFinish(client) rcvd := <-ch fmt.Println(rcvd.Name) }
Output: hi_there
Types ¶
type Backend ¶
type Backend struct {
// contains filtered or unexported fields
}
Backend is a ClientBackend that sends spans into a provided channel. It does not support flushing.
type BackendOption ¶
type BackendOption func(*Backend)
BackendOption is a functional option for Backends provided by this package.
func SendErrors ¶
func SendErrors(src SendErrorSource) BackendOption
SendErrors allows tests to provide a function that will be consulted on whether a send operation should return an error.
type FlushErrorSource ¶
FlushErrorSource is a function that a test can provide. It returns whether flushing a batch of spans should return an error or not.
type FlushingBackend ¶
type FlushingBackend struct {
// contains filtered or unexported fields
}
FlushingBackend is a ClientBackend that behaves much like Backend does, but also supports flushing. On flush, it sends the number of spans contained in each batch.
func NewFlushingBackend ¶
func NewFlushingBackend(ch chan<- []*ssf.SSFSpan, opts ...FlushingBackendOption) *FlushingBackend
NewFlushingBackend constructs a new FlushableClientBackend. It will collect the metrics submitted to it in an array (the order of Spans in the array represents the order in which the backend's SendSync was called).
Example ¶
package main import ( "fmt" "github.com/stripe/veneur/ssf" "github.com/stripe/veneur/trace" "github.com/stripe/veneur/trace/metrics" "github.com/stripe/veneur/trace/testbackend" ) func main() { // The channel is buffered so we don't have to do async // receives: ch := make(chan []*ssf.SSFSpan, 1) be := testbackend.NewFlushingBackend(ch) client, _ := trace.NewBackendClient(be) for i := 0; i < 100; { // Report a metric and ensure it actually got sent: err := metrics.ReportOne(client, ssf.Count("hi_there", 1, map[string]string{})) if err == nil { i++ } } // Call the backend's flush method to avoid the trace client's // debouncing / back-off behavior causing spurious errors in tests: be.Flush() rcvd := <-ch fmt.Println(rcvd[0].Metrics[0].Name) }
Output: hi_there
func (*FlushingBackend) Flush ¶
func (be *FlushingBackend) Flush() error
Flush on a FlushingBackend is an alternative to the Client's flush functionality for tests. It flushes spans deterministically and so ensures that the flush actually happens.
type FlushingBackendOption ¶
type FlushingBackendOption func(*FlushingBackend)
FlushingBackendOption is a functional option for Backends provided by this package.
func FlushErrors ¶
func FlushErrors(sendSrc SendErrorSource, src FlushErrorSource) FlushingBackendOption
FlushErrors allows tests to provide functions that will be consulted on whether a send or flush operation should return an error.
type SendErrorSource ¶
SendErrorSource is a function that a test can provide. It returns whether sending a span should return an error or not.