Skip to content

Commit

Permalink
First working version
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel González Lopes <[email protected]>
  • Loading branch information
dgzlopes committed Feb 24, 2021
0 parents commit 3fad265
Show file tree
Hide file tree
Showing 1,153 changed files with 539,875 additions and 0 deletions.
Empty file added LICENSE
Empty file.
Empty file added README.md
Empty file.
22 changes: 22 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { check } from 'k6';
import remote from 'k6/x/remotewrite';

export let options = {
vus: 10,
duration: '10s',
};

const client = new remote.Client({
endpoint: "<your-remote-write-endpoint>"
});

export default function () {
let res = client.storeNow({
"__name__": `foo_bar${__VU}`,
"foo": "bar"
}, 12356)
check(res, {
'is status 200': (r) => r.status === 200,
});
sleep(1)
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/dgzlopes/xk6-remote-write

go 1.15

require (
github.com/loadimpact/k6 v0.30.0
github.com/m3db/prometheus_remote_client_golang v0.4.4
)
660 changes: 660 additions & 0 deletions go.sum

Large diffs are not rendered by default.

122 changes: 122 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package remotewrite

import (
"context"
"errors"
"fmt"
"log"
"strconv"
"time"

"github.com/loadimpact/k6/js/common"
"github.com/loadimpact/k6/js/modules"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/stats"
"github.com/m3db/prometheus_remote_client_golang/promremote"
)

type dp promremote.Datapoint

// Register the extension on module initialization, available to
// import from JS as "k6/x/remotewrite".
func init() {
modules.Register("k6/x/remotewrite", new(RemoteWrite))
}

// RemoteWrite is the k6 extension for interacting with Kubernetes jobs.
type RemoteWrite struct {
}

// Client is the Kubernetes client wrapper.
type Client struct {
client promremote.Client
}

type Config struct {
Endpoint string
UserAgent string
}

type Response struct {
Status int
}

// XClient represents
func (r *RemoteWrite) XClient(ctxPtr *context.Context, config Config) interface{} {
if config.UserAgent == "" {
config.UserAgent = "k6-remote-write/0.0.1"
}
rt := common.GetRuntime(*ctxPtr)
cfg := promremote.NewConfig(
promremote.WriteURLOption(config.Endpoint),
promremote.UserAgent(config.UserAgent),
)
client, err := promremote.NewClient(cfg)
if err != nil {
log.Fatal(fmt.Errorf("unable to construct client: %v", err))
}
return common.Bind(rt, &Client{client: client}, ctxPtr)
}

// Store a metric
func (c *Client) Store(ctx context.Context, labels map[string]string, value float64, timestamp time.Time) (Response, error) {
var labelList []promremote.Label

for labelName, labelValue := range labels {
labelList = append(labelList, promremote.Label{
Name: labelName,
Value: labelValue,
})
}

tsList := promremote.TSList{
{
Labels: labelList,
Datapoint: promremote.Datapoint(promremote.Datapoint{
Value: value,
Timestamp: timestamp,
}),
},
}

state := lib.GetState(ctx)
err := errors.New("State is nil")

if state == nil {
return Response{}, err
}

stats.PushIfNotDone(ctx, state.Samples, stats.Sample{
Metric: RequestsTotal,
Time: time.Time{},
Value: float64(1),
})

start := time.Now()
result, writeErr := c.client.WriteTimeSeries(context.Background(), tsList, promremote.WriteOptions{})
elapsed := time.Since(start)

if err := error(writeErr); err != nil {
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{
Metric: RequestsDuration,
Tags: stats.IntoSampleTags(&map[string]string{"status": strconv.Itoa(writeErr.StatusCode())}),
Time: time.Time{},
Value: float64(elapsed.Milliseconds()),
})
return Response{Status: writeErr.StatusCode()}, err
}

stats.PushIfNotDone(ctx, state.Samples, stats.Sample{
Metric: RequestsDuration,
Tags: stats.IntoSampleTags(&map[string]string{"status": strconv.Itoa(result.StatusCode)}),
Time: time.Time{},
Value: float64(elapsed.Milliseconds()),
})

return Response{Status: result.StatusCode}, nil
}

// StoreNow a metric
func (c *Client) StoreNow(ctx context.Context, labels map[string]string, value float64) (Response, error) {
return c.Store(ctx, labels, value, time.Now())
}
9 changes: 9 additions & 0 deletions stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package remotewrite

import "github.com/loadimpact/k6/stats"

var (
RequestsTotal = stats.New("remote_write_req_total", stats.Counter)

RequestsDuration = stats.New("remote_write_req_duration", stats.Trend, stats.Time)
)
27 changes: 27 additions & 0 deletions vendor/github.com/dlclark/regexp2/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vendor/github.com/dlclark/regexp2/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

133 changes: 133 additions & 0 deletions vendor/github.com/dlclark/regexp2/ATTRIB

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/dlclark/regexp2/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3fad265

Please sign in to comment.