Skip to content

Commit

Permalink
Fix base path in grpc gateway for api_v3 (#3139)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavolloffay committed Jul 14, 2021
1 parent c7b409b commit 6ee42d0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
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))
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

0 comments on commit 6ee42d0

Please sign in to comment.