diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e6c2022274..587619124ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - The `ErrInvalidHexID`, `ErrInvalidTraceIDLength`, `ErrInvalidSpanIDLength`, `ErrInvalidSpanIDLength`, or `ErrNilSpanID` from the `go.opentelemetry.io/otel` package are unexported now. (#1243) - The `AddEventWithTimestamp` method on the `Span` interface in `go.opentelemetry.io/otel` is removed due to its redundancy. It is replaced by using the `AddEvent` method with a `WithTimestamp` option. (#1254) +- Structs `MockSpan` and `MockTracer` are removed from `go.opentelemetry.io/otel/oteltest`. `Tracer` and `Span` from the same module should be used in their place instead. (#1306) ### Fixed diff --git a/oteltest/alignment_test.go b/oteltest/alignment_test.go index 6416b50f027..31877be1eb4 100644 --- a/oteltest/alignment_test.go +++ b/oteltest/alignment_test.go @@ -33,10 +33,6 @@ func TestMain(m *testing.M) { Name: "Measurement.Number", Offset: unsafe.Offsetof(Measurement{}.Number), }, - { - Name: "MockTracer.StartSpanID", - Offset: unsafe.Offsetof(MockTracer{}.StartSpanID), - }, } if !internaltest.Aligned8Byte(fields, os.Stderr) { os.Exit(1) diff --git a/oteltest/mock_span.go b/oteltest/mock_span.go deleted file mode 100644 index 0eb7fd48f41..00000000000 --- a/oteltest/mock_span.go +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package oteltest // import "go.opentelemetry.io/otel/oteltest" - -import ( - "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/codes" - "go.opentelemetry.io/otel/label" -) - -// MockSpan is a mock span used in association with MockTracer for testing purpose only. -type MockSpan struct { - StatusMsg string - Name string - Status codes.Code - sc otel.SpanContext - tracer otel.Tracer -} - -var _ otel.Span = (*MockSpan)(nil) - -// SpanContext returns associated label.SpanContext. If the receiver is nil it returns -// an empty label.SpanContext -func (ms *MockSpan) SpanContext() otel.SpanContext { - if ms == nil { - return otel.SpanContext{} - } - return ms.sc -} - -// IsRecording always returns false for MockSpan. -func (ms *MockSpan) IsRecording() bool { return false } - -// SetStatus does nothing. -func (ms *MockSpan) SetStatus(status codes.Code, msg string) { - ms.Status = status - ms.StatusMsg = msg -} - -// SetError does nothing. -func (ms *MockSpan) SetError(v bool) {} - -// SetAttributes does nothing. -func (ms *MockSpan) SetAttributes(attributes ...label.KeyValue) {} - -// End does nothing. -func (ms *MockSpan) End(options ...otel.SpanOption) {} - -// RecordError does nothing. -func (ms *MockSpan) RecordError(err error, opts ...otel.EventOption) {} - -// SetName sets the span name. -func (ms *MockSpan) SetName(name string) { ms.Name = name } - -// Tracer returns MockTracer implementation of Tracer. -func (ms *MockSpan) Tracer() otel.Tracer { return ms.tracer } - -// AddEvent does nothing. -func (ms *MockSpan) AddEvent(string, ...otel.EventOption) {} diff --git a/oteltest/mock_tracer.go b/oteltest/mock_tracer.go deleted file mode 100644 index 6414d39bb45..00000000000 --- a/oteltest/mock_tracer.go +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package oteltest // import "go.opentelemetry.io/otel/oteltest" - -import ( - "context" - "crypto/rand" - "encoding/binary" - "sync/atomic" - - "go.opentelemetry.io/otel" - otelparent "go.opentelemetry.io/otel/internal/trace/parent" -) - -// MockTracer is a simple tracer used for testing purpose only. -// It only supports ChildOf option. SpanId is atomically increased every time a -// new span is created. -type MockTracer struct { - // StartSpanID is used to initialize spanId. It is incremented by one - // every time a new span is created. - // - // StartSpanID has to be aligned for 64-bit atomic operations. - StartSpanID *uint64 - - // Sampled specifies if the new span should be sampled or not. - Sampled bool - - // OnSpanStarted is called every time a new trace span is started - OnSpanStarted func(span *MockSpan) -} - -var _ otel.Tracer = (*MockTracer)(nil) - -// Start starts a MockSpan. It creates a new Span based on Parent SpanContext option. -// TraceID is used from Parent Span Context and SpanID is assigned. -// If Parent SpanContext option is not specified then random TraceID is used. -// No other options are supported. -func (mt *MockTracer) Start(ctx context.Context, name string, o ...otel.SpanOption) (context.Context, otel.Span) { - config := otel.NewSpanConfig(o...) - - var span *MockSpan - var sc otel.SpanContext - - parentSpanContext, _, _ := otelparent.GetSpanContextAndLinks(ctx, config.NewRoot) - - if !parentSpanContext.IsValid() { - sc = otel.SpanContext{} - _, _ = rand.Read(sc.TraceID[:]) - if mt.Sampled { - sc.TraceFlags = otel.FlagsSampled - } - } else { - sc = parentSpanContext - } - - binary.BigEndian.PutUint64(sc.SpanID[:], atomic.AddUint64(mt.StartSpanID, 1)) - span = &MockSpan{ - sc: sc, - tracer: mt, - Name: name, - } - if mt.OnSpanStarted != nil { - mt.OnSpanStarted(span) - } - - return otel.ContextWithSpan(ctx, span), span -} diff --git a/oteltest/provider.go b/oteltest/provider.go index e870b8dce8d..e35ba9c959e 100644 --- a/oteltest/provider.go +++ b/oteltest/provider.go @@ -66,3 +66,8 @@ func (p *TracerProvider) Tracer(instName string, opts ...otel.TracerOption) otel } return t } + +// DefaulTracer returns a default tracer for testing purposes. +func DefaultTracer() otel.Tracer { + return NewTracerProvider().Tracer("") +} diff --git a/propagators/trace_context_benchmark_test.go b/propagators/trace_context_benchmark_test.go index a3c7f1170bd..3f5ff715567 100644 --- a/propagators/trace_context_benchmark_test.go +++ b/propagators/trace_context_benchmark_test.go @@ -38,14 +38,10 @@ func BenchmarkInject(b *testing.B) { func injectSubBenchmarks(b *testing.B, fn func(context.Context, *testing.B)) { b.Run("SampledSpanContext", func(b *testing.B) { - var id uint64 spanID, _ := otel.SpanIDFromHex("00f067aa0ba902b7") traceID, _ := otel.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736") - mockTracer := &oteltest.MockTracer{ - Sampled: false, - StartSpanID: &id, - } + mockTracer := oteltest.DefaultTracer() b.ReportAllocs() sc := otel.SpanContext{ TraceID: traceID, diff --git a/propagators/trace_context_test.go b/propagators/trace_context_test.go index 667d19f7684..d63c8b9f2a2 100644 --- a/propagators/trace_context_test.go +++ b/propagators/trace_context_test.go @@ -209,11 +209,7 @@ func TestExtractInvalidTraceContextFromHTTPReq(t *testing.T) { } func TestInjectTraceContextToHTTPReq(t *testing.T) { - var id uint64 - mockTracer := &oteltest.MockTracer{ - Sampled: false, - StartSpanID: &id, - } + mockTracer := oteltest.DefaultTracer() prop := propagators.TraceContext{} tests := []struct { name string @@ -227,7 +223,7 @@ func TestInjectTraceContextToHTTPReq(t *testing.T) { SpanID: spanID, TraceFlags: otel.FlagsSampled, }, - wantHeader: "00-4bf92f3577b34da6a3ce929d0e0e4736-0000000000000001-01", + wantHeader: "00-4bf92f3577b34da6a3ce929d0e0e4736-0000000000000002-01", }, { name: "valid spancontext, not sampled", @@ -235,7 +231,7 @@ func TestInjectTraceContextToHTTPReq(t *testing.T) { TraceID: traceID, SpanID: spanID, }, - wantHeader: "00-4bf92f3577b34da6a3ce929d0e0e4736-0000000000000002-00", + wantHeader: "00-4bf92f3577b34da6a3ce929d0e0e4736-0000000000000003-00", }, { name: "valid spancontext, with unsupported bit set in traceflags", @@ -244,7 +240,7 @@ func TestInjectTraceContextToHTTPReq(t *testing.T) { SpanID: spanID, TraceFlags: 0xff, }, - wantHeader: "00-4bf92f3577b34da6a3ce929d0e0e4736-0000000000000003-01", + wantHeader: "00-4bf92f3577b34da6a3ce929d0e0e4736-0000000000000004-01", }, { name: "invalid spancontext",