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