From 7137d99102d201e3f8ab0b28fd06bf31fe55e9dd Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 18 Jan 2023 09:48:58 +0300 Subject: [PATCH] add span status method Signed-off-by: Vasiliy Tolstov --- tracer/noop.go | 19 +++++++++++++++---- tracer/options.go | 27 +++++++++++++++++++++++++++ tracer/tracer.go | 4 ++++ tracer/wrapper/wrapper.go | 18 ++++++++++++------ 4 files changed, 58 insertions(+), 10 deletions(-) diff --git a/tracer/noop.go b/tracer/noop.go index 33d3e4fc..64fe2248 100644 --- a/tracer/noop.go +++ b/tracer/noop.go @@ -33,10 +33,12 @@ func (t *noopTracer) Name() string { } type noopSpan struct { - ctx context.Context - tracer Tracer - name string - opts SpanOptions + ctx context.Context + tracer Tracer + name string + opts SpanOptions + status SpanStatus + statusMsg string } func (s *noopSpan) Finish(opts ...SpanOption) { @@ -69,6 +71,15 @@ func (s *noopSpan) Kind() SpanKind { return s.opts.Kind } +func (s *noopSpan) Status() (SpanStatus, string) { + return s.status, s.statusMsg +} + +func (s *noopSpan) SetStatus(st SpanStatus, msg string) { + s.status = st + s.statusMsg = msg +} + // NewTracer returns new memory tracer func NewTracer(opts ...Option) Tracer { return &noopTracer{ diff --git a/tracer/options.go b/tracer/options.go index 42a47c94..cf8c88f6 100644 --- a/tracer/options.go +++ b/tracer/options.go @@ -6,6 +6,33 @@ import ( "go.unistack.org/micro/v3/logger" ) +type SpanStatus int + +const ( + // SpanStatusUnset is the default status code. + SpanStatusUnset SpanStatus = 0 + + // SpanStatusError indicates the operation contains an error. + SpanStatusError SpanStatus = 1 + + // SpanStatusOK indicates operation has been validated by an Application developers + // or Operator to have completed successfully, or contain no error. + SpanStatusOK SpanStatus = 2 +) + +func (s SpanStatus) String() string { + switch s { + case SpanStatusUnset: + return "Unset" + case SpanStatusError: + return "Error" + case SpanStatusOK: + return "OK" + default: + return "Unset" + } +} + type SpanKind int const ( diff --git a/tracer/tracer.go b/tracer/tracer.go index 39b96a15..daecd53e 100644 --- a/tracer/tracer.go +++ b/tracer/tracer.go @@ -29,6 +29,10 @@ type Span interface { Context() context.Context // SetName set the span name SetName(name string) + // SetStatus set the span status code and msg + SetStatus(status SpanStatus, msg string) + // Status returns span status and msg + Status() (SpanStatus, string) // SetLabels set the span labels SetLabels(labels ...interface{}) // AddLabels append the span labels diff --git a/tracer/wrapper/wrapper.go b/tracer/wrapper/wrapper.go index 0b6b7145..fba21743 100644 --- a/tracer/wrapper/wrapper.go +++ b/tracer/wrapper/wrapper.go @@ -22,7 +22,8 @@ var ( } } if err != nil { - labels = append(labels, "error", true) + labels = append(labels, "error", err.Error()) + sp.SetStatus(tracer.SpanStatusError, err.Error()) } labels = append(labels, "kind", sp.Kind()) sp.SetLabels(labels...) @@ -38,7 +39,8 @@ var ( } } if err != nil { - labels = append(labels, "error", true) + labels = append(labels, "error", err.Error()) + sp.SetStatus(tracer.SpanStatusError, err.Error()) } labels = append(labels, "kind", sp.Kind()) sp.SetLabels(labels...) @@ -54,7 +56,8 @@ var ( } } if err != nil { - labels = append(labels, "error", true) + labels = append(labels, "error", err.Error()) + sp.SetStatus(tracer.SpanStatusError, err.Error()) } labels = append(labels, "kind", sp.Kind()) sp.SetLabels(labels...) @@ -70,7 +73,8 @@ var ( } } if err != nil { - labels = append(labels, "error", true) + labels = append(labels, "error", err.Error()) + sp.SetStatus(tracer.SpanStatusError, err.Error()) } labels = append(labels, "kind", sp.Kind()) sp.SetLabels(labels...) @@ -86,7 +90,8 @@ var ( } } if err != nil { - labels = append(labels, "error", true) + labels = append(labels, "error", err.Error()) + sp.SetStatus(tracer.SpanStatusError, err.Error()) } labels = append(labels, "kind", sp.Kind()) sp.SetLabels(labels...) @@ -102,7 +107,8 @@ var ( } } if err != nil { - labels = append(labels, "error", true) + labels = append(labels, "error", err.Error()) + sp.SetStatus(tracer.SpanStatusError, err.Error()) } labels = append(labels, "kind", sp.Kind()) sp.SetLabels(labels...)