Out of the box, the Prometheus binaries in the latest release (v2.7.1) clock in rather large, at:
-rwxr-xr-x 1 ward ward 60097588 Jan 31 06:18 prometheus -rwxr-xr-x 1 ward ward 38359349 Jan 31 06:19 promtool
Fillippo Valsorda has a post that describes shrinking golang binaries.
Prometheus uses the promu tool for building its binaries. Adding the necessary ld flags is done by editing .promu.yml file like this:
--- .promu.yml.orig 2019-02-20 08:57:29.413554323 -0500 +++ .promu.yml 2019-02-19 17:44:33.451539440 -0500 @@ -17,6 +17,8 @@ -X github.com/prometheus/common/version.Branch={{.Branch}} -X github.com/prometheus/common/version.BuildUser={{user}}@{{host}} -X github.com/prometheus/common/version.BuildDate={{date "20060102-15:04:05"}} + -s + -w tarball: files: - consoles
After building the prometheus binaries with the latest stable go (1.11.5) using make build, we end up with:
-rwxr-xr-x 1 ward ward 46379136 Feb 19 17:45 prometheus -rwxr-xr-x 1 ward ward 29391136 Feb 19 17:46 promtool
The binaries are now reduced to about 77% of their original size. Better, but still not great.
We can confirm they are now indeed statically linked and stripped of debug info:
$ file prometheus prometheus: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, stripped $ file promtool promtool: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, stripped
Applying upx takes a while but yields excellent results:
$ upx --brute prometheus Ultimate Packer for eXecutables Copyright (C) 1996 - 2013 UPX 3.91 Markus Oberhumer, Laszlo Molnar & John Reiser Sep 30th 2013 File size Ratio Format Name -------------------- ------ ----------- ----------- 46379136 -> 9370100 20.20% linux/ElfAMD prometheus Packed 1 file. $ upx --brute promtool Ultimate Packer for eXecutables Copyright (C) 1996 - 2013 UPX 3.91 Markus Oberhumer, Laszlo Molnar & John Reiser Sep 30th 2013 File size Ratio Format Name -------------------- ------ ----------- ----------- 29391136 -> 6271396 21.34% linux/ElfAMD promtool Packed 1 file.
And here are the resulting binaries:
-rwxr-xr-x 1 ward ward 9370100 Feb 19 17:45 prometheus -rwxr-xr-x 1 ward ward 6271396 Feb 19 17:46 promtool
The binaries are now down to roughly 16% of their original size. There is a noticeable startup delay at runtime for the upx decompression, but for a long-running daemon like prometheus that doesn’t matter at all.
For promtool, which is used more interactively, you might find the startup delay annoying. This is the original binary:
$ time ./promtool.orig usage: promtool.orig [] [ ...] Tooling for the Prometheus monitoring system. ... real 0m0.055s user 0m0.036s sys 0m0.013s
And here’s the UPX-compressed one:
$ time ./promtool usage: promtool [] [ ...] Tooling for the Prometheus monitoring system. ... real 0m0.536s user 0m0.521s sys 0m0.016s