Skip to content

Commit

Permalink
Merge pull request #346 from qmhu/rec-replicas-metrics
Browse files Browse the repository at this point in the history
feat: add metric for replicas recommendation
  • Loading branch information
qmhu authored Jun 6, 2022
2 parents 744d204 + e0faafa commit 4ba0b2c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pkg/metrics/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@ var (
},
[]string{"apiversion", "owner_kind", "namespace", "owner_name", "container", "resource"},
)

ReplicasRecommendation = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "crane",
Subsystem: "analysis",
Name: "replicas_recommendation",
Help: "The workload's replicas recommended value",
},
[]string{"apiversion", "owner_kind", "namespace", "owner_name"},
)
)

func init() {
metrics.Registry.MustRegister(ResourceRecommendation)
metrics.Registry.MustRegister(ResourceRecommendation, ReplicasRecommendation)
}
13 changes: 13 additions & 0 deletions pkg/recommend/advisor/replicas.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/gocrane/crane/pkg/common"
"github.com/gocrane/crane/pkg/metricnaming"
"github.com/gocrane/crane/pkg/metricquery"
"github.com/gocrane/crane/pkg/metrics"
"github.com/gocrane/crane/pkg/prediction/config"
"github.com/gocrane/crane/pkg/recommend/types"
"github.com/gocrane/crane/pkg/utils"
Expand Down Expand Up @@ -197,6 +198,8 @@ func (a *ReplicasAdvisor) Advise(proposed *types.ProposedRecommendation) error {
Replicas: &minReplicas,
}

a.recordReplicasRecommendation(minReplicas)

proposed.EffectiveHPA = proposedEHPA
proposed.ReplicasRecommendation = replicasRecommendation
return nil
Expand Down Expand Up @@ -391,6 +394,16 @@ func (a *ReplicasAdvisor) proposeMaxReplicas(percentileCpu float64, targetUtiliz
return maxReplicas, nil
}

func (a *ReplicasAdvisor) recordReplicasRecommendation(replicas int32) {
labels := map[string]string{
"apiversion": a.Recommendation.Spec.TargetRef.APIVersion,
"owner_kind": a.Recommendation.Spec.TargetRef.Kind,
"namespace": a.Recommendation.Spec.TargetRef.Namespace,
"owner_name": a.Recommendation.Spec.TargetRef.Name,
}
metrics.ReplicasRecommendation.With(labels).Set(float64(replicas))
}

func getPredictionCpuConfig() *config.Config {
return &config.Config{
DSP: &predictionapi.DSP{
Expand Down

0 comments on commit 4ba0b2c

Please sign in to comment.