Skip to content

Commit

Permalink
working version
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Oct 5, 2022
1 parent 7357d5e commit 115e2da
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 20 deletions.
4 changes: 3 additions & 1 deletion operator/api/v1alpha1/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ const PreDeploymentCheckType CheckType = "pre"
const PostDeploymentCheckType CheckType = "post"

type KeptnMeters struct {
DeploymentCount syncint64.Counter
TaskCount syncint64.Counter
TaskDuration syncfloat64.Histogram
TaskActive syncint64.UpDownCounter
DeploymentCount syncint64.Counter
DeploymentDuration syncfloat64.Histogram
DeploymentActive syncint64.UpDownCounter
}
30 changes: 30 additions & 0 deletions operator/api/v1alpha1/keptntask_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"github.com/keptn-sandbox/lifecycle-controller/operator/api/v1alpha1/common"
"go.opentelemetry.io/otel/attribute"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -96,3 +97,32 @@ func (i *KeptnTask) SetEndTime() {
i.Status.EndTime = metav1.NewTime(time.Now().UTC())
}
}

func (i *KeptnTask) IsStartTimeSet() bool {
return !i.Status.StartTime.IsZero()
}

func (i *KeptnTask) IsEndTimeSet() bool {
return !i.Status.EndTime.IsZero()
}

func (i KeptnTask) GetActiveMetricsAttributes() []attribute.KeyValue {
return []attribute.KeyValue{
attribute.Key("KeptnApp").String(i.Spec.AppName),
attribute.Key("KeptnWorkload").String(i.Spec.Workload),
attribute.Key("KeptnVersion").String(i.Spec.WorkloadVersion),
attribute.Key("TaskName").String(i.Name),
attribute.Key("Type").String(string(i.Spec.Type)),
}
}

func (i KeptnTask) GetMetricsAttributes() []attribute.KeyValue {
return []attribute.KeyValue{
attribute.Key("KeptnApp").String(i.Spec.AppName),
attribute.Key("KeptnWorkload").String(i.Spec.Workload),
attribute.Key("KeptnVersion").String(i.Spec.WorkloadVersion),
attribute.Key("TaskName").String(i.Name),
attribute.Key("Type").String(string(i.Spec.Type)),
attribute.Key("Status").String(string(i.Status.Status)),
}
}
28 changes: 28 additions & 0 deletions operator/api/v1alpha1/keptnworkloadinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"github.com/keptn-sandbox/lifecycle-controller/operator/api/v1alpha1/common"
"go.opentelemetry.io/otel/attribute"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -109,6 +110,14 @@ func (i *KeptnWorkloadInstance) SetEndTime() {
}
}

func (i *KeptnWorkloadInstance) IsStartTimeSet() bool {
return !i.Status.StartTime.IsZero()
}

func (i *KeptnWorkloadInstance) IsEndTimeSet() bool {
return !i.Status.EndTime.IsZero()
}

