Skip to content

Commit

Permalink
refactor Prometheus metrics. Less metrics, more labels (#384)
Browse files Browse the repository at this point in the history
* refactor Prometheus metrics. Less metrics, more labels

* migrate distinct counters to gauge
  • Loading branch information
mthenw authored Mar 14, 2018
1 parent 6049ea7 commit cd92090
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 220 deletions.
38 changes: 19 additions & 19 deletions httpapi/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func (h HTTPAPI) RegisterRoutes(router *httprouter.Router) {
router.GET("/v1/status", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {})
router.Handler("GET", "/metrics", promhttp.Handler())

router.GET("/v1/spaces/:space/functions", h.getFunctions)
router.GET("/v1/spaces/:space/functions", h.listFunctions)
router.GET("/v1/spaces/:space/functions/:id", h.getFunction)
router.POST("/v1/spaces/:space/functions", h.registerFunction)
router.POST("/v1/spaces/:space/functions", h.createFunction)
router.PUT("/v1/spaces/:space/functions/:id", h.updateFunction)
router.DELETE("/v1/spaces/:space/functions/:id", h.deleteFunction)

router.GET("/v1/spaces/:space/subscriptions", h.getSubscriptions)
router.GET("/v1/spaces/:space/subscriptions", h.listSubscriptions)
router.GET("/v1/spaces/:space/subscriptions/*id", h.getSubscription)
router.POST("/v1/spaces/:space/subscriptions", h.createSubscription)
router.DELETE("/v1/spaces/:space/subscriptions/*id", h.deleteSubscription)
Expand All @@ -62,10 +62,10 @@ func (h HTTPAPI) getFunction(w http.ResponseWriter, r *http.Request, params http
encoder.Encode(fn)
}

metricFunctionGetRequests.WithLabelValues(space).Inc()
metricConfigRequests.WithLabelValues(space, "function", "get").Inc()
}

func (h HTTPAPI) getFunctions(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
func (h HTTPAPI) listFunctions(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
encoder := json.NewEncoder(w)

Expand All @@ -78,10 +78,10 @@ func (h HTTPAPI) getFunctions(w http.ResponseWriter, r *http.Request, params htt
encoder.Encode(&FunctionsResponse{fns})
}

metricFunctionListRequests.WithLabelValues(space).Inc()
metricConfigRequests.WithLabelValues(space, "function", "list").Inc()
}

func (h HTTPAPI) registerFunction(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
func (h HTTPAPI) createFunction(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
encoder := json.NewEncoder(w)

Expand Down Expand Up @@ -110,10 +110,10 @@ func (h HTTPAPI) registerFunction(w http.ResponseWriter, r *http.Request, params
w.WriteHeader(http.StatusCreated)
encoder.Encode(output)

metricFunctionRegistered.WithLabelValues(fn.Space).Inc()
metricFunctions.WithLabelValues(fn.Space).Inc()
}

metricFunctionRegisterRequests.WithLabelValues(fn.Space).Inc()
metricConfigRequests.WithLabelValues(fn.Space, "function", "create").Inc()
}

func (h HTTPAPI) updateFunction(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
Expand Down Expand Up @@ -146,7 +146,7 @@ func (h HTTPAPI) updateFunction(w http.ResponseWriter, r *http.Request, params h
encoder.Encode(output)
}

metricFunctionUpdateRequests.WithLabelValues(fn.Space).Inc()
metricConfigRequests.WithLabelValues(fn.Space, "function", "update").Inc()
}

func (h HTTPAPI) deleteFunction(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
Expand All @@ -168,13 +168,13 @@ func (h HTTPAPI) deleteFunction(w http.ResponseWriter, r *http.Request, params h
} else {
w.WriteHeader(http.StatusNoContent)

metricFunctionDeleted.WithLabelValues(space).Inc()
metricFunctions.WithLabelValues(space).Dec()
}

metricFunctionDeleteRequests.WithLabelValues(space).Inc()
metricConfigRequests.WithLabelValues(space, "function", "delete").Inc()
}

func (h HTTPAPI) getSubscriptions(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
func (h HTTPAPI) listSubscriptions(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
encoder := json.NewEncoder(w)

Expand All @@ -187,7 +187,7 @@ func (h HTTPAPI) getSubscriptions(w http.ResponseWriter, r *http.Request, params
encoder.Encode(&SubscriptionsResponse{subs})
}

metricSubscriptionListRequests.WithLabelValues(space).Inc()
metricConfigRequests.WithLabelValues(space, "subscription", "list").Inc()
}

func (h HTTPAPI) getSubscription(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
Expand All @@ -208,7 +208,7 @@ func (h HTTPAPI) getSubscription(w http.ResponseWriter, r *http.Request, params
encoder.Encode(fn)
}

metricSubscriptionGetRequests.WithLabelValues(space).Inc()
metricConfigRequests.WithLabelValues(space, "subscription", "get").Inc()
}

func (h HTTPAPI) createSubscription(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
Expand Down Expand Up @@ -244,10 +244,10 @@ func (h HTTPAPI) createSubscription(w http.ResponseWriter, r *http.Request, para
w.WriteHeader(http.StatusCreated)
encoder.Encode(output)

metricSubscriptionCreated.WithLabelValues(s.Space).Inc()
metricSubscriptions.WithLabelValues(s.Space).Inc()
}

metricSubscriptionCreateRequests.WithLabelValues(s.Space).Inc()
metricConfigRequests.WithLabelValues(s.Space, "subscription", "create").Inc()
}

func (h HTTPAPI) deleteSubscription(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
Expand All @@ -266,10 +266,10 @@ func (h HTTPAPI) deleteSubscription(w http.ResponseWriter, r *http.Request, para
} else {
w.WriteHeader(http.StatusNoContent)

metricSubscriptionDeleted.WithLabelValues(space).Inc()
metricSubscriptions.WithLabelValues(space).Dec()
}

metricSubscriptionDeleteRequests.WithLabelValues(space).Inc()
metricConfigRequests.WithLabelValues(space, "subscription", "delete").Inc()
}

// httprouter weirdness: params are based on Request.URL.Path, not Request.URL.RawPath
Expand Down
126 changes: 16 additions & 110 deletions httpapi/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,132 +5,38 @@ import (
)

func init() {
prometheus.MustRegister(metricFunctionRegistered)
prometheus.MustRegister(metricFunctionDeleted)
prometheus.MustRegister(metricFunctions)
prometheus.MustRegister(metricSubscriptions)

prometheus.MustRegister(metricFunctionGetRequests)
prometheus.MustRegister(metricFunctionRegisterRequests)
prometheus.MustRegister(metricFunctionDeleteRequests)
prometheus.MustRegister(metricFunctionUpdateRequests)
prometheus.MustRegister(metricFunctionListRequests)

prometheus.MustRegister(metricSubscriptionCreated)
prometheus.MustRegister(metricSubscriptionDeleted)

prometheus.MustRegister(metricSubscriptionGetRequests)
prometheus.MustRegister(metricSubscriptionCreateRequests)
prometheus.MustRegister(metricSubscriptionDeleteRequests)
prometheus.MustRegister(metricSubscriptionListRequests)
prometheus.MustRegister(metricConfigRequests)
}

// Functions

var metricFunctionRegistered = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "gateway",
Subsystem: "functions",
Name: "registered_total",
Help: "Total of functions registered.",
}, []string{"space"})

var metricFunctionDeleted = prometheus.NewCounterVec(
prometheus.CounterOpts{
var metricFunctions = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "gateway",
Subsystem: "functions",
Name: "deleted_total",
Help: "Total of functions deleted.",
}, []string{"space"})

// Functions Config API

var metricFunctionGetRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "gateway",
Subsystem: "config",
Name: "function_get_requests_total",
Help: "Total of Config API get function requests.",
}, []string{"space"})

var metricFunctionRegisterRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "gateway",
Subsystem: "config",
Name: "function_register_requests_total",
Help: "Total of Config API register function requests.",
}, []string{"space"})

var metricFunctionDeleteRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "gateway",
Subsystem: "config",
Name: "function_delete_requests_total",
Help: "Total of Config API delete function requests.",
}, []string{"space"})

var metricFunctionUpdateRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "gateway",
Subsystem: "config",
Name: "function_update_requests_total",
Help: "Total of Config API update function requests.",
}, []string{"space"})

var metricFunctionListRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "gateway",
Subsystem: "config",
Name: "function_list_requests_total",
Help: "Total of Config API list functions requests.",
Name: "total",
Help: "Gauge of registered functions count.",
}, []string{"space"})

// Subscriptions

var metricSubscriptionCreated = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "gateway",
Subsystem: "subscriptions",
Name: "created_total",
Help: "Total of subscriptions created.",
}, []string{"space"})

var metricSubscriptionDeleted = prometheus.NewCounterVec(
prometheus.CounterOpts{
var metricSubscriptions = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "gateway",
Subsystem: "subscriptions",
Name: "deleted_total",
Help: "Total of subscriptions deleted.",
}, []string{"space"})

// Subscriptions Config API

var metricSubscriptionGetRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "gateway",
Subsystem: "config",
Name: "subscription_get_requests_total",
Help: "Total of Config API get subscription requests.",
}, []string{"space"})

var metricSubscriptionCreateRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "gateway",
Subsystem: "config",
Name: "subscription_create_requests_total",
Help: "Total of Config API create subscription requests.",
Name: "total",
Help: "Gauge of created subscriptions count.",
}, []string{"space"})

var metricSubscriptionDeleteRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "gateway",
Subsystem: "config",
Name: "subscription_delete_requests_total",
Help: "Total of Config API delete subscription requests.",
}, []string{"space"})
// Config API

var metricSubscriptionListRequests = prometheus.NewCounterVec(
var metricConfigRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "gateway",
Subsystem: "config",
Name: "subscription_list_requests_total",
Help: "Total of Config API list subscriptions requests.",
}, []string{"space"})
Name: "requests_total",
Help: "Total of Config API requests.",
}, []string{"space", "resource", "operation"})
Loading

0 comments on commit cd92090

Please sign in to comment.