vtrace

package
Version: v0.1.20 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2021 License: BSD-3-Clause Imports: 9 Imported by: 31

Documentation

Overview

Package vtrace defines a system for collecting debugging information about operations that span a distributed system. We call the debugging information attached to one operation a Trace. A Trace may span many processes on many machines.

Traces are composed of a hierarchy of Spans. A span is a named timespan, that is, it has a name, a start time, and an end time. For example, imagine we are making a new blog post. We may have to first authentiate with an auth server, then write the new post to a database, and finally notify subscribers of the new content. The trace might look like this:

Trace:
<---------------- Make a new blog post ----------->
|                  |                   |
<- Authenticate -> |                   |
                   |                   |
                   <-- Write to DB --> |
                                       <- Notify ->
0s                      1.5s                      3s

Here we have a single trace with four Spans. Note that some Spans are children of other Spans. Vtrace works by attaching data to a Context, and this hierarchical structure falls directly out of our building off of the tree of Contexts. When you derive a new context using WithNewSpan(), you create a Span thats a child of the currently active span in the context. Note that spans that share a parent may overlap in time.

In this case the tree would have been created with code like this:

function MakeBlogPost(ctx *context.T) {
    authCtx, _ := vtrace.WithNewSpan(ctx, "Authenticate")
    Authenticate(authCtx)
    writeCtx, _ := vtrace.WithNewSpan(ctx, "Write To DB")
    Write(writeCtx)
    notifyCtx, _ := vtrace.WithNewSpan(ctx, "Notify")
    Notify(notifyCtx)
}

Just as we have Spans to represent time spans we have Annotations to attach debugging information that is relevant to the current moment. You can add an annotation to the current span by calling the Span's Annotate method:

span := vtrace.GetSpan(ctx)
span.Annotate("Just got an error")

When you make an annotation we record the annotation and the time when it was attached.

Traces can be composed of large numbers of spans containing data collected from large numbers of different processes. Always collecting this information would have a negative impact on performance. By default we don't collect any data. If a particular operation is of special importance you can force it to be collected by calling ForceCollect. You can also use the --v23.vtrace.collect-regexp flag to set a regular expression which will force us to record any matching trace.

If your trace has collected information you can retrieve the data collected so far with the Store's TraceRecord and TraceRecords methods.

By default contexts obtained from v23.Init or in rpc server implementations already have an initialized Trace. The functions in this package allow you to add data to existing traces or start new ones.

Index

Constants

View Source
const AWSXRay = TraceFlags(2)
View Source
const CollectInMemory = TraceFlags(1)
View Source
const Empty = TraceFlags(0)

Variables

This section is empty.

Functions

func ForceCollect

func ForceCollect(ctx *context.T, level int)

ForceCollect forces the store to collect all information about the current trace.

func FormatTrace

func FormatTrace(w io.Writer, record *TraceRecord, loc *time.Location)

FormatTrace writes a text description of the given trace to the given writer. Times will be formatted according to the given location, if loc is nil local times will be used.

func FormatTraces

func FormatTraces(w io.Writer, records []TraceRecord, loc *time.Location)

FormatTraces writes a text description of all the given traces to the given writer. Times will be formatted according to the given location, if loc is nil local times will be used.

func WithManager

func WithManager(ctx *context.T, manager Manager) *context.T

WithManager returns a new context with a Vtrace manager attached.

func WithSpan added in v0.1.17

func WithSpan(ctx *context.T, span Span) *context.T

func WithStore added in v0.1.17

func WithStore(ctx *context.T, store Store) *context.T

Types

type Annotation

type Annotation struct {
	// When the annotation was added.
	When time.Time
	// The annotation message.
	// TODO(mattr): Allow richer annotations.
	Message string
}

An Annotation represents data that is relevant at a specific moment. They can be attached to spans to add useful debugging information.

func (Annotation) VDLIsZero

func (x Annotation) VDLIsZero() bool

func (*Annotation) VDLRead

func (x *Annotation) VDLRead(dec vdl.Decoder) error

func (Annotation) VDLReflect

func (Annotation) VDLReflect(struct {
	Name string `vdl:"v.io/v23/vtrace.Annotation"`
})

func (Annotation) VDLWrite

func (x Annotation) VDLWrite(enc vdl.Encoder) error

type Manager

