예제 #1
0
// TestTeeTracerSpanRefs verifies that ChildOf/FollowsFrom relations are
// reflected correctly in the underlying spans.
func TestTeeTracerSpanRefs(t *testing.T) {
	r1 := basictracer.NewInMemoryRecorder()
	t1 := basictracer.NewWithOptions(basictracer.Options{
		Recorder:     r1,
		ShouldSample: func(traceID uint64) bool { return true }, // always sample
	})
	r2 := basictracer.NewInMemoryRecorder()
	t2 := basictracer.NewWithOptions(basictracer.Options{
		Recorder:     r2,
		ShouldSample: func(traceID uint64) bool { return true }, // always sample
	})
	tr := NewTeeTracer(t1, t2)

	root := tr.StartSpan("x")
	child := tr.StartSpan("x", opentracing.ChildOf(root.Context()))
	child.Finish()
	root.Finish()

	for _, spans := range [][]basictracer.RawSpan{r1.GetSpans(), r2.GetSpans()} {
		if e, a := 2, len(spans); a != e {
			t.Fatalf("expected %d, got %d", e, a)
		}

		if e, a := spans[0].Context.TraceID, spans[1].Context.TraceID; a != e {
			t.Errorf("expected %d, got %d", e, a)
		}
		if e, a := spans[1].Context.SpanID, spans[0].ParentSpanID; a != e {
			t.Errorf("expected %d, got %d", e, a)
		}
	}
}
예제 #2
0
func TestTeeTracer(t *testing.T) {
	r1 := basictracer.NewInMemoryRecorder()
	t1 := basictracer.NewWithOptions(basictracer.Options{
		Recorder:     r1,
		ShouldSample: func(traceID uint64) bool { return true }, // always sample
	})
	r2 := basictracer.NewInMemoryRecorder()
	t2 := basictracer.NewWithOptions(basictracer.Options{
		Recorder:     r2,
		ShouldSample: func(traceID uint64) bool { return true }, // always sample
	})
	tr := NewTeeTracer(t1, t2)

	span := tr.StartSpan("x")
	span.LogKV("k1", "v1", "k2", "v2")
	span.SetTag("tag", "value")
	span.SetBaggageItem("baggage", "baggage-value")
	if e, a := "baggage-value", span.BaggageItem("baggage"); a != e {
		t.Errorf("expected %s, got %s", e, a)
	}

	spanCtx := span.Context()
	var ctxBuffer bytes.Buffer
	if err := tr.Inject(spanCtx, opentracing.Binary, &ctxBuffer); err != nil {
		t.Fatal(err)
	}

	decodedCtx, err := tr.Extract(opentracing.Binary, &ctxBuffer)
	if err != nil {
		t.Fatal(err)
	}
	span2 := tr.StartSpan("y", opentracing.FollowsFrom(decodedCtx))
	span2.LogEvent("event2")
	if e, a := "baggage-value", span2.BaggageItem("baggage"); a != e {
		t.Errorf("expected %s, got %s", e, a)
	}
	span.Finish()
	span2.Finish()

	for _, spans := range [][]basictracer.RawSpan{r1.GetSpans(), r2.GetSpans()} {
		if e, a := 2, len(spans); a != e {
			t.Fatalf("expected %d, got %d", e, a)
		}

		if e, a := "x", spans[0].Operation; a != e {
			t.Errorf("expected %s, got %s", e, a)
		}
		if e, a := (opentracing.Tags{"tag": "value"}), spans[0].Tags; !reflect.DeepEqual(a, e) {
			t.Errorf("expected %s, got %s", e, a)
		}
		if e, a := "k1:v1", spans[0].Logs[0].Fields[0].String(); a != e {
			t.Errorf("expected %s, got %s", e, a)
		}
		if e, a := "k2:v2", spans[0].Logs[0].Fields[1].String(); a != e {
			t.Errorf("expected %s, got %s", e, a)
		}
		if e, a := 1, len(spans[0].Context.Baggage); a != e {
			t.Errorf("expected %d, got %d", e, a)
		}

		if e, a := "y", spans[1].Operation; a != e {
			t.Errorf("expected %s, got %s", e, a)
		}
		if e, a := opentracing.Tags(nil), spans[1].Tags; !reflect.DeepEqual(a, e) {
			t.Errorf("expected %s, got %s", e, a)
		}
		if e, a := "event:event2", spans[1].Logs[0].Fields[0].String(); a != e {
			t.Errorf("expected %s, got %s", e, a)
		}
		if e, a := 1, len(spans[1].Context.Baggage); a != e {
			t.Errorf("expected %d, got %d", e, a)
		}
	}
}