Skip to content

Commit

Permalink
Include build information in binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
kuskoman committed Apr 13, 2023
1 parent c7e2817 commit 3e51e4b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ GOOS_VALUES := linux darwin windows
GOOS_BINARIES := $(foreach goos,$(GOOS_VALUES),out/main-$(goos))
GOOS_EXES := $(foreach goos,$(GOOS_VALUES),$(if $(filter windows,$(goos)),out/main-$(goos),out/main-$(goos)))

GITHUB_REPO := github.com/kuskoman/logstash-exporter
VERSION := $(shell git symbolic-ref --short HEAD)
GIT_COMMIT := $(shell git rev-parse HEAD)

all: $(GOOS_BINARIES)

VERSIONINFO_PKG := config
ldflags := -s -w \
-X '$(GITHUB_REPO)/$(VERSIONINFO_PKG).Version=$(VERSION)' \
-X '$(GITHUB_REPO)/$(VERSIONINFO_PKG).GitCommit=$(GIT_COMMIT)' \
-X '$(GITHUB_REPO)/$(VERSIONINFO_PKG).BuildDate=$(shell date -u +%Y-%m-%dT%H:%M:%S%Z)'

out/main-%:
CGO_ENABLED=0 GOOS=$* go build -a -installsuffix cgo -ldflags="-w -s" -o out/main-$* cmd/exporter/main.go
CGO_ENABLED=0 GOOS=$* go build -a -installsuffix cgo -ldflags="$(ldflags)" -o out/main-$* cmd/exporter/main.go

run:
go run cmd/exporter/main.go
Expand Down
2 changes: 2 additions & 0 deletions cmd/exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func main() {
logstashUrl := config.LogstashUrl

log.Println("Application starting...")
versionInfo := config.GetBuildInfo()
log.Println(versionInfo.String())

collectorManager := collectors.NewCollectorManager(logstashUrl)
server := server.NewAppServer(host, port)
Expand Down
2 changes: 1 addition & 1 deletion config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ type VersionInfo struct {

// String returns a string representation of the VersionInfo struct.
func (v *VersionInfo) String() string {
return fmt.Sprintf("%+v", *v)
return fmt.Sprintf("Version: %s, GitCommit: %s, GoVersion: %s, BuildArch: %s, BuildOS: %s, BuildDate: %s", v.Version, v.GitCommit, v.GoVersion, v.BuildArch, v.BuildOS, v.BuildDate)
}
2 changes: 1 addition & 1 deletion config/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestVersionInfoString(t *testing.T) {
BuildDate: "test-date",
}

expectedString := "{Version:test-version GitCommit:test-commit GoVersion:test-go-version BuildArch:test-arch BuildOS:test-os BuildDate:test-date}"
expectedString := "Version: test-version, GitCommit: test-commit, GoVersion: test-go-version, BuildArch: test-arch, BuildOS: test-os, BuildDate: test-date"
versionInfoString := versionInfo.String()

if versionInfoString != expectedString {
Expand Down

0 comments on commit 3e51e4b

Please sign in to comment.