mocktracer

package
v2.0.0-...-b084466 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: Apache-2.0, BSD-3-Clause, Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package mocktracer provides a mock implementation of the tracer used in testing. It allows querying spans generated at runtime, without having them actually be sent to an agent. It provides a simple way to test that instrumentation is running correctly in your application.

Simply call "Start" at the beginning of your tests to start and obtain an instance of the mock tracer.

Example
package main

import (
	"github.com/DataDog/dd-trace-go/v2/ddtrace/mocktracer"
)

func main() {
	// Start the mock tracer.
	mt := mocktracer.Start()
	defer mt.Stop()

	// ...run some code with generates spans.

	// Query the mock tracer for finished spans.
	spans := mt.FinishedSpans()
	if len(spans) != 1 {
		// should only have 1 span
		panic("expected 1 span")
	}

	// Run assertions...
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func UnwrapSlice

func UnwrapSlice(ss []*Span) []*tracer.Span

Types

type Span

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

func MockSpan

func MockSpan(s *tracer.Span) *Span
func (s *Span) AddLink(spanContext *tracer.SpanContext, attributes map[string]string)

func (*Span) Context

func (s *Span) Context() *tracer.SpanContext

func (*Span) Duration

func (s *Span) Duration() time.Duration

func (*Span) FinishTime

func (s *Span) FinishTime() time.Time

func (*Span) OperationName

func (s *Span) OperationName() string

func (*Span) ParentID

func (s *Span) ParentID() uint64

func (*Span) SetTag

func (s *Span) SetTag(k string, v interface{})

func (*Span) SpanID

func (s *Span) SpanID() uint64

func (*Span) StartTime

func (s *Span) StartTime() time.Time

func (*Span) String

func (s *Span) String() string

func (*Span) Tag

func (s *Span) Tag(k string) interface{}

func (*Span) Tags

func (s *Span) Tags() map[string]interface{}

func (*Span) TraceID

func (s *Span) TraceID() uint64

func (*Span) Unwrap

func (s *Span) Unwrap() *tracer.Span

type Tracer

type Tracer interface {
	tracer.Tracer

	// OpenSpans returns the set of started spans that have not been finished yet.
	OpenSpans() []*Span

	FinishSpan(*tracer.Span)
	// FinishedSpans returns the set of finished spans.
	FinishedSpans() []*Span

	SentDSMBacklogs() []datastreams.Backlog

	// Reset resets the spans and services recorded in the tracer. This is
	// especially useful when running tests in a loop, where a clean start
	// is desired for FinishedSpans calls.
	Reset()

	// Stop deactivates the mock tracer and allows a normal tracer to take over.
	// It should always be called when testing has finished.
	Stop()
}

Tracer exposes an interface for querying the currently running mock tracer.

func Start

func Start() Tracer

Start sets the internal tracer to a mock and returns an interface which allows querying it. Call Start at the beginning of your tests to activate the mock tracer. When your test runs, use the returned interface to query the tracer's state.

Jump to

Keyboard shortcuts

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