type Manager interface {
	// WithNewTrace creates a new vtrace context that is not the child of any
	// other span.  This is useful when starting operations that are
	// disconnected from the activity ctx is performing.  For example
	// this might be used to start background tasks.
	WithNewTrace(ctx *context.T, name string, sr *SamplingRequest) (*context.T, Span)

	// WithContinuedTrace creates a span that represents a continuation of
	// a trace from a remote server.  name is the name of the new span and
	// req contains the parameters needed to connect this span with it's
	// trace.
	WithContinuedTrace(ctx *context.T, name string, sr *SamplingRequest, req Request) (*context.T, Span)

	// WithNewSpan derives a context with a new Span that can be used to
	// trace and annotate operations across process boundaries.
	WithNewSpan(ctx *context.T, name string) (*context.T, Span)

	// Generate a Request from the current context.
	GetRequest(ctx *context.T) Request

	// Generate a Response from the current context.
	GetResponse(ctx *context.T) Response
}

type Node

type Node struct {
	Span     *SpanRecord
	Children []*Node
}

func BuildTree

func BuildTree(trace *TraceRecord) *Node

TODO(mattr): It is useful in general to make a tree of spans for analysis as well as formatting. This interface should be cleaned up and exported.

type Request

type Request struct {
	SpanId          uniqueid.Id // The Id of the span that originated the RPC call.
	TraceId         uniqueid.Id // The Id of the trace this call is a part of.
	RequestMetadata []byte      // Any metadata to be sent with the request.
	Flags           TraceFlags
	LogLevel        int32
}

Request is the object that carries trace information between processes.

func GetRequest

func GetRequest(ctx *context.T) Request

Generate a Request from the current context.

func (Request) VDLIsZero

func (x Request) VDLIsZero() bool

func (*Request) VDLRead

func (x *Request) VDLRead(dec vdl.Decoder) error

func (Request) VDLReflect

func (Request) VDLReflect(struct {
	Name string `vdl:"v.io/v23/vtrace.Request"`
})

func (Request) VDLWrite

func (x Request) VDLWrite(enc vdl.Encoder) error

type Response

type Response struct {
	// Flags give options for trace collection, the client should alter its
	// collection for this trace according to the flags sent back from the
	// server.
	Flags TraceFlags
	// Trace is collected trace data.  This may be empty.
	Trace TraceRecord
}

func GetResponse

func GetResponse(ctx *context.T) Response

Generate a Response from the current context.

func (Response) VDLIsZero

func (x Response) VDLIsZero() bool

func (*Response) VDLRead

func (x *Response) VDLRead(dec vdl.Decoder) error

func (Response) VDLReflect

func (Response) VDLReflect(struct {
	Name string `vdl:"v.io/v23/vtrace.Response"`
})

func (Response) VDLWrite

func (x Response) VDLWrite(enc vdl.Encoder) error

type SamplingRequest added in v0.1.17

type SamplingRequest struct {
	Local  string // The address/name of the local host generating this trace.
	Name   string // The name the traced service is available as, may differ from the name of the span.
	Method string // The method being invoked.
}

SamplingRequest can be used to make sampling decisions about a trace. It is always optional and its behaviour is implementation dependent. Similarly, each of the individual fields is optional. The SamplingRequest is not (currently) included in the vtrace span.

type Span

type Span interface {
	// Name returns the name of the span.
	Name() string

	// ID returns the uniqueid.ID of the span.
	ID() uniqueid.Id

	// Parent returns the uniqueid.ID of this spans parent span.
	Parent() uniqueid.Id

	// Annotate adds a string annotation to the trace.  Where Spans
	// represent time periods Annotations represent data thats relevant
	// at a specific moment.
	Annotate(s string)

	// Annotatef adds an annotation to the trace.  Where Spans represent
	// time periods Annotations represent data thats relevant at a
	// specific moment.
	// format and a are interpreted as with fmt.Printf.
	Annotatef(format string, a ...interface{})

	// AnnotateMetadata can be used to associate key/value with the span. If
	// indexed is false, the underlying implementation may create a searchable
	// index using this metadata. However, indexing itself is an implementation
	// specific feature and need be provided by every implementation.
	// NOTE that metadata is not communicated back to the requestor and is
	// intended for purely per-server/node use.
	AnnotateMetadata(key string, value interface{}, indexed bool) error

	// SetRequestMetadata appends metadata to be associated with this span and
	// hence encoded in any RPC requests made by this span. The interpretation
	// of the metdata is governed by the value of TraceFlags.
	SetRequestMetadata(metadata []byte)

	// Finish ends the span, marking the end time.  The span should
	// not be used after Finish is called.
	Finish(error)

	// Trace returns the id of the trace this Span is a member of.
	Trace() uniqueid.Id

	// Store returns the store that this Span is stored in.
	Store() Store

	Request(ctx *context.T) Request
}

