2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-12-05 00:00:35 +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-12-05 00:00:35 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2020-08-18 05:25:03 +02:00
|
|
|
"go.opentelemetry.io/otel/label"
|
2020-11-07 00:13:31 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2019-12-05 00:00:35 +02:00
|
|
|
)
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
var _ trace.Tracer = (*Tracer)(nil)
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
// Tracer is an OpenTelemetry Tracer implementation used for testing.
|
2019-12-05 00:00:35 +02:00
|
|
|
type Tracer struct {
|
2020-07-28 19:47:08 +02:00
|
|
|
// Name is the instrumentation name.
|
|
|
|
Name string
|
|
|
|
// Version is the instrumentation version.
|
|
|
|
Version string
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
config *config
|
2019-12-05 00:00:35 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 21:27:53 +02:00
|
|
|
// Start creates a span. If t is configured with a SpanRecorder its OnStart
|
|
|
|
// method will be called after the created Span has been initialized.
|
2020-11-07 00:13:31 +02:00
|
|
|
func (t *Tracer) Start(ctx context.Context, name string, opts ...trace.SpanOption) (context.Context, trace.Span) {
|
|
|
|
c := trace.NewSpanConfig(opts...)
|
2019-12-05 00:00:35 +02:00
|
|
|
startTime := time.Now()
|
2020-09-03 16:34:36 +02:00
|
|
|
if st := c.Timestamp; !st.IsZero() {
|
2019-12-05 00:00:35 +02:00
|
|
|
startTime = st
|
|
|
|
}
|
|
|
|
|
|
|
|
span := &Span{
|
2020-07-28 19:47:08 +02:00
|
|
|
tracer: t,
|
|
|
|
startTime: startTime,
|
2020-08-18 05:25:03 +02:00
|
|
|
attributes: make(map[label.Key]label.Value),
|
2020-12-21 23:11:48 +02:00
|
|
|
links: []trace.Link{},
|
2020-07-30 01:06:20 +02:00
|
|
|
spanKind: c.SpanKind,
|
2019-12-05 00:00:35 +02:00
|
|
|
}
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
if c.NewRoot {
|
2020-11-07 00:13:31 +02:00
|
|
|
span.spanContext = trace.SpanContext{}
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-08-18 05:25:03 +02:00
|
|
|
iodKey := label.Key("ignored-on-demand")
|
2020-11-07 00:13:31 +02:00
|
|
|
if lsc := trace.SpanContextFromContext(ctx); lsc.IsValid() {
|
2020-12-21 23:11:48 +02:00
|
|
|
span.links = append(span.links, trace.Link{
|
|
|
|
SpanContext: lsc,
|
|
|
|
Attributes: []label.KeyValue{iodKey.String("current")},
|
|
|
|
})
|
2020-07-28 19:47:08 +02:00
|
|
|
}
|
2020-11-07 00:13:31 +02:00
|
|
|
if rsc := trace.RemoteSpanContextFromContext(ctx); rsc.IsValid() {
|
2020-12-21 23:11:48 +02:00
|
|
|
span.links = append(span.links, trace.Link{
|
|
|
|
SpanContext: rsc,
|
|
|
|
Attributes: []label.KeyValue{iodKey.String("remote")},
|
|
|
|
})
|
2020-07-28 19:47:08 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
span.spanContext = t.config.SpanContextFunc(ctx)
|
2020-11-07 00:13:31 +02:00
|
|
|
if lsc := trace.SpanContextFromContext(ctx); lsc.IsValid() {
|
2020-07-28 19:47:08 +02:00
|
|
|
span.spanContext.TraceID = lsc.TraceID
|
|
|
|
span.parentSpanID = lsc.SpanID
|
2020-11-07 00:13:31 +02:00
|
|
|
} else if rsc := trace.RemoteSpanContextFromContext(ctx); rsc.IsValid() {
|
2020-07-28 19:47:08 +02:00
|
|
|
span.spanContext.TraceID = rsc.TraceID
|
|
|
|
span.parentSpanID = rsc.SpanID
|
|
|
|
}
|
2020-02-04 18:55:03 +02:00
|
|
|
}
|
2020-07-28 19:47:08 +02:00
|
|
|
|
2019-12-05 00:00:35 +02:00
|
|
|
for _, link := range c.Links {
|
2020-12-21 23:11:48 +02:00
|
|
|
for i, sl := range span.links {
|
|
|
|
if sl.SpanContext.SpanID == link.SpanContext.SpanID &&
|
|
|
|
sl.SpanContext.TraceID == link.SpanContext.TraceID &&
|
|
|
|
sl.SpanContext.TraceFlags == link.SpanContext.TraceFlags &&
|
|
|
|
sl.SpanContext.TraceState.String() == link.SpanContext.TraceState.String() {
|
|
|
|
span.links[i].Attributes = link.Attributes
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
span.links = append(span.links, link)
|
2019-12-05 00:00:35 +02:00
|
|
|
}
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
span.SetName(name)
|
|
|
|
span.SetAttributes(c.Attributes...)
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
if t.config.SpanRecorder != nil {
|
|
|
|
t.config.SpanRecorder.OnStart(span)
|
|
|
|
}
|
2020-11-07 00:13:31 +02:00
|
|
|
return trace.ContextWithSpan(ctx, span), span
|
2019-12-05 00:00:35 +02:00
|
|
|
}
|