traefik/pkg/middlewares/tracing/mock_tracing_test.go

71 lines
2.3 KiB
Go
Raw Normal View History

package tracing
import (
2018-11-14 09:18:03 +00:00
"io"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
)
type MockTracer struct {
Span *MockSpan
}
2018-11-14 09:18:03 +00:00
// StartSpan belongs to the Tracer interface.
func (n MockTracer) StartSpan(operationName string, opts ...opentracing.StartSpanOption) opentracing.Span {
n.Span.OpName = operationName
return n.Span
}
// Inject belongs to the Tracer interface.
2020-07-07 12:42:03 +00:00
func (n MockTracer) Inject(sp opentracing.SpanContext, format, carrier interface{}) error {
2018-11-14 09:18:03 +00:00
return nil
}
2018-11-14 09:18:03 +00:00
// Extract belongs to the Tracer interface.
2020-07-07 12:42:03 +00:00
func (n MockTracer) Extract(format, carrier interface{}) (opentracing.SpanContext, error) {
2018-11-14 09:18:03 +00:00
return nil, opentracing.ErrSpanContextNotFound
}
2021-03-04 08:02:03 +00:00
// MockSpanContext a span context mock.
2018-11-14 09:18:03 +00:00
type MockSpanContext struct{}
func (n MockSpanContext) ForeachBaggageItem(handler func(k, v string) bool) {}
2021-03-04 08:02:03 +00:00
// MockSpan a span mock.
2018-11-14 09:18:03 +00:00
type MockSpan struct {
OpName string
Tags map[string]interface{}
}
func (n MockSpan) Context() opentracing.SpanContext { return MockSpanContext{} }
func (n MockSpan) SetBaggageItem(key, val string) opentracing.Span {
return MockSpan{Tags: make(map[string]interface{})}
}
func (n MockSpan) BaggageItem(key string) string { return "" }
func (n MockSpan) SetTag(key string, value interface{}) opentracing.Span {
n.Tags[key] = value
return n
}
func (n MockSpan) LogFields(fields ...log.Field) {}
func (n MockSpan) LogKV(keyVals ...interface{}) {}
func (n MockSpan) Finish() {}
func (n MockSpan) FinishWithOptions(opts opentracing.FinishOptions) {}
func (n MockSpan) SetOperationName(operationName string) opentracing.Span { return n }
func (n MockSpan) Tracer() opentracing.Tracer { return MockTracer{} }
func (n MockSpan) LogEvent(event string) {}
func (n MockSpan) LogEventWithPayload(event string, payload interface{}) {}
func (n MockSpan) Log(data opentracing.LogData) {}
func (n MockSpan) Reset() {
n.Tags = make(map[string]interface{})
}
2018-11-14 09:18:03 +00:00
type trackingBackenMock struct {
tracer opentracing.Tracer
}
2018-11-14 09:18:03 +00:00
func (t *trackingBackenMock) Setup(componentName string) (opentracing.Tracer, io.Closer, error) {
opentracing.SetGlobalTracer(t.tracer)
return t.tracer, nil, nil
}