From c1e3e9c5a26f19607d1e334b42b04f159ead704c Mon Sep 17 00:00:00 2001 From: Evstigneev Denis Date: Wed, 9 Oct 2024 21:16:43 +0300 Subject: [PATCH] add unpack labels --- logger/wrapper/wrapper.go | 52 +++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/logger/wrapper/wrapper.go b/logger/wrapper/wrapper.go index ee96f5c6..52126261 100644 --- a/logger/wrapper/wrapper.go +++ b/logger/wrapper/wrapper.go @@ -224,11 +224,12 @@ func (l *lWrapper) Call(ctx context.Context, req client.Request, rsp interface{} return err } - var labels []string + var labels []interface{} for _, o := range l.opts.ClientCallObservers { - labels = append(labels, o(ctx, req, rsp, opts, err)...) + attrs := convertToInterface(o(ctx, req, rsp, opts, err)) + labels = append(labels, attrs...) } - l.opts.Logger.Fields(labels).Log(ctx, l.opts.Level) + l.opts.Logger.Fields(labels...).Log(ctx, l.opts.Level) return err } @@ -247,11 +248,12 @@ func (l *lWrapper) Stream(ctx context.Context, req client.Request, opts ...clien return stream, err } - var labels []string + var labels []interface{} for _, o := range l.opts.ClientStreamObservers { - labels = append(labels, o(ctx, req, opts, stream, err)...) + attrs := convertToInterface(o(ctx, req, opts, stream, err)) + labels = append(labels, attrs...) } - l.opts.Logger.Fields(labels).Log(ctx, l.opts.Level) + l.opts.Logger.Fields(labels...).Log(ctx, l.opts.Level) return stream, err } @@ -270,11 +272,12 @@ func (l *lWrapper) Publish(ctx context.Context, msg client.Message, opts ...clie return err } - var labels []string + var labels []interface{} for _, o := range l.opts.ClientPublishObservers { - labels = append(labels, o(ctx, msg, opts, err)...) + attrs := convertToInterface(o(ctx, msg, opts, err)) + labels = append(labels, attrs...) } - l.opts.Logger.Fields(labels).Log(ctx, l.opts.Level) + l.opts.Logger.Fields(labels...).Log(ctx, l.opts.Level) return err } @@ -293,11 +296,12 @@ func (l *lWrapper) ServerHandler(ctx context.Context, req server.Request, rsp in return err } - var labels []string + var labels []interface{} for _, o := range l.opts.ServerHandlerObservers { - labels = append(labels, o(ctx, req, rsp, err)...) + attrs := convertToInterface(o(ctx, req, rsp, err)) + labels = append(labels, attrs...) } - l.opts.Logger.Fields(labels).Log(ctx, l.opts.Level) + l.opts.Logger.Fields(labels...).Log(ctx, l.opts.Level) return err } @@ -316,11 +320,12 @@ func (l *lWrapper) ServerSubscriber(ctx context.Context, msg server.Message) err return err } - var labels []string + var labels []interface{} for _, o := range l.opts.ServerSubscriberObservers { - labels = append(labels, o(ctx, msg, err)...) + attrs := convertToInterface(o(ctx, msg, err)) + labels = append(labels, attrs...) } - l.opts.Logger.Fields(labels).Log(ctx, l.opts.Level) + l.opts.Logger.Fields(labels...).Log(ctx, l.opts.Level) return err } @@ -363,11 +368,12 @@ func (l *lWrapper) ClientCallFunc(ctx context.Context, addr string, req client.R return err } - var labels []string + var labels []interface{} for _, o := range l.opts.ClientCallFuncObservers { - labels = append(labels, o(ctx, addr, req, rsp, opts, err)...) + attrs := convertToInterface(o(ctx, addr, req, rsp, opts, err)) + labels = append(labels, attrs...) } - l.opts.Logger.Fields(labels).Log(ctx, l.opts.Level) + l.opts.Logger.Fields(labels...).Log(ctx, l.opts.Level) return err } @@ -397,3 +403,13 @@ func NewServerSubscriberWrapper(opts ...Option) server.SubscriberWrapper { return l.ServerSubscriber } } + +func convertToInterface(attrs []string) []interface{} { + result := make([]interface{}, 0, len(attrs)) + + for idx := range attrs { + result = append(result, attrs[idx]) + } + + return result +}