trace

package
v0.0.0-...-dc62a57 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// GRPCStatusCodeKey 是用于表示gRPC请求数字状态码的约定,
	GRPCStatusCodeKey = attribute.Key("rpc.grpc.status_code")
	// RPCNameKey 是传输或接收的消息的名称,
	RPCNameKey = attribute.Key("name")
	// RPCMessageTypeKey 是传输或接收的消息的类型,
	RPCMessageTypeKey = attribute.Key("message.type")
	// RPCMessageIDKey 是传输或接收的消息的标识符,
	RPCMessageIDKey = attribute.Key("message.id")
	// RPCMessageCompressedSizeKey 是传输或接收的消息的压缩大小,以字节为单位,
	RPCMessageCompressedSizeKey = attribute.Key("message.compressed_size")
	// RPCMessageUncompressedSizeKey 是传输或接收的消息的未压缩大小,以字节为单位,
	RPCMessageUncompressedSizeKey = attribute.Key("message.uncompressed_size")
)
View Source
const KTraceName = "goken"

Variables

View Source
var (
	// RPCSystemGRPC 是将 gRPC 作为远程系统的语义约定,
	RPCSystemGRPC = semconv.RPCSystemKey.String("grpc")
	// RPCNameMessage 是名为message的消息的语义约定,
	RPCNameMessage = RPCNameKey.String("message")
	// RPCMessageTypeSent 是发送的RPC消息类型的语义约定,
	RPCMessageTypeSent = RPCMessageTypeKey.String("SENT")
	// RPCMessageTypeReceived 是接收的RPC消息类型的语义约定,
	RPCMessageTypeReceived = RPCMessageTypeKey.String("RECEIVED")
)

常见RPC属性的otel自建的语义约定

View Source
var (
	// MessageSent 表示已发送的消息类型,
	MessageSent = messageType(RPCMessageTypeSent)
	// MessageReceived 表示已接收的消息类型,
	MessageReceived = messageType(RPCMessageTypeReceived)
)
View Source
var TraceIdKey = http.CanonicalHeaderKey("x-trace-id")

TraceIdKey is the trace id header. https://www.w3.org/TR/trace-context/#trace-id May change it to trace-id afterward.

Functions

func Extract

注意,Extract不会把span信息以context的形式抽取出,而是把span的spanContext信息抽取到ctx中

func ExtractMD

func ExtractMD(ctx context.Context, metadata metadata.MD) context.Context

从metadata中提取spanContext并将其注入到context中

func ExtractSpanFormCtx

func ExtractSpanFormCtx(ctx context.Context, opts ...trace.TracerOption) (trace.Tracer, context.Context)

从ctx中获取记录的tracer和spanCtx以便直接生成新span

func Inject

func Inject(ctx context.Context, carrier propagation.TextMapCarrier)

注意,Inject不会把span信息以context的形式注入到ctx中,而是把span的spanContext信息注入到carrier中

func InjectMD

func InjectMD(ctx context.Context, metadata metadata.MD)

spanContext注入到metadata中

func NewSpanOutgoingContext

func NewSpanOutgoingContext(ctx context.Context, span trace.Span) context.Context

将ctx中的span信息注入到metadata中

func ParseFullMethod

func ParseFullMethod(fullMethod string) (string, []attribute.KeyValue)

ParseFullMethod returns the method name and attributes.

func PeerAddrFromCtx

func PeerAddrFromCtx(ctx context.Context) string

PeerAddrFromCtx 返回客户端信息

func PeerAttr

func PeerAttr(addr string) []attribute.KeyValue

PeerAttr returns the peer attributes.

func RegistorTP

func RegistorTP(ctx context.Context, host string, opts ...TracerOption) error

注册对应的traceProvider

func ResolveGrpcInfo

func ResolveGrpcInfo(fullMethod, peerAddress string) (string, []attribute.KeyValue)

SpanInfo 返回

func SpanIDFromContext

func SpanIDFromContext(ctx context.Context) string

func StatusCodeAttr

func StatusCodeAttr(c gcodes.Code) attribute.KeyValue

StatusCodeAttr 返回一个表示给定gRPC状态码的attribute.KeyValue,

func TracerFromContext

func TracerFromContext(ctx context.Context) (tracer trace.Tracer)

TracerFromContext returns a tracer in ctx, otherwise returns a global tracer.

Types

type Tracer

type Tracer struct {
	Ctx context.Context
	// 记录traceProvider的serviceName
	ServiceName string

	// Sampler 记录基准采样率,派生的span采样率不会低于该百分比,至多百分百采样
	Sampler float64

	ExtraResources []attribute.KeyValue

	// OtlpHeaders是OTLP gRPC或HTTP传输的headers,例如:
	//  uptrace-dsn: 'http://project2_secret_token@localhost:14317/2'
	OtlpHeaders map[string]string

	// OtlpHttpPath是OTLP HTTP传输的路径,例如:/v1/traces
	OtlpHttpPath string

	// OtlpHttpSecure 指定 OTLP HTTP传输是否使用安全的HTTPS协议,
	OtlpHttpSecure bool

	// Global 表示是否将生成的TraceProvider加入到全局中
	Global bool
}

func MustNewTracer

func MustNewTracer(ctx context.Context, opts ...TracerOption) *Tracer

NewTracer 创建一个默认的 Tracer

func (*Tracer) AddResources

func (c *Tracer) AddResources(attrs ...attribute.KeyValue)

func (*Tracer) NewTraceProvider

func (c *Tracer) NewTraceProvider(host string) (*sdktrace.TracerProvider, error)

type TracerOption

type TracerOption func(*Tracer)

func WithExtraResource

func WithExtraResource(kv []attribute.KeyValue) TracerOption

WithGlobal 添加额外的字段信息

func WithGlobal

func WithGlobal(global bool) TracerOption

WithGlobal 设置是否将生成的 TraceProvider 加入到全局中

func WithName

func WithName(name string) TracerOption

WithName 设置tracing的服务名称

func WithOtlpHeaders

func WithOtlpHeaders(headers map[string]string) TracerOption

WithOtlpHeaders 设置 OTLP 传输使用的 headers

func WithOtlpHttpPath

func WithOtlpHttpPath(path string) TracerOption

WithOtlpHttpPath 设置 OTLP HTTP 传输的路径

func WithOtlpHttpSecure

func WithOtlpHttpSecure(secure bool) TracerOption

WithOtlpHttpSecure 设置 OTLP HTTP 传输是否使用 HTTPS

func WithSampler

func WithSampler(sampler float64) TracerOption

WithSampler 设置采样率,值为 [0.0, 1.0] 之间

Jump to

Keyboard shortcuts

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