func (i *WorkloadTaskStatus) SetStartTime() {
if i.StartTime.IsZero() {
i.StartTime = metav1.NewTime(time.Now().UTC())
Expand All @@ -120,3 +129,22 @@ func (i *WorkloadTaskStatus) SetEndTime() {
i.EndTime = metav1.NewTime(time.Now().UTC())
}
}

func (i KeptnWorkloadInstance) GetActiveMetricsAttributes() []attribute.KeyValue {
return []attribute.KeyValue{
attribute.Key("KeptnApp").String(i.Spec.AppName),
attribute.Key("KeptnWorkload").String(i.Spec.WorkloadName),
attribute.Key("KeptnVersion").String(i.Spec.Version),
attribute.Key("Namespace").String(i.Namespace),
}
}

func (i KeptnWorkloadInstance) GetMetricsAttributes() []attribute.KeyValue {
return []attribute.KeyValue{
attribute.Key("KeptnApp").String(i.Spec.AppName),
attribute.Key("KeptnWorkload").String(i.Spec.WorkloadName),
attribute.Key("KeptnVersion").String(i.Spec.Version),
attribute.Key("Namespace").String(i.Namespace),
attribute.Key("Status").String(string(i.Status.PostDeploymentStatus)),
}
}
2 changes: 1 addition & 1 deletion operator/config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ kind: Kustomization
images:
- name: controller
newName: docker.io/odubajdt/keptn-lifecycle-operator
newTag: metrics6
newTag: metrics10
18 changes: 9 additions & 9 deletions operator/controllers/keptntask/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/go-logr/logr"
klcv1alpha1 "github.com/keptn-sandbox/lifecycle-controller/operator/api/v1alpha1"
"github.com/keptn-sandbox/lifecycle-controller/operator/api/v1alpha1/common"
"go.opentelemetry.io/otel/attribute"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -64,6 +63,10 @@ func (r *KeptnTaskReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
return ctrl.Result{Requeue: true, RequeueAfter: 30 * time.Second}, nil
}

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

err := r.Client.Status().Update(ctx, task)
Expand Down Expand Up @@ -97,21 +100,18 @@ func (r *KeptnTaskReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

// WorkloadInstance is completed at this place

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

err = r.Client.Status().Update(ctx, task)
if err != nil {
return ctrl.Result{Requeue: true}, err
}

attrs := []attribute.KeyValue{
attribute.Key("KeptnApp").String(task.Spec.AppName),
attribute.Key("KeptnWorkload").String(task.Spec.Workload),
attribute.Key("KeptnVersion").String(task.Spec.WorkloadVersion),
attribute.Key("TaskName").String(task.Name),
attribute.Key("Type").String(string(task.Spec.Type)),
attribute.Key("Status").String(string(task.Status.Status)),
}
attrs := task.GetMetricsAttributes()

r.Log.Info("Increasing task count")

Expand Down
17 changes: 9 additions & 8 deletions operator/controllers/keptnworkloadinstance/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/go-logr/logr"
"github.com/google/uuid"
"go.opentelemetry.io/otel/attribute"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -93,6 +92,10 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr
return reconcile.Result{}, fmt.Errorf("could not fetch KeptnWorkloadInstance: %+v", err)
}

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

if !workloadInstance.IsPreDeploymentCompleted() {
Expand Down Expand Up @@ -127,20 +130,18 @@ 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.SetEndTime()

err = r.Client.Status().Update(ctx, workloadInstance)
if err != nil {
return ctrl.Result{Requeue: true}, err
}

attrs := []attribute.KeyValue{
attribute.Key("KeptnApp").String(workloadInstance.Spec.AppName),
attribute.Key("KeptnWorkload").String(workloadInstance.Spec.WorkloadName),
attribute.Key("KeptnVersion").String(workloadInstance.Spec.Version),
attribute.Key("Namespace").String(workloadInstance.Namespace),
attribute.Key("Status").String(string(workloadInstance.Status.PostDeploymentStatus)),
}
attrs := workloadInstance.GetMetricsAttributes()

r.Log.Info("Increasing deployment count")

Expand Down
12 changes: 11 additions & 1 deletion operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func main() {
if err != nil {
setupLog.Error(err, "unable to start OTel")
}
deploymentActive, err := meter.SyncInt64().UpDownCounter("keptn.deployment.active", instrument.WithDescription("a simple counter of active deployments for Keptn deployment"))
if err != nil {
setupLog.Error(err, "unable to start OTel")
}
taskCount, err := meter.SyncInt64().Counter("keptn.task.count", instrument.WithDescription("a simple counter for Keptn tasks"))
if err != nil {
setupLog.Error(err, "unable to start OTel")
Expand All @@ -100,12 +104,18 @@ func main() {
if err != nil {
setupLog.Error(err, "unable to start OTel")
}
taskActive, err := meter.SyncInt64().UpDownCounter("keptn.task.active", instrument.WithDescription("a simple counter of active tasks for Keptn tasks"))
if err != nil {
setupLog.Error(err, "unable to start OTel")
}

meters := common.KeptnMeters{
DeploymentCount: deploymentCount,
TaskCount: taskCount,
TaskDuration: taskDuration,
TaskActive: taskActive,
DeploymentCount: deploymentCount,
DeploymentDuration: deploymentDuration,
DeploymentActive: deploymentActive,
}

// Start the prometheus HTTP server and pass the exporter Collector to it
Expand Down

0 comments on commit 115e2da

Please sign in to comment.