From 2c3d45c88a411cdeef0905074e6a007a94bf5fdd Mon Sep 17 00:00:00 2001 From: Jan Fajerski Date: Wed, 9 Mar 2022 10:21:31 +0100 Subject: [PATCH] Replace vfsgen with embed for static web assets (#10220) * remove vfsgen usages Signed-off-by: Jan Fajerski * web: use embed package for static assets This requires go 1.16. Signed-off-by: Jan Fajerski * circleci: drop go generate in web/ui Signed-off-by: Jan Fajerski * Makefile: compress web assets before build This commit add compression before (and decompression after) prometheus is build. This ensures that gzipped assets are embeded in the prometheus binary, if the builtinassets build tag is passed. If the build tag is not passed this step is still executed but has no effect. All this is executed in a subshell so that we can run the decompress step even if the build step fails, but retain the exit code of promu. This cleanup could also cover interrupts, but I left that out for now. Signed-off-by: Jan Fajerski --- .circleci/config.yml | 2 - .gitignore | 1 - Makefile | 21 ++++++---- go.mod | 2 +- go.sum | 3 +- scripts/compress_assets.sh | 5 +++ util/modtimevfs/modtimevfs.go | 67 ------------------------------ web/ui/README.md | 13 +++--- web/ui/{doc.go => assets_embed.go} | 18 ++++---- 9 files changed, 40 insertions(+), 92 deletions(-) create mode 100755 scripts/compress_assets.sh delete mode 100644 util/modtimevfs/modtimevfs.go rename web/ui/{doc.go => assets_embed.go} (65%) diff --git a/.circleci/config.yml b/.circleci/config.yml index 87717a9d5..fa6023a93 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -82,8 +82,6 @@ jobs: command: refreshenv - run: command: | - $env:GOARCH=""; $env:GOOS=""; cd web/ui; go generate - cd ../.. $TestTargets = go list ./... | Where-Object { $_ -NotMatch "(github.com/prometheus/prometheus/discovery.*|github.com/prometheus/prometheus/config|github.com/prometheus/prometheus/web)"} go test $TestTargets -vet=off -v environment: diff --git a/.gitignore b/.gitignore index 35564c059..a63f49951 100644 --- a/.gitignore +++ b/.gitignore @@ -23,7 +23,6 @@ benchmark.txt npm_licenses.tar.bz2 /web/ui/static/react -/web/ui/assets_vfsdata.go /vendor /.build diff --git a/Makefile b/Makefile index 1d4291770..a1d57f9e0 100644 --- a/Makefile +++ b/Makefile @@ -51,12 +51,16 @@ ui-lint: .PHONY: assets assets: ui-install ui-build - @echo ">> writing assets" - # Un-setting GOOS and GOARCH here because the generated Go code is always the same, - # but the cached object code is incompatible between architectures and OSes (which - # breaks cross-building for different combinations on CI in the same container). - cd $(UI_PATH) && GO111MODULE=$(GO111MODULE) GOOS= GOARCH= $(GO) generate -x -v $(GOOPTS) - @$(GOFMT) -w ./$(UI_PATH) + +.PHONY: assets-compress +assets-compress: + @echo '>> compressing assets' + scripts/compress_assets.sh + +.PHONY: assets-decompress +assets-decompress: + @echo '>> decompressing assets' + scripts/compress_assets.sh -d .PHONY: test # If we only want to only test go code we have to change the test target @@ -80,7 +84,7 @@ tarball: npm_licenses common-tarball docker: npm_licenses common-docker .PHONY: build -build: assets common-build +build: assets assets-compress common-build assets-decompress .PHONY: bench_tsdb bench_tsdb: $(PROMU) @@ -93,3 +97,6 @@ bench_tsdb: $(PROMU) @$(GO) tool pprof --alloc_space -svg $(PROMTOOL) $(TSDB_BENCHMARK_OUTPUT_DIR)/mem.prof > $(TSDB_BENCHMARK_OUTPUT_DIR)/memprof.alloc.svg @$(GO) tool pprof -svg $(PROMTOOL) $(TSDB_BENCHMARK_OUTPUT_DIR)/block.prof > $(TSDB_BENCHMARK_OUTPUT_DIR)/blockprof.svg @$(GO) tool pprof -svg $(PROMTOOL) $(TSDB_BENCHMARK_OUTPUT_DIR)/mutex.prof > $(TSDB_BENCHMARK_OUTPUT_DIR)/mutexprof.svg + +.PHONY: clean +clean: assets-decompress diff --git a/go.mod b/go.mod index 13ca6a007..21e2a599f 100644 --- a/go.mod +++ b/go.mod @@ -51,11 +51,11 @@ require ( github.com/prometheus/client_golang v1.12.1 github.com/prometheus/client_model v0.2.0 github.com/prometheus/common v0.32.1 + github.com/prometheus/common/assets v0.1.0 github.com/prometheus/common/sigv4 v0.1.0 github.com/prometheus/exporter-toolkit v0.7.1 github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9 github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 - github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 github.com/stretchr/testify v1.7.0 go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.29.0 go.opentelemetry.io/otel v1.4.1 diff --git a/go.sum b/go.sum index 070b32492..8cad41682 100644 --- a/go.sum +++ b/go.sum @@ -1123,6 +1123,8 @@ github.com/prometheus/common v0.29.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+ github.com/prometheus/common v0.30.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls= github.com/prometheus/common v0.32.1 h1:hWIdL3N2HoUx3B8j3YN9mWor0qhY/NlEKZEaXxuIRh4= github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls= +github.com/prometheus/common/assets v0.1.0 h1:8WlWPDRjbfff4FWCBjaUF0NEIgDD2Mv2anoKfwG+Ums= +github.com/prometheus/common/assets v0.1.0/go.mod h1:D17UVUE12bHbim7HzwUvtqm6gwBEaDQ0F+hIGbFbccI= github.com/prometheus/common/sigv4 v0.1.0 h1:qoVebwtwwEhS85Czm2dSROY5fTo2PAPEVdDeppTwGX4= github.com/prometheus/common/sigv4 v0.1.0/go.mod h1:2Jkxxk9yYvCkE5G1sQT7GuEXm57JrvHu9k5YwTjsNtI= github.com/prometheus/exporter-toolkit v0.6.1/go.mod h1:ZUBIj498ePooX9t/2xtDjeQYwvRpiPP2lh5u4iblj2g= @@ -1169,7 +1171,6 @@ github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAm github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 h1:bUGsEnyNbVPw06Bs80sCeARAlK8lhwqGyi6UT8ymuGk= github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= -github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 h1:pXY9qYc/MP5zdvqWEUH6SjNiu7VhSjuVFTFiTcphaLU= github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw= github.com/simonpasquier/klog-gokit v0.3.0 h1:TkFK21cbwDRS+CiystjqbAiq5ubJcVTk9hLUck5Ntcs= github.com/simonpasquier/klog-gokit v0.3.0/go.mod h1:+SUlDQNrhVtGt2FieaqNftzzk8P72zpWlACateWxA9k= diff --git a/scripts/compress_assets.sh b/scripts/compress_assets.sh new file mode 100755 index 000000000..05d2540e3 --- /dev/null +++ b/scripts/compress_assets.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +# +# [de]compress static assets + +find web/ui/static -type f -exec gzip "$@" {} \; diff --git a/util/modtimevfs/modtimevfs.go b/util/modtimevfs/modtimevfs.go deleted file mode 100644 index 5b58e7936..000000000 --- a/util/modtimevfs/modtimevfs.go +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright 2018 The Prometheus Authors -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Package modtimevfs implements a virtual file system that returns a fixed -// modification time for all files and directories. -package modtimevfs - -import ( - "net/http" - "os" - "time" -) - -type timefs struct { - fs http.FileSystem - t time.Time -} - -// New returns a file system that returns constant modification time for all files. -func New(fs http.FileSystem, t time.Time) http.FileSystem { - return &timefs{fs: fs, t: t} -} - -type file struct { - http.File - os.FileInfo - t time.Time -} - -func (t *timefs) Open(name string) (http.File, error) { - f, err := t.fs.Open(name) - if err != nil { - return f, err - } - defer func() { - if err != nil { - f.Close() - } - }() - - fstat, err := f.Stat() - if err != nil { - return nil, err - } - - return &file{f, fstat, t.t}, nil -} - -// Stat implements the http.File interface. -func (f *file) Stat() (os.FileInfo, error) { - return f, nil -} - -// ModTime implements the os.FileInfo interface. -func (f *file) ModTime() time.Time { - return f.t -} diff --git a/web/ui/README.md b/web/ui/README.md index 3441d8f26..a6c1fe711 100644 --- a/web/ui/README.md +++ b/web/ui/README.md @@ -1,11 +1,12 @@ ## Overview +The `ui` directory contains static files and templates used in the web UI. For +easier distribution they are compressend (c.f. Makefile) and statically compiled +into the Prometheus binary using the embed package. -The `ui` directory contains static files and templates used in the web UI. For easier distribution they are statically -compiled into the Prometheus binary using the vfsgen library (c.f. Makefile). - -During development it is more convenient to always use the files on disk to directly see changes without recompiling. To -make this work, remove the `builtinassets` build tag in the `flags` entry in `.promu.yml`, and then `make build` (or -build Prometheus using +During development it is more convenient to always use the files on disk to +directly see changes without recompiling. +To make this work, remove the `builtinassets` build tag in the `flags` entry +in `.promu.yml`, and then `make build` (or build Prometheus using `go build ./cmd/prometheus`). This will serve all files from your local filesystem. This is for development purposes only. diff --git a/web/ui/doc.go b/web/ui/assets_embed.go similarity index 65% rename from web/ui/doc.go rename to web/ui/assets_embed.go index 00f8e9d38..17186fd8f 100644 --- a/web/ui/doc.go +++ b/web/ui/assets_embed.go @@ -1,4 +1,4 @@ -// Copyright 2018 The Prometheus Authors +// Copyright 2021 The Prometheus Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -11,14 +11,18 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package ui provides the assets via a virtual filesystem. +//go:build builtinassets +// +build builtinassets + package ui import ( - // The blank import is to make Go modules happy. - _ "github.com/shurcooL/httpfs/filter" - _ "github.com/shurcooL/httpfs/union" - _ "github.com/shurcooL/vfsgen" + "embed" + "github.com/prometheus/common/assets" + "net/http" ) -//go:generate go run assets_generate.go +//go:embed static +var EmbedFS embed.FS + +var Assets = http.FS(assets.New(EmbedFS))