otelKit

package
v3.0.905 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2024 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HeaderBaggage 跨服务相关的请求头
	HeaderBaggage = "Baggage"

	KeyTraceId = "trace-id"
	KeySpanId  = "span-id"
)

Variables

View Source
var (
	NotSetupError = errorKit.Newf("haven’t been set up correctly")

	NotOtelRequestError = errorKit.Newf("not otel request")
)
View Source
var NewAddConfig func(opts []metric.AddOption) metric.AddConfig = metric.NewAddConfig

Functions

func ExtractFromRequest added in v3.0.32

func ExtractFromRequest(r *http.Request) (remoteSpanCtx context.Context, err error)

ExtractFromRequest

PS: (1) 需要先 set up (2) 适用场景: 跨服务(跨应用通讯) + 接收端 (3) 返回的error != nil的情况下,可以判断是否等于 otelKit.NotOtelRequestError.

func GetSpanCtxFromGin added in v3.0.36

func GetSpanCtxFromGin(ctx *gin.Context) context.Context

GetSpanCtxFromGin

!!!: 需要先使用中间件 NewOuterGinMiddleware 或 NewSecondaryGinMiddleware.

func GetSpanFromGin added in v3.0.36

func GetSpanFromGin(ctx *gin.Context) trace.Span

GetSpanFromGin

!!!: 需要先使用中间件 NewOuterGinMiddleware 或 NewSecondaryGinMiddleware.

func InjectIntoMap added in v3.0.34

func InjectIntoMap(m map[string]string, spanCtx context.Context, span trace.Span) error

InjectIntoMap

PS: (1) 需要先 set up; (2) 适用场景: 跨服务(跨应用通讯) + 发送端.

func InjectIntoRequest added in v3.0.34

func InjectIntoRequest(r *http.Request, spanCtx context.Context, span trace.Span) error

InjectIntoRequest 使用 baggage 写入 trace id 和 span id(实际上是写到 Header 中).

PS: (1) 需要先 set up; (2) 适用场景: 跨服务(跨应用通讯) + 发送端.

func MustSetUpWithGrpc added in v3.0.36

func MustSetUpWithGrpc(grpcEndpoint, serviceName string, attributeMap map[string]string, opts ...otlptracegrpc.Option)

MustSetUpWithGrpc

@param opts e.g. otlptracegrpc.WithInsecure(), otlptracegrpc.WithDialOption(grpc.WithBlock())

func MustSetUpWithHttp added in v3.0.29

func MustSetUpWithHttp(httpEndpoint, serviceName string, attributeMap map[string]string, opts ...otlptracehttp.Option)

func NewGrpcTracerProvider

func NewGrpcTracerProvider(endpoint, serviceName string, attributeMap map[string]string, opts ...otlptracegrpc.Option) (*trace.TracerProvider, error)

NewGrpcTracerProvider

PS: (1) 使用 otlptracegrpc; (2) 此方法第二个返回值为nil的情况下,建议立即调用 otel.SetTracerProvider.

@param endpoint 可以为"",将采用默认值: "localhost:4317" @param serviceName 服务名 @param attributeMap 可以为nil @param opts (1) 额外配置(不要涉及 otlptracegrpc.WithEndpoint,因为在此处配了也没用)

(2) otlptracegrpc.WithInsecure(): 	(a) 配置的话,将使用非安全协议(http...);
									(b) 不配置的话,将使用安全协议(https...).
(3) 一般情况下,建议使用: otlptracegrpc.WithInsecure(), otlptracegrpc.WithDialOption(grpc.WithBlock())

func NewHttpTracerProvider deprecated

func NewHttpTracerProvider(endpoint, serviceName string, attributeMap map[string]string, opts ...otlptracehttp.Option) (*trace.TracerProvider, error)

NewHttpTracerProvider

Deprecated: Use NewGrpcTracerProvider instead.

PS: (1) 使用 otlptracehttp; (2) 此方法第二个返回值为nil的情况下,建议立即调用 otel.SetTracerProvider.

@param endpoint 可以为"",将采用默认值: "localhost:4318" @param serviceName 服务名 @param attributeMap 可以为nil @param opts 额外配置(不要涉及 otlptracehttp.WithEndpoint,因为在此处配了也没用) @param opts (1) 额外配置(不要涉及 otlptracegrpc.WithEndpoint,因为在此处配了也没用)

(2) otlptracegrpc.WithInsecure(): 	(a) 配置的话,将使用非安全协议(http...);
									(b) 不配置的话,将使用安全协议(https...).
(3) 一般情况下,传个 otlptracegrpc.WithInsecure() 就足够了.

func NewNoopTracer added in v3.0.36

func NewNoopTracer() trace.Tracer

func NewNoopTracerProvider added in v3.0.34

func NewNoopTracerProvider() trace.TracerProvider

NewNoopTracerProvider returns a TracerProvider that does not record any telemetry.

func NewOuterGinMiddleware added in v3.0.34

func NewOuterGinMiddleware() (middleware gin.HandlerFunc, err error)

NewOuterGinMiddleware 适用于: (链路追踪)最外层的服务.

PS: 需要先 set up.

func NewRemoteContext

func NewRemoteContext(traceIdStr, spanIdStr string) (context.Context, error)

NewRemoteContext TODO:

@param traceIdStr 请求头中,"trace-id"对应的值 @param spanIdStr 请求头中,"span-id"对应的值

func NewSecondaryGinMiddleware added in v3.0.34

func NewSecondaryGinMiddleware() (middleware gin.HandlerFunc, err error)

NewSecondaryGinMiddleware 适用于: (链路追踪)次级的服务.

PS: 需要先 set up.

func NewSpan added in v3.0.34

func NewSpan(tracer trace.Tracer, parentSpanCtx context.Context, spanName string, opts ...trace.SpanStartOption) (spanCtx context.Context, span trace.Span, err error)

NewSpan

@param tracer 不能为nil @param parentCtx 父span的Context(没有的话,可以使用 context.TODO()) @param spanName span的名称(UI页面中的Operation) @param opts e.g. UI页面中的Tags

trace.WithAttributes(attribute.String("222", "222"))

@return err == nil的情况下,spanCtx 可以作为子span的传参parentCtx

func NewTracer

func NewTracer(name string, opts ...trace.TracerOption) trace.Tracer

NewTracer

PS: (1) 需要先 set up; (2) 使用全局的TracerProvider.

func SetUpWithGrpc added in v3.0.36

func SetUpWithGrpc(grpcEndpoint, serviceName string, attributeMap map[string]string, opts ...otlptracegrpc.Option) (err error)

func SetUpWithHttp added in v3.0.29

func SetUpWithHttp(httpEndpoint, serviceName string, attributeMap map[string]string, opts ...otlptracehttp.Option) (err error)

func ShutdownTracerProvider

func ShutdownTracerProvider(tp *sdktrace.TracerProvider, timeout time.Duration)

Types

type JaegerConfig added in v3.0.29

type JaegerConfig struct {
	// Type "grpc" || "http"
	Type string `json:"type" yaml:"type" validate:"oneof=grpc http"`

	// Endpoint 可以为"",将采用默认值 "localhost:4317"(grpc) || "localhost:4318"(http)
	Endpoint string `json:"grpcEndpoint" yaml:"grpcEndpoint" validate:"omitempty,hostname_port"`
}

Jump to

Keyboard shortcuts

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