mocktracer

package
v1.999.0-rc.5 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2024 License: Apache-2.0, BSD-3-Clause, Apache-2.0 Imports: 9 Imported by: 2

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 (
	"gopkg.in/DataDog/dd-trace-go.v1/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 IsActive

func IsActive() bool

Types

type MockspanV2Adapter

type MockspanV2Adapter struct {
	Span *v2.Span
}

func (MockspanV2Adapter) BaggageItem

func (msa MockspanV2Adapter) BaggageItem(key string) string

BaggageItem implements ddtrace.Span.

func (MockspanV2Adapter) Context

func (msa MockspanV2Adapter) Context() ddtrace.SpanContext

Context implements Span.

func (MockspanV2Adapter) Finish

func (msa MockspanV2Adapter) Finish(opts ...ddtrace.FinishOption)

Finish implements ddtrace.Span.

func (MockspanV2Adapter) FinishTime

func (msa MockspanV2Adapter) FinishTime() time.Time

FinishTime implements Span.

func (MockspanV2Adapter) OperationName

func (msa MockspanV2Adapter) OperationName() string

OperationName implements Span.

func (MockspanV2Adapter) ParentID

func (msa MockspanV2Adapter) ParentID() uint64

ParentID implements Span.

func (MockspanV2Adapter) SetBaggageItem

func (msa MockspanV2Adapter) SetBaggageItem(key string, val string)

SetBaggageItem implements ddtrace.Span.

func (MockspanV2Adapter) SetOperationName

func (msa MockspanV2Adapter) SetOperationName(operationName string)

SetOperationName implements ddtrace.Span.

func (MockspanV2Adapter) SetTag

func (msa MockspanV2Adapter) SetTag(key string, value interface{})

SetTag implements ddtrace.Span.

func (MockspanV2Adapter) SpanID

func (msa MockspanV2Adapter) SpanID() uint64

SpanID implements Span.

func (MockspanV2Adapter) StartTime

func (msa MockspanV2Adapter) StartTime() time.Time

StartTime implements Span.

func (MockspanV2Adapter) String

func (msa MockspanV2Adapter) String() string

String implements Span.

func (MockspanV2Adapter) Tag

func (msa MockspanV2Adapter) Tag(k string) interface{}

Tag implements Span.

func (MockspanV2Adapter) Tags

func (msa MockspanV2Adapter) Tags() map[string]interface{}

Tags implements Span.

func (MockspanV2Adapter) TraceID

func (msa MockspanV2Adapter) TraceID() uint64

TraceID implements Span.

type Span

type Span interface {
	// SpanID returns the span's ID.
	SpanID() uint64

	// TraceID returns the span's trace ID.
	TraceID() uint64

	// ParentID returns the span's parent ID.
	ParentID() uint64

	// StartTime returns the time when the span has started.
	StartTime() time.Time

	// FinishTime returns the time when the span has finished.
	FinishTime() time.Time

	// OperationName returns the operation name held by this span.
	OperationName() string

	// Tag returns the value of the tag at key k.
	Tag(k string) interface{}

	// Tags returns a copy of all the tags in this span.
	Tags() map[string]interface{}

	// Context returns the span's SpanContext.
	Context() ddtrace.SpanContext

	// Stringer allows pretty-printing the span's fields for debugging.
	fmt.Stringer
}

Span is an interface that allows querying a span returned by the mock tracer.

type Tracer

type Tracer interface {
	// OpenSpans returns the set of started spans that have not been finished yet.
	OpenSpans() []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