github.com/timstclair/heapster@v0.20.0-alpha1/metrics/handlers.go (about)

     1  // Copyright 2014 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"net/http"
    19  	"net/http/pprof"
    20  	"strings"
    21  
    22  	restful "github.com/emicklei/go-restful"
    23  	"k8s.io/heapster/metrics/api/v1"
    24  	"k8s.io/heapster/metrics/sinks/metric"
    25  	"k8s.io/heapster/metrics/util/metrics"
    26  )
    27  
    28  const pprofBasePath = "/debug/pprof/"
    29  
    30  func setupHandlers(metricSink *metricsink.MetricSink) http.Handler {
    31  
    32  	runningInKubernetes := true
    33  
    34  	// Make API handler.
    35  	wsContainer := restful.NewContainer()
    36  	wsContainer.EnableContentEncoding(true)
    37  	a := v1.NewApi(runningInKubernetes, metricSink)
    38  	a.Register(wsContainer)
    39  
    40  	handlePprofEndpoint := func(req *restful.Request, resp *restful.Response) {
    41  		name := strings.TrimPrefix(req.Request.URL.Path, pprofBasePath)
    42  		switch name {
    43  		case "profile":
    44  			pprof.Profile(resp, req.Request)
    45  		case "symbol":
    46  			pprof.Symbol(resp, req.Request)
    47  		case "cmdline":
    48  			pprof.Cmdline(resp, req.Request)
    49  		default:
    50  			pprof.Index(resp, req.Request)
    51  		}
    52  	}
    53  
    54  	// Setup pporf handlers.
    55  	ws := new(restful.WebService).Path(pprofBasePath)
    56  	ws.Route(ws.GET("/{subpath:*}").To(metrics.InstrumentRouteFunc("pprof", handlePprofEndpoint))).Doc("pprof endpoint")
    57  	wsContainer.Add(ws)
    58  
    59  	return wsContainer
    60  }