Beispiel #1
0
// getSpanIDHeader returns the SpanID in the header (specified by
// key), nil if no such header was provided, or an error if the value
// was unparseable.
func getSpanIDHeader(h http.Header, key string) (*appdash.SpanID, error) {
	s := h.Get(key)
	if s == "" {
		return nil, nil
	}
	return appdash.ParseSpanID(s)
}
Beispiel #2
0
func TestTransport(t *testing.T) {
	ms := appdash.NewMemoryStore()
	rec := appdash.NewRecorder(appdash.SpanID{1, 2, 3}, appdash.NewLocalCollector(ms))

	req, _ := http.NewRequest("GET", "http://example.com/foo", nil)
	req.Header.Set("X-Req-Header", "a")
	mt := &mockTransport{
		resp: &http.Response{
			StatusCode:    200,
			ContentLength: 123,
			Header:        http.Header{"X-Resp-Header": []string{"b"}},
		},
	}
	transport := &Transport{
		Recorder:  rec,
		Transport: mt,
	}

	_, err := transport.RoundTrip(req)
	if err != nil {
		t.Fatal(err)
	}

	spanID, err := appdash.ParseSpanID(mt.req.Header.Get("Span-ID"))
	if err != nil {
		t.Fatal(err)
	}
	if want := (appdash.SpanID{1, spanID.Span, 2}); *spanID != want {
		t.Errorf("got Span-ID in header %+v, want %+v", *spanID, want)
	}

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

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

	wantEvent := ClientEvent{
		Request: RequestInfo{
			Method:  "GET",
			Proto:   "HTTP/1.1",
			URI:     "/foo",
			Host:    "example.com",
			Headers: map[string]string{"X-Req-Header": "a"},
		},
		Response: ResponseInfo{
			StatusCode:    200,
			ContentLength: 123,
			Headers:       map[string]string{"X-Resp-Header": "b"},
		},
	}
	delete(e.Request.Headers, "Span-Id")
	e.ClientSend = time.Time{}
	e.ClientRecv = time.Time{}
	if !reflect.DeepEqual(e, wantEvent) {
		t.Errorf("got ClientEvent %+v, want %+v", e, wantEvent)
	}
}