trace

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2023 License: Apache-2.0 Imports: 20 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ApmSpanKey    = "APM_SPAN"
	CtxRequestKey = "CONTEXT_REQUEST"
)

Variables

This section is empty.

Functions

func GetNewTransactionContextFromExist

func GetNewTransactionContextFromExist(parentCtx context.Context) context.Context

GetNewTransactionContextFromExist 从已存在的事务上下文中,获取新的上下文,并绑定到当前事务中(防止当前上下文 cancel,造成下游调用的异常结束)

func GetNewTransactionContextFromGin

func GetNewTransactionContextFromGin(c *gin.Context) context.Context

GetNewTransactionContextFromGin 从 gin 中获取新的上下文,并绑定到当前事务中(防止当前上下文 cancel,造成下游调用的异常结束)

func IgnoreNone

func IgnoreNone(*grpc.UnaryServerInfo) bool

IgnoreNone is a RequestIgnorerFunc which ignores no requests.

func IgnoreNoneStream

func IgnoreNoneStream(*grpc.StreamServerInfo) bool

IgnoreNoneStream is a StreamIgnorerFunc which ignores no stream requests.

func Middleware

func Middleware(engine *gin.Engine, o ...Option) gin.HandlerFunc

Middleware returns a new Gin middleware handler for tracing requests and reporting errors.

This middleware will recover and report panics, so it can be used instead of the standard gin.Recovery middleware.

By default, the middleware will use apm.DefaultTracer. Use WithTracer to specify an alternative tracer.

func NewStreamClientInterceptor

func NewStreamClientInterceptor(o ...ClientOption) grpc.StreamClientInterceptor

NewStreamClientInterceptor returns a grpc.UnaryClientInterceptor that traces gRPC requests with the given options.

The interceptor will trace spans with the "external.grpc" type for each stream request made, for any client method presented with a context containing a sampled apm.Transaction.

Spans are ended when the stream is closed, which can happen in various ways: the initial stream setup request fails, Header, SendMsg or RecvMsg return with an error, or RecvMsg returns for a non-streaming server.

func NewStreamServerInterceptor

func NewStreamServerInterceptor(o ...ServerOption) grpc.StreamServerInterceptor

NewStreamServerInterceptor returns a grpc.StreamServerInterceptor that traces gRPC stream requests with the given options.

The interceptor will trace transactions with the "request" type for each incoming stream request. The transaction will be added to the context, so server methods can use apm.StartSpan with the provided context.

By default, the interceptor will trace with apm.DefaultTracer, and will not recover any panics. Use WithTracer to specify an alternative tracer, and WithRecovery to enable panic recovery.

func NewUnaryClientInterceptor

func NewUnaryClientInterceptor(o ...ClientOption) grpc.UnaryClientInterceptor

NewUnaryClientInterceptor returns a grpc.UnaryClientInterceptor that traces gRPC requests with the given options.

The interceptor will trace spans with the "external.grpc" type for each request made, for any client method presented with a context containing a sampled apm.Transaction.

func NewUnaryServerInterceptor

func NewUnaryServerInterceptor(o ...ServerOption) grpc.UnaryServerInterceptor

NewUnaryServerInterceptor returns a grpc.UnaryServerInterceptor that traces gRPC requests with the given options.

The interceptor will trace transactions with the "request" type for each incoming request. The transaction will be added to the context, so server methods can use apm.StartSpan with the provided context.

By default, the interceptor will trace with apm.DefaultTracer, and will not recover any panics. Use WithTracer to specify an alternative tracer, and WithRecovery to enable panic recovery.

func StartSpan

func StartSpan(ctx context.Context, name string, spanType string) (*apm.Span, context.Context)

StartSpan 开始一个调用执行 ctx: 需要是一个包含事务的上下文 例如:span, ctx := trace.StartSpan(ctx, "SELECT * FROM application_enroll", "db.mysql.query") 注意主要在合适的地方进行 span.End() 若要恢复恐慌并将其与事务一起报告,可使用以下方式:

defer func() {
	if v := recover(); v != nil {
		e := apm.DefaultTracer.Recovered()
		e.SetTransaction(tx) // or e.SetSpan(span)
		e.Send()
	}
}()

func StartTransaction

func StartTransaction(parent context.Context, name string, transactionType string) (*apm.Transaction, context.Context)

StartTransaction 开始一个新的调用链事务 例如:tx, ctx := trace.StartTransaction(context.Background(), "GET /api/v1", "request")

若有 DB 操作,需要将事务上下文绑定到 DB 中:db = db.Set(database.CtxRequestKey, ctx)

注意在合适的地方进行 tx.End()

Types

type ClientOption

type ClientOption func(*clientOptions)

ClientOption sets options for client-side tracing.

type Option

type Option func(*middleware)

Option sets options for tracing.

func WithRequestIgnorer

func WithRequestIgnorer(r apmhttp.RequestIgnorerFunc) Option

WithRequestIgnorer returns a Option which sets r as the function to use to determine whether or not a request should be ignored. If r is nil, all requests will be reported.

func WithTracerMiddleware

func WithTracerMiddleware(t *apm.Tracer) Option

WithTracer returns an Option which sets t as the tracer to use for tracing server requests.

type RequestIgnorerFunc

type RequestIgnorerFunc func(*grpc.UnaryServerInfo) bool

RequestIgnorerFunc is the type of a function for use in WithServerRequestIgnorer.

func DefaultServerRequestIgnorer

func DefaultServerRequestIgnorer() RequestIgnorerFunc

DefaultServerRequestIgnorer returns the default RequestIgnorerFunc to use in handlers.

func NewRegexpRequestIgnorer

func NewRegexpRequestIgnorer(re *regexp.Regexp) RequestIgnorerFunc

NewRegexpRequestIgnorer returns a RequestIgnorerFunc which matches requests' URLs against re. Note that for server requests, typically only Path and possibly RawQuery will be set, so the regular expression should take this into account.

type ServerOption

type ServerOption func(*serverOptions)

ServerOption sets options for server-side tracing.

func WithRecovery

func WithRecovery() ServerOption

WithRecovery returns a ServerOption which enables panic recovery in the gRPC server interceptor.

The interceptor will report panics as errors to Elastic APM, but unless this is enabled, they will still cause the server to be terminated. With recovery enabled, panics will be translated to gRPC errors with the code gprc/codes.Internal.

func WithServerRequestIgnorer

func WithServerRequestIgnorer(r RequestIgnorerFunc) ServerOption

WithServerRequestIgnorer returns a ServerOption which sets r as the function to use to determine whether or not a server request should be ignored. If r is nil, all requests will be reported.

func WithServerStreamIgnorer

func WithServerStreamIgnorer(s StreamIgnorerFunc) ServerOption

WithServerStreamIgnorer returns a ServerOption which sets s as the function to use to determine whether or not a server stream request should be ignored. If s is nil, all stream requests will be reported.

func WithTracerGrpcServer

func WithTracerGrpcServer(t *apm.Tracer) ServerOption

WithTracer returns a ServerOption which sets t as the tracer to use for tracing server requests.

type StreamIgnorerFunc

type StreamIgnorerFunc func(*grpc.StreamServerInfo) bool

StreamIgnorerFunc is the type of a function for use in WithServerStreamIgnorer.

func DefaultServerStreamIgnorer

func DefaultServerStreamIgnorer() StreamIgnorerFunc

DefaultServerStreamIgnorer returns the default StreamIgnorerFunc to use in handlers.

Jump to

Keyboard shortcuts

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