Skip to content

Commit

Permalink
Add version metrics and /health endpoint (#96)
Browse files Browse the repository at this point in the history
* Add version metrics and /health endpoint

* Fix metrics
  • Loading branch information
gagliardetto authored May 7, 2024
1 parent 81c9999 commit b358da6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
73 changes: 72 additions & 1 deletion metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package main

import "github.com/prometheus/client_golang/prometheus"
import (
"runtime/debug"
"time"

"github.com/prometheus/client_golang/prometheus"
)

// - RPC requests by method (counter)
// - Epochs available epoch_available{epoch="200"} = 1
Expand Down Expand Up @@ -74,3 +79,69 @@ var metrics_responseTimeHistogram = prometheus.NewHistogramVec(
},
[]string{"method"},
)

// - Version information of this binary
var metrics_version = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "version",
Help: "Version information of this binary",
},
[]string{"started_at", "tag", "commit", "compiler", "goarch", "goos", "goamd64", "vcs", "vcs_revision", "vcs_time", "vcs_modified"},
)

func init() {
// Add an entry to the metric with the version information.
labeledValues := map[string]string{
"started_at": StartedAt.Format(time.RFC3339),
"tag": GitTag,
"commit": GitCommit,
"compiler": "",
"goarch": "",
"goos": "",
"goamd64": "",
"vcs": "",
"vcs_revision": "",
"vcs_time": "",
"vcs_modified": "",
}
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if isAnyOf(setting.Key,
"-compiler",
"GOARCH",
"GOOS",
"GOAMD64",
"vcs",
"vcs.revision",
"vcs.time",
"vcs.modified",
) {
switch setting.Key {
case "-compiler":
labeledValues["compiler"] = setting.Value
case "GOARCH":
labeledValues["goarch"] = setting.Value
case "GOOS":
labeledValues["goos"] = setting.Value
case "GOAMD64":
labeledValues["goamd64"] = setting.Value
case "vcs":
labeledValues["vcs"] = setting.Value
case "vcs.revision":
labeledValues["vcs_revision"] = setting.Value
case "vcs.time":
labeledValues["vcs_time"] = setting.Value
case "vcs.modified":
labeledValues["vcs_modified"] = setting.Value
}
}
}
}
metrics_version.With(labeledValues).Set(1)
}

var StartedAt = time.Now()

func GetUptime() time.Duration {
return time.Since(StartedAt)
}
8 changes: 8 additions & 0 deletions multiepoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,14 @@ func newMultiEpochHandler(handler *MultiEpoch, lsConf *ListenerConfig) func(ctx
handler(reqCtx)
return
}
{
// Handle the /health endpoint
if string(reqCtx.Path()) == "/health" && reqCtx.IsGet() {
method = "/health"
reqCtx.SetStatusCode(http.StatusOK)
return
}
}
}
{
// make sure the method is POST
Expand Down

0 comments on commit b358da6

Please sign in to comment.