Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(operator): Use Async Gauges for active KLC Entities #206

Merged
merged 5 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions operator/api/v1alpha1/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,12 @@ const PostDeploymentEvaluationCheckType CheckType = "post-eval"
type KeptnMeters struct {
TaskCount syncint64.Counter
TaskDuration syncfloat64.Histogram
TaskActive syncint64.UpDownCounter
DeploymentCount syncint64.Counter
DeploymentDuration syncfloat64.Histogram
DeploymentActive syncint64.UpDownCounter
AppCount syncint64.Counter
AppDuration syncfloat64.Histogram
AppActive syncint64.UpDownCounter
EvaluationCount syncint64.Counter
EvaluationDuration syncfloat64.Histogram
EvaluationActive syncint64.UpDownCounter
}

const (
Expand Down Expand Up @@ -151,3 +147,8 @@ func GenerateEvaluationName(checkType CheckType, evalName string) string {
randomId := rand.Intn(99_999-10_000) + 10000
return fmt.Sprintf("%s-%s-%d", checkType, TruncateString(evalName, 27), randomId)
}

type GaugeValue struct {
Value int64
Attributes []attribute.KeyValue
}
27 changes: 23 additions & 4 deletions operator/controllers/keptnappversion/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}

if !appVersion.IsStartTimeSet() {
// metrics: increment active app counter
r.Meters.AppActive.Add(ctx, 1, appVersion.GetActiveMetricsAttributes()...)
appVersion.SetStartTime()
}

Expand Down Expand Up @@ -145,8 +143,6 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ
// AppVersion is completed at this place

if !appVersion.IsEndTimeSet() {
// metrics: decrement active app counter
r.Meters.AppActive.Add(ctx, -1, appVersion.GetActiveMetricsAttributes()...)
appVersion.Status.CurrentPhase = common.PhaseCompleted.ShortName
appVersion.SetEndTime()
}
Expand Down Expand Up @@ -207,3 +203,26 @@ func (r *KeptnAppVersionReconciler) handlePhase(ctx context.Context, appVersion
}
return ctrl.Result{Requeue: true, RequeueAfter: 5 * time.Second}, nil
}

