Skip to content

Commit

Permalink
feat(metrics): add, latest_version_is_deployed
Browse files Browse the repository at this point in the history
also fix ack_waiting never being changed when ack'd

closes #293
  • Loading branch information
JosephKav committed Jul 19, 2023
1 parent 0e0bdb1 commit 104d50d
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 7 deletions.
12 changes: 12 additions & 0 deletions service/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

dbtype "github.com/release-argus/Argus/db/types"
"github.com/release-argus/Argus/util"
metric "github.com/release-argus/Argus/web/metrics"
)

// statusBase is the base struct for the Status struct.
Expand Down Expand Up @@ -217,6 +218,7 @@ func (s *Status) SetDeployedVersion(version string, writeToDB bool) {
if version == s.approvedVersion {
s.approvedVersion = ""
}
s.setLatestVersionIsDeployedMetric()
}
s.mutex.Unlock()

Expand Down Expand Up @@ -262,6 +264,7 @@ func (s *Status) SetLatestVersion(version string, writeToDB bool) {
{
s.latestVersion = version
s.latestVersionTimestamp = s.lastQueried
s.setLatestVersionIsDeployedMetric()
}
s.mutex.Unlock()

Expand Down Expand Up @@ -396,3 +399,12 @@ func (s *Status) GetWebURL() string {
*s.WebURL,
util.ServiceInfo{LatestVersion: s.LatestVersion()})
}

// setLatestVersionIsDeployedMetric will set the metric for whether the latest version is currently deployed.
func (s *Status) setLatestVersionIsDeployedMetric() {
value := float64(0) // Not deployed
if s.latestVersion == s.deployedVersion {
value = 1 // Is deployed
}
metric.SetPrometheusGauge(metric.LatestVersionIsDeployed, *s.ServiceID, value)
}
99 changes: 92 additions & 7 deletions service/status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
"testing"
"time"

"github.com/prometheus/client_golang/prometheus/testutil"
dbtype "github.com/release-argus/Argus/db/types"
metric "github.com/release-argus/Argus/web/metrics"
)

func TestStatus_Init(t *testing.T) {
Expand Down Expand Up @@ -245,12 +247,20 @@ func TestStatus_DeployedVersion(t *testing.T) {
deploying: approvedVersion,
approvedVersion: "",
deployedVersion: approvedVersion,
latestVersion: latestVersion},
latestVersion: latestVersion,
},
"Deploying unknown Version - DeployedVersion becomes this version": {
deploying: "0.0.4",
approvedVersion: approvedVersion,
deployedVersion: "0.0.4",
latestVersion: latestVersion},
latestVersion: latestVersion,
},
"Deploying LatestVersion - DeployedVersion becomes this version": {
deploying: latestVersion,
approvedVersion: approvedVersion,
deployedVersion: latestVersion,
latestVersion: latestVersion,
},
}

for name, tc := range tests {
Expand All @@ -264,7 +274,7 @@ func TestStatus_DeployedVersion(t *testing.T) {
"", "", "", "", "", "")
status.Init(
0, 0, 0,
stringPtr("test-service"),
&name,
stringPtr("http://example.com"))
status.SetApprovedVersion(approvedVersion, false)
status.SetDeployedVersion(deployedVersion, false)
Expand All @@ -286,13 +296,23 @@ func TestStatus_DeployedVersion(t *testing.T) {
t.Errorf("Expected LatestVersion to be set to %q, not %q",
tc.latestVersion, status.LatestVersion())
}
// and the current time
// AND the DeployedVersionTimestamp is set to current time
d, _ := time.Parse(time.RFC3339, status.DeployedVersionTimestamp())
since := time.Since(d)
if since > time.Second {
t.Errorf("DeployedVersionTimestamp was %v ago, not recent enough!",
since)
}
// AND the LatestVersionIsDeployedVersion metric is updated
got := testutil.ToFloat64(metric.LatestVersionIsDeployed.WithLabelValues(name))
want := float64(0)
if status.LatestVersion() == status.DeployedVersion() {
want = 1
}
if got != want {
t.Errorf("LatestVersionIsDeployedVersion metric should be %f, not %f",
want, got)
}
})
}
}
Expand All @@ -312,7 +332,14 @@ func TestStatus_LatestVersion(t *testing.T) {
deploying: "0.0.4",
approvedVersion: approvedVersion,
deployedVersion: deployedVersion,
latestVersion: "0.0.4"},
latestVersion: "0.0.4",
},
"Set LatestVersion to DeployedVersion": {
deploying: deployedVersion,
approvedVersion: approvedVersion,
deployedVersion: deployedVersion,
latestVersion: deployedVersion,
},
}

