From 8f2869db973caf762b6e184b39ab7ed679566761 Mon Sep 17 00:00:00 2001 From: Matthew Pound Date: Wed, 11 May 2022 10:38:10 -0600 Subject: [PATCH] Add optional dockerTag to commit metric --- reportsha/reportsha.go | 15 +++++++++++---- reportsha/reportsha_test.go | 2 ++ sfxclient/sfxclient_test.go | 2 -- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/reportsha/reportsha.go b/reportsha/reportsha.go index 60ae520..9cd9f49 100644 --- a/reportsha/reportsha.go +++ b/reportsha/reportsha.go @@ -5,6 +5,7 @@ import ( "expvar" "fmt" "io/ioutil" + "os" "sync" "github.com/signalfx/golib/v3/datapoint" @@ -19,6 +20,7 @@ type SHA1Reporter struct { FileName string Logger log.Logger Fi fileInfo + Tag string oc sync.Once } @@ -42,8 +44,9 @@ func load(fileName string) (fileInfo, error) { return fi, nil } -func (s *SHA1Reporter) loadFileInfo() { +func (s *SHA1Reporter) loadInfo() { s.oc.Do(func() { + s.Tag = os.Getenv("DOCKER_TAG") fi, err := load(s.FileName) if err != nil { s.Logger.Log(log.Err, err, logkey.Name, s.FileName, "Cannot load file info!") @@ -56,7 +59,7 @@ func (s *SHA1Reporter) loadFileInfo() { // Var returns an expvar that is the build file info func (s *SHA1Reporter) Var() expvar.Var { - s.loadFileInfo() + s.loadInfo() return expvar.Func(func() interface{} { return s.Fi }) @@ -64,8 +67,12 @@ func (s *SHA1Reporter) Var() expvar.Var { // Datapoints returns a single datapoint that includes the commit sha loaded from a config file func (s *SHA1Reporter) Datapoints() []*datapoint.Datapoint { - s.loadFileInfo() + s.loadInfo() + dims := map[string]string{"commit": s.Fi.Commit} + if len(s.Tag) > 0 { + dims["dockerTag"] = s.Tag + } return []*datapoint.Datapoint{ - sfxclient.Gauge("fileinfo_commit", map[string]string{"commit": s.Fi.Commit}, int64(1)), + sfxclient.Gauge("fileinfo_commit", dims, int64(1)), } } diff --git a/reportsha/reportsha_test.go b/reportsha/reportsha_test.go index e89286f..97fc56a 100644 --- a/reportsha/reportsha_test.go +++ b/reportsha/reportsha_test.go @@ -28,8 +28,10 @@ func TestReportSha(t *testing.T) { } Convey("Commit should load ok", func() { + os.Setenv("DOCKER_TAG", "foo") dps := reporter.Datapoints() So(dps[0].Dimensions["commit"], ShouldEqual, "b70a843b07741e04ce6845aaefdbcb077787b4a3") + So(dps[0].Dimensions["dockerTag"], ShouldEqual, "foo") So(reporter.Var().String(), ShouldContainSubstring, "b70a843b07741e04ce6845aaefdbcb077787b4a3") }) diff --git a/sfxclient/sfxclient_test.go b/sfxclient/sfxclient_test.go index d82b361..2d78d9b 100644 --- a/sfxclient/sfxclient_test.go +++ b/sfxclient/sfxclient_test.go @@ -125,11 +125,9 @@ func TestScheduler_ReportingTimeout(t *testing.T) { func TestCollectDatapointDebug(t *testing.T) { Convey("testing collect datapoints with debug mode enabled", t, func() { - var handleErrors []error var handleErrRet error s := &Scheduler{ ErrorHandler: func(e error) error { - handleErrors = append(handleErrors, e) return errors.Wrap(handleErrRet, e) }, ReportingDelayNs: time.Second.Nanoseconds(),