traefik/pkg/middlewares/observability/mock_tracing_test.go

67 lines
1.8 KiB
Go
Raw Normal View History

2024-03-12 08:48:04 +00:00
package observability
import (
2024-01-08 08:10:06 +00:00
"context"
2024-01-08 08:10:06 +00:00
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/embedded"
)
2024-01-08 08:10:06 +00:00
type mockTracerProvider struct {
embedded.TracerProvider
}
2024-01-08 08:10:06 +00:00
var _ trace.TracerProvider = mockTracerProvider{}
2018-11-14 09:18:03 +00:00
2024-01-08 08:10:06 +00:00
func (p mockTracerProvider) Tracer(string, ...trace.TracerOption) trace.Tracer {
return &mockTracer{}
}
2024-01-08 08:10:06 +00:00
type mockTracer struct {
embedded.Tracer
2024-01-08 08:10:06 +00:00
spans []*mockSpan
}
2018-11-14 09:18:03 +00:00
2024-01-08 08:10:06 +00:00
var _ trace.Tracer = &mockTracer{}
2024-01-08 08:10:06 +00:00
func (t *mockTracer) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
config := trace.NewSpanStartConfig(opts...)
span := &mockSpan{}
span.SetName(name)
span.SetAttributes(attribute.String("span.kind", config.SpanKind().String()))
span.SetAttributes(config.Attributes()...)
t.spans = append(t.spans, span)
return trace.ContextWithSpan(ctx, span), span
2018-11-14 09:18:03 +00:00
}
2024-01-08 08:10:06 +00:00
// mockSpan is an implementation of Span that preforms no operations.
type mockSpan struct {
embedded.Span
2024-01-08 08:10:06 +00:00
name string
attributes []attribute.KeyValue
}
2024-01-08 08:10:06 +00:00
var _ trace.Span = &mockSpan{}
2024-03-12 08:48:04 +00:00
func (*mockSpan) SpanContext() trace.SpanContext {
return trace.NewSpanContext(trace.SpanContextConfig{TraceID: trace.TraceID{1}, SpanID: trace.SpanID{1}})
}
2024-01-08 08:10:06 +00:00
func (*mockSpan) IsRecording() bool { return false }
func (s *mockSpan) SetStatus(_ codes.Code, _ string) {}
func (s *mockSpan) SetAttributes(kv ...attribute.KeyValue) {
s.attributes = append(s.attributes, kv...)
}
2024-01-08 08:10:06 +00:00
func (s *mockSpan) End(...trace.SpanEndOption) {}
func (s *mockSpan) RecordError(_ error, _ ...trace.EventOption) {}
func (s *mockSpan) AddEvent(_ string, _ ...trace.EventOption) {}
func (s *mockSpan) SetName(name string) { s.name = name }
2024-03-12 08:48:04 +00:00
func (s *mockSpan) TracerProvider() trace.TracerProvider {
return nil
}