github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/tools/query-audit/runner.go (about)

     1  package main
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/prometheus/client_golang/api"
     7  	v1 "github.com/prometheus/client_golang/api/prometheus/v1"
     8  	"github.com/prometheus/client_golang/prometheus/promhttp"
     9  )
    10  
    11  // NewAPI instantiates a prometheus api
    12  func NewAPI(backend Backend) (v1.API, error) {
    13  	config := api.Config{
    14  		Address: backend.Host,
    15  	}
    16  
    17  	if len(backend.Headers) > 0 {
    18  		config.RoundTripper = promhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
    19  			for key, value := range backend.Headers {
    20  				req.Header.Add(key, value)
    21  			}
    22  			return http.DefaultTransport.RoundTrip(req)
    23  		})
    24  	}
    25  
    26  	c, err := api.NewClient(config)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	return v1.NewAPI(c), nil
    32  }