2019-09-23 20:51:32 +02:00
|
|
|
// Copyright 2019, 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 trace
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
|
|
apitrace "go.opentelemetry.io/otel/api/trace"
|
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
|
2019-09-23 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ apitrace.Tracer = (*MockTracer)(nil)
|
|
|
|
|
|
|
|
// WithSpan does nothing except executing the body.
|
|
|
|
func (mt *MockTracer) WithSpan(ctx context.Context, name string, body func(context.Context) error) error {
|
|
|
|
return body(ctx)
|
|
|
|
}
|
|
|
|
|
2019-10-21 19:15:49 +02:00
|
|
|
// Start starts a MockSpan. It creates a new Span based on Relation SpanContext option.
|
|
|
|
// TracdID is used from Relation Span Context and SpanID is assigned.
|
|
|
|
// If Relation SpanContext option is not specified then random TraceID is used.
|
2019-09-23 20:51:32 +02:00
|
|
|
// No other options are supported.
|
2019-12-04 23:41:13 +02:00
|
|
|
func (mt *MockTracer) Start(ctx context.Context, name string, o ...apitrace.StartOption) (context.Context, apitrace.Span) {
|
|
|
|
var opts apitrace.StartConfig
|
2019-09-23 20:51:32 +02:00
|
|
|
for _, op := range o {
|
|
|
|
op(&opts)
|
|
|
|
}
|
|
|
|
var span *MockSpan
|
|
|
|
var sc core.SpanContext
|
2019-10-21 19:15:49 +02:00
|
|
|
if !opts.Relation.SpanContext.IsValid() {
|
2019-10-23 08:01:33 +02:00
|
|
|
sc = core.SpanContext{}
|
|
|
|
_, _ = rand.Read(sc.TraceID[:])
|
2019-09-23 20:51:32 +02:00
|
|
|
if mt.Sampled {
|
2019-09-25 23:37:36 +02:00
|
|
|
sc.TraceFlags = core.TraceFlagsSampled
|
2019-09-23 20:51:32 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-21 19:15:49 +02:00
|
|
|
sc = opts.Relation.SpanContext
|
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,
|
|
|
|
}
|
|
|
|
|
2019-12-11 18:51:32 +02:00
|
|
|
return apitrace.ContextWithSpan(ctx, span), span
|
2019-09-23 20:51:32 +02:00
|
|
|
}
|