From 96f9eed2a2a041c77b2c2a7595ee1ff24e661232 Mon Sep 17 00:00:00 2001 From: "yanan.liu" Date: Mon, 15 Jul 2024 19:45:46 +0800 Subject: [PATCH 1/4] feat: tracing add custom report err func --- middleware/tracing/tracer.go | 10 +++++++--- middleware/tracing/tracing.go | 17 ++++++++++++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/middleware/tracing/tracer.go b/middleware/tracing/tracer.go index 67f1a2d958e..c07f577d8a3 100644 --- a/middleware/tracing/tracer.go +++ b/middleware/tracing/tracer.go @@ -62,9 +62,13 @@ 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) { if err != nil { - span.RecordError(err) - if e := errors.FromError(err); e != nil { - span.SetAttributes(attribute.Key("rpc.status_code").Int64(int64(e.Code))) + if t.opt.customReportErr != nil { + t.opt.customReportErr(span, err) + } else { + span.RecordError(err) + if e := errors.FromError(err); e != nil { + span.SetAttributes(attribute.Key("rpc.status_code").Int64(int64(e.Code))) + } } span.SetStatus(codes.Error, err.Error()) } else { diff --git a/middleware/tracing/tracing.go b/middleware/tracing/tracing.go index 7d9b53a1bc2..585b2081998 100644 --- a/middleware/tracing/tracing.go +++ b/middleware/tracing/tracing.go @@ -15,10 +15,14 @@ import ( // Option is tracing option. type Option func(*options) +// customReportErrFunc custom report err func handler +type customReportErrFunc func(span trace.Span, err error) + type options struct { - tracerName string - tracerProvider trace.TracerProvider - propagator propagation.TextMapPropagator + tracerName string + tracerProvider trace.TracerProvider + propagator propagation.TextMapPropagator + customReportErr customReportErrFunc } // WithPropagator with tracer propagator. @@ -43,6 +47,13 @@ func WithTracerName(tracerName string) Option { } } +// WithCustomReportErr with custom report err func +func WithCustomReportErr(f customReportErrFunc) Option { + return func(o *options) { + o.customReportErr = f + } +} + // Server returns a new server middleware for OpenTelemetry. func Server(opts ...Option) middleware.Middleware { tracer := NewTracer(trace.SpanKindServer, opts...) From 9e45a76f3313b36a2e80f29afeb254dd712ad39f Mon Sep 17 00:00:00 2001 From: "yanan.liu" Date: Tue, 16 Jul 2024 17:01:29 +0800 Subject: [PATCH 2/4] feat: optimize the definition of the customReportErr method --- middleware/tracing/tracer.go | 17 ++++++++--------- middleware/tracing/tracing.go | 2 +- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/middleware/tracing/tracer.go b/middleware/tracing/tracer.go index c07f577d8a3..5b46b7622fa 100644 --- a/middleware/tracing/tracer.go +++ b/middleware/tracing/tracer.go @@ -26,6 +26,12 @@ func NewTracer(kind trace.SpanKind, opts ...Option) *Tracer { op := options{ propagator: propagation.NewCompositeTextMapPropagator(Metadata{}, propagation.Baggage{}, propagation.TraceContext{}), tracerName: "kratos", + customReportErr: 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) @@ -60,16 +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 { - if t.opt.customReportErr != nil { - t.opt.customReportErr(span, err) - } else { - span.RecordError(err) - if e := errors.FromError(err); e != nil { - span.SetAttributes(attribute.Key("rpc.status_code").Int64(int64(e.Code))) - } - } + t.opt.customReportErr(ctx, span, err) span.SetStatus(codes.Error, err.Error()) } else { span.SetStatus(codes.Ok, "OK") diff --git a/middleware/tracing/tracing.go b/middleware/tracing/tracing.go index 585b2081998..44fbdec6a74 100644 --- a/middleware/tracing/tracing.go +++ b/middleware/tracing/tracing.go @@ -16,7 +16,7 @@ import ( type Option func(*options) // customReportErrFunc custom report err func handler -type customReportErrFunc func(span trace.Span, err error) +type customReportErrFunc func(ctx context.Context, span trace.Span, err error) type options struct { tracerName string From 674077eb08ed55877ea7bb2c3ac102cd117e5d27 Mon Sep 17 00:00:00 2001 From: "yanan.liu" Date: Tue, 16 Jul 2024 17:47:23 +0800 Subject: [PATCH 3/4] feat: optimize the delcar func name --- middleware/tracing/tracer.go | 4 ++-- middleware/tracing/tracing.go | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/middleware/tracing/tracer.go b/middleware/tracing/tracer.go index 5b46b7622fa..fc550dd3f1f 100644 --- a/middleware/tracing/tracer.go +++ b/middleware/tracing/tracer.go @@ -26,7 +26,7 @@ func NewTracer(kind trace.SpanKind, opts ...Option) *Tracer { op := options{ propagator: propagation.NewCompositeTextMapPropagator(Metadata{}, propagation.Baggage{}, propagation.TraceContext{}), tracerName: "kratos", - customReportErr: func(_ context.Context, span trace.Span, err error) { + reportErrHandle: 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))) @@ -68,7 +68,7 @@ func (t *Tracer) Start(ctx context.Context, operation string, carrier propagatio // End finish tracing span func (t *Tracer) End(ctx context.Context, span trace.Span, m interface{}, err error) { if err != nil { - t.opt.customReportErr(ctx, span, err) + t.opt.reportErrHandle(ctx, span, err) span.SetStatus(codes.Error, err.Error()) } else { span.SetStatus(codes.Ok, "OK") diff --git a/middleware/tracing/tracing.go b/middleware/tracing/tracing.go index 44fbdec6a74..4e6ed43d29c 100644 --- a/middleware/tracing/tracing.go +++ b/middleware/tracing/tracing.go @@ -15,14 +15,14 @@ import ( // Option is tracing option. type Option func(*options) -// customReportErrFunc custom report err func handler -type customReportErrFunc func(ctx context.Context, span trace.Span, err error) +// ReportErrHandle report err func handler +type ReportErrHandle func(ctx context.Context, span trace.Span, err error) type options struct { tracerName string tracerProvider trace.TracerProvider propagator propagation.TextMapPropagator - customReportErr customReportErrFunc + reportErrHandle ReportErrHandle } // WithPropagator with tracer propagator. @@ -47,10 +47,10 @@ func WithTracerName(tracerName string) Option { } } -// WithCustomReportErr with custom report err func -func WithCustomReportErr(f customReportErrFunc) Option { +// WithReportErrHandle with custom report err func +func WithReportErrHandle(f ReportErrHandle) Option { return func(o *options) { - o.customReportErr = f + o.reportErrHandle = f } } From 8174d3dc04c72a317c54f02df1d01c33d4eb31f7 Mon Sep 17 00:00:00 2001 From: "yanan.liu" Date: Tue, 16 Jul 2024 18:16:15 +0800 Subject: [PATCH 4/4] style: optimize the variable declare --- middleware/tracing/tracer.go | 4 ++-- middleware/tracing/tracing.go | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/middleware/tracing/tracer.go b/middleware/tracing/tracer.go index fc550dd3f1f..bbd131f97b7 100644 --- a/middleware/tracing/tracer.go +++ b/middleware/tracing/tracer.go @@ -26,7 +26,7 @@ func NewTracer(kind trace.SpanKind, opts ...Option) *Tracer { op := options{ propagator: propagation.NewCompositeTextMapPropagator(Metadata{}, propagation.Baggage{}, propagation.TraceContext{}), tracerName: "kratos", - reportErrHandle: func(_ context.Context, span trace.Span, err error) { + 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))) @@ -68,7 +68,7 @@ func (t *Tracer) Start(ctx context.Context, operation string, carrier propagatio // End finish tracing span func (t *Tracer) End(ctx context.Context, span trace.Span, m interface{}, err error) { if err != nil { - t.opt.reportErrHandle(ctx, span, err) + t.opt.reportErrorHandle(ctx, span, err) span.SetStatus(codes.Error, err.Error()) } else { span.SetStatus(codes.Ok, "OK") diff --git a/middleware/tracing/tracing.go b/middleware/tracing/tracing.go index 4e6ed43d29c..6bdfacf5d66 100644 --- a/middleware/tracing/tracing.go +++ b/middleware/tracing/tracing.go @@ -15,14 +15,14 @@ import ( // Option is tracing option. type Option func(*options) -// ReportErrHandle report err func handler -type ReportErrHandle func(ctx context.Context, span trace.Span, err error) +// 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 - reportErrHandle ReportErrHandle + tracerName string + tracerProvider trace.TracerProvider + propagator propagation.TextMapPropagator + reportErrorHandle ReportErrorHandle } // WithPropagator with tracer propagator. @@ -47,10 +47,10 @@ func WithTracerName(tracerName string) Option { } } -// WithReportErrHandle with custom report err func -func WithReportErrHandle(f ReportErrHandle) Option { +// WithReportErrorHandle with custom report err func +func WithReportErrorHandle(f ReportErrorHandle) Option { return func(o *options) { - o.reportErrHandle = f + o.reportErrorHandle = f } }