Skip to content

Commit

Permalink
Merge pull request #12 from grafana/templateLabels
Browse files Browse the repository at this point in the history
Template labels
  • Loading branch information
replay authored Jan 27, 2022
2 parents 88cd268 + c996566 commit 8127438
Show file tree
Hide file tree
Showing 6 changed files with 410 additions and 43 deletions.
44 changes: 42 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Then:
$ xk6 build --with github.com/grafana/xk6-client-prometheus-remote@latest
```

## Example
## Basic Example

```javascript
import { check, sleep } from 'k6';
Expand Down Expand Up @@ -100,4 +100,44 @@ default ✓ [======================================] 10 VUs 10s
vus........................: 10 min=10 max=10
vus_max....................: 10 min=10 max=10
```
Inspect examples folder for more details.
Inspect examples folder for more details.
## More advanced use case
The above example shows how you can generate samples in JS and pass them into the extension, the extension will then submit them to the remote_write API endpoint.
When you want to produce samples at a very high rate the overhead which is involved when passing objects from the JS runtime to the extension can get expensive though,
to optimize this the extension also offers the option for the user to pass it a template based on which samples should be generated and then sent,
by generating the samples inside the extension the overhead of passing objects from the JS runtime to the extension can be avoided.
```javascript
const template = {
__name__: 'k6_generated_metric_${series_id/4}', // Name of the series.
series_id: '${series_id}', // Each value of this label will match 1 series.
cardinality_1e1: '${series_id/10}', // Each value of this label will match 10 series.
};
write_client.storeFromTemplates(
100, // minimum random value
200, // maximum random value
1643235433 * 1000, // timestamp in ms
42, // series id range start
45, // series id range end
template,
);
```
The above code could generate and send the following 3 samples, values are randomly chosen from the defined range:
```
Metric: k6_generated_metric_10{cardinality_1e1="4", series_id="42"},
Timestamp: 16432354331000
Value: 193
---
Metric: k6_generated_metric_10{cardinality_1e1="4", series_id="43"},
Timestamp: 16432354331000
Value: 121
---
Metric: k6_generated_metric_11{cardinality_1e1="4", series_id="44"},
Timestamp: 16432354331000
Value: 142
```
31 changes: 31 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package remotewrite

import "testing"

func BenchmarkCompileTemplatesSimple(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = compileTemplate("something ${series_id} else")
}
}

func BenchmarkCompileTemplatesComplex(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = compileTemplate("something ${series_id/1000} else")
}
}

func BenchmarkEvaluateTemplatesSimple(b *testing.B) {
t := compileTemplate("something ${series_id} else")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = t(1151234)
}
}

func BenchmarkEvaluateTemplatesComplex(b *testing.B) {
t := compileTemplate("something ${series_id/1000} else")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = t(1151234)
}
}
36 changes: 34 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,44 @@
module github.com/grafana/xk6-client-prometheus-remote

go 1.16
go 1.17

require (
github.com/golang/protobuf v1.5.2
github.com/golang/snappy v0.0.4
github.com/pkg/errors v0.9.1
github.com/prometheus/prometheus v1.8.2-0.20210621150501-ff58416a0b02
github.com/stretchr/testify v1.7.0
github.com/xhit/go-str2duration/v2 v2.0.0
go.k6.io/k6 v0.34.1
go.k6.io/k6 v0.36.0
)

require (
github.com/Azure/go-ntlmssp v0.0.0-20211209120228-48547f28849e // indirect
github.com/Soontao/goHttpDigestClient v0.0.0-20170320082612-6d28bb1415c5 // indirect
github.com/andybalholm/brotli v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91 // indirect
github.com/dop251/goja v0.0.0-20220110113543-261677941f3c // indirect
github.com/fatih/color v1.12.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.4-0.20211119122758-180fcef48034+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/afero v1.2.2 // indirect
golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e // indirect
golang.org/x/net v0.0.0-20211209100829-84cba5454caf // indirect
golang.org/x/sys v0.0.0-20210611083646-a4fc73990273 // indirect
golang.org/x/text v0.3.7-0.20210503195748-5c7c50ebbd4f // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/guregu/null.v3 v3.3.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading

0 comments on commit 8127438

Please sign in to comment.