This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
POC tracking job metrics calling DB Api #102
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
histogram.Observe(endTime) | ||
} | ||
|
||
func trackSuccessFailure(err error, success prometheus.Counter, failure prometheus.Counter) { | ||
if err == nil { | ||
success.Inc() | ||
} else { | ||
failure.Inc() | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: The
sub
method ontime
produces aduration
which has the method Milliseconds. I'd favor using that over returningnanosec
and dividing or I might have misunderstood what this is doing.There was a problem hiding this comment.
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#timeThere was a problem hiding this comment.
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 withtime.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 likeduration
?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.