mirror of
https://github.com/zrepl/zrepl.git
synced 2024-11-28 19:34:58 +01:00
add tracing package (forgot to commit)
This commit is contained in:
parent
1bd0dcfca6
commit
62c736d696
53
tracing/tracing.go
Normal file
53
tracing/tracing.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package tracing
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
type tracingContextKey int
|
||||||
|
|
||||||
|
const (
|
||||||
|
CallerContext tracingContextKey = 1 + iota
|
||||||
|
)
|
||||||
|
|
||||||
|
type jobSubtree struct {
|
||||||
|
jobid string
|
||||||
|
}
|
||||||
|
|
||||||
|
type ctx struct {
|
||||||
|
parent *ctx
|
||||||
|
job *jobSubtree
|
||||||
|
ident string
|
||||||
|
}
|
||||||
|
|
||||||
|
var root = &ctx{nil, nil, ""}
|
||||||
|
|
||||||
|
func getParentOrRoot(c context.Context) *ctx {
|
||||||
|
parent, ok := c.Value(CallerContext).(*ctx)
|
||||||
|
if !ok {
|
||||||
|
parent = root
|
||||||
|
}
|
||||||
|
return parent
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeChild(c context.Context, child *ctx) context.Context {
|
||||||
|
if child.parent == nil {
|
||||||
|
panic(child)
|
||||||
|
}
|
||||||
|
return context.WithValue(c, CallerContext, child)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Child(c context.Context, ident string) context.Context {
|
||||||
|
parent := getParentOrRoot(c)
|
||||||
|
return makeChild(c, &ctx{parent: parent, ident: ident})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetStack(c context.Context) (idents []string) {
|
||||||
|
ct, ok := c.Value(CallerContext).(*ctx)
|
||||||
|
if !ok {
|
||||||
|
return idents
|
||||||
|
}
|
||||||
|
for ct.parent != nil {
|
||||||
|
idents = append(idents, ct.ident)
|
||||||
|
ct = ct.parent
|
||||||
|
}
|
||||||
|
return idents
|
||||||
|
}
|
21
tracing/tracing_test.go
Normal file
21
tracing/tracing_test.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package tracing
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIt(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
ctx = Child(ctx, "a")
|
||||||
|
ctx = Child(ctx, "b")
|
||||||
|
ctx = Child(ctx, "c")
|
||||||
|
ctx = Child(ctx, "d")
|
||||||
|
|
||||||
|
assert.Equal(t, "dcba", strings.Join(GetStack(ctx), ""))
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user