knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/observability/runtime/pprof.go (about)

     1  /*
     2  Copyright 2025 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package runtime
    18  
    19  import (
    20  	"net/http"
    21  	"net/http/pprof"
    22  	"os"
    23  	"strconv"
    24  	"sync/atomic"
    25  	"time"
    26  )
    27  
    28  const (
    29  	// ProfilingPortEnvKey specified the name of an environment variable that
    30  	// may be used to override the default profiling port.
    31  	ProfilingPortEnvKey = "PROFILING_PORT"
    32  
    33  	// ProfilingDefaultPort specifies the default port where profiling data is available when profiling is enabled
    34  	ProfilingDefaultPort = 8008
    35  )
    36  
    37  type ProfilingServer struct {
    38  	*http.Server
    39  	*ProfilingHandler
    40  }
    41  
    42  type ProfilingHandler struct {
    43  	enabled *atomic.Bool
    44  	handler http.Handler
    45  }
    46  
    47  // NewHandler create a new ProfilingHandler which serves runtime profiling data
    48  // according to the given context path
    49  func NewProfilingHandler() *ProfilingHandler {
    50  	const pprofPrefix = "GET /debug/pprof/"
    51  
    52  	mux := http.NewServeMux()
    53  	mux.HandleFunc(pprofPrefix, pprof.Index)
    54  	mux.HandleFunc(pprofPrefix+"cmdline", pprof.Cmdline)
    55  	mux.HandleFunc(pprofPrefix+"profile", pprof.Profile)
    56  	mux.HandleFunc(pprofPrefix+"symbol", pprof.Symbol)
    57  	mux.HandleFunc(pprofPrefix+"trace", pprof.Trace)
    58  
    59  	enabled := &atomic.Bool{}
    60  	enabled.Store(false)
    61  
    62  	return &ProfilingHandler{
    63  		handler: mux,
    64  		enabled: enabled,
    65  	}
    66  }
    67  
    68  func (h *ProfilingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    69  	if h.enabled.Load() {
    70  		h.handler.ServeHTTP(w, r)
    71  	} else {
    72  		http.NotFound(w, r)
    73  	}
    74  }
    75  
    76  func (h *ProfilingHandler) SetEnabled(enabled bool) (old bool) {
    77  	return h.enabled.Swap(enabled)
    78  }
    79  
    80  // NewServer creates a new http server that exposes profiling data on the default profiling port
    81  func NewProfilingServer() *ProfilingServer {
    82  	port := os.Getenv(ProfilingPortEnvKey)
    83  	if port == "" {
    84  		port = strconv.Itoa(ProfilingDefaultPort)
    85  	}
    86  
    87  	h := NewProfilingHandler()
    88  	return &ProfilingServer{
    89  		ProfilingHandler: h,
    90  		Server: &http.Server{
    91  			Addr:    ":" + port,
    92  			Handler: h,
    93  			// https://medium.com/a-journey-with-go/go-understand-and-mitigate-slowloris-attack-711c1b1403f6
    94  			ReadHeaderTimeout: 15 * time.Second,
    95  		},
    96  	}
    97  }