github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/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  	metricsApi "k8s.io/heapster/metrics/apis/metrics"
    25  	"k8s.io/heapster/metrics/sinks/metric"
    26  	"k8s.io/heapster/metrics/util/metrics"
    27  
    28  	"k8s.io/kubernetes/pkg/client/cache"
    29  )
    30  
    31  const pprofBasePath = "/debug/pprof/"
    32  
    33  func setupHandlers(metricSink *metricsink.MetricSink, podLister *cache.StoreToPodLister) http.Handler {
    34  
    35  	runningInKubernetes := true
    36  
    37  	// Make API handler.
    38  	wsContainer := restful.NewContainer()
    39  	wsContainer.EnableContentEncoding(true)
    40  	wsContainer.Router(restful.CurlyRouter{})
    41  	a := v1.NewApi(runningInKubernetes, metricSink)
    42  	a.Register(wsContainer)
    43  	// Metrics API
    44  	m := metricsApi.NewApi(metricSink, podLister)
    45  	m.Register(wsContainer)
    46  
    47  	handlePprofEndpoint := func(req *restful.Request, resp *restful.Response) {
    48  		name := strings.TrimPrefix(req.Request.URL.Path, pprofBasePath)
    49  		switch name {
    50  		case "profile":
    51  			pprof.Profile(resp, req.Request)
    52  		case "symbol":
    53  			pprof.Symbol(resp, req.Request)
    54  		case "cmdline":
    55  			pprof.Cmdline(resp, req.Request)
    56  		default:
    57  			pprof.Index(resp, req.Request)
    58  		}
    59  	}
    60  
    61  	// Setup pporf handlers.
    62  	ws := new(restful.WebService).Path(pprofBasePath)
    63  	ws.Route(ws.GET("/{subpath:*}").To(metrics.InstrumentRouteFunc("pprof", handlePprofEndpoint))).Doc("pprof endpoint")
    64  	wsContainer.Add(ws)
    65  
    66  	return wsContainer
    67  }