Skip to content

Commit

Permalink
Merge pull request #171 from unistack-org/fixspan
Browse files Browse the repository at this point in the history
add span status method
  • Loading branch information
vtolstov committed Jan 18, 2023
2 parents f76b317 + 7137d99 commit 4debc39
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
19 changes: 15 additions & 4 deletions tracer/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down
27 changes: 27 additions & 0 deletions tracer/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 4 additions & 0 deletions tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions tracer/wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand All @@ -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...)
Expand All @@ -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...)
Expand All @@ -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...)
Expand All @@ -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...)
Expand All @@ -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...)
Expand Down

0 comments on commit 4debc39

Please sign in to comment.