Skip to content

Commit

Permalink
Add build info to metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Jul 13, 2023
1 parent 9c2d265 commit d5a5201
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions service/adapters/prometheus/prometheus.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package prometheus

import (
"runtime/debug"
"time"

"github.com/boreq/errors"
Expand All @@ -14,6 +15,9 @@ const (
labelHandlerName = "handlerName"
labelRelayDownloaderState = "state"
labelTopic = "topic"
labelVcsRevision = "vcsRevision"
labelVcsTime = "vcsTime"
labelGo = "go"

labelResult = "result"
labelResultSuccess = "success"
Expand Down Expand Up @@ -61,13 +65,21 @@ func NewPrometheus(logger logging.Logger) (*Prometheus, error) {
},
[]string{labelTopic},
)
versionGague := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "version",
Help: "This metric exists just to put a commit label on it.",
},
[]string{labelVcsRevision, labelVcsTime, labelGo},
)

reg := prometheus.NewRegistry()
for _, v := range []prometheus.Collector{
applicationHandlerCallsCounter,
applicationHandlerCallDurationHistogram,
relayDownloaderStateGauge,
subscriptionQueueLengthGauge,
versionGague,
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
collectors.NewGoCollector(),
} {
Expand All @@ -76,6 +88,20 @@ func NewPrometheus(logger logging.Logger) (*Prometheus, error) {
}
}

buildInfo, ok := debug.ReadBuildInfo()
if ok {
var vcsRevision, vcsTime string
for _, setting := range buildInfo.Settings {
if setting.Key == "vcs.revision" {
vcsRevision = setting.Value
}
if setting.Key == "vcs.time" {
vcsTime = setting.Value
}
}
versionGague.With(prometheus.Labels{labelGo: buildInfo.GoVersion, labelVcsRevision: vcsRevision, labelVcsTime: vcsTime}).Set(1)
}

return &Prometheus{
applicationHandlerCallsCounter: applicationHandlerCallsCounter,
applicationHandlerCallDurationHistogram: applicationHandlerCallDurationHistogram,
Expand Down

0 comments on commit d5a5201

Please sign in to comment.