-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Add manual implementation of APIv3 HTTP endpoints #5054
Merged
yurishkuro
merged 16 commits into
jaegertracing:main
from
yurishkuro:grpc-gateway-manual-impl
Dec 30, 2023
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ca2856c
IDL update
yurishkuro 8e6a394
fix
yurishkuro d5a13cf
Reduce number of snapshots
yurishkuro a05917f
comment
yurishkuro 7b31e4c
simpler
yurishkuro f60b976
Add tracing middleware
yurishkuro fcb1fdd
remove if
yurishkuro 198e5ae
tests
yurishkuro 42e6fac
tests
yurishkuro 74a84f7
fix time rounding
yurishkuro b413b72
tests
yurishkuro 579209c
fix tests
yurishkuro 7c3ed6b
Merge branch 'main' into grpc-gateway-manual-impl
yurishkuro ab70193
Separate test report collection from the main target
yurishkuro 156d28b
disable json test report
yurishkuro 3abcaa2
Fix tests
yurishkuro 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
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
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
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
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 |
---|---|---|
|
@@ -22,18 +22,16 @@ import ( | |
"io" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
gogojsonpb "github.com/gogo/protobuf/jsonpb" | ||
gogoproto "github.com/gogo/protobuf/proto" | ||
"github.com/gorilla/mux" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc" | ||
|
@@ -60,9 +58,13 @@ const ( | |
// REGENERATE_SNAPSHOTS=true go test -v ./cmd/query/app/apiv3/... | ||
var regenerateSnapshots = os.Getenv("REGENERATE_SNAPSHOTS") == "true" | ||
|
||
// The tests in http_gateway_test.go set this to true to use manual gateway implementation. | ||
var useHTTPGateway = false | ||
|
||
type testGateway struct { | ||
reader *spanstoremocks.Reader | ||
url string | ||
router *mux.Router | ||
} | ||
|
||
type gatewayRequest struct { | ||
|
@@ -76,6 +78,9 @@ func setupGRPCGateway( | |
serverTLS, clientTLS *tlscfg.Options, | ||
tenancyOptions tenancy.Options, | ||
) *testGateway { | ||
if useHTTPGateway { | ||
return setupHTTPGateway(t, basePath, serverTLS, clientTLS, tenancyOptions) | ||
} | ||
gw := &testGateway{ | ||
reader: &spanstoremocks.Reader{}, | ||
} | ||
|
@@ -159,11 +164,12 @@ func (gw *testGateway) execRequest(t *testing.T, gwReq *gatewayRequest) ([]byte, | |
func verifySnapshot(t *testing.T, body []byte) []byte { | ||
// reformat JSON body with indentation, to make diffing easier | ||
var data interface{} | ||
require.NoError(t, json.Unmarshal(body, &data)) | ||
require.NoError(t, json.Unmarshal(body, &data), "response: %s", string(body)) | ||
body, err := json.MarshalIndent(data, "", " ") | ||
require.NoError(t, err) | ||
|
||
snapshotFile := filepath.Join(snapshotLocation, strings.ReplaceAll(t.Name(), "/", "_")+".json") | ||
testName := path.Base(t.Name()) | ||
snapshotFile := filepath.Join(snapshotLocation, testName+".json") | ||
if regenerateSnapshots { | ||
os.WriteFile(snapshotFile, body, 0o644) | ||
} | ||
|
@@ -177,17 +183,6 @@ func parseResponse(t *testing.T, body []byte, obj gogoproto.Message) { | |
require.NoError(t, gogojsonpb.Unmarshal(bytes.NewBuffer(body), obj)) | ||
} | ||
|
||
func parseChunkResponse(t *testing.T, body []byte, obj gogoproto.Message) { | ||
// Unwrap the 'result' container generated by the gateway. | ||
// See https://github.com/grpc-ecosystem/grpc-gateway/issues/2189 | ||
type resultWrapper struct { | ||
Result json.RawMessage `json:"result"` | ||
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. to avoid this ugliness use the new helper types from IDL and marshal in one go. |
||
} | ||
var result resultWrapper | ||
require.NoError(t, json.Unmarshal(body, &result)) | ||
parseResponse(t, result.Result, obj) | ||
} | ||
|
||
func makeTestTrace() (*model.Trace, model.TraceID) { | ||
traceID := model.NewTraceID(150, 160) | ||
return &model.Trace{ | ||
|
@@ -282,36 +277,35 @@ func runGatewayGetTrace(t *testing.T, gw *testGateway, setupRequest func(*http.R | |
require.Equal(t, http.StatusOK, statusCode, "response=%s", string(body)) | ||
body = verifySnapshot(t, body) | ||
|
||
var spansResponse api_v3.SpansResponseChunk | ||
parseChunkResponse(t, body, &spansResponse) | ||
var response api_v3.GRPCGatewayWrapper | ||
parseResponse(t, body, &response) | ||
|
||
assert.Len(t, spansResponse.GetResourceSpans(), 1) | ||
assert.Equal(t, bytesOfTraceID(t, traceID.High, traceID.Low), spansResponse.GetResourceSpans()[0].GetScopeSpans()[0].GetSpans()[0].GetTraceId()) | ||
assert.Len(t, response.Result.ResourceSpans, 1) | ||
assert.Equal(t, | ||
bytesOfTraceID(t, traceID.High, traceID.Low), | ||
response.Result.ResourceSpans[0].ScopeSpans[0].Spans[0].TraceId) | ||
} | ||
|
||
func runGatewayFindTraces(t *testing.T, gw *testGateway, setupRequest func(*http.Request)) { | ||
trace, traceID := makeTestTrace() | ||
q, qp := mockFindQueries() | ||
gw.reader. | ||
On("FindTraces", matchContext, mock.AnythingOfType("*spanstore.TraceQueryParameters")). | ||
On("FindTraces", matchContext, qp). | ||
Return([]*model.Trace{trace}, nil).Once() | ||
|
||
q := url.Values{} | ||
q.Set("query.service_name", "foobar") | ||
q.Set("query.start_time_min", time.Now().Format(time.RFC3339)) | ||
q.Set("query.start_time_max", time.Now().Format(time.RFC3339)) | ||
|
||
body, statusCode := gw.execRequest(t, &gatewayRequest{ | ||
url: "/api/v3/traces?" + q.Encode(), | ||
setupRequest: setupRequest, | ||
}) | ||
require.Equal(t, http.StatusOK, statusCode, "response=%s", string(body)) | ||
body = verifySnapshot(t, body) | ||
|
||
var spansResponse api_v3.SpansResponseChunk | ||
parseChunkResponse(t, body, &spansResponse) | ||
var response api_v3.GRPCGatewayWrapper | ||
parseResponse(t, body, &response) | ||
|
||
assert.Len(t, spansResponse.GetResourceSpans(), 1) | ||
assert.Equal(t, bytesOfTraceID(t, traceID.High, traceID.Low), spansResponse.GetResourceSpans()[0].GetScopeSpans()[0].GetSpans()[0].GetTraceId()) | ||
assert.Len(t, response.Result.ResourceSpans, 1) | ||
assert.Equal(t, | ||
bytesOfTraceID(t, traceID.High, traceID.Low), | ||
response.Result.ResourceSpans[0].ScopeSpans[0].Spans[0].TraceId) | ||
} | ||
|
||
func bytesOfTraceID(t *testing.T, high, low uint64) []byte { | ||
|
@@ -382,8 +376,10 @@ func TestGRPCGatewayTenancyRejection(t *testing.T) { | |
// We don't set tenant header | ||
response, err := http.DefaultClient.Do(req) | ||
require.NoError(t, err) | ||
body, err := io.ReadAll(response.Body) | ||
require.NoError(t, err) | ||
require.NoError(t, response.Body.Close()) | ||
require.Equal(t, http.StatusForbidden, response.StatusCode) | ||
require.Equal(t, http.StatusUnauthorized, response.StatusCode, "response=%s", string(body)) | ||
|
||
// Try again with tenant header set | ||
tm := tenancy.NewManager(&tenancyOptions) | ||
|
Oops, something went wrong.
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.
not the cleanest way, but hopefully we'll just delete grpc-gateway based implementation soon and simplify/consolidate the tests