mirror of
https://gitee.com/johng/gf.git
synced 2024-12-03 04:37:49 +08:00
40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
// Package gctx wraps context.Context and provides extra context features.
|
|
package gctx
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/net/gtrace"
|
|
)
|
|
|
|
type (
|
|
Ctx = context.Context // Ctx is short name alias for context.Context.
|
|
StrKey string // StrKey is a type for warps basic type string as context key.
|
|
)
|
|
|
|
// New creates and returns a context which contains context id.
|
|
func New() context.Context {
|
|
return WithCtx(context.Background())
|
|
}
|
|
|
|
// WithCtx creates and returns a context containing context id upon given parent context `ctx`.
|
|
func WithCtx(ctx context.Context) context.Context {
|
|
if gtrace.IsUsingDefaultProvider() {
|
|
var span *gtrace.Span
|
|
ctx, span = gtrace.NewSpan(ctx, "gctx.WithCtx")
|
|
defer span.End()
|
|
}
|
|
return ctx
|
|
}
|
|
|
|
// CtxId retrieves and returns the context id from context.
|
|
func CtxId(ctx context.Context) string {
|
|
return gtrace.GetTraceID(ctx)
|
|
}
|