Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

version CLI flag #158

Merged
merged 4 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# this is the what ends up in the RPM "Version" field and it is also used as suffix for the built binaries
# if you want to commit to OBS it must be a remotely available Git reference
VERSION ?= $(shell git rev-parse --short HEAD)
VERSION ?= $(shell git describe --tags --abbrev=0)dev+git.$(shell git show -s --format=%ct.%h HEAD)
DATE = $(shell date --iso-8601=seconds)

# we only use this to comply with RPM changelog conventions at SUSE
AUTHOR ?= shap-staff@suse.de
Expand All @@ -25,7 +26,7 @@ build-all: clean-bin $(ARCHS)

$(ARCHS):
@mkdir -p build/bin
CGO_ENABLED=0 GOOS=linux GOARCH=$@ go build -trimpath -ldflags "-s -w -X main.version=$(VERSION)" -o build/bin/ha_cluster_exporter-$(VERSION)-$@
CGO_ENABLED=0 GOOS=linux GOARCH=$@ go build -trimpath -ldflags "-s -w -X main.version=$(VERSION) -X main.buildDate=$(DATE)" -o build/bin/ha_cluster_exporter-$(VERSION)-$@

install:
go install
Expand Down
50 changes: 46 additions & 4 deletions ha_cluster_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"fmt"
"net/http"
"os"
"runtime"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand All @@ -17,6 +19,17 @@ import (
"github.com/ClusterLabs/ha_cluster_exporter/internal"
)

var (
// the released version
version string
// the time the binary was built
buildDate string
// global --help flag
helpFlag *bool
// global --version flag
versionFlag *bool
)

func init() {
config.SetConfigName("ha_cluster_exporter")
config.AddConfigPath("./")
Expand All @@ -35,19 +48,35 @@ func init() {
flag.String("sbd-config-path", "/etc/sysconfig/sbd", "path to sbd configuration")
flag.String("drbdsetup-path", "/sbin/drbdsetup", "path to drbdsetup executable")
flag.String("drbdsplitbrain-path", "/var/run/drbd/splitbrain", "path to drbd splitbrain hooks temporary files")
flag.Bool("enable-timestamps", false, "Add the timestamp to every metric line (hint: don't do this unless you really know what you are doing)")
flag.Bool("enable-timestamps", false, "Add the timestamp to every metric line")
flag.CommandLine.MarkDeprecated("enable-timestamps", "server-side metric timestamping is discouraged by Prometheus best-practices and should be avoided")
flag.CommandLine.SortFlags = false

err := config.BindPFlags(flag.CommandLine)
if err != nil {
log.Errorf("Could not bind config to CLI flags: %v", err)
log.Fatalf("Could not bind config to CLI flags: %v", err)
}

helpFlag = flag.BoolP("help", "h", false, "show this help message")
versionFlag = flag.Bool("version", false, "show version and build information")
}

func main() {
var err error

flag.Parse()

switch {
case *helpFlag:
showHelp()
case *versionFlag:
showVersion()
default:
run()
}
}

func run() {
var err error

err = config.ReadInConfig()
if err != nil {
log.Warn(err)
Expand Down Expand Up @@ -112,3 +141,16 @@ func main() {
log.Infof("Serving metrics on %s", fullListenAddress)
log.Fatal(http.ListenAndServe(fullListenAddress, nil))
}

func showHelp() {
flag.Usage()
os.Exit(0)
}

func showVersion() {
if buildDate == "" {
buildDate = "at unknown time"
}
fmt.Printf("version %s\nbuilt with %s %s/%s %s\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH, buildDate)
os.Exit(0)
}
3 changes: 2 additions & 1 deletion packaging/obs/prometheus-ha_cluster_exporter.spec
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ Prometheus exporter for Pacemaker HA clusters metrics
%setup -q -T -D -a 1 # unpack go dependencies in vendor.tar.gz, which was prepared by the source services

%define shortname ha_cluster_exporter
%define build_date %(date --iso-8601=seconds)

%build

export CGO_ENABLED=0
go build -mod=vendor \
-ldflags="-s -w -X main.version=%{version}" \
-ldflags="-s -w -X main.version=%{version} -X main.buildDate=%{build_date}" \
-o %{shortname}

%install
Expand Down