2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-09-23 20:51:32 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-11-04 19:10:58 +02:00
|
|
|
package oteltest // import "go.opentelemetry.io/otel/oteltest"
|
2019-09-23 20:51:32 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-10-23 08:01:33 +02:00
|
|
|
"crypto/rand"
|
2019-10-28 19:05:06 +02:00
|
|
|
"encoding/binary"
|
2019-09-23 20:51:32 +02:00
|
|
|
"sync/atomic"
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
"go.opentelemetry.io/otel"
|
2020-09-01 17:16:05 +02:00
|
|
|
otelparent "go.opentelemetry.io/otel/internal/trace/parent"
|
2019-09-23 20:51:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2019-10-16 19:24:38 +02:00
|
|
|
// StartSpanID is used to initialize spanId. It is incremented by one
|
2019-09-23 20:51:32 +02:00
|
|
|
// every time a new span is created.
|
2020-01-06 20:08:40 +02:00
|
|
|
//
|
|
|
|
// StartSpanID has to be aligned for 64-bit atomic operations.
|
2019-10-16 19:24:38 +02:00
|
|
|
StartSpanID *uint64
|
2020-01-06 20:08:40 +02:00
|
|
|
|
|
|
|
// Sampled specifies if the new span should be sampled or not.
|
|
|
|
Sampled bool
|
2020-04-06 17:37:25 +02:00
|
|
|
|
|
|
|
// OnSpanStarted is called every time a new trace span is started
|
|
|
|
OnSpanStarted func(span *MockSpan)
|
2019-09-23 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
var _ otel.Tracer = (*MockTracer)(nil)
|
2019-09-23 20:51:32 +02:00
|
|
|
|
2020-02-04 18:55:03 +02:00
|
|
|
// Start starts a MockSpan. It creates a new Span based on Parent SpanContext option.
|
2020-09-01 17:16:05 +02:00
|
|
|
// TraceID is used from Parent Span Context and SpanID is assigned.
|
2020-02-04 18:55:03 +02:00
|
|
|
// If Parent SpanContext option is not specified then random TraceID is used.
|
2019-09-23 20:51:32 +02:00
|
|
|
// No other options are supported.
|
2020-10-09 04:58:56 +02:00
|
|
|
func (mt *MockTracer) Start(ctx context.Context, name string, o ...otel.SpanOption) (context.Context, otel.Span) {
|
|
|
|
config := otel.NewSpanConfig(o...)
|
2020-09-10 21:15:17 +02:00
|
|
|
|
2019-09-23 20:51:32 +02:00
|
|
|
var span *MockSpan
|
2020-10-09 04:58:56 +02:00
|
|
|
var sc otel.SpanContext
|
2020-02-04 18:55:03 +02:00
|
|
|
|
2020-09-03 16:34:36 +02:00
|
|
|
parentSpanContext, _, _ := otelparent.GetSpanContextAndLinks(ctx, config.NewRoot)
|
2020-02-04 18:55:03 +02:00
|
|
|
|
|
|
|
if !parentSpanContext.IsValid() {
|
2020-10-09 04:58:56 +02:00
|
|
|
sc = otel.SpanContext{}
|
2019-10-23 08:01:33 +02:00
|
|
|
_, _ = rand.Read(sc.TraceID[:])
|
2019-09-23 20:51:32 +02:00
|
|
|
if mt.Sampled {
|
2020-10-09 04:58:56 +02:00
|
|
|
sc.TraceFlags = otel.FlagsSampled
|
2019-09-23 20:51:32 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-02-04 18:55:03 +02:00
|
|
|
sc = parentSpanContext
|
2019-09-23 20:51:32 +02:00
|
|
|
}
|
2019-10-28 19:05:06 +02:00
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(sc.SpanID[:], atomic.AddUint64(mt.StartSpanID, 1))
|
2019-09-23 20:51:32 +02:00
|
|
|
span = &MockSpan{
|
|
|
|
sc: sc,
|
|
|
|
tracer: mt,
|
2020-04-06 17:37:25 +02:00
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
if mt.OnSpanStarted != nil {
|
|
|
|
mt.OnSpanStarted(span)
|
2019-09-23 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
return otel.ContextWithSpan(ctx, span), span
|
2019-09-23 20:51:32 +02:00
|
|
|
}
|