mirror of
https://github.com/zrepl/zrepl.git
synced 2024-12-01 21:04:59 +01:00
10a14a8c50
package trace: - introduce the concept of tasks and spans, tracked as linked list within ctx - see package-level docs for an overview of the concepts - **main feature 1**: unique stack of task and span IDs - makes it easy to follow a series of log entries in concurrent code - **main feature 2**: ability to produce a chrome://tracing-compatible trace file - either via an env variable or a `zrepl pprof` subcommand - this is not a CPU profile, we already have go pprof for that - but it is very useful to visually inspect where the replication / snapshotter / pruner spends its time ( fixes #307 ) usage in package daemon/logging: - goal: every log entry should have a trace field with the ID stack from package trace - make `logging.GetLogger(ctx, Subsys)` the authoritative `logger.Logger` factory function - the context carries a linked list of injected fields which `logging.GetLogger` adds to the logger it returns - `logging.GetLogger` also uses package `trace` to get the task-and-span-stack and injects it into the returned logger's fields
75 lines
1.8 KiB
Go
75 lines
1.8 KiB
Go
package trace
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
// use like this:
|
|
//
|
|
// defer WithSpanFromStackUpdateCtx(&existingCtx)()
|
|
//
|
|
//
|
|
func WithSpanFromStackUpdateCtx(ctx *context.Context) DoneFunc {
|
|
childSpanCtx, end := WithSpan(*ctx, getMyCallerOrPanic())
|
|
*ctx = childSpanCtx
|
|
return end
|
|
}
|
|
|
|
// derive task name from call stack (caller's name)
|
|
func WithTaskFromStack(ctx context.Context) (context.Context, DoneFunc) {
|
|
return WithTask(ctx, getMyCallerOrPanic())
|
|
}
|
|
|
|
// derive task name from call stack (caller's name) and update *ctx
|
|
// to point to be the child task ctx
|
|
func WithTaskFromStackUpdateCtx(ctx *context.Context) DoneFunc {
|
|
child, end := WithTask(*ctx, getMyCallerOrPanic())
|
|
*ctx = child
|
|
return end
|
|
}
|
|
|
|
// create a task and a span within it in one call
|
|
func WithTaskAndSpan(ctx context.Context, task string, span string) (context.Context, DoneFunc) {
|
|
ctx, endTask := WithTask(ctx, task)
|
|
ctx, endSpan := WithSpan(ctx, fmt.Sprintf("%s %s", task, span))
|
|
return ctx, func() {
|
|
endSpan()
|
|
endTask()
|
|
}
|
|
}
|
|
|
|
// create a span during which several child tasks are spawned using the `add` function
|
|
func WithTaskGroup(ctx context.Context, taskGroup string) (_ context.Context, add func(f func(context.Context)), waitEnd DoneFunc) {
|
|
var wg sync.WaitGroup
|
|
ctx, endSpan := WithSpan(ctx, taskGroup)
|
|
add = func(f func(context.Context)) {
|
|
wg.Add(1)
|
|
defer wg.Done()
|
|
ctx, endTask := WithTask(ctx, taskGroup)
|
|
defer endTask()
|
|
f(ctx)
|
|
}
|
|
waitEnd = func() {
|
|
wg.Wait()
|
|
endSpan()
|
|
}
|
|
return ctx, add, waitEnd
|
|
}
|
|
|
|
func getMyCallerOrPanic() string {
|
|
pc, _, _, ok := runtime.Caller(2)
|
|
if !ok {
|
|
panic("cannot get caller")
|
|
}
|
|
details := runtime.FuncForPC(pc)
|
|
if ok && details != nil {
|
|
const prefix = "github.com/zrepl/zrepl"
|
|
return strings.TrimPrefix(strings.TrimPrefix(details.Name(), prefix), "/")
|
|
}
|
|
return ""
|
|
}
|