Created
April 16, 2021 11:15
-
-
Save r3code/09dffcf53b6ff8737e30a65d592c51b1 to your computer and use it in GitHub Desktop.
Stub for runtime versioning for GoLang programs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package version | |
import ( | |
"bytes" | |
"fmt" | |
"runtime" | |
"strings" | |
"github.com/prometheus/client_golang/prometheus" | |
) | |
// Build information. Populated at build-time via build tags | |
// | |
var ( | |
Version string | |
Revision string | |
Branch string | |
BuildDate string | |
GoVersion = runtime.Version() | |
) | |
// Info returns version, branch and revision information. | |
func Info() string { | |
return fmt.Sprintf("(version=%s, branch=%s, revision=%s)", Version, Branch, Revision) | |
} | |
// BuildContext returns goVersion and buildDate information. | |
func BuildContext() string { | |
return fmt.Sprintf("(go_version=%s, build_date=%s)", GoVersion, BuildDate) | |
} | |
// NewCollector exports metrics about current version information into prometheus | |
func NewCollector(appName string) *prometheus.GaugeVec { | |
buildInfo := prometheus.NewGaugeVec( | |
prometheus.GaugeOpts{ | |
Namespace: appName, | |
Name: "build_info", | |
Help: fmt.Sprintf( | |
"A metric with a constant '1' value labeled by version, revision, branch, and goversion from which %s was built.", | |
appName, | |
), | |
}, | |
[]string{"version", "revision", "branch", "go_version"}, | |
) | |
buildInfo.WithLabelValues(Version, Revision, Branch, GoVersion).Set(1) | |
return buildInfo | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment