Skip to content

Commit b892cb2

Browse files
authored
feat: add build_info metric (#121)
Signed-off-by: t3mi <[email protected]>
1 parent 4df1af5 commit b892cb2

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

.github/workflows/release.yml

+2
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ jobs:
4343
platforms: linux/amd64,linux/arm64
4444
tags: ${{ steps.meta.outputs.tags }}
4545
labels: ${{ steps.meta.outputs.labels }}
46+
build-args: |
47+
VERSION=${{ steps.meta.outputs.version }}

Dockerfile

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
FROM golang:1.20 AS builder
22

3+
ARG VERSION
4+
ENV PKG github.com/resmoio/kubernetes-event-exporter/pkg
5+
36
ADD . /app
47
WORKDIR /app
5-
RUN CGO_ENABLED=0 GOOS=linux GO11MODULE=on go build -a -o /main .
8+
RUN CGO_ENABLED=0 GOOS=linux GO11MODULE=on go build -ldflags="-s -w -X ${PKG}/version.Version=${VERSION}" -a -o /main .
69

710
FROM gcr.io/distroless/static:nonroot
811
COPY --from=builder --chown=nonroot:nonroot /main /kubernetes-event-exporter

pkg/metrics/metrics.go

+17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/prometheus/client_golang/prometheus/promauto"
1111
"github.com/prometheus/client_golang/prometheus/promhttp"
1212
"github.com/prometheus/exporter-toolkit/web"
13+
"github.com/resmoio/kubernetes-event-exporter/pkg/version"
1314
"github.com/rs/zerolog/log"
1415
)
1516

@@ -18,6 +19,7 @@ type Store struct {
1819
EventsDiscarded prometheus.Counter
1920
WatchErrors prometheus.Counter
2021
SendErrors prometheus.Counter
22+
BuildInfo prometheus.GaugeFunc
2123
}
2224

2325
// promLogger implements promhttp.Logger
@@ -87,6 +89,20 @@ func Init(addr string, tlsConf string) {
8789

8890
func NewMetricsStore(name_prefix string) *Store {
8991
return &Store{
92+
BuildInfo: promauto.NewGaugeFunc(
93+
prometheus.GaugeOpts{
94+
Name: name_prefix + "build_info",
95+
Help: "A metric with a constant '1' value labeled by version, revision, branch, and goversion from which Kubernetes Event Exporter was built.",
96+
ConstLabels: prometheus.Labels{
97+
"version": version.Version,
98+
"revision": version.Revision(),
99+
"goversion": version.GoVersion,
100+
"goos": version.GoOS,
101+
"goarch": version.GoArch,
102+
},
103+
},
104+
func() float64 { return 1 },
105+
),
90106
EventsProcessed: promauto.NewCounter(prometheus.CounterOpts{
91107
Name: name_prefix + "events_sent",
92108
Help: "The total number of events processed",
@@ -111,5 +127,6 @@ func DestroyMetricsStore(store *Store) {
111127
prometheus.Unregister(store.EventsDiscarded)
112128
prometheus.Unregister(store.WatchErrors)
113129
prometheus.Unregister(store.SendErrors)
130+
prometheus.Unregister(store.BuildInfo)
114131
store = nil
115132
}

pkg/version/version.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package version
2+
3+
import (
4+
"runtime"
5+
"runtime/debug"
6+
)
7+
8+
// Build information. Populated at build-time.
9+
var (
10+
Version = "unknown"
11+
GoVersion = runtime.Version()
12+
GoOS = runtime.GOOS
13+
GoArch = runtime.GOARCH
14+
)
15+
16+
func Revision() string {
17+
bi, ok := debug.ReadBuildInfo()
18+
19+
if ok {
20+
for _, kv := range bi.Settings {
21+
switch kv.Key {
22+
case "vcs.revision":
23+
return kv.Value
24+
}
25+
}
26+
}
27+
28+
return "unknown"
29+
}

0 commit comments

Comments
 (0)