type traceContextKey … // 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 Task's [Task.End] method is used to mark the task's end. // The trace tool measures task latency as the time between task creation // and when the End method is called, and provides the latency // distribution per task type. // If the End method is called multiple times, only the first // call is used in the latency measurement. // // ctx, task := trace.NewTask(ctx, "awesomeTask") // trace.WithRegion(ctx, "preparation", prepWork) // // preparation of the task // go func() { // continue processing the task in a separate goroutine. // defer task.End() // trace.WithRegion(ctx, "remainingWork", remainingWork) // }() func NewTask(pctx context.Context, taskType string) (ctx context.Context, task *Task) { … } func fromContext(ctx context.Context) *Task { … } type Task … // End marks the end of the operation represented by the [Task]. func (t *Task) End() { … } var lastTaskID … func newID() uint64 { … } var bgTask … // 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 Log(ctx context.Context, category, message string) { … } // Logf is like [Log], but the value is formatted using the specified format spec. func Logf(ctx context.Context, category, format string, args ...any) { … } const regionStartCode … const regionEndCode … // 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. func WithRegion(ctx context.Context, regionType string, fn func()) { … } // StartRegion starts a region and returns it. // The returned Region's [Region.End] method 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 StartRegion(ctx context.Context, regionType string) *Region { … } type Region … var noopRegion … // End marks the end of the traced code region. func (r *Region) End() { … } // IsEnabled reports whether tracing is enabled. // The information is advisory only. The tracing status // may have changed by the time this function returns. func IsEnabled() bool { … } // emits UserTaskCreate event. func userTaskCreate(id, parentID uint64, taskType string) // emits UserTaskEnd event. func userTaskEnd(id uint64) // emits UserRegion event. func userRegion(id, mode uint64, regionType string) // emits UserLog event. func userLog(id uint64, category, message string)