Skip to content
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

Fix base path in grpc gateway for api_v3 #3139

Merged
merged 2 commits into from
Jul 14, 2021
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
19 changes: 19 additions & 0 deletions cmd/all-in-one/all_in_one_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import (
"testing"
"time"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

ui "github.com/jaegertracing/jaeger/model/json"
"github.com/jaegertracing/jaeger/proto-gen/api_v3"
"github.com/jaegertracing/jaeger/thrift-gen/sampling"
)

Expand All @@ -44,6 +46,8 @@ const (
getServicesURL = queryURL + "/api/services"
getTraceURL = queryURL + "/api/traces?service=jaeger-query&tag=jaeger-debug-id:debug"
getSamplingStrategyURL = agentURL + "/sampling?service=whatever"

getServicesAPIV3URL = queryURL + "/v3/services"
)

var (
Expand All @@ -60,6 +64,7 @@ func TestAllInOne(t *testing.T) {
createTrace(t)
getAPITrace(t)
getSamplingStrategy(t)
getServicesAPIV3(t)
}

func createTrace(t *testing.T) {
Expand Down Expand Up @@ -131,3 +136,17 @@ func healthCheck() error {
}
return fmt.Errorf("query service is not ready")
}

func getServicesAPIV3(t *testing.T) {
req, err := http.NewRequest("GET", getServicesAPIV3URL, nil)
require.NoError(t, err)
resp, err := httpClient.Do(req)
require.NoError(t, err)
body, _ := ioutil.ReadAll(resp.Body)

var servicesResponse api_v3.GetServicesResponse
jsonpb := runtime.JSONPb{}
err = jsonpb.Unmarshal(body, &servicesResponse)
require.NoError(t, err)
assert.Equal(t, []string{"jaeger-query"}, servicesResponse.GetServices())
}
6 changes: 5 additions & 1 deletion cmd/query/app/apiv3/grpc_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ func RegisterGRPCGateway(ctx context.Context, logger *zap.Logger, r *mux.Router,
grpcGatewayMux := runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, jsonpb),
)
r.PathPrefix("/v3/").Handler(http.StripPrefix(basePath, grpcGatewayMux))
Copy link
Member Author

Choose a reason for hiding this comment

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

this was causing the issue. The default base path is passed as /. The StripPrefix is noop only when the base path is empty and the old tests were testing only empty base path.

var handler http.Handler = grpcGatewayMux
if basePath != "/" {
handler = http.StripPrefix(basePath, grpcGatewayMux)
}
r.PathPrefix("/v3/").Handler(handler)

var dialOpts []grpc.DialOption
if grpcTLS.Enabled {
Expand Down
13 changes: 7 additions & 6 deletions cmd/query/app/apiv3/grpc_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (

var testCertKeyLocation = "../../../../pkg/config/tlscfg/testdata/"

func testGRPCGateway(t *testing.T, serverTLS tlscfg.Options, clientTLS tlscfg.Options) {
func testGRPCGateway(t *testing.T, basePath string, serverTLS tlscfg.Options, clientTLS tlscfg.Options) {
defer serverTLS.Close()
defer clientTLS.Close()

Expand Down Expand Up @@ -83,9 +83,10 @@ func testGRPCGateway(t *testing.T, serverTLS tlscfg.Options, clientTLS tlscfg.Op
defer grpcServer.Stop()

router := &mux.Router{}
router = router.PathPrefix(basePath).Subrouter()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := RegisterGRPCGateway(ctx, zap.NewNop(), router, "", lis.Addr().String(), clientTLS)
err := RegisterGRPCGateway(ctx, zap.NewNop(), router, basePath, lis.Addr().String(), clientTLS)
require.NoError(t, err)

httpLis, err := net.Listen("tcp", ":0")
Expand All @@ -98,7 +99,7 @@ func testGRPCGateway(t *testing.T, serverTLS tlscfg.Options, clientTLS tlscfg.Op
require.Equal(t, http.ErrServerClosed, err)
}()
defer httpServer.Shutdown(context.Background())
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost%s/v3/traces/123", strings.Replace(httpLis.Addr().String(), "[::]", "", 1)), nil)
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost%s%s/v3/traces/123", strings.Replace(httpLis.Addr().String(), "[::]", "", 1), basePath), nil)
req.Header.Set("Content-Type", "application/json")
response, err := http.DefaultClient.Do(req)
buf := bytes.Buffer{}
Expand All @@ -117,10 +118,10 @@ func testGRPCGateway(t *testing.T, serverTLS tlscfg.Options, clientTLS tlscfg.Op
}

func TestGRPCGateway(t *testing.T) {
testGRPCGateway(t, tlscfg.Options{}, tlscfg.Options{})
testGRPCGateway(t, "/", tlscfg.Options{}, tlscfg.Options{})
}

func TestGRPCGateway_TLS(t *testing.T) {
func TestGRPCGateway_TLS_with_base_path(t *testing.T) {
serverTLS := tlscfg.Options{
Enabled: true,
CAPath: testCertKeyLocation + "/example-CA-cert.pem",
Expand All @@ -134,7 +135,7 @@ func TestGRPCGateway_TLS(t *testing.T) {
KeyPath: testCertKeyLocation + "/example-client-key.pem",
ServerName: "example.com",
}
testGRPCGateway(t, serverTLS, clientTLS)
testGRPCGateway(t, "/jaeger", serverTLS, clientTLS)
}

// For more details why this is needed see https://github.com/grpc-ecosystem/grpc-gateway/issues/2189
Expand Down