package trace

import "runtime/trace"

go func() {
        trace.WithRegion(ctx, "steamMilk", steamMilk)
        milk <- true
}()
go func() {
        trace.WithRegion(ctx, "extractCoffee", extractCoffee)
        espresso <- true
}()
go func() {
        defer task.End() // When assemble is done, the order is complete.
        <-espresso
        <-milk
        trace.WithRegion(ctx, "mixMilkCoffee", mixMilkCoffee)
}()

The trace tool computes the latency of a task by measuring the time between the task creation and the task end and provides latency distributions for each task type found in the trace.

Example

Example demonstrates the use of the trace package to trace the execution of a Go program. The trace output will be written to the file trace.out

Code:play 

package main

import (
	"fmt"
	"log"
	"os"
	"runtime/trace"
)

// Example demonstrates the use of the trace package to trace
// the execution of a Go program. The trace output will be
// written to the file trace.out
func main() {
	f, err := os.Create("trace.out")
	if err != nil {
		log.Fatalf("failed to create trace output file: %v", err)
	}
	defer func() {
		if err := f.Close(); err != nil {
			log.Fatalf("failed to close trace file: %v", err)
		}
	}()

	if err := trace.Start(f); err != nil {
		log.Fatalf("failed to start trace: %v", err)
	}
	defer trace.Stop()

	// your program here
	RunMyProgram()
}

func RunMyProgram() {
	fmt.Printf("this function will be traced")
}

Index

Examples

Functions

func IsEnabled

func IsEnabled() bool

IsEnabled returns whether tracing is enabled. The information is advisory only. The tracing status may have changed by the time this function returns.

func Log

func Log(ctx context.Context, category, message string)

Log emits a one-off event with the given category and message. Category can be empty and the API assumes there are only a handful of unique categories in the system.

func Logf

func Logf(ctx context.Context, category, format string, args ...interface{})

Logf is like Log, but the value is formatted using the specified format spec.

func Start

func Start(w io.Writer) error

Start enables tracing for the current program. While tracing, the trace will be buffered and written to w. Start returns an error if tracing is already enabled.

func Stop

func Stop()

Stop stops the current tracing, if any. Stop only returns after all the writes for the trace have completed.

func WithRegion

func WithRegion(ctx context.Context, regionType string, fn func())

WithRegion starts a region associated with its calling goroutine, runs fn, and then ends the region. If the context carries a task, the region is associated with the task. Otherwise, the region is attached to the background task.

The regionType is used to classify regions, so there should be only a handful of unique region types.

Types

type Region

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

Region is a region of code whose execution time interval is traced.

func StartRegion

func StartRegion(ctx context.Context, regionType string) *Region

StartRegion starts a region and returns a function for marking the end of the region. The returned Region's End function must be called from the same goroutine where the region was started. Within each goroutine, regions must nest. That is, regions started after this region must be ended before this region can be ended. Recommended usage is

defer trace.StartRegion(ctx, "myTracedRegion").End()

func (*Region) End

func (r *Region) End()

End marks the end of the traced code region.

type Task

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

Task is a data type for tracing a user-defined, logical operation.

func NewTask

func NewTask(pctx context.Context, taskType string) (ctx context.Context, task *Task)

NewTask creates a task instance with the type taskType and returns it along with a Context that carries the task. If the input context contains a task, the new task is its subtask.

The taskType is used to classify task instances. Analysis tools like the Go execution tracer may assume there are only a bounded number of unique task types in the system.

The returned end function is used to mark the task's end. The trace tool measures task latency as the time between task creation and when the end function is called, and provides the latency distribution per task type. If the end function is called multiple times, only the first call is used in the latency measurement.

ctx, task := trace.NewTask(ctx, "awesome task")
trace.WithRegion(ctx, prepWork)
// preparation of the task
go func() {  // continue processing the task in a separate goroutine.
    defer task.End()
    trace.WithRegion(ctx, remainingWork)
}

func (*Task) End

func (t *Task) End()

End marks the end of the operation represented by the Task.

Source Files

annotation.go trace.go

Version
v1.11.0-beta.1
Published
Jun 26, 2018
Platform
darwin/amd64
Imports
7 packages
Last checked
40 seconds ago

Tools for package owners.