api/prometheus: add prometheus metrics to API routes

This commit is contained in:
Jimmy Zelinskie 2016-02-04 15:52:44 -05:00
parent f8b4a52f8a
commit 83b19b6179
4 changed files with 239 additions and 131 deletions

View File

@ -15,28 +15,45 @@
package context package context
import ( import (
"fmt"
"net/http" "net/http"
"strconv"
"time"
"github.com/coreos/pkg/capnslog" "github.com/coreos/pkg/capnslog"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"github.com/prometheus/client_golang/prometheus"
"github.com/coreos/clair/database" "github.com/coreos/clair/database"
"github.com/coreos/clair/utils"
) )
var log = capnslog.NewPackageLogger("github.com/coreos/clair", "api") var (
log = capnslog.NewPackageLogger("github.com/coreos/clair", "api")
type Handler func(http.ResponseWriter, *http.Request, httprouter.Params, *RouteContext) int promResponseDurationMilliseconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "clair_api_response_duration_milliseconds",
Help: "The duration of time it takes to receieve and write a response to an API request",
Buckets: prometheus.ExponentialBuckets(9.375, 2, 10),
}, []string{"route", "code"})
)
func init() {
prometheus.MustRegister(promResponseDurationMilliseconds)
}
type Handler func(http.ResponseWriter, *http.Request, httprouter.Params, *RouteContext) (route string, status int)
func HTTPHandler(handler Handler, ctx *RouteContext) httprouter.Handle { func HTTPHandler(handler Handler, ctx *RouteContext) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
status := handler(w, r, p, ctx) start := time.Now()
statusStr := fmt.Sprintf("%d", status) route, status := handler(w, r, p, ctx)
statusStr := strconv.Itoa(status)
if status == 0 { if status == 0 {
statusStr = "???" statusStr = "???"
} }
utils.PrometheusObserveTimeMilliseconds(promResponseDurationMilliseconds.WithLabelValues(route, statusStr), start)
log.Infof("%s %s %s %s", statusStr, r.Method, r.RequestURI, r.RemoteAddr) log.Infof("%s \"%s %s\" %s", r.RemoteAddr, r.Method, r.RequestURI, statusStr)
} }
} }

View File

@ -62,6 +62,9 @@ func newHealthHandler(ctx *context.RouteContext) http.Handler {
return router return router
} }
func getHealth(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func getHealth(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
return 0 if ctx.Store.Ping() {
return "health", http.StatusOK
}
return "health", http.StatusInternalServerError
} }

View File

@ -30,269 +30,289 @@ import (
) )
// maxBodySize restricts client requests to 1MiB. // maxBodySize restricts client requests to 1MiB.
const maxBodySize int64 = 1048576 const (
maxBodySize int64 = 1048576
postLayerRoute = "v1/postLayer"
getLayerRoute = "v1/getLayer"
deleteLayerRoute = "v1/deleteLayer"
getNamespacesRoute = "v1/getNamespaces"
postVulnerabilityRoute = "v1/postVulnerability"
getVulnerabilityRoute = "v1/getVulnerability"
putVulnerabilityRoute = "v1/putVulnerability"
deleteVulnerabilityRoute = "v1/deleteVulnerability"
getFixesRoute = "v1/getFixes"
putFixRoute = "v1/putFix"
deleteFixRoute = "v1/deleteFix"
getNotificationRoute = "v1/getNotification"
deleteNotificationRoute = "v1/deleteNotification"
getMetricsRoute = "v1/getMetrics"
)
func decodeJSON(r *http.Request, v interface{}) error { func decodeJSON(r *http.Request, v interface{}) error {
defer r.Body.Close() defer r.Body.Close()
return json.NewDecoder(io.LimitReader(r.Body, maxBodySize)).Decode(v) return json.NewDecoder(io.LimitReader(r.Body, maxBodySize)).Decode(v)
} }
func writeResponse(w http.ResponseWriter, resp interface{}) { func writeResponse(w http.ResponseWriter, status int, resp interface{}) {
header := w.Header() header := w.Header()
header.Set("Content-Type", "application/json;charset=utf-8") header.Set("Content-Type", "application/json;charset=utf-8")
header.Set("Server", "clair") header.Set("Server", "clair")
w.WriteHeader(status)
err := json.NewEncoder(w).Encode(resp) err := json.NewEncoder(w).Encode(resp)
if err != nil { if err != nil {
panic("v1: failed to marshal response: " + err.Error()) panic("v1: failed to marshal response: " + err.Error())
} }
} }
func writeHeader(w http.ResponseWriter, status int) int { func postLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
w.WriteHeader(status)
return status
}
func postLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int {
request := LayerEnvelope{} request := LayerEnvelope{}
err := decodeJSON(r, &request) err := decodeJSON(r, &request)
if err != nil { if err != nil {
writeResponse(w, LayerEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusBadRequest, LayerEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusBadRequest) return postLayerRoute, http.StatusBadRequest
} }
if request.Layer == nil { if request.Layer == nil {
writeResponse(w, LayerEnvelope{Error: &Error{"failed to provide layer"}}) writeResponse(w, http.StatusBadRequest, LayerEnvelope{Error: &Error{"failed to provide layer"}})
return writeHeader(w, http.StatusBadRequest) return postLayerRoute, http.StatusBadRequest
} }
err = worker.Process(ctx.Store, request.Layer.Name, request.Layer.ParentName, request.Layer.Path, request.Layer.Format) err = worker.Process(ctx.Store, request.Layer.Name, request.Layer.ParentName, request.Layer.Path, request.Layer.Format)
if err != nil { if err != nil {
if _, ok := err.(*cerrors.ErrBadRequest); ok { if _, ok := err.(*cerrors.ErrBadRequest); ok {
writeResponse(w, LayerEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusBadRequest, LayerEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusBadRequest) return postLayerRoute, http.StatusBadRequest
} }
writeResponse(w, LayerEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusInternalServerError, LayerEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return postLayerRoute, http.StatusInternalServerError
} }
return writeHeader(w, http.StatusCreated) w.WriteHeader(http.StatusCreated)
return postLayerRoute, http.StatusCreated
} }
func getLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func getLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
_, withFeatures := r.URL.Query()["features"] _, withFeatures := r.URL.Query()["features"]
_, withVulnerabilities := r.URL.Query()["vulnerabilities"] _, withVulnerabilities := r.URL.Query()["vulnerabilities"]
dbLayer, err := ctx.Store.FindLayer(p.ByName("layerName"), withFeatures, withVulnerabilities) dbLayer, err := ctx.Store.FindLayer(p.ByName("layerName"), withFeatures, withVulnerabilities)
if err == cerrors.ErrNotFound { if err == cerrors.ErrNotFound {
writeResponse(w, LayerEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusNotFound, LayerEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusNotFound) return getLayerRoute, http.StatusNotFound
} else if err != nil { } else if err != nil {
writeResponse(w, LayerEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusInternalServerError, LayerEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return getLayerRoute, http.StatusInternalServerError
} }
layer := LayerFromDatabaseModel(dbLayer, withFeatures, withVulnerabilities) layer := LayerFromDatabaseModel(dbLayer, withFeatures, withVulnerabilities)
writeResponse(w, LayerEnvelope{Layer: &layer}) writeResponse(w, http.StatusOK, LayerEnvelope{Layer: &layer})
return writeHeader(w, http.StatusOK) return getLayerRoute, http.StatusOK
} }
func deleteLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func deleteLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
err := ctx.Store.DeleteLayer(p.ByName("layerName")) err := ctx.Store.DeleteLayer(p.ByName("layerName"))
if err == cerrors.ErrNotFound { if err == cerrors.ErrNotFound {
writeResponse(w, LayerEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusNotFound, LayerEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusNotFound) return deleteLayerRoute, http.StatusNotFound
} else if err != nil { } else if err != nil {
writeResponse(w, LayerEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusInternalServerError, LayerEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return deleteLayerRoute, http.StatusInternalServerError
} }
return writeHeader(w, http.StatusOK) w.WriteHeader(http.StatusOK)
return deleteLayerRoute, http.StatusOK
} }
func getNamespaces(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func getNamespaces(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
dbNamespaces, err := ctx.Store.ListNamespaces() dbNamespaces, err := ctx.Store.ListNamespaces()
if err != nil { if err != nil {
writeResponse(w, NamespaceEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusInternalServerError, NamespaceEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return getNamespacesRoute, http.StatusInternalServerError
} }
var namespaces []string var namespaces []string
for _, dbNamespace := range dbNamespaces { for _, dbNamespace := range dbNamespaces {
namespaces = append(namespaces, dbNamespace.Name) namespaces = append(namespaces, dbNamespace.Name)
} }
writeResponse(w, NamespaceEnvelope{Namespaces: &namespaces}) writeResponse(w, http.StatusOK, NamespaceEnvelope{Namespaces: &namespaces})
return writeHeader(w, http.StatusOK) return getNamespacesRoute, http.StatusOK
} }
func postVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func postVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
request := VulnerabilityEnvelope{} request := VulnerabilityEnvelope{}
err := decodeJSON(r, &request) err := decodeJSON(r, &request)
if err != nil { if err != nil {
writeResponse(w, VulnerabilityEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusBadRequest, VulnerabilityEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusBadRequest) return postVulnerabilityRoute, http.StatusBadRequest
} }
if request.Vulnerability == nil { if request.Vulnerability == nil {
writeResponse(w, VulnerabilityEnvelope{Error: &Error{"failed to provide vulnerability"}}) writeResponse(w, http.StatusBadRequest, VulnerabilityEnvelope{Error: &Error{"failed to provide vulnerability"}})
return writeHeader(w, http.StatusBadRequest) return postVulnerabilityRoute, http.StatusBadRequest
} }
vuln, err := request.Vulnerability.DatabaseModel() vuln, err := request.Vulnerability.DatabaseModel()
if err != nil { if err != nil {
writeResponse(w, VulnerabilityEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusBadRequest, VulnerabilityEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusBadRequest) return postVulnerabilityRoute, http.StatusBadRequest
} }
err = ctx.Store.InsertVulnerabilities([]database.Vulnerability{vuln}) err = ctx.Store.InsertVulnerabilities([]database.Vulnerability{vuln})
if err != nil { if err != nil {
writeResponse(w, VulnerabilityEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusInternalServerError, VulnerabilityEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return postVulnerabilityRoute, http.StatusInternalServerError
} }
return writeHeader(w, http.StatusCreated) w.WriteHeader(http.StatusCreated)
return postVulnerabilityRoute, http.StatusCreated
} }
func getVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func getVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
_, withFixedIn := r.URL.Query()["fixedIn"] _, withFixedIn := r.URL.Query()["fixedIn"]
dbVuln, err := ctx.Store.FindVulnerability(p.ByName("namespaceName"), p.ByName("vulnerabilityName")) dbVuln, err := ctx.Store.FindVulnerability(p.ByName("namespaceName"), p.ByName("vulnerabilityName"))
if err == cerrors.ErrNotFound { if err == cerrors.ErrNotFound {
writeResponse(w, VulnerabilityEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusNotFound, VulnerabilityEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusNotFound) return getVulnerabilityRoute, http.StatusNotFound
} else if err != nil { } else if err != nil {
writeResponse(w, VulnerabilityEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusInternalServerError, VulnerabilityEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return getVulnerabilityRoute, http.StatusInternalServerError
} }
vuln := VulnerabilityFromDatabaseModel(dbVuln, withFixedIn) vuln := VulnerabilityFromDatabaseModel(dbVuln, withFixedIn)
writeResponse(w, VulnerabilityEnvelope{Vulnerability: &vuln}) writeResponse(w, http.StatusOK, VulnerabilityEnvelope{Vulnerability: &vuln})
return writeHeader(w, http.StatusOK) return getVulnerabilityRoute, http.StatusOK
} }
func putVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func putVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
request := VulnerabilityEnvelope{} request := VulnerabilityEnvelope{}
err := decodeJSON(r, &request) err := decodeJSON(r, &request)
if err != nil { if err != nil {
writeResponse(w, VulnerabilityEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusBadRequest, VulnerabilityEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusBadRequest) return putVulnerabilityRoute, http.StatusBadRequest
} }
if request.Vulnerability == nil { if request.Vulnerability == nil {
writeResponse(w, VulnerabilityEnvelope{Error: &Error{"failed to provide vulnerability"}}) writeResponse(w, http.StatusBadRequest, VulnerabilityEnvelope{Error: &Error{"failed to provide vulnerability"}})
return writeHeader(w, http.StatusBadRequest) return putVulnerabilityRoute, http.StatusBadRequest
} }
if len(request.Vulnerability.FixedIn) != 0 { if len(request.Vulnerability.FixedIn) != 0 {
writeResponse(w, VulnerabilityEnvelope{Error: &Error{"Vulnerability.FixedIn must be empty"}}) writeResponse(w, http.StatusBadRequest, VulnerabilityEnvelope{Error: &Error{"Vulnerability.FixedIn must be empty"}})
return writeHeader(w, http.StatusBadRequest) return putVulnerabilityRoute, http.StatusBadRequest
} }
vuln, err := request.Vulnerability.DatabaseModel() vuln, err := request.Vulnerability.DatabaseModel()
if err != nil { if err != nil {
writeResponse(w, VulnerabilityEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusBadRequest, VulnerabilityEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusBadRequest) return putVulnerabilityRoute, http.StatusBadRequest
} }
err = ctx.Store.InsertVulnerabilities([]database.Vulnerability{vuln}) err = ctx.Store.InsertVulnerabilities([]database.Vulnerability{vuln})
if err != nil { if err != nil {
writeResponse(w, VulnerabilityEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusInternalServerError, VulnerabilityEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return putVulnerabilityRoute, http.StatusInternalServerError
} }
return writeHeader(w, http.StatusOK) w.WriteHeader(http.StatusOK)
return putVulnerabilityRoute, http.StatusOK
} }
func deleteVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func deleteVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
err := ctx.Store.DeleteVulnerability(p.ByName("namespaceName"), p.ByName("vulnerabilityName")) err := ctx.Store.DeleteVulnerability(p.ByName("namespaceName"), p.ByName("vulnerabilityName"))
if err == cerrors.ErrNotFound { if err == cerrors.ErrNotFound {
writeResponse(w, VulnerabilityEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusNotFound, VulnerabilityEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusNotFound) return deleteVulnerabilityRoute, http.StatusNotFound
} else if err != nil { } else if err != nil {
writeResponse(w, VulnerabilityEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusInternalServerError, VulnerabilityEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return deleteVulnerabilityRoute, http.StatusInternalServerError
} }
return writeHeader(w, http.StatusOK) w.WriteHeader(http.StatusOK)
return deleteVulnerabilityRoute, http.StatusOK
} }
func getFixes(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func getFixes(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
dbVuln, err := ctx.Store.FindVulnerability(p.ByName("namespaceName"), p.ByName("vulnerabilityName")) dbVuln, err := ctx.Store.FindVulnerability(p.ByName("namespaceName"), p.ByName("vulnerabilityName"))
if err == cerrors.ErrNotFound { if err == cerrors.ErrNotFound {
writeResponse(w, FeatureEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusNotFound, FeatureEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusNotFound) return getFixesRoute, http.StatusNotFound
} else if err != nil { } else if err != nil {
writeResponse(w, FeatureEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusInternalServerError, FeatureEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return getFixesRoute, http.StatusInternalServerError
} }
vuln := VulnerabilityFromDatabaseModel(dbVuln, true) vuln := VulnerabilityFromDatabaseModel(dbVuln, true)
writeResponse(w, FeatureEnvelope{Features: &vuln.FixedIn}) writeResponse(w, http.StatusOK, FeatureEnvelope{Features: &vuln.FixedIn})
return writeHeader(w, http.StatusOK) return getFixesRoute, http.StatusOK
} }
func putFix(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func putFix(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
request := FeatureEnvelope{} request := FeatureEnvelope{}
err := decodeJSON(r, &request) err := decodeJSON(r, &request)
if err != nil { if err != nil {
writeResponse(w, FeatureEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusBadRequest, FeatureEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusBadRequest) return putFixRoute, http.StatusBadRequest
} }
if request.Feature == nil { if request.Feature == nil {
writeResponse(w, FeatureEnvelope{Error: &Error{"failed to provide feature"}}) writeResponse(w, http.StatusBadRequest, FeatureEnvelope{Error: &Error{"failed to provide feature"}})
return writeHeader(w, http.StatusBadRequest) return putFixRoute, http.StatusBadRequest
} }
if request.Feature.Name != p.ByName("fixName") { if request.Feature.Name != p.ByName("fixName") {
writeResponse(w, FeatureEnvelope{Error: &Error{"feature name in URL and JSON do not match"}}) writeResponse(w, http.StatusBadRequest, FeatureEnvelope{Error: &Error{"feature name in URL and JSON do not match"}})
return writeHeader(w, http.StatusBadRequest) return putFixRoute, http.StatusBadRequest
} }
dbFix, err := request.Feature.DatabaseModel() dbFix, err := request.Feature.DatabaseModel()
if err != nil { if err != nil {
writeResponse(w, FeatureEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusBadRequest, FeatureEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusBadRequest) return putFixRoute, http.StatusBadRequest
} }
err = ctx.Store.InsertVulnerabilityFixes(p.ByName("vulnerabilityNamespace"), p.ByName("vulnerabilityName"), []database.FeatureVersion{dbFix}) err = ctx.Store.InsertVulnerabilityFixes(p.ByName("vulnerabilityNamespace"), p.ByName("vulnerabilityName"), []database.FeatureVersion{dbFix})
if err == cerrors.ErrNotFound { if err == cerrors.ErrNotFound {
writeResponse(w, FeatureEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusNotFound, FeatureEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusNotFound) return putFixRoute, http.StatusNotFound
} else if err != nil { } else if err != nil {
writeResponse(w, FeatureEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusInternalServerError, FeatureEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return putFixRoute, http.StatusInternalServerError
} }
return writeHeader(w, http.StatusCreated) w.WriteHeader(http.StatusCreated)
return putFixRoute, http.StatusCreated
} }
func deleteFix(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func deleteFix(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
err := ctx.Store.DeleteVulnerabilityFix(p.ByName("vulnerabilityNamespace"), p.ByName("vulnerabilityName"), p.ByName("fixName")) err := ctx.Store.DeleteVulnerabilityFix(p.ByName("vulnerabilityNamespace"), p.ByName("vulnerabilityName"), p.ByName("fixName"))
if err == cerrors.ErrNotFound { if err == cerrors.ErrNotFound {
writeResponse(w, FeatureEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusNotFound, FeatureEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusNotFound) return deleteFixRoute, http.StatusNotFound
} else if err != nil { } else if err != nil {
writeResponse(w, FeatureEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusInternalServerError, FeatureEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return deleteFixRoute, http.StatusInternalServerError
} }
return writeHeader(w, http.StatusOK) w.WriteHeader(http.StatusOK)
return deleteFixRoute, http.StatusOK
} }
func getNotification(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func getNotification(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
query := r.URL.Query() query := r.URL.Query()
limitStrs, limitExists := query["limit"] limitStrs, limitExists := query["limit"]
if !limitExists { if !limitExists {
writeResponse(w, NotificationEnvelope{Error: &Error{"must provide limit query parameter"}}) writeResponse(w, http.StatusBadRequest, NotificationEnvelope{Error: &Error{"must provide limit query parameter"}})
return writeHeader(w, http.StatusBadRequest) return getNotificationRoute, http.StatusBadRequest
} }
limit, err := strconv.Atoi(limitStrs[0]) limit, err := strconv.Atoi(limitStrs[0])
if err != nil { if err != nil {
writeResponse(w, NotificationEnvelope{Error: &Error{"invalid limit format: " + err.Error()}}) writeResponse(w, http.StatusBadRequest, NotificationEnvelope{Error: &Error{"invalid limit format: " + err.Error()}})
return writeHeader(w, http.StatusBadRequest) return getNotificationRoute, http.StatusBadRequest
} }
page := database.VulnerabilityNotificationFirstPage page := database.VulnerabilityNotificationFirstPage
@ -300,37 +320,38 @@ func getNotification(w http.ResponseWriter, r *http.Request, p httprouter.Params
if pageExists { if pageExists {
page, err = pageStringToDBPageNumber(pageStrs[0]) page, err = pageStringToDBPageNumber(pageStrs[0])
if err != nil { if err != nil {
writeResponse(w, NotificationEnvelope{Error: &Error{"invalid page format: " + err.Error()}}) writeResponse(w, http.StatusBadRequest, NotificationEnvelope{Error: &Error{"invalid page format: " + err.Error()}})
return writeHeader(w, http.StatusBadRequest) return getNotificationRoute, http.StatusBadRequest
} }
} }
dbNotification, nextPage, err := ctx.Store.GetNotification(p.ByName("notificationName"), limit, page) dbNotification, nextPage, err := ctx.Store.GetNotification(p.ByName("notificationName"), limit, page)
if err != nil { if err != nil {
writeResponse(w, NotificationEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusInternalServerError, NotificationEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return getNotificationRoute, http.StatusInternalServerError
} }
notification := NotificationFromDatabaseModel(dbNotification, limit, page, nextPage) notification := NotificationFromDatabaseModel(dbNotification, limit, page, nextPage)
writeResponse(w, NotificationEnvelope{Notification: &notification}) writeResponse(w, http.StatusOK, NotificationEnvelope{Notification: &notification})
return writeHeader(w, http.StatusOK) return getNotificationRoute, http.StatusOK
} }
func deleteNotification(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func deleteNotification(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
err := ctx.Store.DeleteNotification(p.ByName("notificationName")) err := ctx.Store.DeleteNotification(p.ByName("notificationName"))
if err == cerrors.ErrNotFound { if err == cerrors.ErrNotFound {
writeResponse(w, NotificationEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusNotFound, NotificationEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusNotFound) return deleteNotificationRoute, http.StatusNotFound
} else if err != nil { } else if err != nil {
writeResponse(w, NotificationEnvelope{Error: &Error{err.Error()}}) writeResponse(w, http.StatusInternalServerError, NotificationEnvelope{Error: &Error{err.Error()}})
return writeHeader(w, http.StatusInternalServerError) return deleteNotificationRoute, http.StatusInternalServerError
} }
return writeHeader(w, http.StatusOK) w.WriteHeader(http.StatusOK)
return deleteNotificationRoute, http.StatusOK
} }
func getMetrics(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) int { func getMetrics(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) {
prometheus.Handler().ServeHTTP(w, r) prometheus.Handler().ServeHTTP(w, r)
return 0 return getMetricsRoute, 0
} }

View File

@ -715,14 +715,14 @@
"points": false, "points": false,
"renderer": "flot", "renderer": "flot",
"seriesOverrides": [], "seriesOverrides": [],
"span": 8, "span": 5,
"stack": false, "stack": false,
"steppedLine": false, "steppedLine": false,
"targets": [ "targets": [
{ {
"expr": "sum(rate(clair_api_query_duration_milliseconds_sum[$rate])) by (query, subquery) / sum(rate(clair_api_query_duration_milliseconds_count[$rate])) by (query)", "expr": "sum(rate(clair_api_response_duration_milliseconds_sum[$rate])) by (route) / sum(rate(clair_api_response_duration_milliseconds_count[$rate])) by (route)",
"intervalFactor": 2, "intervalFactor": 2,
"legendFormat": "{{query}}", "legendFormat": "{{route}}",
"metric": "", "metric": "",
"refId": "A", "refId": "A",
"step": 2 "step": 2
@ -743,6 +743,73 @@
"short" "short"
] ]
}, },
{
"aliasColors": {},
"bars": false,
"datasource": null,
"editable": true,
"error": false,
"fill": 1,
"grid": {
"leftLogBase": 1,
"leftMax": null,
"leftMin": null,
"rightLogBase": 1,
"rightMax": null,
"rightMin": null,
"threshold1": null,
"threshold1Color": "rgba(216, 200, 27, 0.27)",
"threshold2": null,
"threshold2Color": "rgba(234, 112, 112, 0.22)"
},
"id": 13,
"isNew": true,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 2,
"links": [],
"nullPointMode": "connected",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"span": 3,
"stack": false,
"steppedLine": false,
"targets": [
{
"expr": "sum(rate(clair_api_response_duration_milliseconds_count[$rate])) by (code)",
"intervalFactor": 2,
"legendFormat": "{{code}}",
"metric": "clair_api_response_duration_milliseconds_count",
"refId": "A",
"step": 2
}
],
"timeFrom": null,
"timeShift": null,
"title": "API - Status code",
"tooltip": {
"shared": true,
"value_type": "cumulative"
},
"type": "graph",
"x-axis": true,
"y-axis": true,
"y_formats": [
"short",
"short"
]
},
{ {
"cacheTimeout": null, "cacheTimeout": null,
"colorBackground": false, "colorBackground": false,
@ -777,7 +844,7 @@
}, },
"targets": [ "targets": [
{ {
"expr": "sum(rate(clair_api_query_duration_milliseconds_bucket{le=\"300\", query=\"GETLayers\"}[$rate])) / sum(rate(clair_api_query_duration_milliseconds_count{query=\"GETLayers\"}[$rate])) * 100", "expr": "sum(rate(clair_api_response_duration_milliseconds_bucket{le=\"300\", route=\"v1/getLayer\"}[$rate])) / sum(rate(clair_api_response_duration_milliseconds_count{route=\"v1/getLayer\"}[$rate])) * 100",
"intervalFactor": 2, "intervalFactor": 2,
"legendFormat": "", "legendFormat": "",
"refId": "A", "refId": "A",
@ -830,11 +897,11 @@
}, },
"targets": [ "targets": [
{ {
"expr": "histogram_quantile(0.95, sum(rate(clair_api_query_duration_milliseconds_bucket{query=\"getLayer\"}[$rate])) by (le))", "expr": "histogram_quantile(0.95, sum(rate(clair_api_response_duration_milliseconds_bucket{route=\"v1/getLayer\"}[$rate])) by (le))",
"interval": "", "interval": "",
"intervalFactor": 2, "intervalFactor": 2,
"legendFormat": "", "legendFormat": "",
"metric": "clair_api_query_duration_milliseconds_bucket", "metric": "clair_api_response_duration_milliseconds_bucket",
"refId": "A", "refId": "A",
"step": 21 "step": 21
} }
@ -1026,6 +1093,6 @@
}, },
"refresh": "5s", "refresh": "5s",
"schemaVersion": 8, "schemaVersion": 8,
"version": 2, "version": 1,
"links": [] "links": []
} }