Skip to content

Commit

Permalink
feat: add build_info metric (#121)
Browse files Browse the repository at this point in the history
Signed-off-by: t3mi <[email protected]>
  • Loading branch information
t3mi authored Oct 25, 2023
1 parent 4df1af5 commit b892cb2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ jobs:
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
VERSION=${{ steps.meta.outputs.version }}
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
FROM golang:1.20 AS builder

ARG VERSION
ENV PKG github.com/resmoio/kubernetes-event-exporter/pkg

ADD . /app
WORKDIR /app
RUN CGO_ENABLED=0 GOOS=linux GO11MODULE=on go build -a -o /main .
RUN CGO_ENABLED=0 GOOS=linux GO11MODULE=on go build -ldflags="-s -w -X ${PKG}/version.Version=${VERSION}" -a -o /main .

FROM gcr.io/distroless/static:nonroot
COPY --from=builder --chown=nonroot:nonroot /main /kubernetes-event-exporter
Expand Down
17 changes: 17 additions & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/exporter-toolkit/web"
"github.com/resmoio/kubernetes-event-exporter/pkg/version"
"github.com/rs/zerolog/log"
)

Expand All @@ -18,6 +19,7 @@ type Store struct {
EventsDiscarded prometheus.Counter
WatchErrors prometheus.Counter
SendErrors prometheus.Counter
BuildInfo prometheus.GaugeFunc
}

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

func NewMetricsStore(name_prefix string) *Store {
return &Store{
BuildInfo: promauto.NewGaugeFunc(
prometheus.GaugeOpts{
Name: name_prefix + "build_info",
Help: "A metric with a constant '1' value labeled by version, revision, branch, and goversion from which Kubernetes Event Exporter was built.",
ConstLabels: prometheus.Labels{
"version": version.Version,
"revision": version.Revision(),
"goversion": version.GoVersion,
"goos": version.GoOS,
"goarch": version.GoArch,
},
},
func() float64 { return 1 },
),
EventsProcessed: promauto.NewCounter(prometheus.CounterOpts{
Name: name_prefix + "events_sent",
Help: "The total number of events processed",
Expand All @@ -111,5 +127,6 @@ func DestroyMetricsStore(store *Store) {
prometheus.Unregister(store.EventsDiscarded)
prometheus.Unregister(store.WatchErrors)
prometheus.Unregister(store.SendErrors)
prometheus.Unregister(store.BuildInfo)
store = nil
}
29 changes: 29 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package version

import (
"runtime"
"runtime/debug"
)

// Build information. Populated at build-time.
var (
Version = "unknown"
GoVersion = runtime.Version()
GoOS = runtime.GOOS
GoArch = runtime.GOARCH
)

func Revision() string {
bi, ok := debug.ReadBuildInfo()

if ok {
for _, kv := range bi.Settings {
switch kv.Key {
case "vcs.revision":
return kv.Value
}
}
}

return "unknown"
}

0 comments on commit b892cb2

Please sign in to comment.