From 26a2d18766559a77102d347b9c39ca29d1bd457e Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sat, 24 Dec 2022 18:09:48 +0300 Subject: [PATCH 1/2] tracer: fix span options Signed-off-by: Vasiliy Tolstov --- tracer/noop.go | 6 +++--- tracer/options.go | 10 +++++++++- tracer/tracer.go | 31 +------------------------------ 3 files changed, 13 insertions(+), 34 deletions(-) diff --git a/tracer/noop.go b/tracer/noop.go index 261c1ac6..13a2e0c7 100644 --- a/tracer/noop.go +++ b/tracer/noop.go @@ -35,7 +35,7 @@ type noopSpan struct { ctx context.Context tracer Tracer name string - labels []Label + opts SpanOptions } func (s *noopSpan) Finish(opts ...SpanOption) { @@ -56,8 +56,8 @@ func (s *noopSpan) SetName(name string) { s.name = name } -func (s *noopSpan) SetLabels(labels ...Label) { - s.labels = labels +func (s *noopSpan) SetLabels(labels ...interface{}) { + s.opts.Labels = labels } // NewTracer returns new memory tracer diff --git a/tracer/options.go b/tracer/options.go index d0c27604..917d0f95 100644 --- a/tracer/options.go +++ b/tracer/options.go @@ -3,7 +3,9 @@ package tracer import "go.unistack.org/micro/v3/logger" // SpanOptions contains span option -type SpanOptions struct{} +type SpanOptions struct { + Labels []interface{} +} // SpanOption func signature type SpanOption func(o *SpanOptions) @@ -14,6 +16,12 @@ type EventOptions struct{} // EventOption func signature type EventOption func(o *EventOptions) +func SpanLabels(labels ...interface{}) SpanOption { + return func(o *SpanOptions) { + o.Labels = labels + } +} + // Options struct type Options struct { // Logger used for logging diff --git a/tracer/tracer.go b/tracer/tracer.go index becd6f09..7d6ea856 100644 --- a/tracer/tracer.go +++ b/tracer/tracer.go @@ -30,34 +30,5 @@ type Span interface { // SetName set the span name SetName(name string) // SetLabels set the span labels - SetLabels(labels ...Label) -} - -type Label struct { - val interface{} - key string -} - -func LabelAny(k string, v interface{}) Label { - return Label{key: k, val: v} -} - -func LabelString(k string, v string) Label { - return Label{key: k, val: v} -} - -func LabelInt(k string, v int) Label { - return Label{key: k, val: v} -} - -func LabelInt64(k string, v int64) Label { - return Label{key: k, val: v} -} - -func LabelFloat64(k string, v float64) Label { - return Label{key: k, val: v} -} - -func LabelBool(k string, v bool) Label { - return Label{key: k, val: v} + SetLabels(labels ...interface{}) } -- 2.45.2 From 0be584ef0d7a1cc7c78f652affcd0df65fac9064 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sat, 24 Dec 2022 18:18:42 +0300 Subject: [PATCH 2/2] fix wrapper Signed-off-by: Vasiliy Tolstov --- tracer/wrapper/wrapper.go | 50 +++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/tracer/wrapper/wrapper.go b/tracer/wrapper/wrapper.go index 626895ab..3121d911 100644 --- a/tracer/wrapper/wrapper.go +++ b/tracer/wrapper/wrapper.go @@ -14,95 +14,95 @@ import ( var ( DefaultClientCallObserver = func(ctx context.Context, req client.Request, rsp interface{}, opts []client.CallOption, sp tracer.Span, err error) { sp.SetName(fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())) - var labels []tracer.Label + var labels []interface{} if md, ok := metadata.FromOutgoingContext(ctx); ok { - labels = make([]tracer.Label, 0, len(md)) + labels = make([]interface{}, 0, len(md)) for k, v := range md { - labels = append(labels, tracer.LabelString(k, v)) + labels = append(labels, k, v) } } if err != nil { - labels = append(labels, tracer.LabelBool("error", true)) + labels = append(labels, "error", true) } sp.SetLabels(labels...) } DefaultClientStreamObserver = func(ctx context.Context, req client.Request, opts []client.CallOption, stream client.Stream, sp tracer.Span, err error) { sp.SetName(fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())) - var labels []tracer.Label + var labels []interface{} if md, ok := metadata.FromOutgoingContext(ctx); ok { - labels = make([]tracer.Label, 0, len(md)) + labels = make([]interface{}, 0, len(md)) for k, v := range md { - labels = append(labels, tracer.LabelString(k, v)) + labels = append(labels, k, v) } } if err != nil { - labels = append(labels, tracer.LabelBool("error", true)) + labels = append(labels, "error", true) } sp.SetLabels(labels...) } DefaultClientPublishObserver = func(ctx context.Context, msg client.Message, opts []client.PublishOption, sp tracer.Span, err error) { sp.SetName(fmt.Sprintf("Pub to %s", msg.Topic())) - var labels []tracer.Label + var labels []interface{} if md, ok := metadata.FromOutgoingContext(ctx); ok { - labels = make([]tracer.Label, 0, len(md)) + labels = make([]interface{}, 0, len(md)) for k, v := range md { - labels = append(labels, tracer.LabelString(k, v)) + labels = append(labels, k, v) } } if err != nil { - labels = append(labels, tracer.LabelBool("error", true)) + labels = append(labels, "error", true) } sp.SetLabels(labels...) } DefaultServerHandlerObserver = func(ctx context.Context, req server.Request, rsp interface{}, sp tracer.Span, err error) { sp.SetName(fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())) - var labels []tracer.Label + var labels []interface{} if md, ok := metadata.FromIncomingContext(ctx); ok { - labels = make([]tracer.Label, 0, len(md)) + labels = make([]interface{}, 0, len(md)) for k, v := range md { - labels = append(labels, tracer.LabelString(k, v)) + labels = append(labels, k, v) } } if err != nil { - labels = append(labels, tracer.LabelBool("error", true)) + labels = append(labels, "error", true) } sp.SetLabels(labels...) } DefaultServerSubscriberObserver = func(ctx context.Context, msg server.Message, sp tracer.Span, err error) { sp.SetName(fmt.Sprintf("Sub from %s", msg.Topic())) - var labels []tracer.Label + var labels []interface{} if md, ok := metadata.FromIncomingContext(ctx); ok { - labels = make([]tracer.Label, 0, len(md)) + labels = make([]interface{}, 0, len(md)) for k, v := range md { - labels = append(labels, tracer.LabelString(k, v)) + labels = append(labels, k, v) } } if err != nil { - labels = append(labels, tracer.LabelBool("error", true)) + labels = append(labels, "error", true) } sp.SetLabels(labels...) } DefaultClientCallFuncObserver = func(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions, sp tracer.Span, err error) { sp.SetName(fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())) - var labels []tracer.Label + var labels []interface{} if md, ok := metadata.FromOutgoingContext(ctx); ok { - labels = make([]tracer.Label, 0, len(md)) + labels = make([]interface{}, 0, len(md)) for k, v := range md { - labels = append(labels, tracer.LabelString(k, v)) + labels = append(labels, k, v) } } if err != nil { - labels = append(labels, tracer.LabelBool("error", true)) + labels = append(labels, "error", true) } sp.SetLabels(labels...) } - DefaultSkipEndpoints = []string{"Meter.Metrics"} + DefaultSkipEndpoints = []string{"Meter.Metrics", "Health.Live", "Health.Ready", "Health.Version"} ) type tWrapper struct { -- 2.45.2