-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
[WIP] Implement proper payload chunked encoding in HTTP api_v3 #6479
Open
chahatsagarmain
wants to merge
5
commits into
jaegertracing:main
Choose a base branch
from
chahatsagarmain:chunked_api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,14 @@ | |
package apiv3 | ||
|
||
import ( | ||
"compress/gzip" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gogo/protobuf/jsonpb" | ||
|
@@ -51,6 +53,12 @@ | |
Tracer trace.TracerProvider | ||
} | ||
|
||
type chunkedGzipWriter struct { | ||
http.ResponseWriter | ||
gzipWriter *gzip.Writer | ||
flusher http.Flusher | ||
} | ||
|
||
// RegisterRoutes registers HTTP endpoints for APIv3 into provided mux. | ||
// The called can create a subrouter if it needs to prepend a base path. | ||
func (h *HTTPGateway) RegisterRoutes(router *mux.Router) { | ||
|
@@ -60,15 +68,58 @@ | |
h.addRoute(router, h.getOperations, routeGetOperations).Methods(http.MethodGet) | ||
} | ||
|
||
// addRoute adds a new endpoint to the router with given path and handler function. | ||
// This code is mostly copied from ../http_handler. | ||
func (w *chunkedGzipWriter) Write(b []byte) (int, error) { | ||
n, err := w.gzipWriter.Write(b) | ||
if err != nil { | ||
return n, err | ||
} | ||
|
||
if err := w.gzipWriter.Flush(); err != nil { | ||
return n, err | ||
} | ||
|
||
w.flusher.Flush() | ||
return n, nil | ||
} | ||
|
||
func gzipMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { | ||
next.ServeHTTP(w, r) | ||
return | ||
} | ||
|
||
flusher, ok := w.(http.Flusher) | ||
if !ok { | ||
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// Set chunked transfer encoding before any writes | ||
w.Header().Set("Transfer-Encoding", "chunked") | ||
w.Header().Set("Content-Encoding", "gzip") | ||
w.Header().Set("Vary", "Accept-Encoding") | ||
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 is this? |
||
|
||
gz := gzip.NewWriter(w) | ||
defer gz.Close() | ||
|
||
writer := &chunkedGzipWriter{ | ||
ResponseWriter: w, | ||
gzipWriter: gz, | ||
flusher: flusher, | ||
} | ||
|
||
next.ServeHTTP(writer, r) | ||
}) | ||
} | ||
|
||
func (*HTTPGateway) addRoute( | ||
router *mux.Router, | ||
f func(http.ResponseWriter, *http.Request), | ||
route string, | ||
_ ...any, /* args */ | ||
) *mux.Route { | ||
var handler http.Handler = http.HandlerFunc(f) | ||
handler = gzipMiddleware(handler) | ||
handler = otelhttp.WithRouteTag(route, handler) | ||
handler = spanNameHandler(route, handler) | ||
return router.HandleFunc(route, handler.ServeHTTP) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"bytes" | ||
"context" | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"testing" | ||
|
@@ -915,6 +916,21 @@ func TestServerHTTP_TracesRequest(t *testing.T) { | |
resp, err := client.Do(req) | ||
require.NoError(t, err) | ||
|
||
var fullResponse bytes.Buffer | ||
|
||
buf := make([]byte, 1024) | ||
for { | ||
n, err := resp.Body.Read(buf) | ||
|
||
if n > 0 { | ||
fullResponse.Write(buf[:n]) | ||
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. if you concatenate the chunks you will end up with invalid JSON |
||
} | ||
|
||
if err == io.EOF { | ||
break | ||
} | ||
} | ||
|
||
if test.expectedTrace != "" { | ||
assert.Len(t, exporter.GetSpans(), 1, "HTTP request was traced and span reported") | ||
assert.Equal(t, test.expectedTrace, exporter.GetSpans()[0].Name) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
chunked encoding requires each chunk to be prefixed with its size