Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tracing add custom report err func #3370

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions middleware/tracing/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ func NewTracer(kind trace.SpanKind, opts ...Option) *Tracer {
op := options{
propagator: propagation.NewCompositeTextMapPropagator(Metadata{}, propagation.Baggage{}, propagation.TraceContext{}),
tracerName: "kratos",
reportErrorHandle: func(_ context.Context, span trace.Span, err error) {
span.RecordError(err)
if e := errors.FromError(err); e != nil {
span.SetAttributes(attribute.Key("rpc.status_code").Int64(int64(e.Code)))
}
},
}
for _, o := range opts {
o(&op)
Expand Down Expand Up @@ -60,12 +66,9 @@ func (t *Tracer) Start(ctx context.Context, operation string, carrier propagatio
}

// End finish tracing span
func (t *Tracer) End(_ context.Context, span trace.Span, m interface{}, err error) {
func (t *Tracer) End(ctx context.Context, span trace.Span, m interface{}, err error) {
if err != nil {
span.RecordError(err)
if e := errors.FromError(err); e != nil {
span.SetAttributes(attribute.Key("rpc.status_code").Int64(int64(e.Code)))
}
t.opt.reportErrorHandle(ctx, span, err)
span.SetStatus(codes.Error, err.Error())
} else {
span.SetStatus(codes.Ok, "OK")
Expand Down
17 changes: 14 additions & 3 deletions middleware/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ import (
// Option is tracing option.
type Option func(*options)

// ReportErrorHandle report err func handler
type ReportErrorHandle func(ctx context.Context, span trace.Span, err error)

type options struct {
tracerName string
tracerProvider trace.TracerProvider
propagator propagation.TextMapPropagator
tracerName string
tracerProvider trace.TracerProvider
propagator propagation.TextMapPropagator
reportErrorHandle ReportErrorHandle
}

// WithPropagator with tracer propagator.
Expand All @@ -43,6 +47,13 @@ func WithTracerName(tracerName string) Option {
}
}

// WithReportErrorHandle with custom report err func
func WithReportErrorHandle(f ReportErrorHandle) Option {
return func(o *options) {
o.reportErrorHandle = f
}
}

// Server returns a new server middleware for OpenTelemetry.
func Server(opts ...Option) middleware.Middleware {
tracer := NewTracer(trace.SpanKindServer, opts...)
Expand Down
Loading