-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Implemented chunked encoding #7555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,8 +137,7 @@ func (h *HTTPGateway) returnTraces(traces []ptrace.Traces, err error, w http.Res | |
http.Error(w, string(resp), http.StatusNotFound) | ||
return | ||
} | ||
// TODO: the response should be streamed back to the client | ||
// https://github.com/jaegertracing/jaeger/issues/6467 | ||
|
||
combinedTrace := ptrace.NewTraces() | ||
for _, t := range traces { | ||
resources := t.ResourceSpans() | ||
|
@@ -150,6 +149,79 @@ func (h *HTTPGateway) returnTraces(traces []ptrace.Traces, err error, w http.Res | |
h.returnTrace(combinedTrace, w) | ||
} | ||
|
||
func (h *HTTPGateway) streamTraces(tracesIter func(yield func([]ptrace.Traces, error) bool), w http.ResponseWriter) { | ||
flusher, ok := w.(http.Flusher) | ||
if !ok { | ||
traces, err := jiter.FlattenWithErrors(tracesIter) | ||
h.returnTraces(traces, err, w) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.Header().Set("X-Content-Type-Options", "nosniff") | ||
w.Header().Set("Content-Encoding", "identity") | ||
|
||
tracesFound := false | ||
firstChunk := true | ||
hasError := false | ||
|
||
tracesIter(func(traces []ptrace.Traces, err error) bool { | ||
if err != nil { | ||
if firstChunk { | ||
h.tryHandleError(w, err, http.StatusInternalServerError) | ||
} else { | ||
h.Logger.Error("Error while streaming traces", zap.Error(err)) | ||
} | ||
hasError = true | ||
return false | ||
} | ||
|
||
if len(traces) == 0 { | ||
return true | ||
} | ||
Comment on lines
+179
to
+181
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current handling of empty trace arrays may lead to ambiguity in the API response. When all iterations return empty arrays, Spotted by Graphite Agent There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a reasonable callout. Can we add a test for this use case? |
||
|
||
tracesFound = true | ||
|
||
for _, td := range traces { | ||
tracesData := jptrace.TracesData(td) | ||
response := &api_v3.GRPCGatewayWrapper{ | ||
Result: &tracesData, | ||
} | ||
|
||
if firstChunk { | ||
w.WriteHeader(http.StatusOK) | ||
firstChunk = false | ||
} | ||
|
||
marshaler := jsonpb.Marshaler{} | ||
if err := marshaler.Marshal(w, response); err != nil { | ||
h.Logger.Error("Failed to marshal trace chunk", zap.Error(err)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens to the output stream if we just log error and exit? |
||
return false | ||
} | ||
|
||
if _, err := w.Write([]byte("\n")); err != nil { | ||
h.Logger.Error("Failed to write chunk separator", zap.Error(err)) | ||
return false | ||
} | ||
|
||
flusher.Flush() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if I understand correctly you serialize each chunk and flush it to the client. What happens to content-length header in this case? And how does the client know that there are multiple chunks to be received? |
||
} | ||
|
||
return true | ||
}) | ||
|
||
if !tracesFound && !hasError { | ||
errorResponse := api_v3.GRPCGatewayError{ | ||
Error: &api_v3.GRPCGatewayError_GRPCGatewayErrorDetails{ | ||
HttpCode: http.StatusNotFound, | ||
Message: "No traces found", | ||
}, | ||
} | ||
resp, _ := json.Marshal(&errorResponse) | ||
http.Error(w, string(resp), http.StatusNotFound) | ||
} | ||
} | ||
|
||
func (*HTTPGateway) marshalResponse(response proto.Message, w http.ResponseWriter) { | ||
_ = new(jsonpb.Marshaler).Marshal(w, response) | ||
} | ||
|
@@ -193,8 +265,7 @@ func (h *HTTPGateway) getTrace(w http.ResponseWriter, r *http.Request) { | |
request.RawTraces = rawTraces | ||
} | ||
getTracesIter := h.QueryService.GetTraces(r.Context(), request) | ||
trc, err := jiter.FlattenWithErrors(getTracesIter) | ||
h.returnTraces(trc, err, w) | ||
h.streamTraces(getTracesIter, w) | ||
} | ||
|
||
func (h *HTTPGateway) findTraces(w http.ResponseWriter, r *http.Request) { | ||
|
@@ -204,8 +275,7 @@ func (h *HTTPGateway) findTraces(w http.ResponseWriter, r *http.Request) { | |
} | ||
|
||
findTracesIter := h.QueryService.FindTraces(r.Context(), *queryParams) | ||
traces, err := jiter.FlattenWithErrors(findTracesIter) | ||
h.returnTraces(traces, err, w) | ||
h.streamTraces(findTracesIter, w) | ||
} | ||
|
||
func (h *HTTPGateway) parseFindTracesQuery(q url.Values, w http.ResponseWriter) (*querysvc.TraceQueryParams, bool) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is "identity" encoding?