doghouse

package module
v0.0.0-...-92cba1e Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: Unlicense Imports: 14 Imported by: 0

README

Datadog Tracing Test Server

Sometimes you want to test the trace output you send to Datadog. This library facilitates that by acting as a test Datadog server that captures and stores any traces sent to it through its httptest server.

This library is fairly special-purpose as it hijacks the DD_TRACE_AGENT_URL environment variable in the running binary and ensures only one instance of the test server is ever running in a process. This is important due to the fact that the underlying Datadog tracer is global and multiple calls to reconfigure the tracer will ultimately affect any other running test - therefore, if this mock tracer is used, it should only ever be initialized at the beginning of a test suite run, and tests that use it should ensure that they don't conflict with each other (i.e. emitting the same traces). Alternatively, the tests should be run serially and the state of the server can be reset between runs via a call to server.Reset().

Example Usage

var server *doghouse.MockDatadogServer

func TestMain(m *testing.M) {
	server = doghouse.New()
	ret := m.Run()
	server.Close()
	os.Exit(ret)
}

func TestSpan(t *testing.T) {
	span := tracer.StartSpan("test.span")
	span.Finish()

	tracer.Flush()

	server.WaitForSpan(t, "test.span")
}

Dependencies

This library uses github.com/tinylib/msgp for generating messagepack marshalers, you can install it with

go install github.com/tinylib/msgp

in order to run go generate.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Batch

type Batch []Trace

Batch contains a collection of traces sent in bulk to the server.

func (*Batch) DecodeMsg

func (z *Batch) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (Batch) EncodeMsg

func (z Batch) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (Batch) MarshalMsg

func (z Batch) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (Batch) Msgsize

func (z Batch) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*Batch) UnmarshalMsg

func (z *Batch) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type MockDatadogServer

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

MockDatadogServer is a test server that collects traces sent via Datadog's tracing library.

func New

func New(opts ...tracer.StartOption) *MockDatadogServer

New creates a new MockDatadogServer. This should only be ever used as a singleton due to the fact that the Datadog tracer library uses global state for publishing.

func (*MockDatadogServer) Close

func (s *MockDatadogServer) Close()

Close the underlying test server.

func (*MockDatadogServer) ExpectDurationNoSpan

func (s *MockDatadogServer) ExpectDurationNoSpan(t *testing.T, duration time.Duration, name string)

ExpectDurationNoSpan ensures that the named span has not been received in the given duration.

func (*MockDatadogServer) ExpectNoSpan

func (s *MockDatadogServer) ExpectNoSpan(t *testing.T, name string)

ExpectNoSpan ensures that the named span has not been received within 100 milliseconds.

func (*MockDatadogServer) ExpectSpan

func (s *MockDatadogServer) ExpectSpan(t *testing.T, name string, parents ...string)

Expect a named span with the given optional parents to have been received.

func (*MockDatadogServer) ExpectSpanFn

func (s *MockDatadogServer) ExpectSpanFn(t *testing.T, name string, fn func(span Span) bool, msg string, args ...interface{})

Expect a named span with the given verification function to exist.

func (*MockDatadogServer) Reset

func (s *MockDatadogServer) Reset()

Reset the internal state of the server between test runs.

func (*MockDatadogServer) ServeHTTP

func (s *MockDatadogServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is the main handler for requests from the tracing library.

func (*MockDatadogServer) SetTracePath

func (s *MockDatadogServer) SetTracePath(path string)

SetTracePath changes the url path for which the mock server accepts Datadog traces.

func (*MockDatadogServer) WaitDurationForSpan

func (s *MockDatadogServer) WaitDurationForSpan(t *testing.T, duration time.Duration, name string, parents ...string)

WaitDurationForSpan waits a sepecified duration for the server to receive the named span with optional parent matching.

func (*MockDatadogServer) WaitForSpan

func (s *MockDatadogServer) WaitForSpan(t *testing.T, name string, parents ...string)

WaitForSpan waits 10 milliseconds for the server to receive the named span with optional parent matching.

type Span

type Span struct {
	Name     string             `msg:"name"`
	Service  string             `msg:"service"`
	Resource string             `msg:"resource"`
	Type     string             `msg:"type"`
	Start    int64              `msg:"start"`
	Duration int64              `msg:"duration"`
	Meta     map[string]string  `msg:"meta,omitempty"`
	Metrics  map[string]float64 `msg:"metrics,omitempty"`
	SpanID   uint64             `msg:"span_id"`
	TraceID  uint64             `msg:"trace_id"`
	ParentID uint64             `msg:"parent_id"`
	Error    int32              `msg:"error"`
}

Span represents a single span.

func (*Span) DecodeMsg

func (z *Span) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (*Span) EncodeMsg

func (z *Span) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (*Span) MarshalMsg

func (z *Span) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (*Span) Msgsize

func (z *Span) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*Span) UnmarshalMsg

func (z *Span) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type Trace

type Trace []Span

Trace contains a collection of associated spans.

func (*Trace) DecodeMsg

func (z *Trace) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (Trace) EncodeMsg

func (z Trace) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (Trace) MarshalMsg

func (z Trace) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (Trace) Msgsize

func (z Trace) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*Trace) UnmarshalMsg

func (z *Trace) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

Jump to

Keyboard shortcuts

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