veneur: github.com/stripe/veneur/trace/testbackend Index | Examples | Files

package testbackend

import "github.com/stripe/veneur/trace/testbackend"

Package testbackend contains helpers to make it easier to test the tracing behavior of code using veneur's trace API.

Index

Examples

Package Files

backend.go flush_backend.go

func NewBackend Uses

func NewBackend(ch chan<- *ssf.SSFSpan, opts ...BackendOption) trace.ClientBackend

NewBackend returns a new trace.ClientBackend that sends spans down a channel.

Code:

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

type Backend Uses

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

Backend is a ClientBackend that sends spans into a provided channel. It does not support flushing.

func (*Backend) Close Uses

func (be *Backend) Close() error

Close is a no-op.

func (*Backend) SendSync Uses

func (be *Backend) SendSync(ctx context.Context, span *ssf.SSFSpan) error

SendSync sends the span into the Backend's channel.

type BackendOption Uses

type BackendOption func(*Backend)

BackendOption is a functional option for Backends provided by this package.

func SendErrors Uses

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 Uses

type FlushErrorSource func([]*ssf.SSFSpan) error

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 Uses

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 Uses

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

Code:

// 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) Close Uses

func (be *FlushingBackend) Close() error

Close is a no-op.

func (*FlushingBackend) Flush Uses

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.

func (*FlushingBackend) FlushSync Uses

func (be *FlushingBackend) FlushSync(ctx context.Context) error

FlushSync sends the batch of submitted spans back.

func (*FlushingBackend) SendSync Uses

func (be *FlushingBackend) SendSync(ctx context.Context, span *ssf.SSFSpan) error

SendSync sends the span into the FlushingBackend's channel and counts it.

type FlushingBackendOption Uses

type FlushingBackendOption func(*FlushingBackend)

FlushingBackendOption is a functional option for Backends provided by this package.

func FlushErrors Uses

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 Uses

type SendErrorSource func(*ssf.SSFSpan) error

SendErrorSource is a function that a test can provide. It returns whether sending a span should return an error or not.

Package testbackend imports 4 packages (graph). Updated 2018-11-30. Refresh now. Tools for package owners.