micro/tracer/noop.go

123 lines
2.1 KiB
Go
Raw Permalink Normal View History

package tracer
import (
"context"
"go.unistack.org/micro/v4/options"
"go.unistack.org/micro/v4/util/id"
)
var _ Tracer = (*noopTracer)(nil)
type noopTracer struct {
2023-09-08 13:58:11 +03:00
opts Options
spans []Span
}
func (t *noopTracer) Spans() []Span {
return t.spans
}
func (t *noopTracer) Start(ctx context.Context, name string, opts ...options.Option) (context.Context, Span) {
2023-09-08 13:58:11 +03:00
options := NewSpanOptions(opts...)
span := &noopSpan{
name: name,
ctx: ctx,
tracer: t,
2023-09-08 13:58:11 +03:00
kind: options.Kind,
}
span.spanID.s, _ = id.New()
span.traceID.s, _ = id.New()
if span.ctx == nil {
span.ctx = context.Background()
}
2023-09-08 13:58:11 +03:00
t.spans = append(t.spans, span)
return NewSpanContext(ctx, span), span
}
func (t *noopTracer) Flush(_ context.Context) error {
return nil
}
func (t *noopTracer) Init(opts ...options.Option) error {
for _, o := range opts {
o(&t.opts)
}
return nil
}
func (t *noopTracer) Name() string {
return t.opts.Name
}
type noopStringer struct {
s string
}
func (s noopStringer) String() string {
return s.s
}
type noopSpan struct {
ctx context.Context
tracer Tracer
name string
statusMsg string
traceID noopStringer
spanID noopStringer
2023-09-08 13:58:11 +03:00
kind SpanKind
status SpanStatus
}
func (s *noopSpan) Finish(_ ...options.Option) {
}
func (s *noopSpan) Context() context.Context {
return s.ctx
}
func (s *noopSpan) Tracer() Tracer {
return s.tracer
}
func (s *noopSpan) AddEvent(_ string, _ ...options.Option) {
}
func (s *noopSpan) SetName(name string) {
s.name = name
}
func (s *noopSpan) AddLogs(_ ...interface{}) {
}
func (s *noopSpan) AddLabels(_ ...interface{}) {
}
func (s *noopSpan) Kind() SpanKind {
2023-09-08 13:58:11 +03:00
return s.kind
}
func (s *noopSpan) TraceID() string {
return s.traceID.String()
}
func (s *noopSpan) SpanID() string {
return s.spanID.String()
}
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 ...options.Option) Tracer {
return &noopTracer{
opts: NewOptions(opts...),
}
}