func (r *KeptnAppVersionReconciler) GetActiveApps(ctx context.Context) ([]common.GaugeValue, error) {
appInstances := &klcv1alpha1.KeptnAppVersionList{}
err := r.List(ctx, appInstances)
if err != nil {
return nil, fmt.Errorf("could not retrieve app versions: %w", err)
}

res := []common.GaugeValue{}

for _, appInstance := range appInstances.Items {
gaugeValue := int64(0)
if !appInstance.IsEndTimeSet() {
gaugeValue = int64(1)
}
res = append(res, common.GaugeValue{
Value: gaugeValue,
Attributes: appInstance.GetActiveMetricsAttributes(),
})
}

return res, nil
}
27 changes: 23 additions & 4 deletions operator/controllers/keptnevaluation/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ func (r *KeptnEvaluationReconciler) Reconcile(ctx context.Context, req ctrl.Requ
semconv.AddAttributeFromEvaluation(span, *evaluation)

if !evaluation.IsStartTimeSet() {
// metrics: increment active evaluation counter
r.Meters.EvaluationActive.Add(ctx, 1, evaluation.GetActiveMetricsAttributes()...)
evaluation.SetStartTime()
}

Expand Down Expand Up @@ -183,8 +181,6 @@ func (r *KeptnEvaluationReconciler) updateFinishedEvaluationMetrics(ctx context.
r.recordEvent("Normal", evaluation, string(evaluation.Status.OverallStatus), "the evaluation has "+string(evaluation.Status.OverallStatus))

if !evaluation.IsEndTimeSet() {
// metrics: decrement active evaluation counter
r.Meters.EvaluationActive.Add(ctx, -1, evaluation.GetActiveMetricsAttributes()...)
evaluation.SetEndTime()
}

Expand Down Expand Up @@ -329,3 +325,26 @@ func (r *KeptnEvaluationReconciler) checkValue(objective klcv1alpha1.Objective,
func (r *KeptnEvaluationReconciler) recordEvent(eventType string, evaluation *klcv1alpha1.KeptnEvaluation, shortReason string, longReason string) {
r.Recorder.Event(evaluation, eventType, shortReason, fmt.Sprintf("%s / Namespace: %s, Name: %s, WorkloadVersion: %s ", longReason, evaluation.Namespace, evaluation.Name, evaluation.Spec.WorkloadVersion))
}

func (r *KeptnEvaluationReconciler) GetActiveEvaluations(ctx context.Context) ([]common.GaugeValue, error) {
evaluations := &klcv1alpha1.KeptnEvaluationList{}
err := r.List(ctx, evaluations)
if err != nil {
return nil, fmt.Errorf("could not retrieve workload instances: %w", err)
}

res := []common.GaugeValue{}

for _, evaluation := range evaluations.Items {
gaugeValue := int64(0)
if !evaluation.IsEndTimeSet() {
gaugeValue = int64(1)
}
res = append(res, common.GaugeValue{
Value: gaugeValue,
Attributes: evaluation.GetActiveMetricsAttributes(),
})
}

return res, nil
}
27 changes: 23 additions & 4 deletions operator/controllers/keptntask/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ func (r *KeptnTaskReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
semconv.AddAttributeFromTask(span, *task)

if !task.IsStartTimeSet() {
// metrics: increment active task counter
r.Meters.TaskActive.Add(ctx, 1, task.GetActiveMetricsAttributes()...)
task.SetStartTime()
}

Expand Down Expand Up @@ -119,8 +117,6 @@ func (r *KeptnTaskReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
// Task is completed at this place

if !task.IsEndTimeSet() {
// metrics: decrement active task counter
r.Meters.TaskActive.Add(ctx, -1, task.GetActiveMetricsAttributes()...)
task.SetEndTime()
}

Expand Down Expand Up @@ -175,3 +171,26 @@ func (r *KeptnTaskReconciler) JobExists(ctx context.Context, task klcv1alpha1.Ke

return false, nil
}

func (r *KeptnTaskReconciler) GetActiveTasks(ctx context.Context) ([]common.GaugeValue, error) {
tasks := &klcv1alpha1.KeptnTaskList{}
err := r.List(ctx, tasks)
if err != nil {
return nil, fmt.Errorf("could not retrieve workload instances: %w", err)
}

res := []common.GaugeValue{}

for _, task := range tasks.Items {
gaugeValue := int64(0)
if !task.IsEndTimeSet() {
gaugeValue = int64(1)
}
res = append(res, common.GaugeValue{
Value: gaugeValue,
Attributes: task.GetActiveMetricsAttributes(),
})
}

return res, nil
}
27 changes: 23 additions & 4 deletions operator/controllers/keptnworkloadinstance/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr
semconv.AddAttributeFromWorkloadInstance(span, *workloadInstance)

if !workloadInstance.IsStartTimeSet() {
// metrics: increment active deployment counter
r.Meters.DeploymentActive.Add(ctx, 1, workloadInstance.GetActiveMetricsAttributes()...)
workloadInstance.SetStartTime()
}

Expand Down Expand Up @@ -173,8 +171,6 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr

// WorkloadInstance is completed at this place
if !workloadInstance.IsEndTimeSet() {
// metrics: decrement active deployment counter
r.Meters.DeploymentActive.Add(ctx, -1, workloadInstance.GetActiveMetricsAttributes()...)
workloadInstance.Status.CurrentPhase = common.PhaseCompleted.ShortName
workloadInstance.SetEndTime()
}
Expand All @@ -200,6 +196,29 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr
return ctrl.Result{}, nil
}

func (r *KeptnWorkloadInstanceReconciler) GetActiveDeployments(ctx context.Context) ([]common.GaugeValue, error) {
workloadInstances := &klcv1alpha1.KeptnWorkloadInstanceList{}
err := r.List(ctx, workloadInstances)
if err != nil {
return nil, fmt.Errorf("could not retrieve workload instances: %w", err)
}

res := []common.GaugeValue{}

for _, workloadInstance := range workloadInstances.Items {
gaugeValue := int64(0)
if !workloadInstance.IsEndTimeSet() {
gaugeValue = int64(1)
}
res = append(res, common.GaugeValue{
Value: gaugeValue,
Attributes: workloadInstance.GetActiveMetricsAttributes(),
})
}

return res, nil
}

func (r *KeptnWorkloadInstanceReconciler) handlePhase(ctx context.Context, workloadInstance *klcv1alpha1.KeptnWorkloadInstance, phase common.KeptnPhaseType, span trace.Span, phaseFailed func() bool, reconcilePhase func() (common.KeptnState, error)) (ctrl.Result, error) {
r.Log.Info(phase.LongName + " not finished")
oldPhase := workloadInstance.Status.CurrentPhase
Expand Down
Loading