Skip to content

Commit

Permalink
Removed the prometheus/prometheus go library
Browse files Browse the repository at this point in the history
It has been replaced with a lighter version based on buf.build
  • Loading branch information
codebien committed Oct 21, 2022
1 parent bc1906b commit 504cffa
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 2,153 deletions.
38 changes: 8 additions & 30 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,58 +1,36 @@
module github.com/grafana/xk6-output-prometheus-remote

go 1.17
go 1.18

require (
github.com/golang/protobuf v1.5.2
github.com/gogo/protobuf v1.3.2
github.com/golang/snappy v0.0.4
github.com/kubernetes/helm v2.17.0+incompatible
github.com/prometheus/common v0.32.1
github.com/prometheus/prometheus v1.8.2-0.20211005150130-f29caccc4255
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.8.0
go.buf.build/grpc/go/prometheus/prometheus v1.4.3
go.k6.io/k6 v0.40.0
gopkg.in/guregu/null.v3 v3.5.0
)

require (
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 // indirect
github.com/aws/aws-sdk-go v1.40.37 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dennwc/varint v1.0.0 // indirect
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-kit/log v0.1.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/opentracing-contrib/go-stdlib v1.0.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common/sigv4 v0.1.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/spf13/afero v1.3.4 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/net v0.0.0-20220630215102-69896b714898 // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
go.buf.build/grpc/go/gogo/protobuf v1.4.9 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
google.golang.org/genproto v0.0.0-20210903162649-d08c68adba83 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
1,782 changes: 7 additions & 1,775 deletions go.sum

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions pkg/remote/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package remote

import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"time"

"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
prompb "go.buf.build/grpc/go/prometheus/prometheus"
)

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

type BasicAuth struct {
User, Password string
}

type WriteClient struct {
hc *http.Client
url *url.URL
cfg HTTPConfig
}

func NewWriteClient(endpoint string, cfg *HTTPConfig) (*WriteClient, error) {
if cfg == nil {
cfg = &HTTPConfig{}
}
u, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
return &WriteClient{
hc: &http.Client{
Timeout: cfg.Timeout,
},
url: u,
cfg: *cfg,
}, nil
}

// Store sends a batch of samples to the HTTP endpoint,
// the request is the proto marshalled and encoded.
func (c *WriteClient) Store(ctx context.Context, series []*prompb.TimeSeries) error {
b, err := proto.Marshal(&prompb.WriteRequest{
Timeseries: series,
})
if err != nil {
return fmt.Errorf("encoding series as protobuf write request failed: %w", err)
}
b = snappy.Encode(nil, b) // TODO: this call can panic
req, err := http.NewRequest(http.MethodPost, c.url.String(), bytes.NewReader(b))
if err != nil {
return fmt.Errorf("create new HTTP request failed: %w", err)
}

req.Header.Add("Content-Encoding", "snappy")
req.Header.Set("Content-Type", "application/x-protobuf")
req.Header.Set("User-Agent", "k6-prometheus-rw-output")
req.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.0")

resp, err := c.hc.Do(req)
if err != nil {
return fmt.Errorf("HTTP POST request failed: %w", err)
}
defer resp.Body.Close()
io.Copy(ioutil.Discard, resp.Body)

if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("got status code: %s instead expected: 204 No Content", resp.Status)
}
return nil
}
103 changes: 103 additions & 0 deletions pkg/remote/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package remote

import (
"context"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
prompb "go.buf.build/grpc/go/prometheus/prometheus"
)

func TestNewWrtiteClient(t *testing.T) {
t.Parallel()
t.Run("DefaultConfig", func(t *testing.T) {
t.Parallel()
wc, err := NewWriteClient("http://example.com/api/v1/write", nil)
require.NoError(t, err)
require.NotNil(t, wc)
assert.Equal(t, wc.cfg, HTTPConfig{})
})

t.Run("CustomConfig", func(t *testing.T) {
t.Parallel()
hc := HTTPConfig{Timeout: time.Second}
wc, err := NewWriteClient("http://example.com/api/v1/write", &hc)
require.NoError(t, err)
require.NotNil(t, wc)
assert.Equal(t, wc.cfg, hc)
})

t.Run("InvalidURL", func(t *testing.T) {
t.Parallel()
wc, err := NewWriteClient("fake://bad url", nil)
require.Error(t, err)
assert.Nil(t, wc)
})
}

func TestClientStore(t *testing.T) {
t.Parallel()
h := func(rw http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Header.Get("Content-Encoding"), "snappy")
assert.Equal(t, r.Header.Get("Content-Type"), "application/x-protobuf")
assert.Equal(t, r.Header.Get("User-Agent"), "k6-prometheus-rw-output")
assert.Equal(t, r.Header.Get("X-Prometheus-Remote-Write-Version"), "0.1.0")
assert.NotEmpty(t, r.Header.Get("Content-Length"))

b, err := io.ReadAll(r.Body)
assert.NoError(t, err)
assert.NotEmpty(t, len(b))

rw.WriteHeader(http.StatusNoContent)
}
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,
}
data := &prompb.TimeSeries{
Labels: []*prompb.Label{
{
Name: "label1",
Value: "label1-val",
},
},
Samples: []*prompb.Sample{
{
Value: 8.5,
Timestamp: time.Now().UnixMilli(),
},
},
}
err = c.Store(context.Background(), []*prompb.TimeSeries{data})
assert.NoError(t, err)
}

func TestClientStoreHTTPError(t *testing.T) {
t.Parallel()
h := func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "bad bad", http.StatusUnauthorized)
}
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,
}
assert.Error(t, c.Store(context.Background(), nil))
}
Loading

0 comments on commit 504cffa

Please sign in to comment.