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

Inject version info into index.html #2547

Merged
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
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

How about replacing '%s' with %q ?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it only makes the logs uglier because JSON logger will have to escape quotes, etc.

}
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"`
}
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 could be considered a minor breaking change, I don't know why there was a mismatch in the name casing.

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 JSON output is used in version command and /version endpoint:

$ curl http://localhost:14269/version
{"gitCommit":"","gitVersion":"","buildDate":""}

Hard to imaging there would be dependencies on the exact string output of these.

Copy link
Contributor

Choose a reason for hiding this comment

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

I really wouldn't worry about it...


// Get creates and initialized Info object
Expand Down