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
- func ForceCollect(ctx *context.T, level int)
- func FormatTrace(w io.Writer, record *TraceRecord, loc *time.Location)
- func FormatTraces(w io.Writer, records []TraceRecord, loc *time.Location)
- func WithManager(ctx *context.T, manager Manager) *context.T
- type Annotation
- type Manager
- type Node
- type Request
- type Response
- type Span
- type SpanRecord
- type Store
- type TraceFlags
- type TraceRecord
Constants ¶
const CollectInMemory = TraceFlags(1)
const Empty = TraceFlags(0)
Variables ¶
This section is empty.
Functions ¶
func ForceCollect ¶
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.
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) VDLReflect ¶
func (Annotation) VDLReflect(struct { Name string `vdl:"v.io/v23/vtrace.Annotation"` })
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) (*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, 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) // Span finds the currently active span. GetSpan(ctx *context.T) Span // Store returns the current Store. GetStore(ctx *context.T) Store // 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. Flags TraceFlags LogLevel int32 }
Request is the object that carries trace information between processes.
func GetRequest ¶
Generate a Request from the current context.
func (Request) VDLReflect ¶
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 ¶
Generate a Response from the current context.
func (Response) VDLReflect ¶
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{}) // Finish ends the span, marking the end time. The span should // not be used after Finish is called. Finish() // Trace returns the id of the trace this Span is a member of. Trace() uniqueid.Id }
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 WithContinuedTrace ¶
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 ¶
WithNewSpan derives a context with a new Span that can be used to trace and annotate operations across process boundaries.
func WithNewTrace ¶
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 }
A SpanRecord is the wire format for a Span.
func (SpanRecord) VDLIsZero ¶
func (x SpanRecord) VDLIsZero() bool
func (SpanRecord) VDLReflect ¶
func (SpanRecord) VDLReflect(struct { Name string `vdl:"v.io/v23/vtrace.SpanRecord"` })
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) }
Store selectively collects information about traces in the system.
type TraceFlags ¶
type TraceFlags int32
func (TraceFlags) VDLIsZero ¶
func (x TraceFlags) VDLIsZero() bool
func (TraceFlags) VDLReflect ¶
func (TraceFlags) VDLReflect(struct { Name string `vdl:"v.io/v23/vtrace.TraceFlags"` })
type TraceRecord ¶
type TraceRecord struct { Id uniqueid.Id Spans []SpanRecord }
func (TraceRecord) VDLIsZero ¶
func (x TraceRecord) VDLIsZero() bool
func (TraceRecord) VDLReflect ¶
func (TraceRecord) VDLReflect(struct { Name string `vdl:"v.io/v23/vtrace.TraceRecord"` })