Skip to content

Commit

Permalink
fixup! Removed the prometheus/prometheus go library
Browse files Browse the repository at this point in the history
  • Loading branch information
codebien committed Oct 25, 2022
1 parent 7fff356 commit 5ac5d17
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
20 changes: 14 additions & 6 deletions pkg/remote/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import (
)

type HTTPConfig struct {
UserAgent string
Timeout time.Duration
TLSConfig tls.Config
BasicAuth BasicAuth
TLSConfig *tls.Config
BasicAuth *BasicAuth
Headers http.Header
}

Expand All @@ -44,17 +43,23 @@ func NewWriteClient(endpoint string, cfg *HTTPConfig) (*WriteClient, error) {
if err != nil {
return nil, err
}
return &WriteClient{
wc := &WriteClient{
hc: &http.Client{
Timeout: cfg.Timeout,
},
url: u,
cfg: cfg,
}, nil
}
if cfg.TLSConfig != nil {
wc.hc.Transport = &http.Transport{
TLSClientConfig: cfg.TLSConfig,
}
}
return wc, nil
}

// Store sends a batch of samples to the HTTP endpoint,
// the request is the proto marshalled and encoded.
// the request is the proto marshaled and encoded.
func (c *WriteClient) Store(ctx context.Context, series []*prompb.TimeSeries) error {
b, err := c.requestBody(series)
if err != nil {
Expand All @@ -65,6 +70,9 @@ func (c *WriteClient) Store(ctx context.Context, series []*prompb.TimeSeries) er
if err != nil {
return fmt.Errorf("create new HTTP request failed: %w", err)
}
if c.cfg.BasicAuth != nil {
req.SetBasicAuth(c.cfg.BasicAuth.User, c.cfg.BasicAuth.Password)
}

req.Header.Set("User-Agent", "k6-prometheus-rw-output")

Expand Down
29 changes: 29 additions & 0 deletions pkg/remote/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestClientStore(t *testing.T) {
c := &WriteClient{
hc: ts.Client(),
url: u,
cfg: &HTTPConfig{},
}
data := &prompb.TimeSeries{
Labels: []*prompb.Label{
Expand Down Expand Up @@ -98,6 +99,34 @@ func TestClientStoreHTTPError(t *testing.T) {
c := &WriteClient{
hc: ts.Client(),
url: u,
cfg: &HTTPConfig{},
}
assert.Error(t, c.Store(context.Background(), nil))
}

func TestClientStoreHTTPBasic(t *testing.T) {
t.Parallel()
h := func(w http.ResponseWriter, r *http.Request) {
u, pwd, ok := r.BasicAuth()
require.True(t, ok)
assert.Equal(t, "usertest", u)
assert.Equal(t, "pwdtest", pwd)
}
ts := httptest.NewServer(http.HandlerFunc(h))
defer ts.Close()

u, err := url.Parse(ts.URL)
require.NoError(t, err)

c := &WriteClient{
hc: ts.Client(),
url: u,
cfg: &HTTPConfig{
BasicAuth: &BasicAuth{
User: "usertest",
Password: "pwdtest",
},
},
}
assert.Error(t, c.Store(context.Background(), nil))
}
4 changes: 2 additions & 2 deletions pkg/remotewrite/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ func (conf Config) RemoteConfig() (*remote.HTTPConfig, error) {
Timeout: defaultTimeout,
}

hc.TLSConfig = tls.Config{
hc.TLSConfig = &tls.Config{
InsecureSkipVerify: conf.InsecureSkipTLSVerify.Bool,
}

// if at least valid user was configured, use basic auth
if conf.User.Valid {
hc.BasicAuth = remote.BasicAuth{
hc.BasicAuth = &remote.BasicAuth{
User: conf.User.String,
Password: conf.Password.String,
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/remotewrite/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ func TestConstructRemoteConfig(t *testing.T) {
headers.Set("X-MYCUSTOM-HEADER", "val1")
exprcc := &remote.HTTPConfig{
Timeout: 5 * time.Second,
TLSConfig: tls.Config{
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
BasicAuth: remote.BasicAuth{
BasicAuth: &remote.BasicAuth{
User: "myuser",
Password: "mypass",
},
Expand Down

0 comments on commit 5ac5d17

Please sign in to comment.