예제 #1
0
func TestMiddleware_createNewSpan(t *testing.T) {
	ms := appdash.NewMemoryStore()
	c := appdash.NewLocalCollector(ms)

	req, _ := http.NewRequest("GET", "http://example.com/foo", nil)

	var setContextSpan appdash.SpanID
	mw := Middleware(c, &MiddlewareConfig{
		SetContextSpan: func(r *http.Request, id appdash.SpanID) { setContextSpan = id },
	})

	w := httptest.NewRecorder()
	mw(w, req, func(http.ResponseWriter, *http.Request) {})

	if setContextSpan == (appdash.SpanID{0, 0, 0}) {
		t.Errorf("context span is zero, want it to be set")
	}

	trace, err := ms.Trace(setContextSpan.Trace)
	if err != nil {
		t.Fatal(err)
	}

	var e ServerEvent
	if err := appdash.UnmarshalEvent(trace.Span.Annotations, &e); err != nil {
		t.Fatal(err)
	}

	wantEvent := ServerEvent{
		Request: RequestInfo{
			Method: "GET",
			Proto:  "HTTP/1.1",
			URI:    "/foo",
			Host:   "example.com",
		},
		Response: ResponseInfo{
			StatusCode: 200,
			Headers:    map[string]string{"Span-Id": setContextSpan.String()},
		},
	}
	delete(e.Request.Headers, "Span-Id")
	e.ServerRecv = time.Time{}
	e.ServerSend = time.Time{}
	if !reflect.DeepEqual(e, wantEvent) {
		t.Errorf("got ServerEvent %+v, want %+v", e, wantEvent)
	}
}
예제 #2
0
// SetSpanIDHeader sets the Span-ID header.
func SetSpanIDHeader(h http.Header, e appdash.SpanID) {
	h.Set(HeaderSpanID, e.String())
}