forked from kubernetes/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added prometheus metrics for api calls (kubernetes#1501)
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
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
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,50 @@ | ||
package handler | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
var ( | ||
requestCounter = prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "apiserver_request_count", | ||
Help: "Counter of apiserver requests broken out for each verb, API resource, client, and HTTP response contentType and code.", | ||
}, | ||
[]string{"verb", "resource", "client", "contentType", "code"}, | ||
) | ||
requestLatencies = prometheus.NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Name: "apiserver_request_latencies", | ||
Help: "Response latency distribution in microseconds for each verb, resource and client.", | ||
// Use buckets ranging from 125 ms to 8 seconds. | ||
Buckets: prometheus.ExponentialBuckets(125000, 2.0, 7), | ||
}, | ||
[]string{"verb", "resource"}, | ||
) | ||
requestLatenciesSummary = prometheus.NewSummaryVec( | ||
prometheus.SummaryOpts{ | ||
Name: "apiserver_request_latencies_summary", | ||
Help: "Response latency summary in microseconds for each verb and resource.", | ||
// Make the sliding window of 1h. | ||
MaxAge: time.Hour, | ||
}, | ||
[]string{"verb", "resource"}, | ||
) | ||
) | ||
|
||
// Initialize all metrics in prometheus | ||
func RegisterMetrics() { | ||
prometheus.MustRegister(requestCounter) | ||
prometheus.MustRegister(requestLatencies) | ||
prometheus.MustRegister(requestLatenciesSummary) | ||
} | ||
|
||
// Track API call in prometheus | ||
func Monitor(verb, resource string, client, contentType string, httpCode int, reqStart time.Time) { | ||
elapsed := float64((time.Since(reqStart)) / time.Microsecond) | ||
requestCounter.WithLabelValues(verb, resource, client, contentType, strconv.Itoa(httpCode)).Inc() | ||
requestLatencies.WithLabelValues(verb, resource).Observe(elapsed) | ||
requestLatenciesSummary.WithLabelValues(verb, resource).Observe(elapsed) | ||
} |