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.
|
|
|
|
|
|
|
|
package testtrace
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-28 23:44:53 +02:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2019-12-05 00:00:35 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
"go.opentelemetry.io/otel/api/kv/value"
|
|
|
|
|
2019-12-05 00:00:35 +02:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
"go.opentelemetry.io/otel/api/kv"
|
2019-12-05 00:00:35 +02:00
|
|
|
"go.opentelemetry.io/otel/api/trace"
|
|
|
|
)
|
|
|
|
|
2020-02-28 23:44:53 +02:00
|
|
|
const (
|
2020-05-14 01:06:03 +02:00
|
|
|
errorTypeKey = kv.Key("error.type")
|
|
|
|
errorMessageKey = kv.Key("error.message")
|
2020-02-28 23:44:53 +02:00
|
|
|
errorEventName = "error"
|
|
|
|
)
|
|
|
|
|
2019-12-05 00:00:35 +02:00
|
|
|
var _ trace.Span = (*Span)(nil)
|
|
|
|
|
|
|
|
type Span struct {
|
2020-03-07 20:38:59 +02:00
|
|
|
lock *sync.RWMutex
|
|
|
|
tracer *Tracer
|
2020-05-02 14:17:11 +02:00
|
|
|
spanContext trace.SpanContext
|
|
|
|
parentSpanID trace.SpanID
|
2020-03-07 20:38:59 +02:00
|
|
|
ended bool
|
|
|
|
name string
|
|
|
|
startTime time.Time
|
|
|
|
endTime time.Time
|
|
|
|
statusCode codes.Code
|
|
|
|
statusMessage string
|
2020-05-14 01:06:03 +02:00
|
|
|
attributes map[kv.Key]value.Value
|
2020-03-07 20:38:59 +02:00
|
|
|
events []Event
|
2020-05-14 01:06:03 +02:00
|
|
|
links map[trace.SpanContext][]kv.KeyValue
|
2019-12-05 00:00:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Span) Tracer() trace.Tracer {
|
|
|
|
return s.tracer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Span) End(opts ...trace.EndOption) {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
if s.ended {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var c trace.EndConfig
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&c)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.endTime = time.Now()
|
|
|
|
|
|
|
|
if endTime := c.EndTime; !endTime.IsZero() {
|
|
|
|
s.endTime = endTime
|
|
|
|
}
|
|
|
|
|
|
|
|
s.ended = true
|
|
|
|
}
|
|
|
|
|
2020-02-28 23:44:53 +02:00
|
|
|
func (s *Span) RecordError(ctx context.Context, err error, opts ...trace.ErrorOption) {
|
|
|
|
if err == nil || s.ended {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := trace.ErrorConfig{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Timestamp.IsZero() {
|
|
|
|
cfg.Timestamp = time.Now()
|
|
|
|
}
|
|
|
|
|
2020-03-07 20:38:59 +02:00
|
|
|
if cfg.StatusCode != codes.OK {
|
|
|
|
s.SetStatus(cfg.StatusCode, "")
|
2020-02-28 23:44:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errType := reflect.TypeOf(err)
|
|
|
|
errTypeString := fmt.Sprintf("%s.%s", errType.PkgPath(), errType.Name())
|
|
|
|
if errTypeString == "." {
|
|
|
|
errTypeString = errType.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.AddEventWithTimestamp(ctx, cfg.Timestamp, errorEventName,
|
|
|
|
errorTypeKey.String(errTypeString),
|
|
|
|
errorMessageKey.String(err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
func (s *Span) AddEvent(ctx context.Context, name string, attrs ...kv.KeyValue) {
|
2019-12-18 20:13:05 +02:00
|
|
|
s.AddEventWithTimestamp(ctx, time.Now(), name, attrs...)
|
2019-12-05 00:00:35 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
func (s *Span) AddEventWithTimestamp(ctx context.Context, timestamp time.Time, name string, attrs ...kv.KeyValue) {
|
2019-12-05 00:00:35 +02:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
if s.ended {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
attributes := make(map[kv.Key]value.Value)
|
2019-12-05 00:00:35 +02:00
|
|
|
|
|
|
|
for _, attr := range attrs {
|
|
|
|
attributes[attr.Key] = attr.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
s.events = append(s.events, Event{
|
|
|
|
Timestamp: timestamp,
|
2019-12-18 20:13:05 +02:00
|
|
|
Name: name,
|
2019-12-05 00:00:35 +02:00
|
|
|
Attributes: attributes,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Span) IsRecording() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-05-02 14:17:11 +02:00
|
|
|
func (s *Span) SpanContext() trace.SpanContext {
|
2019-12-05 00:00:35 +02:00
|
|
|
return s.spanContext
|
|
|
|
}
|
|
|
|
|
2020-03-07 20:38:59 +02:00
|
|
|
func (s *Span) SetStatus(code codes.Code, msg string) {
|
2019-12-05 00:00:35 +02:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
if s.ended {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-07 20:38:59 +02:00
|
|
|
s.statusCode = code
|
|
|
|
s.statusMessage = msg
|
2019-12-05 00:00:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Span) SetName(name string) {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
if s.ended {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.name = name
|
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
func (s *Span) SetAttributes(attrs ...kv.KeyValue) {
|
2019-12-05 00:00:35 +02:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
if s.ended {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, attr := range attrs {
|
|
|
|
s.attributes[attr.Key] = attr.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 23:24:47 +02:00
|
|
|
func (s *Span) SetAttribute(k string, v interface{}) {
|
2020-05-14 01:06:03 +02:00
|
|
|
s.SetAttributes(kv.Infer(k, v))
|
2020-04-28 23:24:47 +02:00
|
|
|
}
|
|
|
|
|
2019-12-05 00:00:35 +02:00
|
|
|
// Name returns the name most recently set on the Span, either at or after creation time.
|
|
|
|
// It cannot be change after End has been called on the Span.
|
|
|
|
func (s *Span) Name() string {
|
|
|
|
return s.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParentSpanID returns the SpanID of the parent Span.
|
|
|
|
// If the Span is a root Span and therefore does not have a parent, the returned SpanID will be invalid
|
|
|
|
// (i.e., it will contain all zeroes).
|
2020-05-02 14:17:11 +02:00
|
|
|
func (s *Span) ParentSpanID() trace.SpanID {
|
2019-12-05 00:00:35 +02:00
|
|
|
return s.parentSpanID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attributes returns the attributes set on the Span, either at or after creation time.
|
|
|
|
// If the same attribute key was set multiple times, the last call will be used.
|
|
|
|
// Attributes cannot be changed after End has been called on the Span.
|
2020-05-14 01:06:03 +02:00
|
|
|
func (s *Span) Attributes() map[kv.Key]value.Value {
|
2019-12-05 00:00:35 +02:00
|
|
|
s.lock.RLock()
|
|
|
|
defer s.lock.RUnlock()
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
attributes := make(map[kv.Key]value.Value)
|
2019-12-05 00:00:35 +02:00
|
|
|
|
|
|
|
for k, v := range s.attributes {
|
|
|
|
attributes[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return attributes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Events returns the events set on the Span.
|
|
|
|
// Events cannot be changed after End has been called on the Span.
|
|
|
|
func (s *Span) Events() []Event {
|
|
|
|
return s.events
|
|
|
|
}
|
|
|
|
|
|
|
|
// Links returns the links set on the Span at creation time.
|
|
|
|
// If multiple links for the same SpanContext were set, the last link will be used.
|
2020-05-14 01:06:03 +02:00
|
|
|
func (s *Span) Links() map[trace.SpanContext][]kv.KeyValue {
|
|
|
|
links := make(map[trace.SpanContext][]kv.KeyValue)
|
2019-12-05 00:00:35 +02:00
|
|
|
|
|
|
|
for sc, attributes := range s.links {
|
2020-05-14 01:06:03 +02:00
|
|
|
links[sc] = append([]kv.KeyValue{}, attributes...)
|
2019-12-05 00:00:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return links
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartTime returns the time at which the Span was started.
|
|
|
|
// This will be the wall-clock time unless a specific start time was provided.
|
|
|
|
func (s *Span) StartTime() time.Time {
|
|
|
|
return s.startTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndTime returns the time at which the Span was ended if at has been ended,
|
|
|
|
// or false otherwise.
|
|
|
|
// If the span has been ended, the returned time will be the wall-clock time
|
|
|
|
// unless a specific end time was provided.
|
|
|
|
func (s *Span) EndTime() (time.Time, bool) {
|
|
|
|
return s.endTime, s.ended
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ended returns whether the Span has been ended,
|
|
|
|
// i.e., whether End has been called at least once on the Span.
|
|
|
|
func (s *Span) Ended() bool {
|
|
|
|
return s.ended
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status returns the status most recently set on the Span,
|
|
|
|
// or codes.OK if no status has been explicitly set.
|
|
|
|
// It cannot be changed after End has been called on the Span.
|
2020-03-07 20:38:59 +02:00
|
|
|
func (s *Span) StatusCode() codes.Code {
|
|
|
|
return s.statusCode
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatusMessage returns the status message most recently set on the
|
|
|
|
// Span or the empty string if no status mesaage was set.
|
|
|
|
func (s *Span) StatusMessage() string {
|
|
|
|
return s.statusMessage
|
2019-12-05 00:00:35 +02:00
|
|
|
}
|