github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/analytics/metrics.go (about)

     1  package analytics
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/prometheus/client_golang/prometheus"
     9  	"github.com/prometheus/common/expfmt"
    10  	"github.com/pyroscope-io/pyroscope/pkg/util/slices"
    11  	"github.com/sirupsen/logrus"
    12  )
    13  
    14  var metricsToCollect = []string{
    15  	"go_info",
    16  	"pyroscope_build_info",
    17  	"process_cpu_seconds_total",
    18  	"process_start_time_seconds",
    19  	"go_goroutines",
    20  	"go_memstats_sys_bytes",
    21  	"pyroscope_parser_incoming_requests_total",
    22  	"pyroscope_parser_incoming_requests_bytes",
    23  }
    24  
    25  func (s *Service) sendMetrics() {
    26  	res, err := prometheus.DefaultGatherer.Gather()
    27  	if err != nil {
    28  		logrus.WithError(err).Error("failed to gather prometheus metrics")
    29  	}
    30  	tmp := &bytes.Buffer{}
    31  	for _, g := range res {
    32  		if slices.StringContains(metricsToCollect, *g.Name) {
    33  			expfmt.MetricFamilyToText(tmp, g)
    34  		}
    35  	}
    36  	url := fmt.Sprintf(host+"/metrics/job/analytics/install_id/%s", s.s.InstallID())
    37  	http.Post(url, "text/plain", tmp)
    38  }