github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/runsc/metricserver/metricserver_http.go (about)

     1  // Copyright 2023 The gVisor 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  package metricserver
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"net/http"
    21  	"runtime"
    22  	"strings"
    23  	"time"
    24  
    25  	"github.com/ttpreport/gvisor-ligolo/pkg/log"
    26  )
    27  
    28  // httpTimeout is the timeout used for all connect/read/write operations of the HTTP server.
    29  const httpTimeout = 1 * time.Minute
    30  
    31  // httpResult is returned by HTTP handlers.
    32  type httpResult struct {
    33  	code int
    34  	err  error
    35  }
    36  
    37  // httpOK is the "everything went fine" HTTP result.
    38  var httpOK = httpResult{code: http.StatusOK}
    39  
    40  // serveIndex serves the index page.
    41  func (m *metricServer) serveIndex(w http.ResponseWriter, req *http.Request) httpResult {
    42  	if req.URL.Path != "/" {
    43  		if strings.HasPrefix(req.URL.Path, "/metrics?") {
    44  			// Prometheus's scrape_config.metrics_path takes in a query path and automatically encodes
    45  			// all special characters in it to %-form, including the "?" character.
    46  			// This can prevent use of query parameters, and we end up here instead.
    47  			// To address this, rewrite the URL to undo this transformation.
    48  			// This means requesting "/metrics%3Ffoo=bar" is rewritten to "/metrics?foo=bar".
    49  			req.URL.RawQuery = strings.TrimPrefix(req.URL.Path, "/metrics?")
    50  			req.URL.Path = "/metrics"
    51  			return m.serveMetrics(w, req)
    52  		}
    53  		return httpResult{http.StatusNotFound, errors.New("path not found")}
    54  	}
    55  	fmt.Fprintf(w, "<html><head><title>runsc metrics</title></head><body>")
    56  	fmt.Fprintf(w, "<p>You have reached the runsc metrics server page!</p>")
    57  	fmt.Fprintf(w, `<p>To see actual metric data, head over to <a href="/metrics">/metrics</a>.</p>`)
    58  	fmt.Fprintf(w, "</body></html>")
    59  	return httpOK
    60  }
    61  
    62  // logRequest wraps an HTTP handler and adds logging to it.
    63  func logRequest(f func(w http.ResponseWriter, req *http.Request) httpResult) func(w http.ResponseWriter, req *http.Request) {
    64  	return func(w http.ResponseWriter, req *http.Request) {
    65  		log.Infof("Request: %s %s", req.Method, req.URL.Path)
    66  		defer func() {
    67  			if r := recover(); r != nil {
    68  				log.Warningf("Request: %s %s: Panic:\n%v", req.Method, req.URL.Path, r)
    69  			}
    70  		}()
    71  		result := f(w, req)
    72  		if result.err != nil {
    73  			http.Error(w, result.err.Error(), result.code)
    74  			log.Warningf("Request: %s %s: Failed with HTTP code %d: %v", req.Method, req.URL.Path, result.code, result.err)
    75  		}
    76  		// Run GC after every request to keep memory usage as predictable and as flat as possible.
    77  		runtime.GC()
    78  	}
    79  }