tracing API

tracing

package

API reference for the tracing package.

I
interface

Span

Span represents a tracing span that can be ended and annotated.

core/tracing/tracer.go:8-12
type Span interface

Methods

End
Method
func End(...)
SetAttributes
Method

Parameters

kv ...Attribute
func SetAttributes(...)
RecordError
Method

Parameters

err error
func RecordError(...)
S
struct

Attribute

Attribute is a key-value pair for span metadata.

core/tracing/tracer.go:15-18
type Attribute struct

Fields

Name Type Description
Key string
Value any
I
interface

Tracer

Tracer creates new spans.

core/tracing/tracer.go:21-23
type Tracer interface

Methods

StartSpan
Method

Parameters

name string
func StartSpan(...)
S
struct
Implements: Span

noopSpan

core/tracing/tracer.go:25-25
type noopSpan struct

Methods

End
Method
func (noopSpan) End()
{}
SetAttributes
Method

Parameters

kv ...Attribute
func (noopSpan) SetAttributes(kv ...Attribute)
{}
RecordError
Method

Parameters

err error
func (noopSpan) RecordError(err error)
{}
S
struct
Implements: Tracer

noopTracer

core/tracing/tracer.go:31-31
type noopTracer struct

Methods

StartSpan
Method

Parameters

name string
func (noopTracer) StartSpan(ctx context.Context, name string) (context.Context, Span)
{
	return ctx, noopSpan{}
}
F
function

StartSpan

StartSpan starts a span from the given tracer, falling back to Noop.

Parameters

tracer
name
string
core/tracing/tracer.go:41-46
func StartSpan(ctx context.Context, tracer Tracer, name string) (context.Context, Span)

{
	if tracer == nil {
		tracer = Noop
	}
	return tracer.StartSpan(ctx, name)
}
S
struct
Implements: Tracer

testTracer

core/tracing/tracer_test.go:9-11
type testTracer struct

Methods

StartSpan
Method

Parameters

name string
func (*testTracer) StartSpan(ctx context.Context, name string) (context.Context, Span)
{
	t.name = name
	return context.WithValue(ctx, "span", name), &testSpan{}
}

Fields

Name Type Description
name string
S
struct
Implements: Span

testSpan

core/tracing/tracer_test.go:18-21
type testSpan struct

Methods

End
Method
func (*testSpan) End()
{ s.ended = true }
SetAttributes
Method

Parameters

kv ...Attribute
func (*testSpan) SetAttributes(kv ...Attribute)
{}
RecordError
Method

Parameters

err error
func (*testSpan) RecordError(err error)
{ s.err = err }

Fields

Name Type Description
ended bool
err error
F
function

TestStartSpanUsesNoopForNilTracer

Parameters

core/tracing/tracer_test.go:27-35
func TestStartSpanUsesNoopForNilTracer(t *testing.T)

{
	ctx, span := StartSpan(context.Background(), nil, "op")
	if ctx == nil || span == nil {
		t.Fatal("StartSpan() returned nil values")
	}
	span.SetAttributes(Attribute{Key: "k", Value: "v"})
	span.RecordError(errors.New("x"))
	span.End()
}
F
function

TestStartSpanUsesTracer

Parameters

core/tracing/tracer_test.go:37-49
func TestStartSpanUsesTracer(t *testing.T)

{
	tracer := &testTracer{}

	ctx, span := StartSpan(context.Background(), tracer, "op")

	if tracer.name != "op" {
		t.Fatalf("name = %q, want op", tracer.name)
	}
	if ctx.Value("span") != "op" {
		t.Fatalf("ctx value = %v, want op", ctx.Value("span"))
	}
	span.End()
}