Skip to content

[RFC] Replace vfsgen with embed for static web assets - #9380

Closed
jan--f wants to merge 4 commits into
prometheus:mainfrom
jan--f:replace-genvfs-with-embed
Closed

jan--f wants to merge 4 commits into
prometheus:mainfrom
jan--f:replace-genvfs-with-embed

Conversation

@jan--f

@jan--f jan--f commented Sep 23, 2021

Copy link
Copy Markdown
Contributor

$title describes the PR pretty well. The embed package was added in go 1.16 and offers a nice way to embed static files into a binary.

I marked this RFC because embed currently offers no exclude directive, i.e. this initial version of the PR adds the resources excluded here to the binary. The resulting binary is about 10.6 MB larger than a v2.30.0 build (111522587 B instead of 100382142 B) on my machine. I'm not sure though if that is purely caused by the additional assets (iiuc that is currently only bootstrap.js and bootstrap.css, which certainly don't sum up to 10 MB).
Furthermore the current version will include any file that is added to the static and templates subdirectory.

Possible solutions:

  1. Embed any file that is present but add a filter layer, similar to what ui.go implements. This however bloats the resulting binary.
  2. Craft a list of //go:embed directives, that include only the things that are needed instead of relying on naive globbing.
  3. Ensure that the respective directory trees only contain the needed files before building.

One of the latter two is probably desirable, I'm just not sure which is easier to maintain. I also might be missing other options.

This requires go 1.16.

Signed-off-by: Jan Fajerski <[email protected]>
@jan--f
jan--f requested a review from juliusv as a code owner September 23, 2021 08:31
@jan--f

jan--f commented Sep 23, 2021

Copy link
Copy Markdown
Contributor Author

cc @roidelapluie

@roidelapluie

Copy link
Copy Markdown
Member

That's a nice improvement and cleanup. Now that go 1.17.1 is out, it is also reasonable to use go1.16 features.

However you have raised a few interesting points, I will try to investigate as well. 10MB seems a lot.

@jan--f

jan--f commented Sep 23, 2021

Copy link
Copy Markdown
Contributor Author

I had a look at the respective intermediate compiled packages. Using embed results in a 15 MB intermediate package (that only includes embed, net/http and the file content). The vfsgen based asset package only amounts to ~130 KB. This likely is due to vfsgen using compression internaly.
There seem to be potential future plans to add compression to embed.

I can also look into compressing the assets before embedding, though that may have performance implications.

@jan--f jan--f changed the title [RFC] Replace genvfs with embed for static web assets [RFC] Replace vfsgen with embed for static web assets Sep 23, 2021
Signed-off-by: Jan Fajerski <[email protected]>
This adds a PoC gzip layer to the embed fs in order to reduce the binary
size. This requires an additional asset compression  step before building prometheus:
find web/ui/templates web/ui/static -type f -exec gzip {} \;
After the build completed run the following to reset the repository:
find web/ui/static web/ui/templates -name "*gz" -exec rm {} \; && g restore web/ui/static web/ui/templates

Signed-off-by: Jan Fajerski <[email protected]>
@jan--f
jan--f force-pushed the replace-genvfs-with-embed branch from 6a1275c to 73dc69e Compare October 5, 2021 12:15
@jan--f

jan--f commented Oct 5, 2021

Copy link
Copy Markdown
Contributor Author

@roidelapluie I added an additional layer that unzips content on the fly. So if the embedFS contains dir/file.gz instead of dir/file the former is used and decompressed when dir/file accessed. The file will be decompressed in memory as a whole when opened. Not ideal but for Prometheus' use case this should be fine (iiuc the web interface reads only whole files anyway).

Unfortunately this currently requires an additional step before building (see below), so it doesn't improve on the existing solution in that respect. It does however bring down the binary size to something closer that a current main build yields (-rwxr-xr-x 1 jan jan 98085936 Oct 5 14:27 prometheus on my system).

I do think this still offers improvements:

  • uses the standard library packages
  • embed may improve with respect to handling compressed content in future versions
  • potentially enables the web ui to serve (at least some) content compressed (?)

In order to try this run this before building the binary:

find web/ui/templates web/ui/static -type f -exec gzip {} \;

and to cleanup run

find web/ui/static web/ui/templates -name "*gz" -exec rm {} \; && g restore web/ui/static web/ui/templates

@jan--f

jan--f commented Oct 5, 2021

Copy link
Copy Markdown
Contributor Author

In order to try this run this before building the binary:

find web/ui/templates web/ui/static -type f -exec gzip {} \;

and to cleanup run

find web/ui/static web/ui/templates -name "*gz" -exec rm {} \; && g restore web/ui/static web/ui/templates

Note that without this step, the build should still succeed and produce a functioning binary, it will however contain the uncompressed static assets and thus be roughly 10MB bigger than needed.

@jan--f

jan--f commented Nov 19, 2021

Copy link
Copy Markdown
Contributor Author

Closing in favor of #9719

@jan--f jan--f closed this Nov 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants