2016-01-27 19:07:58 +00:00
|
|
|
// Copyright 2015 clair authors
|
|
|
|
//
|
|
|
|
// 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 context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-02-04 20:52:44 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2016-01-27 19:07:58 +00:00
|
|
|
|
|
|
|
"github.com/coreos/pkg/capnslog"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2016-02-04 20:52:44 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2016-01-27 19:07:58 +00:00
|
|
|
|
2016-02-05 21:22:17 +00:00
|
|
|
"github.com/coreos/clair/config"
|
2016-01-27 19:07:58 +00:00
|
|
|
"github.com/coreos/clair/database"
|
2016-02-04 20:52:44 +00:00
|
|
|
"github.com/coreos/clair/utils"
|
2016-01-27 19:07:58 +00:00
|
|
|
)
|
|
|
|
|
2016-02-04 20:52:44 +00:00
|
|
|
var (
|
|
|
|
log = capnslog.NewPackageLogger("github.com/coreos/clair", "api")
|
2016-01-27 19:07:58 +00:00
|
|
|
|
2016-02-04 20:52:44 +00:00
|
|
|
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)
|
2016-01-27 19:07:58 +00:00
|
|
|
|
|
|
|
func HTTPHandler(handler Handler, ctx *RouteContext) httprouter.Handle {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
2016-02-04 20:52:44 +00:00
|
|
|
start := time.Now()
|
|
|
|
route, status := handler(w, r, p, ctx)
|
|
|
|
statusStr := strconv.Itoa(status)
|
2016-01-27 19:07:58 +00:00
|
|
|
if status == 0 {
|
|
|
|
statusStr = "???"
|
|
|
|
}
|
2016-02-04 20:52:44 +00:00
|
|
|
utils.PrometheusObserveTimeMilliseconds(promResponseDurationMilliseconds.WithLabelValues(route, statusStr), start)
|
2016-01-27 19:07:58 +00:00
|
|
|
|
2016-02-20 20:38:51 +00:00
|
|
|
log.Infof("%s \"%s %s\" %s (%s)", r.RemoteAddr, r.Method, r.RequestURI, statusStr, time.Since(start))
|
2016-01-27 19:07:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RouteContext struct {
|
2016-02-05 21:22:17 +00:00
|
|
|
Store database.Datastore
|
|
|
|
Config *config.APIConfig
|
2016-01-27 19:07:58 +00:00
|
|
|
}
|