From 87ba3a16a87097fb4e6ad84d33fd5e18c53bbcce Mon Sep 17 00:00:00 2001 From: Phil Porada Date: Mon, 20 May 2024 11:44:22 -0400 Subject: [PATCH] Add build flags --- .github/workflows/release.yml | 2 +- unbound_exporter.go | 7 +++- util/buildVars.go | 61 +++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 util/buildVars.go diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d7c3822..aad52c2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -23,7 +23,7 @@ jobs: persist-credentials: false - name: build binary - run: go build + run: go build -ldflags "-X github.com/letsencrypt/unbound_exporter/util.BuildID=$(git rev-parse --short HEAD) -X github.com/letsencrypt/unbound_exporter/util.BuildBranch=$(git rev-parse --abbrev-ref HEAD) -X github.com/letsencrypt/unbound_exporter/util.BuildTime=$(date +%F-%T -u)" - name: install nfpm run: go install github.com/goreleaser/nfpm/v2/cmd/nfpm@v2.15.1 diff --git a/unbound_exporter.go b/unbound_exporter.go index 291bb00..3929e62 100644 --- a/unbound_exporter.go +++ b/unbound_exporter.go @@ -25,12 +25,14 @@ import ( "net/url" "os" "regexp" + "runtime" "strconv" "strings" "sort" "github.com/go-kit/log/level" + "github.com/letsencrypt/unbound_exporter/util" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/promlog" @@ -548,7 +550,10 @@ func main() { ) flag.Parse() - _ = level.Info(log).Log("Starting unbound_exporter") + _ = level.Info(log).Log( + "msg", "Starting unbound_exporter", + "version", fmt.Sprintf("(version=%s, branch=%s, revision=%s)", runtime.Version(), util.GetBuildBranch(), util.GetBuildID()), + ) exporter, err := NewUnboundExporter(*unboundHost, *unboundCa, *unboundCert, *unboundKey) if err != nil { panic(err) diff --git a/util/buildVars.go b/util/buildVars.go new file mode 100644 index 0000000..4d9b259 --- /dev/null +++ b/util/buildVars.go @@ -0,0 +1,61 @@ +package util + +import ( + "expvar" +) + +const Unspecified = "Unspecified" + +// BuildID is set by the compiler (using -ldflags "-X util.BuildID $(git rev-parse --short HEAD)") +// and is used by GetBuildID +var BuildID string + +// BuildHost is set by the compiler and is used by GetBuildHost +var BuildHost string + +// BuildTime is set by the compiler and is used by GetBuildTime +var BuildTime string + +// BuildBranch is set by the compiler and is used by GetBuildBranch +var BuildBranch string + +func init() { + expvar.NewString("BuildID").Set(BuildID) + expvar.NewString("BuildTime").Set(BuildTime) +} + +// GetBuildID identifies what build is running. +func GetBuildID() (retID string) { + retID = BuildID + if retID == "" { + retID = Unspecified + } + return +} + +// GetBuildTime identifies when this build was made +func GetBuildTime() (retID string) { + retID = BuildTime + if retID == "" { + retID = Unspecified + } + return +} + +// GetBuildHost identifies the building host +func GetBuildHost() (retID string) { + retID = BuildHost + if retID == "" { + retID = Unspecified + } + return +} + +// GetBuildBranch identifies the building host +func GetBuildBranch() (retID string) { + retID = BuildBranch + if retID == "" { + retID = Unspecified + } + return +}