2020-03-01 16:35:55 -07:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package export
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
"golang.org/x/tools/internal/telemetry/event"
|
2020-03-01 16:35:55 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type SpanContext struct {
|
|
|
|
TraceID TraceID
|
|
|
|
SpanID SpanID
|
|
|
|
}
|
|
|
|
|
|
|
|
type Span struct {
|
|
|
|
Name string
|
|
|
|
ID SpanContext
|
|
|
|
ParentID SpanID
|
|
|
|
Start time.Time
|
|
|
|
Finish time.Time
|
2020-03-10 08:13:00 -06:00
|
|
|
Tags event.TagSet
|
2020-03-07 16:02:27 -07:00
|
|
|
Events []event.Event
|
2020-03-01 16:35:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type contextKeyType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
spanContextKey = contextKeyType(iota)
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetSpan(ctx context.Context) *Span {
|
|
|
|
v := ctx.Value(spanContextKey)
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.(*Span)
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:41:57 -07:00
|
|
|
// ContextSpan is an exporter that maintains hierarchical span structure in the
|
|
|
|
// context.
|
|
|
|
// It creates new spans on EventStartSpan, adds events to the current span on
|
|
|
|
// EventLog or EventTag, and closes the span on EventEndSpan.
|
|
|
|
// The span structure can then be used by other exporters.
|
2020-03-16 08:01:57 -06:00
|
|
|
func ContextSpan(ctx context.Context, ev event.Event) (context.Context, event.Event) {
|
2020-03-07 16:02:27 -07:00
|
|
|
switch {
|
2020-03-16 08:01:57 -06:00
|
|
|
case ev.IsLog(), ev.IsLabel():
|
2020-03-01 16:35:55 -07:00
|
|
|
if span := GetSpan(ctx); span != nil {
|
2020-03-07 16:02:27 -07:00
|
|
|
span.Events = append(span.Events, ev)
|
2020-03-01 16:35:55 -07:00
|
|
|
}
|
2020-03-07 16:02:27 -07:00
|
|
|
case ev.IsStartSpan():
|
2020-03-01 16:35:55 -07:00
|
|
|
span := &Span{
|
2020-03-07 16:02:27 -07:00
|
|
|
Name: ev.Message,
|
|
|
|
Start: ev.At,
|
|
|
|
Tags: ev.Tags,
|
2020-03-01 16:35:55 -07:00
|
|
|
}
|
|
|
|
if parent := GetSpan(ctx); parent != nil {
|
|
|
|
span.ID.TraceID = parent.ID.TraceID
|
|
|
|
span.ParentID = parent.ID.SpanID
|
|
|
|
} else {
|
|
|
|
span.ID.TraceID = newTraceID()
|
|
|
|
}
|
|
|
|
span.ID.SpanID = newSpanID()
|
|
|
|
ctx = context.WithValue(ctx, spanContextKey, span)
|
2020-03-07 16:02:27 -07:00
|
|
|
case ev.IsEndSpan():
|
2020-03-01 16:35:55 -07:00
|
|
|
if span := GetSpan(ctx); span != nil {
|
2020-03-07 16:02:27 -07:00
|
|
|
span.Finish = ev.At
|
2020-03-01 16:35:55 -07:00
|
|
|
}
|
2020-03-07 16:02:27 -07:00
|
|
|
case ev.IsDetach():
|
2020-03-16 08:01:57 -06:00
|
|
|
return context.WithValue(ctx, spanContextKey, nil), ev
|
2020-03-01 16:35:55 -07:00
|
|
|
}
|
2020-03-16 08:01:57 -06:00
|
|
|
return ctx, ev
|
2020-03-01 16:35:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SpanContext) Format(f fmt.State, r rune) {
|
|
|
|
fmt.Fprintf(f, "%v:%v", s.TraceID, s.SpanID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Span) Format(f fmt.State, r rune) {
|
|
|
|
fmt.Fprintf(f, "%v %v", s.Name, s.ID)
|
|
|
|
if s.ParentID.IsValid() {
|
|
|
|
fmt.Fprintf(f, "[%v]", s.ParentID)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(f, " %v->%v", s.Start, s.Finish)
|
|
|
|
}
|