Created
March 5, 2016 18:44
-
-
Save bg451/423c5afcabfaa07f7390 to your computer and use it in GitHub Desktop.
basictracer recorder impl for Appdash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"fmt" | |
basictracer "github.com/opentracing/basictracer-go" | |
"sourcegraph.com/sourcegraph/appdash" | |
) | |
type Recorder struct { | |
collector appdash.Collector | |
} | |
func NewRecorder(collector appdash.Collector) *Recorder { | |
return &Recorder{collector} | |
} | |
func (r *Recorder) RecordSpan(sp basictracer.RawSpan) { | |
spanID := appdash.SpanID{ | |
Span: appdash.ID(uint64(sp.SpanID)), | |
Trace: appdash.ID(uint64(sp.TraceID)), | |
Parent: appdash.ID(uint64(sp.ParentSpanID)), | |
} | |
r.collectEvent(spanID, appdash.SpanName(sp.Operation)) | |
for _, log := range sp.Logs { | |
r.collectEvent(spanID, appdash.LogWithTimestamp(log.Event, log.Timestamp)) | |
} | |
for key, value := range sp.Tags { | |
val := []byte(fmt.Sprintf("%v", value)) | |
r.collector.Collect(spanID, appdash.Annotation{Key: key, Value: val}) | |
} | |
for key, val := range sp.Baggage { | |
r.collector.Collect(spanID, appdash.Annotation{Key: key, Value: []byte(val)}) | |
} | |
approxEndTime := sp.Start.Add(sp.Duration) | |
r.collectEvent(spanID, appdash.Timespan{S: sp.Start, E: approxEndTime}) | |
} | |
func (r *Recorder) collectEvent(spanID appdash.SpanID, e appdash.Event) { | |
ans, err := appdash.MarshalEvent(e) | |
if err != nil { | |
r.logError(spanID, err) | |
return | |
} | |
r.collector.Collect(spanID, ans...) | |
} | |
// logError converts an error into a log event and collects it. | |
func (r *Recorder) logError(spanID appdash.SpanID, err error) { | |
an, _ := appdash.MarshalEvent(appdash.Log(err.Error())) | |
r.collector.Collect(spanID, an...) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment