Replace vfsgen with embed for static web assets (#10220)

* remove vfsgen usages

Signed-off-by: Jan Fajerski <jfajersk@redhat.com>

* web: use embed package for static assets

This requires go 1.16.

Signed-off-by: Jan Fajerski <jfajersk@redhat.com>

* circleci: drop go generate in web/ui

Signed-off-by: Jan Fajerski <jfajersk@redhat.com>

* 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 <jfajersk@redhat.com>
This commit is contained in:
Jan Fajerski 2022-03-09 10:21:31 +01:00 committed by GitHub
parent 16e610ee8f
commit 2c3d45c88a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 40 additions and 92 deletions

View file

@ -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:

1
.gitignore vendored
View file

@ -23,7 +23,6 @@ benchmark.txt
npm_licenses.tar.bz2
/web/ui/static/react
/web/ui/assets_vfsdata.go
/vendor
/.build

View file

@ -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

2
go.mod
View file

@ -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

3
go.sum
View file

@ -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=

5
scripts/compress_assets.sh Executable file
View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
#
# [de]compress static assets
find web/ui/static -type f -exec gzip "$@" {} \;

View file

@ -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
}

View file

@ -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.

View file

@ -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))