Skip to content
This repository has been archived by the owner on Jan 28, 2022. It is now read-only.

POC tracking job metrics calling DB Api #102

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
34 changes: 30 additions & 4 deletions controllers/djob_controller_databricks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import (
"fmt"
"reflect"
"strings"
"time"

databricksv1alpha1 "github.com/microsoft/azure-databricks-operator/api/v1alpha1"
models "github.com/xinsnake/databricks-sdk-golang/azure/models"
"k8s.io/apimachinery/pkg/types"
)

Expand All @@ -31,7 +33,8 @@ func (r *DjobReconciler) submit(instance *databricksv1alpha1.Djob) error {

instance.Spec.Name = instance.GetName()

job, err := r.APIClient.Jobs().Create(*instance.Spec)
job, err := createJob(r, instance)

if err != nil {
return err
}
Expand All @@ -48,7 +51,8 @@ func (r *DjobReconciler) refresh(instance *databricksv1alpha1.Djob) error {

jobID := instance.Status.JobStatus.JobID

job, err := r.APIClient.Jobs().Get(jobID)
job, err := getJob(r, jobID)

if err != nil {
return err
}
Expand Down Expand Up @@ -89,12 +93,34 @@ func (r *DjobReconciler) delete(instance *databricksv1alpha1.Djob) error {
jobID := instance.Status.JobStatus.JobID

// Check if the job exists before trying to delete it
if _, err := r.APIClient.Jobs().Get(jobID); err != nil {
if _, err := getJob(r, jobID); err != nil {
if strings.Contains(err.Error(), "does not exist") {
return nil
}
return err
}

return r.APIClient.Jobs().Delete(jobID)
return trackExecutionTime(djobDeleteDuration, func() error {
return r.APIClient.Jobs().Delete(jobID)
})
}

func getJob(r *DjobReconciler, jobID int64) (job models.Job, err error) {
defer trackMillisecondsTaken(time.Now(), djobGetDuration)

job, err = r.APIClient.Jobs().Get(jobID)

trackSuccessFailure(err, djobGetSuccess, djobGetFailure)

return job, err
}

func createJob(r *DjobReconciler, instance *databricksv1alpha1.Djob) (job models.Job, err error) {
defer trackMillisecondsTaken(time.Now(), djobCreateDuration)

job, err = r.APIClient.Jobs().Create(*instance.Spec)

trackSuccessFailure(err, djobCreateSuccess, djobCreateFailure)

return job, err
}
74 changes: 74 additions & 0 deletions controllers/djob_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2019 microsoft.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers

import (
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

var (
djobCreateSuccess = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "djob_success_total",
Help: "Number of create djob success",
},
)
djobCreateFailure = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "djob_failures_total",
Help: "Number of create djob failures",
},
)

djobGetSuccess = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "djob_success_total",
Help: "Number of create djob success",
},
)
djobGetFailure = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "djob_failures_total",
Help: "Number of create djob failures",
},
)

djobCreateDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "djob_creation_duration",
Help: "Duration of DB api create calls.",
Buckets: prometheus.LinearBuckets(100, 10, 20),
})

djobGetDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "djob_get_duration",
Help: "Duration of DB api get calls.",
Buckets: prometheus.LinearBuckets(100, 10, 20),
})

djobDeleteDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "djob_delete_duration",
Help: "Duration of DB api delete calls.",
Buckets: prometheus.LinearBuckets(100, 10, 20),
})
)

func init() {
// Register custom metrics with the global prometheus registry
metrics.Registry.MustRegister(djobCreateSuccess, djobCreateFailure, djobGetSuccess, djobGetFailure,
djobCreateDuration, djobGetDuration, djobDeleteDuration)
}
44 changes: 44 additions & 0 deletions controllers/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2019 microsoft.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers

import (
"time"

"github.com/prometheus/client_golang/prometheus"
)

func trackExecutionTime(histogram prometheus.Histogram, f func() error) error {
startTime := time.Now()

defer trackMillisecondsTaken(startTime, histogram)

return f()
}

func trackMillisecondsTaken(startTime time.Time, histogram prometheus.Histogram) {
endTime := float64(time.Now().Sub(startTime).Nanoseconds() / int64(time.Millisecond))
Copy link
Contributor

@lawrencegripper lawrencegripper Nov 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The sub method on time produces a duration which has the method Milliseconds. I'd favor using that over returning nanosec and dividing or I might have misunderstood what this is doing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally got to the bottom of this, yes there is a Milliseconds method, but only in golang v1.13 and this project currently targets 1.12 in its dev container. https://golang.org/doc/go1.13#time

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: In time.Now().Sub(startTime).Nanoseconds() I think you can replace it with time.Now().Sub(startTime).
nit: I don't think the int64 conversion is needed if you don't call Nanoseconds
nit: the variable is called endTime but looks more like duration?
Also, is the float conversion designed to allow sub-millisecond resolution in the metrics? If so, I'm not sure that the conversion on the outside will achieve that.

Suggested change
endTime := float64(time.Now().Sub(startTime).Nanoseconds() / int64(time.Millisecond))
duration := float64(time.Now().Sub(startTime) / time.Millisecond)

histogram.Observe(endTime)
}

func trackSuccessFailure(err error, success prometheus.Counter, failure prometheus.Counter) {
if err == nil {
success.Inc()
} else {
failure.Inc()
}
}