Spans represent a named time period. You can create new spans to represent new parts of your computation. Spans are safe to use from multiple goroutines simultaneously.

func GetSpan

func GetSpan(ctx *context.T) Span

Span finds the currently active span.

func WithContinuedTrace

func WithContinuedTrace(ctx *context.T, name string, sr *SamplingRequest, req Request) (*context.T, Span)

WithContinuedTrace creates a span that represents a continuation of a trace from a remote server. name is the name of the new span and req contains the parameters needed to connect this span with it's trace.

func WithNewSpan

func WithNewSpan(ctx *context.T, name string) (*context.T, Span)

WithNewSpan derives a context with a new Span that can be used to trace and annotate operations across process boundaries.

func WithNewTrace

func WithNewTrace(ctx *context.T, name string, sr *SamplingRequest) (*context.T, Span)

WithNewTrace creates a new vtrace context that is not the child of any other span. This is useful when starting operations that are disconnected from the activity ctx is performing. For example this might be used to start background tasks.

type SpanRecord

type SpanRecord struct {
	Id     uniqueid.Id // The Id of the Span.
	Parent uniqueid.Id // The Id of this Span's parent.
	Name   string      // The Name of this span.
	Start  time.Time   // The start time of this span.
	End    time.Time   // The end time of this span.
	// A series of annotations.
	Annotations []Annotation
	// RequestMetadata that will be sent along with the request.
	RequestMetadata []byte
}

A SpanRecord is the wire format for a Span.

func (SpanRecord) VDLIsZero

func (x SpanRecord) VDLIsZero() bool

func (*SpanRecord) VDLRead

func (x *SpanRecord) VDLRead(dec vdl.Decoder) error

func (SpanRecord) VDLReflect

func (SpanRecord) VDLReflect(struct {
	Name string `vdl:"v.io/v23/vtrace.SpanRecord"`
})

func (SpanRecord) VDLWrite

func (x SpanRecord) VDLWrite(enc vdl.Encoder) error

type Store

type Store interface {
	// TraceRecords returns TraceRecords for all traces saved in the store.
	TraceRecords() []TraceRecord

	// TraceRecord returns a TraceRecord for a given ID.  Returns
	// nil if the given id is not present.
	TraceRecord(traceid uniqueid.Id) *TraceRecord

	// ForceCollect forces the store to collect all information about a given trace and to capture
	// the log messages at the given log level.
	ForceCollect(traceid uniqueid.Id, level int)

	// Merge merges a vtrace.Response into the current store.
	Merge(response Response)

	// Return the log level in effect for this trace.
	LogLevel(traceid uniqueid.Id) int

	Flags(traceid uniqueid.Id) TraceFlags

	Start(traceid uniqueid.Id, span SpanRecord)

	Finish(traceid uniqueid.Id, span SpanRecord, timestamp time.Time)

	// Annotate records the requested annotation.
	Annotate(traceid uniqueid.Id, span SpanRecord, annotation Annotation)

	// AnnotateMetadata records the requested key value data with optional
	// indexing.
	AnnotateMetadata(traceid uniqueid.Id, span SpanRecord, key string, value interface{}, indexed bool) error
}

Store selectively collects information about traces in the system.

func GetStore

func GetStore(ctx *context.T) Store

GetStore returns the current Store.

type TraceFlags

type TraceFlags int32

TraceFlags represents a bit mask that determines

func (TraceFlags) VDLIsZero

func (x TraceFlags) VDLIsZero() bool

func (*TraceFlags) VDLRead

func (x *TraceFlags) VDLRead(dec vdl.Decoder) error

func (TraceFlags) VDLReflect

func (TraceFlags) VDLReflect(struct {
	Name string `vdl:"v.io/v23/vtrace.TraceFlags"`
})

func (TraceFlags) VDLWrite

func (x TraceFlags) VDLWrite(enc vdl.Encoder) error

type TraceRecord

type TraceRecord struct {
	Id    uniqueid.Id
	Spans []SpanRecord
}

func (TraceRecord) VDLIsZero

func (x TraceRecord) VDLIsZero() bool

func (*TraceRecord) VDLRead

func (x *TraceRecord) VDLRead(dec vdl.Decoder) error

func (TraceRecord) VDLReflect

func (TraceRecord) VDLReflect(struct {
	Name string `vdl:"v.io/v23/vtrace.TraceRecord"`
})

func (TraceRecord) VDLWrite

func (x TraceRecord) VDLWrite(enc vdl.Encoder) error

Jump to

Keyboard shortcuts

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