for name, tc := range tests {
Expand All @@ -324,7 +351,10 @@ func TestStatus_LatestVersion(t *testing.T) {
status := New(
nil, &dbChannel, nil,
"", "", "", "", "", "")
status.ServiceID = stringPtr("test-service")
status.Init(
0, 0, 0,
&name,
stringPtr("http://example.com"))
status.SetApprovedVersion(approvedVersion, false)
status.SetDeployedVersion(deployedVersion, false)
status.SetLatestVersion(latestVersion, false)
Expand All @@ -345,11 +375,21 @@ func TestStatus_LatestVersion(t *testing.T) {
t.Errorf("Expected ApprovedVersion to be set to %q, not %q",
tc.approvedVersion, status.ApprovedVersion())
}
// and the current time
// AND the LatestVersionTimestamp is set to the current time
if status.LatestVersionTimestamp() != status.LastQueried() {
t.Errorf("LatestVersionTimestamp should've been set to LastQueried \n%q, not \n%q",
status.LastQueried(), status.LatestVersionTimestamp())
}
// AND the LatestVersionIsDeployedVersion metric is updated
got := testutil.ToFloat64(metric.LatestVersionIsDeployed.WithLabelValues(name))
want := float64(0)
if status.LatestVersion() == status.DeployedVersion() {
want = 1
}
if got != want {
t.Errorf("LatestVersionIsDeployedVersion metric should be %f, not %f",
want, got)
}
})
}
}
Expand Down Expand Up @@ -807,3 +847,48 @@ shoutrrr: {bash: false, bish: nil, bosh: true},
})
}
}

func TestSetLatestVersionIsDeployedMetric(t *testing.T) {
// GIVEN a Status
tests := map[string]struct {
latestVersion string
deployedVersion string
want float64
}{
"latest version is deployed": {
latestVersion: "1.2.3",
deployedVersion: "1.2.3",
want: 1,
},
"latest version is not deployed": {
latestVersion: "1.2.3",
deployedVersion: "1.2.4",
want: 0,
},
}

for name, tc := range tests {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()

status := Status{}
status.Init(
0, 0, 0,
&name,
stringPtr("http://example.com"))
status.SetLatestVersion(tc.latestVersion, false)
status.SetDeployedVersion(tc.deployedVersion, false)

// WHEN setLatestVersion is called on it
status.setLatestVersionIsDeployedMetric()
got := testutil.ToFloat64(metric.LatestVersionIsDeployed.WithLabelValues(name))

// THEN the metric is as expected
if got != tc.want {
t.Errorf("Expected SetLatestVersionIsDeployedMetric to be %f, not %f",
tc.want, got)
}
})
}
}
6 changes: 6 additions & 0 deletions web/metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ var (
[]string{
"id",
})
LatestVersionIsDeployed = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "latest_version_is_deployed",
Help: "Whether this service's latest version is the same as its deployed version (0=no, 1=yes)."},
[]string{
"id",
})
AckWaiting = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "ack_waiting",
Help: "Whether a new release is waiting to be acknowledged (skipped/approved; 0=no, 1=yes)."},
Expand Down

0 comments on commit 104d50d

Please sign in to comment.