2021-08-11 13:20:00 +08:00
|
|
|
// 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
|
|
|
|
|
2021-08-20 15:44:08 +08:00
|
|
|
import (
|
|
|
|
"context"
|
2022-06-17 16:47:01 +08:00
|
|
|
"os"
|
|
|
|
"strings"
|
2021-11-13 23:34:16 +08:00
|
|
|
|
2022-06-17 16:47:01 +08:00
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel/propagation"
|
2022-09-26 22:11:13 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/net/gtrace"
|
2021-08-20 15:44:08 +08:00
|
|
|
)
|
2021-08-11 13:20:00 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
)
|
2021-08-20 15:44:08 +08:00
|
|
|
|
2022-06-17 16:47:01 +08:00
|
|
|
var (
|
2022-09-23 20:50:48 +08:00
|
|
|
// initCtx is the context initialized from process environment.
|
|
|
|
initCtx context.Context
|
2022-06-17 16:47:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// All environment key-value pairs.
|
|
|
|
m := make(map[string]string)
|
|
|
|
i := 0
|
|
|
|
for _, s := range os.Environ() {
|
|
|
|
i = strings.IndexByte(s, '=')
|
2022-08-15 20:40:17 +08:00
|
|
|
if i == -1 {
|
|
|
|
continue
|
|
|
|
}
|
2022-06-17 16:47:01 +08:00
|
|
|
m[s[0:i]] = s[i+1:]
|
|
|
|
}
|
|
|
|
// OpenTelemetry from environments.
|
2022-09-23 20:50:48 +08:00
|
|
|
initCtx = otel.GetTextMapPropagator().Extract(
|
2022-06-17 16:47:01 +08:00
|
|
|
context.Background(),
|
|
|
|
propagation.MapCarrier(m),
|
|
|
|
)
|
2023-04-12 10:35:24 +08:00
|
|
|
initCtx = WithCtx(initCtx)
|
2022-06-17 16:47:01 +08:00
|
|
|
}
|
|
|
|
|
2021-08-20 15:44:08 +08:00
|
|
|
// New creates and returns a context which contains context id.
|
|
|
|
func New() context.Context {
|
2022-09-23 20:50:48 +08:00
|
|
|
return WithCtx(context.Background())
|
2021-08-20 15:44:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithCtx creates and returns a context containing context id upon given parent context `ctx`.
|
|
|
|
func WithCtx(ctx context.Context) context.Context {
|
2022-06-17 16:47:01 +08:00
|
|
|
if CtxId(ctx) != "" {
|
|
|
|
return ctx
|
|
|
|
}
|
2022-02-09 02:49:29 +08:00
|
|
|
if gtrace.IsUsingDefaultProvider() {
|
|
|
|
var span *gtrace.Span
|
|
|
|
ctx, span = gtrace.NewSpan(ctx, "gctx.WithCtx")
|
|
|
|
defer span.End()
|
|
|
|
}
|
2021-12-16 23:09:37 +08:00
|
|
|
return ctx
|
2021-08-20 15:44:08 +08:00
|
|
|
}
|
|
|
|
|
2021-10-11 20:25:02 +08:00
|
|
|
// CtxId retrieves and returns the context id from context.
|
|
|
|
func CtxId(ctx context.Context) string {
|
2021-12-16 23:09:37 +08:00
|
|
|
return gtrace.GetTraceID(ctx)
|
2021-08-20 15:44:08 +08:00
|
|
|
}
|
2022-07-12 19:27:42 +08:00
|
|
|
|
|
|
|
// SetInitCtx sets custom initialization context.
|
|
|
|
// Note that this function cannot be called in multiple goroutines.
|
|
|
|
func SetInitCtx(ctx context.Context) {
|
|
|
|
initCtx = ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInitCtx returns the initialization context.
|
2022-09-23 20:50:48 +08:00
|
|
|
// Initialization context is used in `main` or `init` functions.
|
2022-07-12 19:27:42 +08:00
|
|
|
func GetInitCtx() context.Context {
|
|
|
|
return initCtx
|
|
|
|
}
|