From be11738ee06865a9a854a413078cbeb8184c5f33 Mon Sep 17 00:00:00 2001 From: Yuri Shkuro Date: Mon, 19 Mar 2018 14:54:01 -0400 Subject: [PATCH 1/7] Allow overriding base path for UI/API routes Signed-off-by: Yuri Shkuro --- cmd/query/app/flags.go | 12 ++++----- cmd/query/app/handler.go | 11 ++++---- cmd/query/app/handler_options.go | 11 ++++++-- cmd/query/app/static_handler.go | 46 ++++++++++++++++++++++++++++---- cmd/query/main.go | 7 +++-- cmd/standalone/main.go | 4 ++- jaeger-ui | 2 +- 7 files changed, 71 insertions(+), 22 deletions(-) diff --git a/cmd/query/app/flags.go b/cmd/query/app/flags.go index 824842a33f6..9cdfb757be7 100644 --- a/cmd/query/app/flags.go +++ b/cmd/query/app/flags.go @@ -22,7 +22,7 @@ import ( const ( queryPort = "query.port" - queryPrefix = "query.prefix" + queryBasePath = "query.base-path" queryStaticFiles = "query.static-files" queryUIConfig = "query.ui-config" queryHealthCheckHTTPPort = "query.health-check-http-port" @@ -32,8 +32,8 @@ const ( type QueryOptions struct { // Port is the port that the query service listens in on Port int - // Prefix is the prefix of the query service api - Prefix string + // BasePath is the prefix for all UI and API HTTP routes + BasePath string // StaticAssets is the path for the static assets for the UI (https://github.com/uber/jaeger-ui) StaticAssets string // UIConfig is the path to a configuration file for the UI @@ -45,8 +45,8 @@ type QueryOptions struct { // AddFlags adds flags for QueryOptions func AddFlags(flagSet *flag.FlagSet) { flagSet.Int(queryPort, 16686, "The port for the query service") - flagSet.String(queryPrefix, "api", "The prefix for the url of the query service") - flagSet.String(queryStaticFiles, "jaeger-ui-build/build/", "The path for the static assets for the UI") + flagSet.String(queryBasePath, "/", "The base path for all HTTP routes, e.g. /jaeger; useful when running behind a reverse proxy") + flagSet.String(queryStaticFiles, "jaeger-ui-build/build/", "The directory path for the static assets for the UI") flagSet.String(queryUIConfig, "", "The path to the UI configuration file in JSON format") flagSet.Int(queryHealthCheckHTTPPort, 16687, "The http port for the health check service") } @@ -54,7 +54,7 @@ func AddFlags(flagSet *flag.FlagSet) { // InitFromViper initializes QueryOptions with properties from viper func (qOpts *QueryOptions) InitFromViper(v *viper.Viper) *QueryOptions { qOpts.Port = v.GetInt(queryPort) - qOpts.Prefix = v.GetString(queryPrefix) + qOpts.BasePath = v.GetString(queryBasePath) qOpts.StaticAssets = v.GetString(queryStaticFiles) qOpts.UIConfig = v.GetString(queryUIConfig) qOpts.HealthCheckHTTPPort = v.GetInt(queryHealthCheckHTTPPort) diff --git a/cmd/query/app/handler.go b/cmd/query/app/handler.go index 2224266c389..b499bac1a2b 100644 --- a/cmd/query/app/handler.go +++ b/cmd/query/app/handler.go @@ -43,7 +43,7 @@ const ( defaultDependencyLookbackDuration = time.Hour * 24 defaultTraceQueryLookbackDuration = time.Hour * 24 * 2 - defaultHTTPPrefix = "api" + defaultAPIPrefix = "api" ) var ( @@ -78,7 +78,8 @@ type APIHandler struct { adjuster adjuster.Adjuster logger *zap.Logger queryParser queryParser - httpPrefix string + basePath string + apiPrefix string tracer opentracing.Tracer } @@ -96,8 +97,8 @@ func NewAPIHandler(spanReader spanstore.Reader, dependencyReader dependencystore for _, option := range options { option(aH) } - if aH.httpPrefix == "" { - aH.httpPrefix = defaultHTTPPrefix + if aH.apiPrefix == "" { + aH.apiPrefix = defaultAPIPrefix } if aH.adjuster == nil { aH.adjuster = adjuster.Sequence(StandardAdjusters...) @@ -141,7 +142,7 @@ func (aH *APIHandler) handleFunc( } func (aH *APIHandler) route(route string, args ...interface{}) string { - args = append([]interface{}{aH.httpPrefix}, args...) + args = append([]interface{}{aH.apiPrefix}, args...) return fmt.Sprintf("/%s"+route, args...) } diff --git a/cmd/query/app/handler_options.go b/cmd/query/app/handler_options.go index b494688d594..49cec6feec0 100644 --- a/cmd/query/app/handler_options.go +++ b/cmd/query/app/handler_options.go @@ -47,10 +47,17 @@ func (handlerOptions) Adjusters(adjusters ...adjuster.Adjuster) HandlerOption { } } -// Prefix creates a HandlerOption that initializes prefix HTTP prefix of the API +// BasePath creates a HandlerOption that initializes the base path for all HTTP routes +func (handlerOptions) BasePath(prefix string) HandlerOption { + return func(apiHandler *APIHandler) { + apiHandler.basePath = prefix + } +} + +// Prefix creates a HandlerOption that initializes the HTTP prefix for API routes func (handlerOptions) Prefix(prefix string) HandlerOption { return func(apiHandler *APIHandler) { - apiHandler.httpPrefix = prefix + apiHandler.apiPrefix = prefix } } diff --git a/cmd/query/app/static_handler.go b/cmd/query/app/static_handler.go index 5924a306177..0d30197536a 100644 --- a/cmd/query/app/static_handler.go +++ b/cmd/query/app/static_handler.go @@ -31,11 +31,17 @@ import ( var ( staticRootFiles = []string{"favicon.ico"} configPattern = regexp.MustCompile("JAEGER_CONFIG *= *DEFAULT_CONFIG;") + basePathPattern = regexp.MustCompile(` Date: Mon, 19 Mar 2018 14:56:24 -0400 Subject: [PATCH 2/7] Allow overriding base path for UI/API routes Signed-off-by: Yuri Shkuro --- cmd/query/app/flags_test.go | 4 ++-- cmd/query/app/handler_test.go | 2 +- cmd/query/app/static_handler_test.go | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/query/app/flags_test.go b/cmd/query/app/flags_test.go index 555d3920290..dcdb843c23f 100644 --- a/cmd/query/app/flags_test.go +++ b/cmd/query/app/flags_test.go @@ -27,12 +27,12 @@ func TestQueryBuilderFlags(t *testing.T) { command.ParseFlags([]string{ "--query.static-files=/dev/null", "--query.ui-config=some.json", - "--query.prefix=api", + "--query.base-path=/jaeger", "--query.port=80", }) qOpts := new(QueryOptions).InitFromViper(v) assert.Equal(t, "/dev/null", qOpts.StaticAssets) assert.Equal(t, "some.json", qOpts.UIConfig) - assert.Equal(t, "api", qOpts.Prefix) + assert.Equal(t, "/jaeger", qOpts.BasePath) assert.Equal(t, 80, qOpts.Port) } diff --git a/cmd/query/app/handler_test.go b/cmd/query/app/handler_test.go index 7fb833c9fa4..ae520527c2f 100644 --- a/cmd/query/app/handler_test.go +++ b/cmd/query/app/handler_test.go @@ -85,7 +85,7 @@ func initializeTestServerWithHandler(options ...HandlerOption) (*httptest.Server append( []HandlerOption{ HandlerOptions.Logger(zap.NewNop()), - HandlerOptions.Prefix(defaultHTTPPrefix), + HandlerOptions.Prefix(defaultAPIPrefix), HandlerOptions.QueryLookbackDuration(defaultTraceQueryLookbackDuration), }, options..., diff --git a/cmd/query/app/static_handler_test.go b/cmd/query/app/static_handler_test.go index 315bb4962be..fc900799d1c 100644 --- a/cmd/query/app/static_handler_test.go +++ b/cmd/query/app/static_handler_test.go @@ -32,7 +32,7 @@ import ( func TestStaticAssetsHandler(t *testing.T) { r := mux.NewRouter() - handler, err := NewStaticAssetsHandler("fixture", "") + handler, err := NewStaticAssetsHandler("fixture", StaticAssetsHandlerOptions{}) require.NoError(t, err) handler.RegisterRoutes(r) server := httptest.NewServer(r) @@ -49,13 +49,13 @@ func TestStaticAssetsHandler(t *testing.T) { } func TestDefaultStaticAssetsRoot(t *testing.T) { - handler, err := NewStaticAssetsHandler("", "") + handler, err := NewStaticAssetsHandler("", StaticAssetsHandlerOptions{}) assert.Nil(t, handler) assert.Nil(t, err) } func TestNotExistingUiConfig(t *testing.T) { - handler, err := NewStaticAssetsHandler("/foo/bar", "") + handler, err := NewStaticAssetsHandler("/foo/bar", StaticAssetsHandlerOptions{}) require.Error(t, err) assert.Contains(t, err.Error(), "Cannot read UI static assets") assert.Nil(t, handler) @@ -103,10 +103,10 @@ func TestRegisterStaticHandler(t *testing.T) { } func TestNewStaticAssetsHandlerWithConfig(t *testing.T) { - _, err := NewStaticAssetsHandler("fixture", "fixture/invalid-config") + _, err := NewStaticAssetsHandler("fixture", StaticAssetsHandlerOptions{UIConfigPath: "fixture/invalid-config"}) assert.Error(t, err) - handler, err := NewStaticAssetsHandler("fixture", "fixture/ui-config.json") + handler, err := NewStaticAssetsHandler("fixture", StaticAssetsHandlerOptions{UIConfigPath: "fixture/ui-config.json"}) require.NoError(t, err) require.NotNil(t, handler) html := string(handler.indexHTML) From 01d09b6ece2916a19ac7eb271f642b6d55479003 Mon Sep 17 00:00:00 2001 From: Yuri Shkuro Date: Mon, 19 Mar 2018 18:59:38 -0400 Subject: [PATCH 3/7] Add tests Signed-off-by: Yuri Shkuro --- cmd/query/app/fixture/index.html | 1 + cmd/query/app/fixture/static/asset.txt | 1 + cmd/query/app/handler_test.go | 2 + cmd/query/app/static_handler.go | 7 +- cmd/query/app/static_handler_test.go | 104 +++++++++++++++++++------ jaeger-ui | 2 +- 6 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 cmd/query/app/fixture/static/asset.txt diff --git a/cmd/query/app/fixture/index.html b/cmd/query/app/fixture/index.html index 159ece5a20d..bc5b89f5a43 100644 --- a/cmd/query/app/fixture/index.html +++ b/cmd/query/app/fixture/index.html @@ -1,6 +1,7 @@ + Test Page diff --git a/cmd/query/app/fixture/static/asset.txt b/cmd/query/app/fixture/static/asset.txt new file mode 100644 index 00000000000..3e20b5edaef --- /dev/null +++ b/cmd/query/app/fixture/static/asset.txt @@ -0,0 +1 @@ +some asset diff --git a/cmd/query/app/handler_test.go b/cmd/query/app/handler_test.go index ae520527c2f..2d88f21e43c 100644 --- a/cmd/query/app/handler_test.go +++ b/cmd/query/app/handler_test.go @@ -85,7 +85,9 @@ func initializeTestServerWithHandler(options ...HandlerOption) (*httptest.Server append( []HandlerOption{ HandlerOptions.Logger(zap.NewNop()), + // add options for test coverage HandlerOptions.Prefix(defaultAPIPrefix), + HandlerOptions.BasePath("/"), HandlerOptions.QueryLookbackDuration(defaultTraceQueryLookbackDuration), }, options..., diff --git a/cmd/query/app/static_handler.go b/cmd/query/app/static_handler.go index 0d30197536a..6569ff9ab46 100644 --- a/cmd/query/app/static_handler.go +++ b/cmd/query/app/static_handler.go @@ -33,7 +33,7 @@ var ( configPattern = regexp.MustCompile("JAEGER_CONFIG *= *DEFAULT_CONFIG;") basePathPattern = regexp.MustCompile(` Date: Mon, 19 Mar 2018 21:08:04 -0400 Subject: [PATCH 4/7] Remove unnecessary test Signed-off-by: Yuri Shkuro --- cmd/query/app/static_handler_test.go | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/cmd/query/app/static_handler_test.go b/cmd/query/app/static_handler_test.go index 3ee5ea1de1e..c512bfc0c12 100644 --- a/cmd/query/app/static_handler_test.go +++ b/cmd/query/app/static_handler_test.go @@ -132,30 +132,6 @@ func TestRegisterStaticHandler(t *testing.T) { } } -func TestNewStaticAssetsHandlerSubstitutions(t *testing.T) { - testCases := []struct { - base string - expected string - }{ - {base: "", expected: ` Date: Mon, 19 Mar 2018 21:32:05 -0400 Subject: [PATCH 5/7] clean-up tests Signed-off-by: Yuri Shkuro --- cmd/query/app/static_handler.go | 6 ++- cmd/query/app/static_handler_test.go | 60 ++++++++++------------------ cmd/query/main.go | 3 +- 3 files changed, 25 insertions(+), 44 deletions(-) diff --git a/cmd/query/app/static_handler.go b/cmd/query/app/static_handler.go index 6569ff9ab46..f8623bc8eb7 100644 --- a/cmd/query/app/static_handler.go +++ b/cmd/query/app/static_handler.go @@ -29,7 +29,8 @@ import ( ) var ( - staticRootFiles = []string{"favicon.ico"} + favoriteIcon = "favicon.ico" + staticRootFiles = []string{favoriteIcon} configPattern = regexp.MustCompile("JAEGER_CONFIG *= *DEFAULT_CONFIG;") basePathPattern = regexp.MustCompile(` Date: Mon, 19 Mar 2018 21:37:41 -0400 Subject: [PATCH 6/7] Fix error msg Signed-off-by: Yuri Shkuro --- cmd/query/app/static_handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/query/app/static_handler.go b/cmd/query/app/static_handler.go index f8623bc8eb7..7af5d18207c 100644 --- a/cmd/query/app/static_handler.go +++ b/cmd/query/app/static_handler.go @@ -34,7 +34,7 @@ var ( configPattern = regexp.MustCompile("JAEGER_CONFIG *= *DEFAULT_CONFIG;") basePathPattern = regexp.MustCompile(` Date: Fri, 23 Mar 2018 14:07:51 -0400 Subject: [PATCH 7/7] Bump ui Signed-off-by: Yuri Shkuro --- jaeger-ui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jaeger-ui b/jaeger-ui index 8d62be8b909..e20963bb40a 160000 --- a/jaeger-ui +++ b/jaeger-ui @@ -1 +1 @@ -Subproject commit 8d62be8b909d35b1fe378386e8dca59b96023234 +Subproject commit e20963bb40a40ce49191d4794edb765f4adae6a2