github.com/grafana/synthetic-monitoring-agent@v0.64.2/internal/pusher/clients.go (about)

     1  package pusher
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"time"
     7  
     8  	"github.com/grafana/synthetic-monitoring-agent/internal/pkg/prom"
     9  	"github.com/grafana/synthetic-monitoring-agent/internal/version"
    10  	sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
    11  )
    12  
    13  func ClientFromRemoteInfo(remote *sm.RemoteInfo) (*prom.ClientConfig, error) {
    14  	// TODO(mem): this is hacky.
    15  	//
    16  	// it's trying to deal with the fact that the URL shown to users
    17  	// is not the push URL but the base for the API endpoints
    18  	u, err := url.Parse(remote.Url + "/push")
    19  	if err != nil {
    20  		// XXX(mem): do we really return an error here?
    21  		return nil, fmt.Errorf("parsing URL: %w", err)
    22  	}
    23  
    24  	clientCfg := prom.ClientConfig{
    25  		URL:       u,
    26  		Timeout:   5 * time.Second,
    27  		UserAgent: version.UserAgent(),
    28  	}
    29  
    30  	if remote.Username != "" {
    31  		clientCfg.HTTPClientConfig.BasicAuth = &prom.BasicAuth{
    32  			Username: remote.Username,
    33  			Password: remote.Password,
    34  		}
    35  	}
    36  
    37  	if clientCfg.Headers == nil {
    38  		clientCfg.Headers = make(map[string]string)
    39  	}
    40  
    41  	clientCfg.Headers["X-Prometheus-Remote-Write-Version"] = "0.1.0"
    42  
    43  	return &clientCfg, nil
    44  }