github.com/openshift/installer@v1.4.17/pkg/metrics/pushclient/pushclient.go (about)

     1  package pushclient
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/pkg/errors"
     7  	"github.com/prometheus/client_golang/prometheus"
     8  	"github.com/prometheus/client_golang/prometheus/push"
     9  	"github.com/prometheus/common/expfmt"
    10  )
    11  
    12  // PushClient is used to send Prometheus metrics objects to any Prometheus
    13  // Push Gateway. It stores the URL and the client that will be used to push
    14  // to the desired gateway.
    15  type PushClient struct {
    16  	URL     string
    17  	Client  *http.Client
    18  	JobName string
    19  }
    20  
    21  // Push uses all the configuration settings from the client and pushes to the prometheus
    22  // aggregation gateway. It takes in an additional list of collectors and pushes all of
    23  // them to the previously configured url.
    24  func (p *PushClient) Push(collectors ...prometheus.Collector) error {
    25  	pushClient := push.New(p.URL, p.JobName).Client(p.Client).Format(expfmt.NewFormat(expfmt.TypeTextPlain))
    26  
    27  	for _, value := range collectors {
    28  		pushClient.Collector(value)
    29  	}
    30  
    31  	err := pushClient.Push()
    32  	if err != nil {
    33  		return errors.Wrap(err, "failed to push metrics")
    34  	}
    35  	return nil
    36  }