mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-09-22 06:46:34 +08:00
WIP: (api): Update metrics handler to take options
This commit is contained in:
@@ -188,7 +188,7 @@ func muxRouter(opts Options, rc redis.UniversalClient, inspector *asynq.Inspecto
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Time series metrics endpoints.
|
// Time series metrics endpoints.
|
||||||
api.HandleFunc("/metrics", newGetMetricsHandlerFunc(http.DefaultClient)).Methods("GET")
|
api.HandleFunc("/metrics", newGetMetricsHandlerFunc(http.DefaultClient, opts.PrometheusAddress)).Methods("GET")
|
||||||
|
|
||||||
// Everything else, route to uiAssetsHandler.
|
// Everything else, route to uiAssetsHandler.
|
||||||
router.NotFoundHandler = &uiAssetsHandler{
|
router.NotFoundHandler = &uiAssetsHandler{
|
||||||
|
@@ -14,29 +14,21 @@ type getMetricsResponse struct {
|
|||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
func unixTimeString(t time.Time) string {
|
type metricsFetchOptions struct {
|
||||||
return strconv.Itoa(int(t.Unix()))
|
// Specifies the number of seconds to scan for metrics.
|
||||||
|
duration time.Duration
|
||||||
|
|
||||||
|
// Specifies the end time when fetching metrics.
|
||||||
|
endTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func newGetMetricsHandlerFunc(client *http.Client) http.HandlerFunc {
|
func newGetMetricsHandlerFunc(client *http.Client, prometheusAddr string) http.HandlerFunc {
|
||||||
|
// Optional query params:
|
||||||
|
// `duration_sec`: specifies the number of seconds to scan
|
||||||
|
// `end_time`: specifies the end_time in Unix time seconds
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
const (
|
opts := extractMetricsFetchOptions(r)
|
||||||
baseAddr = "http://localhost:9090"
|
url := buildPrometheusURL(prometheusAddr, "asynq_queue_size", opts)
|
||||||
apiPath = "/api/v1/query_range"
|
|
||||||
promQL = "asynq_queue_size"
|
|
||||||
)
|
|
||||||
var b strings.Builder
|
|
||||||
v := url.Values{}
|
|
||||||
b.WriteString(baseAddr)
|
|
||||||
b.WriteString(apiPath)
|
|
||||||
v.Add("query", promQL)
|
|
||||||
now := time.Now()
|
|
||||||
v.Add("start", unixTimeString(now.Add(-30*time.Minute)))
|
|
||||||
v.Add("end", unixTimeString(now))
|
|
||||||
v.Add("step", (1 * time.Minute).String())
|
|
||||||
b.WriteString("?")
|
|
||||||
b.WriteString(v.Encode())
|
|
||||||
url := b.String()
|
|
||||||
fmt.Printf("DEBUG: url: %s\n", url)
|
fmt.Printf("DEBUG: url: %s\n", url)
|
||||||
resp, err := client.Get(url)
|
resp, err := client.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -50,3 +42,31 @@ func newGetMetricsHandlerFunc(client *http.Client) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const prometheusAPIPath = "/api/v1/query_range"
|
||||||
|
|
||||||
|
func extractMetricsFetchOptions(r *http.Request) *metricsFetchOptions {
|
||||||
|
// TODO: Extract these options from the request if any, and default to these values if none are specified
|
||||||
|
return &metricsFetchOptions{
|
||||||
|
duration: 60 * time.Minute,
|
||||||
|
endTime: time.Now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildPrometheusURL(baseAddr, promQL string, opts *metricsFetchOptions) string {
|
||||||
|
var b strings.Builder
|
||||||
|
b.WriteString(strings.TrimSuffix(baseAddr, "/"))
|
||||||
|
b.WriteString(prometheusAPIPath)
|
||||||
|
v := url.Values{}
|
||||||
|
v.Add("query", promQL)
|
||||||
|
v.Add("start", unixTimeString(opts.endTime.Add(-opts.duration)))
|
||||||
|
v.Add("end", unixTimeString(opts.endTime))
|
||||||
|
v.Add("step", (1 * time.Minute).String())
|
||||||
|
b.WriteString("?")
|
||||||
|
b.WriteString(v.Encode())
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func unixTimeString(t time.Time) string {
|
||||||
|
return strconv.Itoa(int(t.Unix()))
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user