Skip to content

Commit

Permalink
Inject version info into index.html (#2547)
Browse files Browse the repository at this point in the history
* Inject version info into index.html

Signed-off-by: Yuri Shkuro <github@ysh.us>

* delint and regenerate assets

Signed-off-by: Yuri Shkuro <github@ysh.us>

* fix test

Signed-off-by: Yuri Shkuro <github@ysh.us>
  • Loading branch information
yurishkuro committed Oct 9, 2020
1 parent e3a73e1 commit 0f88343
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
1 change: 1 addition & 0 deletions cmd/query/app/fixture/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
<base href="/" data-inject-target="BASE_URL"/>
<title>Test Page</title>
<!-- JAEGER_CONFIG=DEFAULT_CONFIG; -->
<!-- JAEGER_VERSION=DEFAULT_VERSION; -->
</html>
24 changes: 16 additions & 8 deletions cmd/query/app/static_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ import (
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/cmd/query/app/ui"
"github.com/jaegertracing/jaeger/pkg/version"
)

var (
favoriteIcon = "favicon.ico"
staticRootFiles = []string{favoriteIcon}

// The following patterns are searched and replaced in the index.html as a way of customizing the UI.
configPattern = regexp.MustCompile("JAEGER_CONFIG *= *DEFAULT_CONFIG;")
basePathPattern = regexp.MustCompile(`<base href="/"`)
basePathReplace = `<base href="%s/"`
errBadBasePath = "Invalid base path '%s'. Must start but not end with a slash '/', e.g. '/jaeger/ui'"
versionPattern = regexp.MustCompile("JAEGER_VERSION *= *DEFAULT_VERSION;")
basePathPattern = regexp.MustCompile(`<base href="/"`) // Note: tag is not closed
)

// RegisterStaticHandler adds handler for static assets to the router.
Expand Down Expand Up @@ -81,7 +83,7 @@ func NewStaticAssetsHandler(staticAssetsRoot string, options StaticAssetsHandler
options.Logger = zap.NewNop()
}

indexHTML, err := loadIndexBytes(assetsFS.Open, options)
indexHTML, err := loadAndEnrichIndexHTML(assetsFS.Open, options)
if err != nil {
return nil, err
}
Expand All @@ -97,11 +99,12 @@ func NewStaticAssetsHandler(staticAssetsRoot string, options StaticAssetsHandler
return h, nil
}

func loadIndexBytes(open func(string) (http.File, error), options StaticAssetsHandlerOptions) ([]byte, error) {
func loadAndEnrichIndexHTML(open func(string) (http.File, error), options StaticAssetsHandlerOptions) ([]byte, error) {
indexBytes, err := loadIndexHTML(open)
if err != nil {
return nil, fmt.Errorf("cannot load index.html: %w", err)
}
// replace UI config
configString := "JAEGER_CONFIG = DEFAULT_CONFIG"
if config, err := loadUIConfig(options.UIConfigPath); err != nil {
return nil, err
Expand All @@ -113,14 +116,19 @@ func loadIndexBytes(open func(string) (http.File, error), options StaticAssetsHa
configString = fmt.Sprintf("JAEGER_CONFIG = %v", string(bytes))
}
indexBytes = configPattern.ReplaceAll(indexBytes, []byte(configString+";"))
// replace Jaeger version
versionJSON, _ := json.Marshal(version.Get())
versionString := fmt.Sprintf("JAEGER_VERSION = %s;", string(versionJSON))
indexBytes = versionPattern.ReplaceAll(indexBytes, []byte(versionString))
// replace base path
if options.BasePath == "" {
options.BasePath = "/"
}
if options.BasePath != "/" {
if !strings.HasPrefix(options.BasePath, "/") || strings.HasSuffix(options.BasePath, "/") {
return nil, fmt.Errorf(errBadBasePath, options.BasePath)
return nil, fmt.Errorf("invalid base path '%s'. Must start but not end with a slash '/', e.g. '/jaeger/ui'", options.BasePath)
}
indexBytes = basePathPattern.ReplaceAll(indexBytes, []byte(fmt.Sprintf(basePathReplace, options.BasePath)))
indexBytes = basePathPattern.ReplaceAll(indexBytes, []byte(fmt.Sprintf(`<base href="%s/"`, options.BasePath)))
}

return indexBytes, nil
Expand All @@ -144,7 +152,7 @@ func (sH *StaticAssetsHandler) configListener(watcher *fsnotify.Watcher) {
}
// this will catch events for all files inside the same directory, which is OK if we don't have many changes
sH.options.Logger.Info("reloading UI config", zap.String("filename", sH.options.UIConfigPath))
content, err := loadIndexBytes(sH.assetsFS.Open, sH.options)
content, err := loadAndEnrichIndexHTML(sH.assetsFS.Open, sH.options)
if err != nil {
sH.options.Logger.Error("error while reloading the UI config", zap.Error(err))
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/query/app/static_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func TestRegisterStaticHandler(t *testing.T) {

html := httpGet("") // get home page
assert.Contains(t, html, `JAEGER_CONFIG = {"x":"y"};`, "actual: %v", html)
assert.Contains(t, html, `JAEGER_VERSION = {"gitCommit":"","gitVersion":"","buildDate":""};`, "actual: %v", html)
assert.Contains(t, html, testCase.expectedBaseHTML, "actual: %v", html)

asset := httpGet("static/asset.txt")
Expand All @@ -110,7 +111,7 @@ func TestNewStaticAssetsHandlerErrors(t *testing.T) {
for _, base := range []string{"x", "x/", "/x/"} {
_, err := NewStaticAssetsHandler("fixture", StaticAssetsHandlerOptions{UIConfigPath: "fixture/ui-config.json", BasePath: base})
require.Errorf(t, err, "basePath=%s", base)
assert.Contains(t, err.Error(), "Invalid base path")
assert.Contains(t, err.Error(), "invalid base path")
}
}

Expand Down
18 changes: 9 additions & 9 deletions cmd/query/app/ui/placeholder/gen_assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/query/app/ui/placeholder/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<base href="/" data-inject-target="BASE_URL"/>
<title>Test Page</title>
<!-- JAEGER_CONFIG=DEFAULT_CONFIG; -->
<!-- JAEGER_VERSION="unknown"; -->
<body>
This is a placeholder for Jaeger UI home page.
If you are seeing this, you are running a binary
Expand Down
4 changes: 2 additions & 2 deletions pkg/version/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ var (
// Info holds build information
type Info struct {
GitCommit string `json:"gitCommit"`
GitVersion string `json:"GitVersion"`
BuildDate string `json:"BuildDate"`
GitVersion string `json:"gitVersion"`
BuildDate string `json:"buildDate"`
}

// Get creates and initialized Info object
Expand Down

0 comments on commit 0f88343

Please sign in to comment.