2021-03-04 01:12:16 +03:00
|
|
|
package tracer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-29 00:40:58 +03:00
|
|
|
|
|
|
|
"go.unistack.org/micro/v4/options"
|
2023-10-16 23:43:39 +03:00
|
|
|
"go.unistack.org/micro/v4/util/id"
|
2021-03-04 01:12:16 +03:00
|
|
|
)
|
|
|
|
|
2023-07-04 00:23:50 +03:00
|
|
|
var _ Tracer = (*noopTracer)(nil)
|
|
|
|
|
2021-03-04 01:12:16 +03:00
|
|
|
type noopTracer struct {
|
2023-09-08 13:58:11 +03:00
|
|
|
opts Options
|
|
|
|
spans []Span
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *noopTracer) Spans() []Span {
|
|
|
|
return t.spans
|
2021-03-04 01:12:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
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...)
|
2021-03-04 01:12:16 +03:00
|
|
|
span := &noopSpan{
|
|
|
|
name: name,
|
|
|
|
ctx: ctx,
|
|
|
|
tracer: t,
|
2023-09-08 13:58:11 +03:00
|
|
|
labels: options.Labels,
|
|
|
|
kind: options.Kind,
|
2021-03-04 01:12:16 +03:00
|
|
|
}
|
2023-10-17 01:00:00 +03:00
|
|
|
span.spanID.s, _ = id.New()
|
|
|
|
span.traceID.s, _ = id.New()
|
2021-03-04 01:12:16 +03:00
|
|
|
if span.ctx == nil {
|
|
|
|
span.ctx = context.Background()
|
|
|
|
}
|
2023-09-08 13:58:11 +03:00
|
|
|
t.spans = append(t.spans, span)
|
2021-03-04 01:12:16 +03:00
|
|
|
return NewSpanContext(ctx, span), span
|
|
|
|
}
|
|
|
|
|
2023-07-04 00:23:50 +03:00
|
|
|
func (t *noopTracer) Flush(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (t *noopTracer) Init(opts ...options.Option) error {
|
2021-03-04 01:12:16 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&t.opts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *noopTracer) Name() string {
|
|
|
|
return t.opts.Name
|
|
|
|
}
|
|
|
|
|
2023-09-08 13:58:11 +03:00
|
|
|
type noopEvent struct {
|
|
|
|
name string
|
|
|
|
labels []interface{}
|
|
|
|
}
|
|
|
|
|
2023-10-17 01:00:00 +03:00
|
|
|
type noopStringer struct {
|
|
|
|
s string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s noopStringer) String() string {
|
|
|
|
return s.s
|
|
|
|
}
|
|
|
|
|
2021-03-04 01:12:16 +03:00
|
|
|
type noopSpan struct {
|
2023-01-18 09:48:58 +03:00
|
|
|
ctx context.Context
|
|
|
|
tracer Tracer
|
|
|
|
name string
|
|
|
|
statusMsg string
|
2023-10-17 01:00:00 +03:00
|
|
|
traceID noopStringer
|
|
|
|
spanID noopStringer
|
2023-09-08 13:58:11 +03:00
|
|
|
events []*noopEvent
|
|
|
|
labels []interface{}
|
|
|
|
logs []interface{}
|
|
|
|
kind SpanKind
|
|
|
|
status SpanStatus
|
2021-03-04 01:12:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (s *noopSpan) Finish(opts ...options.Option) {
|
2021-03-04 01:12:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *noopSpan) Context() context.Context {
|
|
|
|
return s.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *noopSpan) Tracer() Tracer {
|
|
|
|
return s.tracer
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (s *noopSpan) AddEvent(name string, opts ...options.Option) {
|
2023-09-08 13:58:11 +03:00
|
|
|
options := NewEventOptions(opts...)
|
|
|
|
s.events = append(s.events, &noopEvent{name: name, labels: options.Labels})
|
2021-03-04 01:12:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *noopSpan) SetName(name string) {
|
|
|
|
s.name = name
|
|
|
|
}
|
|
|
|
|
2023-09-08 13:58:11 +03:00
|
|
|
func (s *noopSpan) AddLogs(kv ...interface{}) {
|
|
|
|
s.logs = append(s.logs, kv...)
|
2021-03-04 01:12:16 +03:00
|
|
|
}
|
|
|
|
|
2023-09-08 13:58:11 +03:00
|
|
|
func (s *noopSpan) AddLabels(kv ...interface{}) {
|
|
|
|
s.labels = append(s.labels, kv...)
|
2022-12-24 19:20:22 +03:00
|
|
|
}
|
|
|
|
|
2023-01-18 00:21:18 +03:00
|
|
|
func (s *noopSpan) Kind() SpanKind {
|
2023-09-08 13:58:11 +03:00
|
|
|
return s.kind
|
2023-01-18 00:21:18 +03:00
|
|
|
}
|
|
|
|
|
2023-10-16 23:43:39 +03:00
|
|
|
func (s *noopSpan) TraceID() string {
|
2023-10-17 01:00:00 +03:00
|
|
|
return s.traceID.String()
|
2023-10-16 23:43:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *noopSpan) SpanID() string {
|
2023-10-17 01:00:00 +03:00
|
|
|
return s.spanID.String()
|
2023-10-16 23:43:39 +03:00
|
|
|
}
|
|
|
|
|
2023-01-18 09:48:58 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-03-04 01:12:16 +03:00
|
|
|
// NewTracer returns new memory tracer
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewTracer(opts ...options.Option) Tracer {
|
2021-03-04 01:12:16 +03:00
|
|
|
return &noopTracer{
|
|
|
|
opts: NewOptions(opts...),
|
|
|
|
}
|
|
|
|
}
|