Skip to content

Commit

Permalink
feat(operator): Added metrics (#55)
Browse files Browse the repository at this point in the history
Signed-off-by: Giovanni Liva <giovanni.liva@dynatrace.com>
Signed-off-by: RealAnna <anna.reale@dynatrace.com>
Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com>
  • Loading branch information
RealAnna committed Oct 6, 2022
1 parent 5a2ef61 commit f8a3cee
Show file tree
Hide file tree
Showing 19 changed files with 407 additions and 60 deletions.
24 changes: 24 additions & 0 deletions operator/api/v1alpha1/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package common

import (
"fmt"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/instrument/syncfloat64"
"go.opentelemetry.io/otel/metric/instrument/syncint64"
)

const WorkloadAnnotation = "keptn.sh/workload"
Expand Down Expand Up @@ -45,3 +49,23 @@ type CheckType string

const PreDeploymentCheckType CheckType = "pre"
const PostDeploymentCheckType CheckType = "post"

type KeptnMeters struct {
TaskCount syncint64.Counter
TaskDuration syncfloat64.Histogram
TaskActive syncint64.UpDownCounter
DeploymentCount syncint64.Counter
DeploymentDuration syncfloat64.Histogram
DeploymentActive syncint64.UpDownCounter
}

const (
ApplicationName attribute.Key = attribute.Key("keptn.deployment.app_name")
Workload attribute.Key = attribute.Key("keptn.deployment.workload")
Version attribute.Key = attribute.Key("keptn.deployment.version")
Namespace attribute.Key = attribute.Key("keptn.deployment.namespace")
DeploymentStatus attribute.Key = attribute.Key("keptn.deployment.status")
TaskStatus attribute.Key = attribute.Key("keptn.deployment.task.status")
TaskName attribute.Key = attribute.Key("keptn.deployment.task.name")
TaskType attribute.Key = attribute.Key("keptn.deployment.taks.type")
)
50 changes: 48 additions & 2 deletions operator/api/v1alpha1/keptntask_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ limitations under the License.
package v1alpha1

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 @@ -45,8 +48,10 @@ type SecureParameters struct {

// KeptnTaskStatus defines the observed state of KeptnTask
type KeptnTaskStatus struct {
JobName string `json:"jobName,omitempty"`
Status common.KeptnState `json:"status,omitempty"`
JobName string `json:"jobName,omitempty"`
Status common.KeptnState `json:"status,omitempty"`
StartTime metav1.Time `json:"startTime,omitempty"`
EndTime metav1.Time `json:"endTime,omitempty"`
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}
Expand Down Expand Up @@ -80,3 +85,44 @@ type KeptnTaskList struct {
func init() {
SchemeBuilder.Register(&KeptnTask{}, &KeptnTaskList{})
}

func (i *KeptnTask) SetStartTime() {
if i.Status.StartTime.IsZero() {
i.Status.StartTime = metav1.NewTime(time.Now().UTC())
}
}

func (i *KeptnTask) SetEndTime() {
if i.Status.EndTime.IsZero() {
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{
common.ApplicationName.String(i.Spec.AppName),
common.Workload.String(i.Spec.Workload),
common.Version.String(i.Spec.WorkloadVersion),
common.TaskName.String(i.Name),
common.TaskType.String(string(i.Spec.Type)),
}
}

func (i KeptnTask) GetMetricsAttributes() []attribute.KeyValue {
return []attribute.KeyValue{
common.ApplicationName.String(i.Spec.AppName),
common.Workload.String(i.Spec.Workload),
common.Version.String(i.Spec.WorkloadVersion),
common.TaskName.String(i.Name),
common.TaskType.String(string(i.Spec.Type)),
common.TaskStatus.String(string(i.Status.Status)),
}
}
58 changes: 58 additions & 0 deletions operator/api/v1alpha1/keptnworkloadinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ limitations under the License.
package v1alpha1

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 All @@ -40,12 +43,16 @@ type KeptnWorkloadInstanceStatus struct {
PostDeploymentStatus common.KeptnState `json:"postDeploymentStatus,omitempty"`
PreDeploymentTaskStatus []WorkloadTaskStatus `json:"preDeploymentTaskStatus,omitempty"`
PostDeploymentTaskStatus []WorkloadTaskStatus `json:"postDeploymentTaskStatus,omitempty"`
StartTime metav1.Time `json:"startTime,omitempty"`
EndTime metav1.Time `json:"endTime,omitempty"`
}

type WorkloadTaskStatus struct {
TaskDefinitionName string `json:"TaskDefinitionName,omitempty"`
Status common.KeptnState `json:"status,omitempty"`
TaskName string `json:"taskName,omitempty"`
StartTime metav1.Time `json:"startTime,omitempty"`
EndTime metav1.Time `json:"endTime,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down Expand Up @@ -90,3 +97,54 @@ func (i KeptnWorkloadInstance) IsPostDeploymentCompleted() bool {
func (i KeptnWorkloadInstance) IsDeploymentCompleted() bool {
return i.Status.DeploymentStatus.IsCompleted()
}

func (i *KeptnWorkloadInstance) SetStartTime() {
if i.Status.StartTime.IsZero() {
i.Status.StartTime = metav1.NewTime(time.Now().UTC())
}
}

func (i *KeptnWorkloadInstance) SetEndTime() {
if i.Status.EndTime.IsZero() {
i.Status.EndTime = metav1.NewTime(time.Now().UTC())
}
}

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())
}
}

func (i *WorkloadTaskStatus) SetEndTime() {
if i.EndTime.IsZero() {
i.EndTime = metav1.NewTime(time.Now().UTC())
}
}

func (i KeptnWorkloadInstance) GetActiveMetricsAttributes() []attribute.KeyValue {
return []attribute.KeyValue{
common.ApplicationName.String(i.Spec.AppName),
common.Workload.String(i.Spec.WorkloadName),
common.Version.String(i.Spec.Version),
common.Namespace.String(i.Namespace),
}
}

func (i KeptnWorkloadInstance) GetMetricsAttributes() []attribute.KeyValue {
return []attribute.KeyValue{
common.ApplicationName.String(i.Spec.AppName),
common.Workload.String(i.Spec.WorkloadName),
common.Version.String(i.Spec.Version),
common.Namespace.String(i.Namespace),
common.DeploymentStatus.String(string(i.Status.PostDeploymentStatus)),
}
}
19 changes: 19 additions & 0 deletions operator/api/v1alpha1/semconv/semconv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package semconv

import (
"github.com/keptn-sandbox/lifecycle-controller/operator/api/v1alpha1"
"github.com/keptn-sandbox/lifecycle-controller/operator/api/v1alpha1/common"
"go.opentelemetry.io/otel/trace"
)

func AddAttributeFromWorkload(s trace.Span, w v1alpha1.KeptnWorkload) {
s.SetAttributes(common.ApplicationName.String(w.Spec.AppName))
s.SetAttributes(common.Workload.String(w.Name))
s.SetAttributes(common.Version.String(w.Spec.Version))
}

func AddAttributeFromAnnotations(s trace.Span, annotations map[string]string) {
s.SetAttributes(common.ApplicationName.String(annotations[common.AppAnnotation]))
s.SetAttributes(common.Workload.String(annotations[common.WorkloadAnnotation]))
s.SetAttributes(common.Version.String(annotations[common.VersionAnnotation]))
}
16 changes: 13 additions & 3 deletions operator/api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions operator/config/crd/bases/lifecycle.keptn.sh_keptntasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,14 @@ spec:
status:
description: KeptnTaskStatus defines the observed state of KeptnTask
properties:
endTime:
format: date-time
type: string
jobName:
type: string
startTime:
format: date-time
type: string
status:
type: string
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ spec:
deploymentStatus:
default: Pending
type: string
endTime:
format: date-time
type: string
postDeploymentStatus:
default: Pending
type: string
Expand All @@ -112,6 +115,12 @@ spec:
properties:
TaskDefinitionName:
type: string
endTime:
format: date-time
type: string
startTime:
format: date-time
type: string
status:
type: string
taskName:
Expand All @@ -126,12 +135,21 @@ spec:
properties:
TaskDefinitionName:
type: string
endTime:
format: date-time
type: string
startTime:
format: date-time
type: string
status:
type: string
taskName:
type: string
type: object
type: array
startTime:
format: date-time
type: string
type: object
type: object
served: true
Expand Down
4 changes: 2 additions & 2 deletions operator/config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: keptn-lifecycle-operator
newTag: 0.1.1-amd64
newName: docker.io/annadreal/keptn-lifecycle-operator
newTag: latest
2 changes: 2 additions & 0 deletions operator/config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ spec:
metadata:
annotations:
kubectl.kubernetes.io/default-container: manager
metrics.dynatrace.com/scrape: 'true'
metrics.dynatrace.com/port: '2222'
labels:
control-plane: controller-manager
spec:
Expand Down

0 comments on commit f8a3cee

Please sign in to comment.