Skip to content

Commit

Permalink
modify things in factory.go
Browse files Browse the repository at this point in the history
  • Loading branch information
huyan0 committed Aug 4, 2020
1 parent 96807eb commit 4182996
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
4 changes: 2 additions & 2 deletions exporter/cortexexporter/cortex.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
type cortexExporter struct {
namespace string
endpoint string
client http.Client
client *http.Client
wg *sync.WaitGroup
closeChan chan struct{}
}
Expand Down Expand Up @@ -258,7 +258,7 @@ func newCortexExporter(ns string, ep string, client *http.Client) *cortexExporte
return &cortexExporter{
namespace: ns,
endpoint: ep,
client: *client,
client: client,
wg: new(sync.WaitGroup),
closeChan: make(chan struct{}),
}
Expand Down
16 changes: 5 additions & 11 deletions exporter/cortexexporter/cortex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func Test_handleScalarMetric(t *testing.T) {
})
}
}

// export is not complete yet, so the request is never sent, thus the handler function never run
func Test_handleHistogramMetric(t *testing.T) {
sum := "sum"
count := "count"
Expand Down Expand Up @@ -624,7 +624,7 @@ func Test_pushMetrics(t *testing.T) {
assert.NotNil(t, r.Header.Get("Tenant-id"))
wr := &prompb.WriteRequest{}
ok := proto.Unmarshal(body, wr)
require.NotNil(t, ok)
require.Nil(t, ok)
assert.EqualValues(t, 2, len(wr.Timeseries))
},
0,
Expand All @@ -645,16 +645,10 @@ func Test_pushMetrics(t *testing.T) {
serverURL, err := url.Parse(server.URL)
assert.NoError(t, err)

config := &Config{
ExporterSettings: configmodels.ExporterSettings{},
TimeoutSettings: exporterhelper.TimeoutSettings{},
QueueSettings: exporterhelper.QueueSettings{},
RetrySettings: exporterhelper.RetrySettings{},
Namespace: "",
HTTPClientSettings: confighttp.HTTPClientSettings{Endpoint: serverURL.String()},
}
config := createDefaultConfig().(*Config)
config.HTTPClientSettings.Endpoint = serverURL.String()
c, err := config.HTTPClientSettings.ToClient()
assert.NotNil(t,err)
assert.Nil(t,err)
sender := newCortexExporter(config.HTTPClientSettings.Endpoint, config.Namespace, c)

numDroppedTimeSeries, err := sender.pushMetrics(context.Background(), *tt.md)
Expand Down
21 changes: 8 additions & 13 deletions exporter/cortexexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"context"
"net/http"
"time"

"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
Expand All @@ -31,26 +32,20 @@ import (
)

// This will be added to cortex_test, but currently I'm going to put it here in order to not have merge conflicts. Also, will readjust to fit our pipeline, not prometheus
type WriteRequest struct {
Timeseries []TimeSeries `protobuf:"bytes,1,rep,name=timeseries,proto3" json:"timeseries"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}

func (c *Exporter) WrapTimeSeries(ts *prompb.TimeSeries) {
func (c *cortexExporter) WrapTimeSeries(ts *prompb.TimeSeries) {
return //will populate later
}
// To Daniel: I have created a empty version of the Export function in cortex.go. It has takes the parameter we defined
// in the implementation note.
func Export(ctx context.Context, tsMap map[string]*prompb.TimeSeries) error {
func (ce *cortexExporter)Export(ctx context.Context, tsMap map[string]*prompb.TimeSeries) error {
//TODO:: Error handling
data, err := proto.Marshal(req)
data, err := proto.Marshal(&prompb.WriteRequest{})
if err != nil {
return err
}
compressed := snappy.Encode(nil, data)
httpReq, err := http.NewRequest("POST", c.url.String(), bytes.NewReader(compressed))
httpReq, err := http.NewRequest("POST", ce.endpoint, bytes.NewReader(compressed))
if err != nil {
return err
}
Expand All @@ -59,11 +54,11 @@ func Export(ctx context.Context, tsMap map[string]*prompb.TimeSeries) error {
httpReq.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.0")
httpReq = httpReq.WithContext(ctx)

ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(1))
defer cancel()

httpResp, err := ctxhttp.Do(ctx, c.client, httpReq)
return err
_, httpErr := ctxhttp.Do(ctx, ce.client, httpReq)
return httpErr
}

const (
Expand Down

0 comments on commit 4182996

Please sign in to comment.