package tracectx import "testing" func TestResolveLoopbackHopAndDuplicates(t *testing.T) { // outer nginx (public client) -> inner nginx (loopback) -> app span; the // same 401 is recorded at every hop. spans := []Span{ {TraceID: "t", SpanID: "outer", Service: "nginx", Edge: true, PeerIP: "203.0.113.9", IsError: true, StatusCode: 401}, {TraceID: "t", SpanID: "inner", ParentID: "outer", Service: "nginx", Edge: true, PeerIP: "127.0.0.1", IsError: true, StatusCode: 401}, {TraceID: "t", SpanID: "app", ParentID: "inner", Service: "orchestrator", PeerIP: "10.244.4.195", IsError: true, StatusCode: 401}, } got := Resolve(spans) inner := got[Key{"t", "inner"}] if inner.ClientIP != "203.0.113.9" || inner.ClientHops != 1 { t.Fatalf("inner client = %+v", inner) } app := got[Key{"t", "app"}] if app.ClientIP != "203.0.113.9" || app.ClientHops != 2 || app.EdgePeer != "203.0.113.9" { t.Fatalf("app client = %+v", app) } if got[Key{"t", "outer"}].IsDuplicate || !got[Key{"t", "inner"}].IsDuplicate || !got[Key{"t", "app"}].IsDuplicate { t.Fatal("only the outermost error may own the request") } if !app.IsRootCause || got[Key{"t", "outer"}].IsRootCause { t.Fatal("root cause is the deepest error") } if outer := got[Key{"t", "outer"}]; outer.Children != 1 || outer.UpstreamChildren != 0 || outer.Depth != 0 { t.Fatalf("outer = %+v", outer) } if inner.UpstreamChildren != 1 { t.Fatalf("inner should have one upstream (other service) child: %+v", inner) } } func TestResolveDifferentStatusIsNotDuplicate(t *testing.T) { got := Resolve([]Span{ {TraceID: "t", SpanID: "a", Service: "nginx", Edge: true, PeerIP: "203.0.113.9", IsError: true, StatusCode: 502}, {TraceID: "t", SpanID: "b", ParentID: "a", Service: "api", IsError: true, StatusCode: 500}, }) if got[Key{"t", "b"}].IsDuplicate { t.Fatal("a 500 under a 502 is a distinct failure") } } func TestResolveMissingParentAndCycle(t *testing.T) { got := Resolve([]Span{ {TraceID: "t", SpanID: "x", ParentID: "gone", Service: "nginx", PeerIP: "198.51.100.7"}, {TraceID: "t", SpanID: "c1", ParentID: "c2", Service: "s", IsError: true}, {TraceID: "t", SpanID: "c2", ParentID: "c1", Service: "s", IsError: true}, }) if !got[Key{"t", "x"}].ParentMissing || got[Key{"t", "x"}].ClientIP != "198.51.100.7" { t.Fatalf("x = %+v", got[Key{"t", "x"}]) } if _, ok := got[Key{"t", "c1"}]; !ok { // must terminate t.Fatal("cycle dropped a span") } }