Skip to content

Commit

Permalink
Add build flags
Browse files Browse the repository at this point in the history
  • Loading branch information
pgporada committed May 20, 2024
1 parent ab17c00 commit 87ba3a1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
Expand Down
7 changes: 6 additions & 1 deletion unbound_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
61 changes: 61 additions & 0 deletions util/buildVars.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 87ba3a1

Please sign in to comment.