Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions cmd/collector/app/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"

"github.com/apache/thrift/lib/go/thrift"
Expand All @@ -29,11 +28,17 @@ import (
)

const (
formatParam = "format"
// UnableToReadBodyErrFormat is an error message for invalid requests
UnableToReadBodyErrFormat = "Unable to process request body: %v"
)

var (
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my own improvement: why the parenthesis?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No strong reason, it looks consistent with const block as declarative section.

acceptedThriftFormats = map[string]struct{}{
"application/x-thrift": {},
"application/vnd.apache.thrift.binary": {},
}
)

// APIHandler handles all HTTP calls to the collector
type APIHandler struct {
jaegerBatchesHandler JaegerBatchesHandler
Expand Down Expand Up @@ -61,26 +66,24 @@ func (aH *APIHandler) saveSpan(w http.ResponseWriter, r *http.Request) {
return
}

format := r.FormValue(formatParam)
switch strings.ToLower(format) {
case "jaeger.thrift":
tdes := thrift.NewTDeserializer()
// (NB): We decided to use this struct instead of straight batches to be as consistent with tchannel intake as possible.
batch := &tJaeger.Batch{}
if err = tdes.Read(batch, bodyBytes); err != nil {
http.Error(w, fmt.Sprintf(UnableToReadBodyErrFormat, err), http.StatusBadRequest)
return
}
ctx, cancel := tchanThrift.NewContext(time.Minute)
defer cancel()
batches := []*tJaeger.Batch{batch}
if _, err = aH.jaegerBatchesHandler.SubmitBatches(ctx, batches); err != nil {
http.Error(w, fmt.Sprintf("Cannot submit Jaeger batch: %v", err), http.StatusInternalServerError)
return
}
contentType := r.Header.Get("Content-Type")
if _, ok := acceptedThriftFormats[contentType]; !ok {
http.Error(w, fmt.Sprintf("Unsupported content type: %v", contentType), http.StatusBadRequest)
return
}

default:
http.Error(w, fmt.Sprintf("Unsupported format type: %v", format), http.StatusBadRequest)
tdes := thrift.NewTDeserializer()
// (NB): We decided to use this struct instead of straight batches to be as consistent with tchannel intake as possible.
batch := &tJaeger.Batch{}
if err = tdes.Read(batch, bodyBytes); err != nil {
http.Error(w, fmt.Sprintf(UnableToReadBodyErrFormat, err), http.StatusBadRequest)
return
}
ctx, cancel := tchanThrift.NewContext(time.Minute)
defer cancel()
batches := []*tJaeger.Batch{batch}
if _, err = aH.jaegerBatchesHandler.SubmitBatches(ctx, batches); err != nil {
http.Error(w, fmt.Sprintf("Cannot submit Jaeger batch: %v", err), http.StatusInternalServerError)
return
}

Expand Down
15 changes: 8 additions & 7 deletions cmd/collector/app/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ func TestThriftFormat(t *testing.T) {
server, handler := initializeTestServer(nil)
defer server.Close()

statusCode, resBodyStr, err := postBytes(server.URL+`/api/traces?format=jaeger.thrift`, someBytes)
statusCode, resBodyStr, err := postBytes("application/x-thrift", server.URL+`/api/traces`, someBytes)
assert.NoError(t, err)
assert.EqualValues(t, http.StatusAccepted, statusCode)
assert.EqualValues(t, "", resBodyStr)

handler.jaegerBatchesHandler.(*mockJaegerHandler).err = fmt.Errorf("Bad times ahead")
statusCode, resBodyStr, err = postBytes(server.URL+`/api/traces?format=jaeger.thrift`, someBytes)
statusCode, resBodyStr, err = postBytes("application/vnd.apache.thrift.binary", server.URL+`/api/traces`, someBytes)
assert.NoError(t, err)
assert.EqualValues(t, http.StatusInternalServerError, statusCode)
assert.EqualValues(t, "Cannot submit Jaeger batch: Bad times ahead\n", resBodyStr)
Expand All @@ -95,7 +95,7 @@ func TestViaClient(t *testing.T) {
defer server.Close()

sender := transport.NewHTTPTransport(
server.URL+`/api/traces?format=jaeger.thrift`,
server.URL+`/api/traces`,
transport.HTTPBatchSize(1),
)

Expand Down Expand Up @@ -128,7 +128,7 @@ func TestBadBody(t *testing.T) {
server, _ := initializeTestServer(nil)
defer server.Close()
bodyBytes := []byte("not good")
statusCode, resBodyStr, err := postBytes(server.URL+`/api/traces?format=jaeger.thrift`, bodyBytes)
statusCode, resBodyStr, err := postBytes("application/x-thrift", server.URL+`/api/traces`, bodyBytes)
assert.NoError(t, err)
assert.EqualValues(t, http.StatusBadRequest, statusCode)
assert.EqualValues(t, "Unable to process request body: *jaeger.Batch field 25711 read error: unexpected EOF\n", resBodyStr)
Expand All @@ -137,10 +137,10 @@ func TestBadBody(t *testing.T) {
func TestWrongFormat(t *testing.T) {
server, _ := initializeTestServer(nil)
defer server.Close()
statusCode, resBodyStr, err := postBytes(server.URL+`/api/traces?format=nosoupforyou`, []byte{})
statusCode, resBodyStr, err := postBytes("nosoupforyou", server.URL+`/api/traces`, []byte{})
assert.NoError(t, err)
assert.EqualValues(t, http.StatusBadRequest, statusCode)
assert.EqualValues(t, "Unsupported format type: nosoupforyou\n", resBodyStr)
assert.EqualValues(t, "Unsupported content type: nosoupforyou\n", resBodyStr)
}

func TestCannotReadBodyFromRequest(t *testing.T) {
Expand Down Expand Up @@ -177,11 +177,12 @@ func (d *dummyResponseWriter) WriteHeader(statusCode int) {
d.myStatusCode = statusCode
}

func postBytes(urlStr string, bodyBytes []byte) (int, string, error) {
func postBytes(contentType, urlStr string, bodyBytes []byte) (int, string, error) {
req, err := http.NewRequest(http.MethodPost, urlStr, bytes.NewBuffer([]byte(bodyBytes)))
if err != nil {
return 0, "", err
}
req.Header.Set("Content-Type", contentType)
res, err := httpClient.Do(req)
if err != nil {
return 0, "", err
Expand Down