func()

in pkg/metrics/metrics.go [231:273]


func (c *metricsCollector) Collect(ch chan<- prometheus.Metric) {
	pod, err := c.clientset.GetPod(c.namespace, c.podName)
	if err != nil || pod == nil || pod.Status.Phase != corev1.PodRunning || pod.DeletionTimestamp != nil {
		klog.V(6).Infof("pod %v/%v does not exist, skip metrics scraping", c.namespace, c.podName)

		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	req, err := http.NewRequestWithContext(ctx, http.MethodGet, unixURL, nil)
	if err != nil {
		klog.Errorf("failed to create scrape metrics request: %v", err)

		return
	}

	resp, err := c.httpClient.Do(req)
	if err != nil {
		klog.Errorf("failed to scrape metrics: %v", err)

		return
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		klog.Errorf("unexpected HTTP status: %v", resp.Status)

		return
	}

	families, err := ProcessMetricsData(resp.Body)
	if err != nil {
		klog.Errorf("failed to process metrics data: %v", err)

		return
	}

	for _, mf := range families {
		c.emitMetricFamily(mf, ch)
	}
}