func TestFromContext(t *testing.T) { const ( hostport = "5.5.5.5:5555" serviceName = "foo-service" methodName = "foo-method" traceID int64 = 14 spanID int64 = 36 parentSpanID int64 = 58 ) newSpan := zipkin.NewSpan(hostport, serviceName, methodName, traceID, spanID, parentSpanID) newSpan.Sample() ctx := context.WithValue( context.Background(), zipkin.SpanContextKey, newSpan, ) span, ok := zipkin.FromContext(ctx) if !ok { t.Fatalf("expected a context value in %q", zipkin.SpanContextKey) } if span == nil { t.Fatal("expected a Zipkin span object") } for want, haveFunc := range map[int64]func() int64{ traceID: span.TraceID, spanID: span.SpanID, parentSpanID: span.ParentSpanID, } { if have := haveFunc(); want != have { name := runtime.FuncForPC(reflect.ValueOf(haveFunc).Pointer()).Name() name = strings.Split(name, "ยท")[0] toks := strings.Split(name, ".") name = toks[len(toks)-1] t.Errorf("%s: want %d, have %d", name, want, have) } } if want, have := true, span.IsSampled(); want != have { t.Errorf("IsSampled: want %v, have %v", want, have) } }
func TestScribeCollector(t *testing.T) { server := newScribeServer(t) timeout := time.Second batchInterval := time.Millisecond c, err := zipkin.NewScribeCollector(server.addr(), timeout, 0, batchInterval) if err != nil { t.Fatal(err) } var ( serviceName = "service" methodName = "method" traceID = int64(123) spanID = int64(456) parentSpanID = int64(0) value = "foo" duration = 42 * time.Millisecond ) span := zipkin.NewSpan("1.2.3.4:1234", serviceName, methodName, traceID, spanID, parentSpanID) span.AnnotateDuration("foo", 42*time.Millisecond) if err := c.Collect(span); err != nil { t.Errorf("error during collection: %v", err) } // Need to yield to the select loop to accept the send request, and then // yield again to the send operation to write to the socket. I think the // best way to do that is just give it some time. deadline := time.Now().Add(1 * time.Second) for { if time.Now().After(deadline) { t.Fatalf("never received a span") } if want, have := 1, len(server.spans()); want != have { time.Sleep(time.Millisecond) continue } break } gotSpan := server.spans()[0] if want, have := methodName, gotSpan.GetName(); want != have { t.Errorf("want %q, have %q", want, have) } if want, have := traceID, gotSpan.GetTraceId(); want != have { t.Errorf("want %d, have %d", want, have) } if want, have := spanID, gotSpan.GetId(); want != have { t.Errorf("want %d, have %d", want, have) } if want, have := parentSpanID, gotSpan.GetParentId(); want != have { t.Errorf("want %d, have %d", want, have) } if want, have := 1, len(gotSpan.GetAnnotations()); want != have { t.Fatalf("want %d, have %d", want, have) } gotAnnotation := gotSpan.GetAnnotations()[0] if want, have := value, gotAnnotation.GetValue(); want != have { t.Errorf("want %q, have %q", want, have) } if want, have := duration, time.Duration(gotAnnotation.GetDuration())*time.Microsecond; want != have { t.Errorf("want %s, have %s", want, have) } }
package zipkin_test import ( "fmt" "testing" "github.com/go-kit/kit/tracing/zipkin" ) var s = zipkin.NewSpan("203.0.113.10:1234", "service1", "avg", 123, 456, 0) func TestNopCollector(t *testing.T) { c := zipkin.NopCollector{} if err := c.Collect(s); err != nil { t.Error(err) } if err := c.Close(); err != nil { t.Error(err) } } type stubCollector struct { errid int collected bool closed bool } func (c *stubCollector) Collect(*zipkin.Span) error { c.collected = true if c.errid != 0 { return fmt.Errorf("error %d", c.errid)
} func (p *stubProducer) Input() chan<- *sarama.ProducerMessage { return p.in } func (p *stubProducer) Successes() <-chan *sarama.ProducerMessage { return nil } func (p *stubProducer) Errors() <-chan *sarama.ProducerError { return p.err } func newStubProducer(kdown bool) *stubProducer { return &stubProducer{ make(chan *sarama.ProducerMessage), make(chan *sarama.ProducerError), kdown, false, } } var spans = []*zipkin.Span{ zipkin.NewSpan("203.0.113.10:1234", "service1", "avg", 123, 456, 0), zipkin.NewSpan("203.0.113.10:1234", "service2", "sum", 123, 789, 456), zipkin.NewSpan("203.0.113.10:1234", "service2", "div", 123, 101112, 456), } func TestKafkaProduce(t *testing.T) { p := newStubProducer(false) c, err := zipkin.NewKafkaCollector( []string{"192.0.2.10:9092"}, zipkin.KafkaProducer(p), ) if err != nil { t.Fatal(err) } for _, want := range spans { m := collectSpan(t, c, p, want)