// RecordSpan converts a RawSpan into the Appdash representation of a span // and records it to the underlying collector. func (r *Recorder) RecordSpan(sp basictracer.RawSpan) { if !sp.Sampled { return } 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)) // Record all of the logs. Payloads are thrown out. 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.collectAnnotation(spanID, appdash.Annotation{Key: key, Value: val}) } for key, val := range sp.Baggage { r.collectAnnotation(spanID, appdash.Annotation{Key: key, Value: []byte(val)}) } // Add the duration to the start time to get an approximate end time. approxEndTime := sp.Start.Add(sp.Duration) r.collectEvent(spanID, appdash.Timespan{S: sp.Start, E: approxEndTime}) }
func TestOpentracingRecorder(t *testing.T) { var packets []*wire.CollectPacket mc := collectorFunc(func(span appdash.SpanID, anns ...appdash.Annotation) error { packets = append(packets, newCollectPacket(span, anns)) return nil }) baggageKey := "somelongval" baggageVal := "val" opName := "testOperation" r := NewRecorder(mc, Options{}) raw := basictracer.RawSpan{ Context: basictracer.Context{ TraceID: 1, SpanID: 2, ParentSpanID: 3, Sampled: true, }, Tags: map[string]interface{}{ "tag": 1, }, Baggage: map[string]string{ baggageKey: baggageVal, }, Operation: opName, Duration: time.Duration(1), } unsampledRaw := basictracer.RawSpan{ Context: basictracer.Context{ TraceID: 1, SpanID: 2, ParentSpanID: 3, Sampled: false, }, } r.RecordSpan(raw) r.RecordSpan(unsampledRaw) tsAnnotations := marshalEvent(appdash.Timespan{raw.Start, raw.Start.Add(raw.Duration)}) want := []*wire.CollectPacket{ newCollectPacket(appdash.SpanID{1, 2, 3}, appdash.Annotations{{"tag", []byte("1")}}), newCollectPacket(appdash.SpanID{1, 2, 3}, appdash.Annotations{{baggageKey, []byte(baggageVal)}}), newCollectPacket(appdash.SpanID{1, 2, 3}, marshalEvent(appdash.SpanName(opName))), newCollectPacket(appdash.SpanID{1, 2, 3}, tsAnnotations), } sort.Sort(byTraceID(packets)) sort.Sort(byTraceID(want)) if !reflect.DeepEqual(packets, want) { t.Errorf("Got packets %v, want %v", packets, want) } }