ctxs

package
v0.6.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2023 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UserInfoKey     string = "iThings-user"
	UserTokenKey    string = "iThings-token"
	UserSetTokenKey string = "iThings-set-token"
	MetadataKey     string = "iThings-meta"
)

Variables

Functions

func CopyContext

func CopyContext(ctx context.Context) context.Context

func GetDeadLine

func GetDeadLine(ctx context.Context, defaultDeadLine time.Time) time.Time

func GetMetaProjectID

func GetMetaProjectID(ctx context.Context) int64

获取meta里的项目ID(企业版功能)

func GetMetaVal

func GetMetaVal(ctx context.Context, field string) []string

func SetMetaCtx

func SetMetaCtx(ctx context.Context, maps MetadataCtx) context.Context

func SetUserCtx

func SetUserCtx(ctx context.Context, userCtx *UserCtx) context.Context

func StartSpan

func StartSpan(ctx context.Context, method, target string) (context.Context, trace.Span)

链路追踪接口,满足opentracing规范,通过ctx 将链路tid和spanid传递 使用方法:

将上游ctx作为参数传入,tp为yaml中配置的jaeger的信息的全局对象,
instrumentationName和spanName可自由命名,会在jaeger可视化中显示出来,后续定制命名规范。
返回本节点的ctx和span信息,以备下游继续传递使用

示例:

		1、
			dd.yaml中配置:
			Telemetry:
			  Name: dd.rpc                                --service name
			  Endpoint: http://127.0.0.1:14268/api/traces --推送jaeger的已远程url地址
			  Sampler: 1.0                                --默认1.0
			  Batcher: jaeger                             --可选择 jaeger或者zipkin
		2、
            ctx_sub0 := context.Background()
			ctx_sub1, span1 := tr.StartSpan(ctx_sub0, "fun1", "target1")
			defer span1.End() -- 接口结束前必须调用span.End() 否则无法将本节点链路信息推送jaeger
			logx.Infof("traceid:%s, spanid:%s", span1.SpanContext().TraceID(), span1.SpanContext().SpanID())

----------- 再将上游的ctx_sub1 传递到下游节点形成父子关系:

            ctx_sub2, span2 := tr.StartSpan(ctx_sub1, "fun2", "target2")
			defer span2.End()
			logx.Infof("traceid:%s, spanid:%s", span2.SpanContext().TraceID(), span2.SpanContext().SpanID())

Types

type MetaField

type MetaField string
const (
	MetaFieldProjectID MetaField = "Ithings-Project-Id" //meta里的项目权限控制ID字段(企业版功能)
)

注意:val值 必须是 首字母大写,其他小写

type MetadataCtx

type MetadataCtx = map[string][]string

func GetMetaCtx

func GetMetaCtx(ctx context.Context) MetadataCtx

type UserCtx

type UserCtx struct {
	IsOpen    bool   //是否开放认证用户
	UserID    int64  //用户id(开放认证用户值为0)
	Role      int64  //用户角色(开放认证用户值为0)
	IsAllData bool   //是否所有数据权限(开放认证用户值为true)
	IP        string //用户的ip地址
	Os        string //操作系统
}

func GetUserCtx

func GetUserCtx(ctx context.Context) *UserCtx

使用该函数前必须传了UserCtx

func GetUserCtxOrNil

func GetUserCtxOrNil(ctx context.Context) *UserCtx

使用该函数前必须传了UserCtx

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL