This commit is contained in:
C菌
2022-04-20 03:50:21 +08:00
commit c113e38d82
11 changed files with 465 additions and 0 deletions

32
pkg/trace/trace.go Normal file
View File

@@ -0,0 +1,32 @@
package trace
import (
"context"
"github.com/flamego/flamego"
"github.com/google/uuid"
)
type contextKey struct{}
var activeSpanKey = contextKey{}
func Context(ctx context.Context) context.Context {
x := uuid.New().String()
return context.WithValue(ctx, activeSpanKey, x)
}
func Trace(ctx context.Context) string {
traceValue := ctx.Value(activeSpanKey)
if trace, ok := traceValue.(string); ok {
return trace
}
return ""
}
func Tracer() flamego.ContextInvoker {
return func(ctx flamego.Context) {
r := ctx.Request().Clone(Context(context.Background()))
ctx.Map(r)
}
}