...

Source file src/github.com/linkerd/linkerd2/pkg/k8s/metrics.go

Documentation: github.com/linkerd/linkerd2/pkg/k8s

     1  package k8s
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net/http"
     7  	"os"
     8  
     9  	corev1 "k8s.io/api/core/v1"
    10  )
    11  
    12  // AdminHTTPPortName is the name of the port used by the admin http server.
    13  const AdminHTTPPortName string = "admin-http"
    14  
    15  // GetContainerMetrics returns the metrics exposed by a container on the passed in portName
    16  func GetContainerMetrics(
    17  	k8sAPI *KubernetesAPI,
    18  	pod corev1.Pod,
    19  	container corev1.Container,
    20  	emitLogs bool,
    21  	portName string,
    22  ) ([]byte, error) {
    23  	portForward, err := NewContainerMetricsForward(k8sAPI, pod, container, emitLogs, portName)
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	defer portForward.Stop()
    29  	if err = portForward.Init(); err != nil {
    30  		fmt.Fprintf(os.Stderr, "Error running port-forward: %s", err)
    31  		return nil, err
    32  	}
    33  
    34  	metricsURL := portForward.URLFor("/metrics")
    35  	return getResponse(metricsURL)
    36  }
    37  
    38  // getResponse makes a http Get request to the passed url and returns the response/error
    39  func getResponse(url string) ([]byte, error) {
    40  	// url has been constructed by k8s.newPortForward and is not passed in by
    41  	// the user.
    42  	//nolint:gosec
    43  	resp, err := http.Get(url)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	defer resp.Body.Close()
    48  	return io.ReadAll(resp.Body)
    49  }
    50  

View as plain text