Skip to content

Commit 5481906

Browse files
authored
Read Go build info from the binary (#276)
1 parent 2be529b commit 5481906

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,3 @@ jobs:
140140
cache-to: type=gha,mode=max
141141
build-args: |
142142
VERSION=${{ steps.meta.outputs.version }}
143-
GIT_COMMIT=${{ github.sha }}

Makefile

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,9 @@ VERSION = 0.10.0
22
TAG = $(VERSION)
33
PREFIX = nginx/nginx-prometheus-exporter
44

5-
DOCKERFILEPATH = build
6-
DOCKERFILE = Dockerfile
7-
8-
GIT_COMMIT = $(shell git rev-parse HEAD)
9-
DATE = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
10-
11-
export DOCKER_BUILDKIT = 1
12-
135
.PHONY: nginx-prometheus-exporter
146
nginx-prometheus-exporter:
15-
CGO_ENABLED=0 go build -trimpath -ldflags "-s -w -X main.version=$(VERSION) -X main.commit=$(GIT_COMMIT) -X main.date=$(DATE)" -o nginx-prometheus-exporter
7+
CGO_ENABLED=0 go build -trimpath -ldflags "-s -w -X main.version=$(VERSION)" -o nginx-prometheus-exporter
168

179
.PHONY: build-goreleaser
1810
build-goreleaser: ## Build all binaries using GoReleaser
@@ -29,7 +21,7 @@ test:
2921

3022
.PHONY: container
3123
container:
32-
docker build --build-arg VERSION=$(VERSION) --build-arg GIT_COMMIT=$(GIT_COMMIT) --build-arg DATE=$(DATE) --target container -f $(DOCKERFILEPATH)/$(DOCKERFILE) -t $(PREFIX):$(TAG) .
24+
docker build --build-arg VERSION=$(VERSION) --target container -f build/Dockerfile -t $(PREFIX):$(TAG) .
3325

3426
.PHONY: push
3527
push: container

build/Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# syntax=docker/dockerfile:1.4
22
FROM golang:1.18 as base
33
ARG VERSION
4-
ARG GIT_COMMIT
5-
ARG DATE
64
ARG TARGETARCH
75

86
WORKDIR /go/src/github.com/nginxinc/nginx-prometheus-exporter
@@ -13,7 +11,7 @@ RUN go mod download
1311
COPY --link *.go ./
1412
COPY --link collector ./collector
1513
COPY --link client ./client
16-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=$TARGETARCH go build -trimpath -a -ldflags "-s -w -X main.version=${VERSION} -X main.commit=${GIT_COMMIT} -X main.date=${DATE}" -o nginx-prometheus-exporter .
14+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=$TARGETARCH go build -trimpath -a -ldflags "-s -w -X main.version=${VERSION}" -o nginx-prometheus-exporter .
1715

1816

1917
FROM scratch as intermediate

exporter.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"net/http"
1212
"os"
1313
"os/signal"
14+
"runtime"
15+
"runtime/debug"
1416
"strconv"
1517
"strings"
1618
"syscall"
@@ -218,8 +220,6 @@ func getListener(listenAddress string) (net.Listener, error) {
218220
var (
219221
// Set during go build
220222
version string
221-
commit string
222-
date string
223223

224224
// Defaults values
225225
defaultListenAddress = getEnv("LISTEN_ADDRESS", ":9113")
@@ -297,12 +297,16 @@ For NGINX, the stub_status page must be available through the URI. For NGINX Plu
297297
func main() {
298298
flag.Parse()
299299

300+
commitHash, commitTime, dirtyBuild := getBuildInfo()
301+
arch := fmt.Sprintf("%v/%v", runtime.GOOS, runtime.GOARCH)
302+
303+
fmt.Printf("NGINX Prometheus Exporter version=%v commit=%v date=%v, dirty=%v, arch=%v, go=%v\n", version, commitHash, commitTime, dirtyBuild, arch, runtime.Version())
304+
300305
if *displayVersion {
301-
fmt.Printf("NGINX Prometheus Exporter version=%v commit=%v date=%v\n", version, commit, date)
302306
os.Exit(0)
303307
}
304308

305-
log.Printf("Starting NGINX Prometheus Exporter version=%v commit=%v date=%v", version, commit, date)
309+
log.Printf("Starting...")
306310

307311
registry := prometheus.NewRegistry()
308312

@@ -314,8 +318,11 @@ func main() {
314318
constLabels.labels,
315319
prometheus.Labels{
316320
"version": version,
317-
"commit": commit,
318-
"date": date,
321+
"commit": commitHash,
322+
"date": commitTime,
323+
"dirty": strconv.FormatBool(dirtyBuild),
324+
"arch": arch,
325+
"go": runtime.Version(),
319326
},
320327
),
321328
},
@@ -464,3 +471,23 @@ func cloneRequest(req *http.Request) *http.Request {
464471
}
465472
return r
466473
}
474+
475+
func getBuildInfo() (string, string, bool) {
476+
var commitHash, commitTime string
477+
var dirtyBuild bool
478+
info, ok := debug.ReadBuildInfo()
479+
if !ok {
480+
return "", "", false
481+
}
482+
for _, kv := range info.Settings {
483+
switch kv.Key {
484+
case "vcs.revision":
485+
commitHash = kv.Value
486+
case "vcs.time":
487+
commitTime = kv.Value
488+
case "vcs.modified":
489+
dirtyBuild = kv.Value == "true"
490+
}
491+
}
492+
return commitHash, commitTime, dirtyBuild
493+
}

0 commit comments

Comments
 (0)