Skip to content

Commit

Permalink
add time window in HTTP API for fetching trace by id
Browse files Browse the repository at this point in the history
Signed-off-by: rim99 <[email protected]>
  • Loading branch information
rim99 committed Nov 24, 2024
1 parent 4ac5eb8 commit 851c3ad
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
18 changes: 17 additions & 1 deletion cmd/query/app/apiv3/http_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,26 @@ func (h *HTTPGateway) getTrace(w http.ResponseWriter, r *http.Request) {
if h.tryParamError(w, err, paramTraceID) {
return
}
// TODO: add start time & end time
request := spanstore.GetTraceParameters{
TraceID: traceID,
}
http_query := r.URL.Query()
startTime := http_query.Get(paramStartTime)
if startTime != "" {
timeParsed, err := time.Parse(time.RFC3339Nano, startTime)
if h.tryParamError(w, err, paramStartTime) {
return
}
request.StartTime = timeParsed
}
endTime := http_query.Get(paramEndTime)
if endTime != "" {
timeParsed, err := time.Parse(time.RFC3339Nano, endTime)
if h.tryParamError(w, err, paramEndTime) {
return
}
request.EndTime = timeParsed
}
trc, err := h.QueryService.GetTrace(r.Context(), request)
if h.tryHandleError(w, err, http.StatusInternalServerError) {
return
Expand Down
26 changes: 26 additions & 0 deletions cmd/query/app/apiv3/http_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,32 @@ func TestHTTPGatewayGetTraceErrors(t *testing.T) {
assert.Contains(t, w.Body.String(), simErr)
}

func TestHTTPGatewayGetTraceWithTimeWindowErrors(t *testing.T) {
gw := setupHTTPGatewayNoServer(t, "")

// error from span reader
const simErr = "simulated error"
startTime := time.Date(2020, time.January, 1, 12, 0, 0, 0, time.UTC)
endTime := time.Date(2020, time.January, 1, 13, 0, 0, 0, time.UTC)
expectedTraceGetParameters := spanstore.GetTraceParameters{
TraceID: model.TraceID{High: 0, Low: 0x123},
StartTime: startTime,
EndTime: endTime,
}
gw.reader.
On("GetTrace", matchContext, expectedTraceGetParameters).
Return(nil, errors.New(simErr)).Once()

q := url.Values{}
q.Set(paramStartTime, startTime.Format(time.RFC3339Nano))
q.Set(paramEndTime, endTime.Format(time.RFC3339Nano))
r, err := http.NewRequest(http.MethodGet, "/api/v3/traces/123?"+q.Encode(), nil)
require.NoError(t, err)
w := httptest.NewRecorder()
gw.router.ServeHTTP(w, r)
assert.Contains(t, w.Body.String(), simErr)
}

func mockFindQueries() (url.Values, *spanstore.TraceQueryParameters) {
// mock performs deep comparison of the timestamps and can fail
// if they are different in the timezone or the monotonic clocks.
Expand Down

0 comments on commit 851c3ad

Please sign in to comment.