...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/http.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon

     1  // Copyright 2021 Chaos Mesh Authors.
     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  
    16  package chaosdaemon
    17  
    18  import (
    19  	"net/http"
    20  	"net/http/pprof"
    21  
    22  	"github.com/prometheus/client_golang/prometheus"
    23  	"github.com/prometheus/client_golang/prometheus/promhttp"
    24  )
    25  
    26  type httpServerBuilder struct {
    27  	mux       *http.ServeMux
    28  	addr      string
    29  	profiling bool
    30  	reg       prometheus.Gatherer
    31  }
    32  
    33  func newHTTPServerBuilder() *httpServerBuilder {
    34  	return &httpServerBuilder{
    35  		mux: http.NewServeMux(),
    36  	}
    37  }
    38  
    39  // Addr sets the addr of http server
    40  func (b *httpServerBuilder) Addr(addr string) *httpServerBuilder {
    41  	b.addr = addr
    42  
    43  	return b
    44  }
    45  
    46  // Metrics sets the prometheus gatherer for http server
    47  func (b *httpServerBuilder) Metrics(reg prometheus.Gatherer) *httpServerBuilder {
    48  	b.reg = reg
    49  
    50  	return b
    51  }
    52  
    53  // Profiling turns on or off profiling server of http server
    54  func (b *httpServerBuilder) Profiling(profiling bool) *httpServerBuilder {
    55  	b.profiling = profiling
    56  
    57  	return b
    58  }
    59  
    60  // Build builds an http server
    61  func (b *httpServerBuilder) Build() *http.Server {
    62  	registerMetrics(b.mux, b.reg)
    63  
    64  	if b.profiling {
    65  		registerProfiler(b.mux)
    66  	}
    67  
    68  	return &http.Server{
    69  		Addr:    b.addr,
    70  		Handler: b.mux,
    71  	}
    72  }
    73  
    74  func registerMetrics(mux *http.ServeMux, g prometheus.Gatherer) {
    75  	if g != nil {
    76  		mux.Handle("/metrics", promhttp.HandlerFor(g, promhttp.HandlerOpts{}))
    77  	}
    78  }
    79  
    80  func registerProfiler(mux *http.ServeMux) {
    81  	mux.HandleFunc("/debug/pprof/", pprof.Index)
    82  	mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
    83  	mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
    84  	mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
    85  	mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
    86  }
    87