Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add monitoring instrumentation for the remaining HTTP handlers in the apiserver. #4299

Merged
merged 1 commit into from
Feb 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion pkg/apiserver/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,25 @@ type ProxyHandler struct {
}

func (r *ProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var verb string
var apiResource string
var httpCode int
reqStart := time.Now()
defer func() { monitor("proxy", verb, apiResource, httpCode, reqStart) }()

requestInfo, err := r.apiRequestInfoResolver.GetAPIRequestInfo(req)
if err != nil {
notFound(w, req)
httpCode = http.StatusNotFound
return
}
verb = requestInfo.Verb
namespace, resource, parts := requestInfo.Namespace, requestInfo.Resource, requestInfo.Parts

ctx := api.WithNamespace(api.NewContext(), namespace)
if len(parts) < 2 {
notFound(w, req)
httpCode = http.StatusNotFound
return
}
id := parts[1]
Expand All @@ -110,13 +119,15 @@ func (r *ProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if !ok {
httplog.LogOf(req, w).Addf("'%v' has no storage object", resource)
notFound(w, req)
httpCode = http.StatusNotFound
return
}
apiResource = resource

redirector, ok := storage.(Redirector)
if !ok {
httplog.LogOf(req, w).Addf("'%v' is not a redirector", resource)
errorJSON(errors.NewMethodNotSupported(resource, "proxy"), r.codec, w)
httpCode = errorJSON(errors.NewMethodNotSupported(resource, "proxy"), r.codec, w)
return
}

Expand All @@ -125,18 +136,21 @@ func (r *ProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
httplog.LogOf(req, w).Addf("Error getting ResourceLocation: %v", err)
status := errToAPIStatus(err)
writeJSON(status.Code, r.codec, status, w)
httpCode = status.Code
return
}
if location == "" {
httplog.LogOf(req, w).Addf("ResourceLocation for %v returned ''", id)
notFound(w, req)
httpCode = http.StatusNotFound
return
}

destURL, err := url.Parse(location)
if err != nil {
status := errToAPIStatus(err)
writeJSON(status.Code, r.codec, status, w)
httpCode = status.Code
return
}
if destURL.Scheme == "" {
Expand All @@ -151,8 +165,10 @@ func (r *ProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
status := errToAPIStatus(err)
writeJSON(status.Code, r.codec, status, w)
notFound(w, req)
httpCode = status.Code
return
}
httpCode = http.StatusOK
newReq.Header = req.Header

proxy := httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "http", Host: destURL.Host})
Expand Down
11 changes: 9 additions & 2 deletions pkg/apiserver/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net"
"net/http"
"strconv"
"time"

"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
)
Expand Down Expand Up @@ -72,6 +73,10 @@ type ServerStatus struct {
}

func (v *validator) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var httpCode int
reqStart := time.Now()
defer func() { monitor("validate", "get", "", httpCode, reqStart) }()

reply := []ServerStatus{}
for name, server := range v.servers() {
status, msg, err := server.check(v.client)
Expand All @@ -85,11 +90,13 @@ func (v *validator) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
data, err := json.MarshalIndent(reply, "", " ")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
httpCode = http.StatusInternalServerError
w.WriteHeader(httpCode)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
httpCode = http.StatusOK
w.WriteHeader(httpCode)
w.Write(data)
}

Expand Down
19 changes: 16 additions & 3 deletions pkg/apiserver/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path"
"regexp"
"strings"
"time"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
Expand Down Expand Up @@ -83,39 +84,51 @@ func isWebsocketRequest(req *http.Request) bool {

// ServeHTTP processes watch requests.
func (h *WatchHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var verb string
var apiResource string
var httpCode int
reqStart := time.Now()
defer func() { monitor("watch", verb, apiResource, httpCode, reqStart) }()

if req.Method != "GET" {
notFound(w, req)
httpCode = http.StatusNotFound
return
}

requestInfo, err := h.apiRequestInfoResolver.GetAPIRequestInfo(req)
if err != nil {
notFound(w, req)
httpCode = http.StatusNotFound
return
}
verb = requestInfo.Verb
ctx := api.WithNamespace(api.NewContext(), requestInfo.Namespace)

storage := h.storage[requestInfo.Resource]
if storage == nil {
notFound(w, req)
httpCode = http.StatusNotFound
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this makes me slightly worries that in the future changes we'll forget to add this annotation

return
}
apiResource = requestInfo.Resource
watcher, ok := storage.(ResourceWatcher)
if !ok {
errorJSON(errors.NewMethodNotSupported(requestInfo.Resource, "watch"), h.codec, w)
httpCode = errorJSON(errors.NewMethodNotSupported(requestInfo.Resource, "watch"), h.codec, w)
return
}

label, field, resourceVersion, err := getWatchParams(req.URL.Query())
if err != nil {
errorJSON(err, h.codec, w)
httpCode = errorJSON(err, h.codec, w)
return
}
watching, err := watcher.Watch(ctx, label, field, resourceVersion)
if err != nil {
errorJSON(err, h.codec, w)
httpCode = errorJSON(err, h.codec, w)
return
}
httpCode = http.StatusOK

// TODO: This is one watch per connection. We want to multiplex, so that
// multiple watches of the same thing don't create two watches downstream.
Expand Down