-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add availability metrics Signed-off-by: Maysun J Faisal <[email protected]> * Add in availability test Signed-off-by: Maysun J Faisal <[email protected]> --------- Signed-off-by: Maysun J Faisal <[email protected]>
- Loading branch information
1 parent
77ecf15
commit 9adfc1d
Showing
8 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// Copyright 2024 Red Hat, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package availability | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/redhat-appstudio/application-service/pkg/github" | ||
"github.com/redhat-appstudio/application-service/pkg/metrics" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
) | ||
|
||
// Inspired from https://github.com/konflux-ci/remote-secret/blob/main/pkg/availability/storage_watchdog.go | ||
|
||
type AvailabilityWatchdog struct { | ||
GitHubTokenClient github.GitHubToken | ||
} | ||
|
||
func (r *AvailabilityWatchdog) Start(ctx context.Context) error { | ||
// Check every 20 minutes | ||
ticker := time.NewTicker(20 * time.Minute) | ||
go func() { | ||
for { | ||
// make call immediately to avoid waiting for the first tick | ||
r.checkAvailability(ctx) | ||
select { | ||
case <-ticker.C: | ||
continue | ||
case <-ctx.Done(): | ||
ticker.Stop() | ||
return | ||
} | ||
} | ||
}() | ||
return nil | ||
} | ||
|
||
func (r *AvailabilityWatchdog) checkAvailability(ctx context.Context) { | ||
log := ctrl.LoggerFrom(ctx) | ||
checkGitLabel := prometheus.Labels{"check": "github"} | ||
|
||
ghClient, err := r.GitHubTokenClient.GetNewGitHubClient("") | ||
if err != nil { | ||
log.Error(err, "Unable to create Go-GitHub client due to error, marking HAS availability as down") | ||
metrics.HASAvailabilityGauge.With(checkGitLabel).Set(0) | ||
} | ||
|
||
isGitAvailable, err := ghClient.GetGitStatus(ctx) | ||
if !isGitAvailable { | ||
log.Error(err, "Unable to create Go-GitHub client due to error, marking HAS availability as down") | ||
metrics.HASAvailabilityGauge.With(checkGitLabel).Set(0) | ||
} else { | ||
log.Info("HAS is marked as available, Git check has passed...") | ||
metrics.HASAvailabilityGauge.With(checkGitLabel).Set(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// Copyright 2024 Red Hat, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package availability | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
"github.com/redhat-appstudio/application-service/pkg/github" | ||
"github.com/redhat-appstudio/application-service/pkg/metrics" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCheckAvailability(t *testing.T) { | ||
|
||
t.Run("check availability", func(t *testing.T) { | ||
checkGitLabel := prometheus.Labels{"check": "github"} | ||
r := AvailabilityWatchdog{ | ||
GitHubTokenClient: github.MockGitHubTokenClient{}, | ||
} | ||
|
||
r.checkAvailability(context.TODO()) | ||
|
||
assert.Equal(t, 1.0, testutil.ToFloat64(metrics.HASAvailabilityGauge.With(checkGitLabel))) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters