Skip to content

Commit

Permalink
Add drand_version and drand_build_time metrics (#942)
Browse files Browse the repository at this point in the history
Co-authored-by: Yolan Romailler <AnomalRoil@users.noreply.github.com>
  • Loading branch information
mcamou and AnomalRoil committed Mar 30, 2022
1 parent c90dc31 commit 22e77d2
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ drand-relay-s3
test/docker/tmp
test/docker/data*
**/nohup.out
.tool-versions
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ARG major=0
ARG minor=0
ARG patch=0
ARG gitCommit
ARG buildDate

ENV GOPATH /go
ENV SRC_PATH $GOPATH/src/github.com/drand/drand/
Expand Down Expand Up @@ -37,6 +38,8 @@ RUN \
"-X github.com/drand/drand/common.MAJOR=${major} \
-X github.com/drand/drand/common.MINOR=${minor} \
-X github.com/drand/drand/common.PATCH=${patch} \
-X github.com/drand/drand/common.COMMIT=${patch} \
-X github.com/drand/drand/common.BUILDDATE=${patch} \
-X github.com/drand/drand/cmd/drand-cli.buildDate=`date -u +%d/%m/%Y@%H:%M:%S` \
-X github.com/drand/drand/cmd/drand-cli.gitCommit=${gitCommit}"

Expand Down
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,30 @@ build_proto:

# create the "drand" binary and install it in $GOBIN
install:
go install -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X $(CLI_PACKAGE).buildDate=$(BUILD_DATE) -X $(CLI_PACKAGE).gitCommit=$(GIT_REVISION)"
go install -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X $(CLI_PACKAGE).buildDate=$(BUILD_DATE) -X $(CLI_PACKAGE).gitCommit=$(GIT_REVISION)"

# create the "drand" binary in the current folder
build:
go build -o drand -mod=readonly -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X $(CLI_PACKAGE).buildDate=$(BUILD_DATE) -X $(CLI_PACKAGE).gitCommit=$(GIT_REVISION)"
go build -o drand -mod=readonly -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X $(CLI_PACKAGE).buildDate=$(BUILD_DATE) -X $(CLI_PACKAGE).gitCommit=$(GIT_REVISION)"

# create the "drand-client" binary in the current folder
client:
go build -o drand-client -mod=readonly -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X main.buildDate=$(BUILD_DATE) -X main.gitCommit=$(GIT_REVISION)" ./cmd/client
go build -o drand-client -mod=readonly -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X main.buildDate=$(BUILD_DATE) -X main.gitCommit=$(GIT_REVISION)" ./cmd/client
drand-client: client

# create the "drand-relay-http" binary in the current folder
relay-http:
go build -o drand-relay-http -mod=readonly -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X main.buildDate=$(BUILD_DATE) -X main.gitCommit=$(GIT_REVISION)" ./cmd/relay
go build -o drand-relay-http -mod=readonly -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X main.buildDate=$(BUILD_DATE) -X main.gitCommit=$(GIT_REVISION)" ./cmd/relay
drand-relay-http: relay-http

# create the "drand-relay-gossip" binary in the current folder
relay-gossip:
go build -o drand-relay-gossip -mod=readonly -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X main.buildDate=$(BUILD_DATE) -X main.gitCommit=$(GIT_REVISION)" ./cmd/relay-gossip
go build -o drand-relay-gossip -mod=readonly -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X main.buildDate=$(BUILD_DATE) -X main.gitCommit=$(GIT_REVISION)" ./cmd/relay-gossip
drand-relay-gossip: relay-gossip

# create the "drand-relay-s3" binary in the current folder
relay-s3:
go build -o drand-relay-s3 -mod=readonly -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X main.buildDate=$(BUILD_DATE) -X main.gitCommit=$(GIT_REVISION)" ./cmd/relay-s3
go build -o drand-relay-s3 -mod=readonly -ldflags "-X $(VER_PACKAGE).MAJOR=$(MAJOR) -X $(VER_PACKAGE).MINOR=$(MINOR) -X $(VER_PACKAGE).PATCH=$(PATCH) -X $(VER_PACKAGE).COMMIT=$(GIT_REVISION) -X $(VER_PACKAGE).BUILDDATE=$(BUILD_DATE) -X main.buildDate=$(BUILD_DATE) -X main.gitCommit=$(GIT_REVISION)" ./cmd/relay-s3
drand-relay-s3: relay-s3

build_all: drand drand-client drand-relay-http drand-relay-gossip drand-relay-s3
Expand Down
1 change: 1 addition & 0 deletions cmd/client/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ MAINTAINER Hector Sanjuan <hector@protocol.ai>

ARG version=unknown
ARG gitCommit
ARG buildDate

ENV GOPATH /go
ENV SRC_PATH $GOPATH/src/github.com/drand/drand/
Expand Down
18 changes: 15 additions & 3 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@ const (

var LoadProcess sync.Once
var version Version

// Set via -ldflags
// Example (it should all be one line):
// go install -ldflags
// "-X common.BUILDDATE=`date -u +%d/%m/%Y@%H:%M:%S`
// -X common.GITCOMMIT=`git rev-parse HEAD`
// -X common.MAJOR=1
// -X common.MINOR=2
// -X common.PATCH=3"
// See the Makefile and the Dockerfile in the root directory of the repo
var (
MAJOR = "0"
MINOR = "0"
PATCH = "0"
MAJOR = "0"
MINOR = "0"
PATCH = "0"
COMMIT = ""
BUILDDATE = ""
)

func GetAppVersion() Version {
Expand Down
44 changes: 44 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"net/http"
"runtime"
"strings"
"time"

"github.com/drand/drand/common"
"github.com/drand/drand/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -149,6 +151,20 @@ var (
[]string{"url"},
)

// DrandVersion emits the current version of the drand binary
DrandVersion = prometheus.NewUntypedFunc(prometheus.UntypedOpts{
Name: "drand_version",
Help: "Version of the drand binary, in MMMNNNPPP format",
ConstLabels: map[string]string{"build": common.COMMIT},
}, func() float64 { return float64(getVersionNum(common.GetAppVersion())) })

// DrandBuildTime emits the timestamp when the binary was built in Unix time.
DrandBuildTime = prometheus.NewUntypedFunc(prometheus.UntypedOpts{
Name: "drand_build_time",
Help: "Timestamp when the binary was built in seconds since the Epoch",
ConstLabels: map[string]string{"build": common.COMMIT},
}, func() float64 { return float64(getBuildTimestamp(common.BUILDDATE)) })

metricsBound = false
)

Expand All @@ -166,6 +182,17 @@ func bindMetrics() error {
return err
}

// Private metrics
private := []prometheus.Collector{
DrandVersion,
DrandBuildTime,
}
for _, c := range private {
if err := PrivateMetrics.Register(c); err != nil {
return err
}
}

// Group metrics
group := []prometheus.Collector{
APICallCounter,
Expand Down Expand Up @@ -312,3 +339,20 @@ func (l *lazyPeerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.URL.Path = "/metrics"
handler.ServeHTTP(w, r)
}

func getVersionNum(version common.Version) uint32 {
return version.Major*1_000_000 + version.Minor*1_000 + version.Patch
}

func getBuildTimestamp(buildDate string) int64 {
if buildDate == "" {
return 0.0
}

layout := "02/01/2006@15:04:05"
t, err := time.Parse(layout, buildDate)
if err != nil {
return 0.0
}
return t.Unix()
}
31 changes: 31 additions & 0 deletions metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"fmt"
"net/http"
"testing"
"time"

"github.com/drand/drand/common"
)

func TestMetricReshare(t *testing.T) {
Expand Down Expand Up @@ -49,3 +52,31 @@ func TestMetricReshare(t *testing.T) {
}
_ = resp.Body.Close()
}

func TestAppVersionFormat(t *testing.T) {
version := common.Version{
Major: 1,
Minor: 2,
Patch: 3,
}
expected := uint32(1002003)
actual := getVersionNum(version)
if actual != expected {
t.Fatalf("Error converting version to number. Expected %v, actual %v", expected, actual)
}
}

func TestBuildTimestamp(t *testing.T) {
buildTimestamp := "29/04/2021@20:23:35"

reference, err := time.Parse(time.RFC3339, "2021-04-29T20:23:35Z")
if err != nil {
t.Fatalf("Error parsing reference time: %s", err)
}
expected := reference.Unix()

actual := getBuildTimestamp(buildTimestamp)
if actual != expected {
t.Fatalf("Error converting build timestamp to number. Expected %v, actual %v", expected, actual)
}
}
1 change: 1 addition & 0 deletions test/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ARG major=0
ARG minor=0
ARG patch=0
ARG gitCommit
ARG buildDate

ENV GOPATH /go
ENV SRC_PATH $GOPATH/src/github.com/drand/drand/
Expand Down

0 comments on commit 22e77d2

Please sign in to comment.