2020-07-28 19:47:08 +02:00
|
|
|
// 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.
|
|
|
|
|
2020-11-04 19:10:58 +02:00
|
|
|
package oteltest // import "go.opentelemetry.io/otel/oteltest"
|
2020-07-28 19:47:08 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/binary"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-07-28 19:47:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// defaultSpanContextFunc returns the default SpanContextFunc.
|
2020-11-07 00:13:31 +02:00
|
|
|
func defaultSpanContextFunc() func(context.Context) trace.SpanContext {
|
2020-07-28 19:47:08 +02:00
|
|
|
var traceID, spanID uint64 = 1, 1
|
2020-11-07 00:13:31 +02:00
|
|
|
return func(ctx context.Context) trace.SpanContext {
|
|
|
|
var sc trace.SpanContext
|
2021-03-29 17:55:44 +02:00
|
|
|
if current := trace.SpanContextFromContext(ctx); current.IsValid() {
|
|
|
|
sc = current
|
2020-07-28 19:47:08 +02:00
|
|
|
} else {
|
2021-03-09 18:17:29 +02:00
|
|
|
var tid trace.TraceID
|
|
|
|
binary.BigEndian.PutUint64(tid[:], atomic.AddUint64(&traceID, 1))
|
|
|
|
sc = sc.WithTraceID(tid)
|
2020-07-28 19:47:08 +02:00
|
|
|
}
|
2021-03-09 18:17:29 +02:00
|
|
|
var sid trace.SpanID
|
|
|
|
binary.BigEndian.PutUint64(sid[:], atomic.AddUint64(&spanID, 1))
|
|
|
|
return sc.WithSpanID(sid)
|
2020-07-28 19:47:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
// SpanContextFunc returns a SpanContext from an parent Context for a
|
|
|
|
// new span.
|
2020-11-07 00:13:31 +02:00
|
|
|
SpanContextFunc func(context.Context) trace.SpanContext
|
2020-07-28 19:47:08 +02:00
|
|
|
|
|
|
|
// SpanRecorder keeps track of spans.
|
2021-02-17 17:31:59 +02:00
|
|
|
SpanRecorder *SpanRecorder
|
2020-07-28 19:47:08 +02:00
|
|
|
}
|
|
|
|
|
2020-09-11 23:14:25 +02:00
|
|
|
func newConfig(opts ...Option) config {
|
2020-07-28 19:47:08 +02:00
|
|
|
conf := config{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.Apply(&conf)
|
|
|
|
}
|
|
|
|
if conf.SpanContextFunc == nil {
|
|
|
|
conf.SpanContextFunc = defaultSpanContextFunc()
|
|
|
|
}
|
|
|
|
return conf
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:27:53 +02:00
|
|
|
// Option applies an option to a config.
|
2020-07-28 19:47:08 +02:00
|
|
|
type Option interface {
|
|
|
|
Apply(*config)
|
2021-02-24 20:03:35 +02:00
|
|
|
|
|
|
|
// A private method to prevent users implementing the
|
|
|
|
// interface and so future additions to it will not
|
|
|
|
// violate compatibility.
|
|
|
|
private()
|
2020-07-28 19:47:08 +02:00
|
|
|
}
|
|
|
|
|
2021-02-24 20:03:35 +02:00
|
|
|
type option struct{}
|
|
|
|
|
|
|
|
func (option) private() {}
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
type spanContextFuncOption struct {
|
2021-02-24 20:03:35 +02:00
|
|
|
option
|
2020-11-07 00:13:31 +02:00
|
|
|
SpanContextFunc func(context.Context) trace.SpanContext
|
2020-07-28 19:47:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o spanContextFuncOption) Apply(c *config) {
|
|
|
|
c.SpanContextFunc = o.SpanContextFunc
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:27:53 +02:00
|
|
|
// WithSpanContextFunc sets the SpanContextFunc used to generate a new Spans
|
|
|
|
// context from a parent SpanContext.
|
2020-11-07 00:13:31 +02:00
|
|
|
func WithSpanContextFunc(f func(context.Context) trace.SpanContext) Option {
|
2021-02-24 20:03:35 +02:00
|
|
|
return spanContextFuncOption{SpanContextFunc: f}
|
2020-07-28 19:47:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type spanRecorderOption struct {
|
2021-02-24 20:03:35 +02:00
|
|
|
option
|
2021-02-17 17:31:59 +02:00
|
|
|
SpanRecorder *SpanRecorder
|
2020-07-28 19:47:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o spanRecorderOption) Apply(c *config) {
|
|
|
|
c.SpanRecorder = o.SpanRecorder
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:27:53 +02:00
|
|
|
// WithSpanRecorder sets the SpanRecorder to use with the TracerProvider for
|
|
|
|
// testing.
|
2021-02-17 17:31:59 +02:00
|
|
|
func WithSpanRecorder(sr *SpanRecorder) Option {
|
2021-02-24 20:03:35 +02:00
|
|
|
return spanRecorderOption{SpanRecorder: sr}
|
2020-07-28 19:47:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 21:27:53 +02:00
|
|
|
// SpanRecorder performs operations to record a span as it starts and ends.
|
2021-02-17 17:31:59 +02:00
|
|
|
// It is designed to be concurrent safe and can by used by multiple goroutines.
|
|
|
|
type SpanRecorder struct {
|
2020-07-28 19:47:08 +02:00
|
|
|
startedMu sync.RWMutex
|
|
|
|
started []*Span
|
|
|
|
|
|
|
|
doneMu sync.RWMutex
|
|
|
|
done []*Span
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:27:53 +02:00
|
|
|
// OnStart records span as started.
|
2021-02-17 17:31:59 +02:00
|
|
|
func (ssr *SpanRecorder) OnStart(span *Span) {
|
2020-07-28 19:47:08 +02:00
|
|
|
ssr.startedMu.Lock()
|
|
|
|
defer ssr.startedMu.Unlock()
|
|
|
|
ssr.started = append(ssr.started, span)
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:27:53 +02:00
|
|
|
// OnEnd records span as completed.
|
2021-02-17 17:31:59 +02:00
|
|
|
func (ssr *SpanRecorder) OnEnd(span *Span) {
|
2020-07-28 19:47:08 +02:00
|
|
|
ssr.doneMu.Lock()
|
|
|
|
defer ssr.doneMu.Unlock()
|
|
|
|
ssr.done = append(ssr.done, span)
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:27:53 +02:00
|
|
|
// Started returns a copy of all started Spans in the order they were started.
|
2021-02-17 17:31:59 +02:00
|
|
|
func (ssr *SpanRecorder) Started() []*Span {
|
2020-07-28 19:47:08 +02:00
|
|
|
ssr.startedMu.RLock()
|
|
|
|
defer ssr.startedMu.RUnlock()
|
|
|
|
started := make([]*Span, len(ssr.started))
|
|
|
|
for i := range ssr.started {
|
|
|
|
started[i] = ssr.started[i]
|
|
|
|
}
|
|
|
|
return started
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:27:53 +02:00
|
|
|
// Completed returns a copy of all ended Spans in the order they were ended.
|
2021-02-17 17:31:59 +02:00
|
|
|
func (ssr *SpanRecorder) Completed() []*Span {
|
2020-07-28 19:47:08 +02:00
|
|
|
ssr.doneMu.RLock()
|
|
|
|
defer ssr.doneMu.RUnlock()
|
|
|
|
done := make([]*Span, len(ssr.done))
|
|
|
|
for i := range ssr.done {
|
|
|
|
done[i] = ssr.done[i]
|
|
|
|
}
|
|
|
|
return done
|
|
|
|
}
|