Skip to content

Commit

Permalink
Send version of bpfink as metric and in each log entry (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramyahasini authored Aug 3, 2020
1 parent 73f4fcd commit 5743b55
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
BINARY = bpfink
LD_FLAGS ?= -w -s -X main.BuildDate=$(shell date +%F)
LD_FLAGS ?= -w -s -X main.BuildDate=$(shell date +%F) -X main.Version=$(BINARY_VERSION)
PREFIX ?= /usr
BINARY_VERSION ?=

all: build
build: $(BINARY)
Expand Down
6 changes: 6 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
// nolint:gochecknoglobals
var (
BuildDate = "(development)"
Version string
MetricsInitialised struct {
metrics *pkg.Metrics
err error
Expand Down Expand Up @@ -82,7 +83,10 @@ const (

// LogHook to send a graphite metric for each log entry
func (h LogHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
// Send log type metric
h.metric.RecordByLogTypes(level.String())
// Send version in each log entry
e.Str("version", Version)
}

func (c Configuration) logger() (logger zerolog.Logger) {
Expand Down Expand Up @@ -426,6 +430,8 @@ func run() error {

// increment the host count by 1
metrics.RecordByInstalledHost()
// send version metric
metrics.RecordVersion(Version)
err = metrics.RecordBPFMetrics()
if err != nil {
logger.Fatal().Err(err).Msg("error starting bpf metrics")
Expand Down
12 changes: 12 additions & 0 deletions pkg/graphite.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ func (m *Metrics) RecordByEventsCaught() {
goMetrics.GetOrRegisterCounter(metricName, m.EveryMinuteRegister).Inc(1)
}

// RecordVersion graphite metric to show the version of bpfink running on each host
func (m *Metrics) RecordVersion(version string) {
// If rolename is not empty, override the defaultRolename
if m.RoleName != "" {
defaultRolename = m.RoleName
}

versionInt, _ := strconv.ParseInt(strings.Replace(version, ".", "", -1), 10, 64)
metricName := fmt.Sprintf("installed.by_role.%s.%s.version.hourly", quote(defaultRolename), quote(m.Hostname))
goMetrics.GetOrRegisterGauge(metricName, m.EveryHourRegister).Update(versionInt)
}

// RecordByInstalledHost graphite metric to show how manay host have bpfink installed
func (m *Metrics) RecordByInstalledHost() {
// If rolename is not empty, override the defaultRolename
Expand Down
10 changes: 10 additions & 0 deletions pkg/graphite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ func TestEventsCaughtMetric(t *testing.T) {
})
}

func TestVersionMetric(t *testing.T) {
m := InitMetrics()
defer m.EveryHourRegister.UnregisterAll()
m.RecordVersion("0.1.12")

testIfMetricsAreExpected(t, m.EveryHourRegister, map[string]float64{
"security.piv.bpfink.installed.by_role.unknown_role.test_host.version.hourly": 112,
})
}

func testIfMetricsAreExpected(t *testing.T, registry goMetrics.Registry, expectedMetrics map[string]float64) {
actualMetrics := registry.GetAll()
if len(expectedMetrics) != len(actualMetrics) {
Expand Down

0 comments on commit 5743b55

Please sign in to comment.