Skip to content

Commit

Permalink
refactor(metrics): merge ack_waiting into latest_version_is_deployed
Browse files Browse the repository at this point in the history
0=no, 1=yes, 2=approved, 3=skipped
  • Loading branch information
JosephKav committed Jul 19, 2023
1 parent 104d50d commit 7c5bc60
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 89 deletions.
10 changes: 0 additions & 10 deletions commands/init.go
Expand Up @@ -80,13 +80,6 @@ func (c *Controller) InitMetrics() {
"",
"FAIL")
}

// ##########
// # Gauges #
// ##########
metric.SetPrometheusGauge(metric.AckWaiting,
*c.ServiceStatus.ServiceID,
float64(0))
}

// DeleteMetrics for this Controller.
Expand All @@ -108,9 +101,6 @@ func (c *Controller) DeleteMetrics() {
"",
"FAIL")
}

metric.DeletePrometheusGauge(metric.AckWaiting,
*c.ServiceStatus.ServiceID)
}

// FormattedString will convert Command to a string in the format of '[ "arg0", "arg1" ]'
Expand Down
8 changes: 4 additions & 4 deletions commands/init_test.go
Expand Up @@ -318,7 +318,7 @@ func TestController_Metrics(t *testing.T) {

// WHEN the Prometheus metrics are initialised with initMetrics
hadC := testutil.CollectAndCount(metric.CommandMetric)
hadG := testutil.CollectAndCount(metric.AckWaiting)
hadG := testutil.CollectAndCount(metric.LatestVersionIsDeployed)
controller.InitMetrics()

// THEN it can be collected
Expand All @@ -330,8 +330,8 @@ func TestController_Metrics(t *testing.T) {
(gotC - hadC), wantC)
}
// gauges
gotG := testutil.CollectAndCount(metric.AckWaiting)
wantG := 1
gotG := testutil.CollectAndCount(metric.LatestVersionIsDeployed)
wantG := 0
if (gotG - hadG) != wantG {
t.Errorf("%d Gauge metrics's were initialised, expecting %d",
(gotG - hadG), wantG)
Expand All @@ -346,7 +346,7 @@ func TestController_Metrics(t *testing.T) {
gotC, hadC)
}
// gauges
gotG = testutil.CollectAndCount(metric.AckWaiting)
gotG = testutil.CollectAndCount(metric.LatestVersionIsDeployed)
if gotG != hadG {
t.Errorf("Gauge metrics's were deleted, got %d. expecting %d",
gotG, hadG)
Expand Down
4 changes: 0 additions & 4 deletions service/handlers.go
Expand Up @@ -19,7 +19,6 @@ import (
"time"

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

// UpdatedVersion will register the version change, setting `s.Status.DeployedVersion`
Expand Down Expand Up @@ -100,9 +99,6 @@ func (s *Service) HandleUpdateActions(writeToDB bool) {
} else {
jLog.Info("Waiting for approval on the Web UI", util.LogFrom{Primary: s.ID}, true)

metric.SetPrometheusGauge(metric.AckWaiting,
s.ID,
1)
s.Status.AnnounceQueryNewVersion()
}
} else {
Expand Down
1 change: 1 addition & 0 deletions service/init.go
Expand Up @@ -168,6 +168,7 @@ func (s *Service) DeleteMetrics() {
s.Notify.DeleteMetrics()
s.CommandController.DeleteMetrics()
s.WebHook.DeleteMetrics()
s.Status.DeleteMetrics()
}

// ResetMetrics of the Service.
Expand Down
42 changes: 40 additions & 2 deletions service/status/status.go
Expand Up @@ -186,7 +186,22 @@ func (s *Status) ApprovedVersion() string {
// SetApprovedVersion.
func (s *Status) SetApprovedVersion(version string, writeToDB bool) {
s.mutex.Lock()
s.approvedVersion = version
{
s.approvedVersion = version

// Update metrics if we're acting on the latest version
if strings.HasSuffix(s.approvedVersion, s.latestVersion) {
value := float64(3) // Skipping latest version
if s.approvedVersion == s.latestVersion {
value = 2 // Approving latest version
}
if s.ServiceID != nil {
metric.SetPrometheusGauge(metric.LatestVersionIsDeployed,
*s.ServiceID,
value)
}
}
}
s.mutex.Unlock()

if writeToDB {
Expand Down Expand Up @@ -402,9 +417,32 @@ func (s *Status) GetWebURL() string {

// setLatestVersionIsDeployedMetric will set the metric for whether the latest version is currently deployed.
func (s *Status) setLatestVersionIsDeployedMetric() {
if s.ServiceID == nil {
return
}

value := float64(0) // Not deployed
if s.latestVersion == s.deployedVersion {
value = 1 // Is deployed

// Latest version isn't deployed, but has been approved/skipped, so carry that over
} else if strings.HasSuffix(s.approvedVersion, s.latestVersion) {
value = 3 // Latest version was skipped
if s.approvedVersion == s.latestVersion {
value = 2 // Latest version was approved
}
}
metric.SetPrometheusGauge(metric.LatestVersionIsDeployed,
*s.ServiceID,
value)
}

// DeleteMetrics of the Status.
func (s *Status) DeleteMetrics() {
if s == nil || s.ServiceID == nil {
return
}
metric.SetPrometheusGauge(metric.LatestVersionIsDeployed, *s.ServiceID, value)

metric.DeletePrometheusGauge(metric.LatestVersionIsDeployed,
*s.ServiceID)
}
164 changes: 117 additions & 47 deletions service/status/status_test.go
Expand Up @@ -182,53 +182,83 @@ func TestStatus_SetLastQueried(t *testing.T) {
}

func TestStatus_ApprovedVersion(t *testing.T) {
// GIVEN a Status
approvedVersion := "0.0.2"
deployedVersion := "0.0.1"
latestVersion := "0.0.3"
announceChannel := make(chan []byte, 4)
databaseChannel := make(chan dbtype.Message, 4)
status := New(
&announceChannel, &databaseChannel, nil,
"", "", "", "", "", "")
status.Init(
0, 0, 0,
stringPtr("TestStatus_SetApprovedVersion"),
stringPtr("https://example.com"))
status.SetLatestVersion(latestVersion, false)
status.SetDeployedVersion(deployedVersion, false)

// WHEN SetApprovedVersion is called
status.SetApprovedVersion(approvedVersion, true)

// THEN the Status is as expected
// ApprovedVersion
got := status.ApprovedVersion()
if got != approvedVersion {
t.Errorf("ApprovedVersion not set to %s. Got %s",
approvedVersion, got)
}
// LatestVersion
got = status.LatestVersion()
if got != latestVersion {
t.Errorf("LatestVersion not set to %s. Got %s",
latestVersion, got)
}
// DeployedVersion
got = status.DeployedVersion()
if got != deployedVersion {
t.Errorf("DeployedVersion not set to %s. Got %s",
deployedVersion, got)
}
// AnnounceChannel
if len(*status.AnnounceChannel) != 1 {
t.Errorf("AnnounceChannel should have 1 message, but has %d",
len(*status.AnnounceChannel))
}
// DatabaseChannel
if len(*status.DatabaseChannel) != 1 {
t.Errorf("DatabaseChannel should have 1 message, but has %d",
len(*status.DatabaseChannel))
// GIVEN a Status
tests := map[string]struct {
approving string
latestVersionIsDeployedMetric float64
}{
"Approving LatestVersion": {
approving: latestVersion,
latestVersionIsDeployedMetric: 2,
},
"Skipping LatestVersion": {
approving: "SKIP_" + latestVersion,
latestVersionIsDeployedMetric: 3,
},
"Approving non-latest version": {
approving: "0.0.2a",
latestVersionIsDeployedMetric: 0,
},
}

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

announceChannel := make(chan []byte, 4)
databaseChannel := make(chan dbtype.Message, 4)
status := New(
&announceChannel, &databaseChannel, nil,
"", "", "", "", "", "")
status.Init(
0, 0, 0,
stringPtr("TestStatus_SetApprovedVersion_"+name),
stringPtr("https://example.com"))
status.SetLatestVersion(latestVersion, false)
status.SetDeployedVersion(deployedVersion, false)

// WHEN SetApprovedVersion is called
status.SetApprovedVersion(tc.approving, true)

// THEN the Status is as expected
// ApprovedVersion
got := status.ApprovedVersion()
if got != tc.approving {
t.Errorf("ApprovedVersion not set to %s. Got %s",
tc.approving, got)
}
// LatestVersion
got = status.LatestVersion()
if got != latestVersion {
t.Errorf("LatestVersion not set to %s. Got %s",
latestVersion, got)
}
// DeployedVersion
got = status.DeployedVersion()
if got != deployedVersion {
t.Errorf("DeployedVersion not set to %s. Got %s",
deployedVersion, got)
}
// AnnounceChannel
if len(*status.AnnounceChannel) != 1 {
t.Errorf("AnnounceChannel should have 1 message, but has %d",
len(*status.AnnounceChannel))
}
// DatabaseChannel
if len(*status.DatabaseChannel) != 1 {
t.Errorf("DatabaseChannel should have 1 message, but has %d",
len(*status.DatabaseChannel))
}
// AND LatestVersionIsDeployedVersion metric is updated
gotMetric := testutil.ToFloat64(metric.LatestVersionIsDeployed.WithLabelValues(*status.ServiceID))
if gotMetric != tc.latestVersionIsDeployedMetric {
t.Errorf("LatestVersionIsDeployedVersion metric should be %f, not %f",
tc.latestVersionIsDeployedMetric, gotMetric)
}
})
}
}

Expand Down Expand Up @@ -799,7 +829,7 @@ shoutrrr: {bash: false, bish: nil, bosh: true},
for name, tc := range tests {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
// t.Parallel()
t.Parallel()

tc.status.SetApprovedVersion(tc.approvedVersion, false)
tc.status.SetDeployedVersion(tc.deployedVersion, false)
Expand Down Expand Up @@ -848,7 +878,7 @@ shoutrrr: {bash: false, bish: nil, bosh: true},
}
}

func TestSetLatestVersionIsDeployedMetric(t *testing.T) {
func TestStatus_SetLatestVersionIsDeployedMetric(t *testing.T) {
// GIVEN a Status
tests := map[string]struct {
latestVersion string
Expand Down Expand Up @@ -892,3 +922,43 @@ func TestSetLatestVersionIsDeployedMetric(t *testing.T) {
})
}
}

func TestStatus_DeleteMetrics(t *testing.T) {
// GIVEN a Status
tests := map[string]struct {
serviceID *string
}{
"nil serviceID": {
serviceID: nil,
},
"non-nil serviceID": {
serviceID: stringPtr("TestStatus_DeleteMetrics"),
},
}

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,
tc.serviceID,
stringPtr("http://example.com"))

// WHEN DeleteMetrics is called on it
status.DeleteMetrics()

// THEN the metrics are deleted
got := float64(0)
if tc.serviceID != nil {
got = testutil.ToFloat64(metric.LatestVersionIsDeployed.WithLabelValues(*status.ServiceID))
}
if got != 0 {
t.Errorf("Expected LatestVersionIsDeployed to be 0, not %f",
got)
}
})
}
}
6 changes: 3 additions & 3 deletions web/api/v1/http-api-edit_test.go
Expand Up @@ -94,7 +94,7 @@ func TestHTTP_VersionRefreshUncreated(t *testing.T) {
"url": "https://valid.release-argus.io/plain",
"regex": `stable version: "v?([0-9.+)"`,
},
wantBody: `"error":"values failed validity check:.*Invalid RegEx`,
wantBody: `"error":"values failed validity check:.*regex: .*invalid`,
wantStatusCode: http.StatusBadRequest,
},
}
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestHTTP_VersionRefresh(t *testing.T) {
params: map[string]string{
"url_commands": `[{"type":"regex","regex":"beta: \"v?([0-9.+-beta)\""}]`,
"semantic_versioning": "false"},
wantBody: `{.*"error":".*Invalid RegEx`,
wantBody: `{.*"error":".*regex: .*invalid`,
wantStatusCode: http.StatusOK,
wantLatestVersion: "",
},
Expand Down Expand Up @@ -237,7 +237,7 @@ func TestHTTP_VersionRefresh(t *testing.T) {
deployedVersion: true,
params: map[string]string{
"regex": "v?([0-9.+)"},
wantBody: `\{.*"error":".*Invalid RegEx`,
wantBody: `\{.*"error":".*regex: .*invalid`,
wantStatusCode: http.StatusOK,
wantLatestVersion: "",
},
Expand Down

0 comments on commit 7c5bc60

Please sign in to comment.