From 88cd26879190f8ccb184deaa544375980d62229b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gonz=C3=A1lez?= Date: Fri, 29 Oct 2021 10:02:34 +0200 Subject: [PATCH] Add native constructors (#6) * Add sample and timeseries constructor * Don't keep the request body Co-authored-by: Mihail Stoykov --- remote_write.go | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/remote_write.go b/remote_write.go index 4978147..ec5d79e 100644 --- a/remote_write.go +++ b/remote_write.go @@ -27,8 +27,7 @@ func init() { } // RemoteWrite is the k6 extension for interacting Prometheus Remote Write endpoints. -type RemoteWrite struct { -} +type RemoteWrite struct{} // Client is the client wrapper. type Client struct { @@ -63,14 +62,37 @@ func (r *RemoteWrite) XClient(ctxPtr *context.Context, config Config) interface{ } type Timeseries struct { - Labels []struct { - Name string `json:"name"` - Value string `json:"value"` - } `json:"labels"` - Samples []struct { - Value float64 `json:"value"` - Timestamp int64 `json:"timestamp"` - } `json:"samples"` + Labels []Label + Samples []Sample +} + +type Label struct { + Name, Value string +} + +type Sample struct { + Value float64 + Timestamp int64 +} + +func (r *RemoteWrite) XSample(value float64, timestamp int64) Sample { + return Sample{ + Value: value, + Timestamp: timestamp, + } +} + +func (r *RemoteWrite) XTimeseries(labels map[string]string, samples []Sample) *Timeseries { + t := &Timeseries{ + Labels: make([]Label, 0, len(labels)), + Samples: samples, + } + + for k, v := range labels { + t.Labels = append(t.Labels, Label{Name: k, Value: v}) + } + + return t } func (c *Client) Store(ctx context.Context, ts []Timeseries) (httpext.Response, error) { @@ -100,6 +122,7 @@ func (c *Client) Store(ctx context.Context, ts []Timeseries) (httpext.Response, if err != nil { return *httpext.NewResponse(ctx), errors.Wrap(err, "remote-write request failed") } + res.Request.Body = "" return res, nil }