Skip to content
Open
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 any, err error) {
func (t *Tracer) End(ctx context.Context, span trace.Span, m any, 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ErrorHandler


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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WithErrorHandler

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