-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #850 from qmhu/recommend-metrics
Monitor recommendation metrics
- Loading branch information
Showing
5 changed files
with
114 additions
and
3 deletions.
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
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,65 @@ | ||
package recommendation | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
analysisv1alpha1 "github.com/gocrane/api/analysis/v1alpha1" | ||
|
||
"github.com/gocrane/crane/pkg/metrics" | ||
) | ||
|
||
type Checker struct { | ||
client.Client | ||
MonitorInterval time.Duration | ||
OutDateInterval time.Duration | ||
} | ||
|
||
func (r Checker) Run(stopCh <-chan struct{}) { | ||
go func() { | ||
ticker := time.NewTicker(r.MonitorInterval) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-stopCh: | ||
return | ||
case <-ticker.C: | ||
r.runChecker() | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (r Checker) runChecker() { | ||
recommendList := &analysisv1alpha1.RecommendationList{} | ||
err := r.Client.List(context.TODO(), recommendList, []client.ListOption{}...) | ||
if err != nil { | ||
klog.Errorf("Failed to list recommendation: %v", err) | ||
} | ||
|
||
for _, recommend := range recommendList.Items { | ||
updateStatus := "Updated" | ||
if time.Now().Sub(recommend.Status.LastUpdateTime.Time) > r.OutDateInterval { | ||
updateStatus = "OutDate" | ||
} | ||
|
||
resultStatus := "Failed" | ||
if len(recommend.Status.RecommendedInfo) != 0 || len(recommend.Status.RecommendedValue) != 0 { | ||
resultStatus = "Success" | ||
} | ||
|
||
metrics.RecommendationsStatus.With(map[string]string{ | ||
"type": string(recommend.Spec.Type), | ||
"apiversion": recommend.Spec.TargetRef.APIVersion, | ||
"owner_kind": recommend.Spec.TargetRef.Kind, | ||
"namespace": recommend.Spec.TargetRef.Namespace, | ||
"owner_name": recommend.Spec.TargetRef.Name, | ||
"update_status": updateStatus, | ||
"result_status": resultStatus, | ||
}).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
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