Skip to content

Merge Prometheus remote write output #4519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 183 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
183 commits
Select commit Hold shift + click to select a range
c70cbd6
Initial commit
yorugac Sep 13, 2021
6776a0f
add initial working extension
yorugac Sep 15, 2021
24fa30b
full refactor to fit current k6 and prometheus logic details
yorugac Sep 20, 2021
ab32194
add setup and logic for TLS and authentication
yorugac Sep 20, 2021
11c93d8
add unit tests for config and fixes
yorugac Sep 28, 2021
fdeb22e
more cleanup and add README
yorugac Sep 30, 2021
dfb1199
add groundwork for k6 metric types
yorugac Sep 30, 2021
a30cb0f
update README
yorugac Oct 1, 2021
c47c8b2
add basic support for Prometheus as remote agent and refactor
yorugac Oct 6, 2021
65ff881
switch on labels and update README
yorugac Oct 7, 2021
a5d64f1
add Trend for Prometheus and clean up tags processing
yorugac Oct 11, 2021
1acf540
add configurable mapping of metrics
yorugac Oct 11, 2021
0954a1c
add workarounds for overloaded remote instance and update README
yorugac Oct 12, 2021
017f430
prometheus: add ad-hoc optimization for trends
yorugac Oct 13, 2021
54b1125
update deps
yorugac Oct 26, 2021
97ccc54
add CI; bugfix for median
yorugac Oct 26, 2021
cf6dbd6
ci: change check deps to test-build
yorugac Oct 26, 2021
41e41ed
ci: fix typo
yorugac Oct 26, 2021
591f19a
refactor; unit tests; update README
yorugac Oct 29, 2021
52c4189
Add http headers support for write api. (#7)
johncming Dec 14, 2021
f5c3d42
add Grafana dashboard to example
yorugac Dec 16, 2021
a133ad2
Adding build with docker and updating docker-compose
javaducky Mar 10, 2022
c1026c9
Moving and renaming existing dashboard
javaducky Mar 10, 2022
95f025a
Tweaking instructions for running against docker
javaducky Mar 16, 2022
d8c2b37
Remove tagging from the example script
javaducky Mar 16, 2022
a2eea72
add keepUrlTag config bool
chr15murray Mar 30, 2022
4127047
add keepUrlTag tests
chr15murray Mar 30, 2022
0751e79
Merge pull request #17 from chr15murray/main
javaducky Mar 30, 2022
d50ae15
Providing Makefile and CoC as well as minor README tweaks
javaducky Mar 30, 2022
1ee039b
Refactor usage of stats to metrics as required for k6 v0.38.0
javaducky Apr 15, 2022
5716e72
Renabled the goproxy for the CI
codebien May 5, 2022
656a93e
Merge pull request #20 from grafana/not-use-godirect
codebien May 5, 2022
bc04dfa
Bumped the k6 version to v0.38.0
codebien May 5, 2022
a79a353
Merge pull request #18 from grafana/paul/refactor-stats
codebien May 5, 2022
deca0b9
Set the scheme for prometheus datasource
codebien May 26, 2022
78df114
Merge pull request #22 from grafana/fix-docker-compose
codebien May 30, 2022
a97d401
Not sink the global metric
codebien May 26, 2022
70a9df8
Removed the hack for duplicates
codebien Jun 6, 2022
5ac1293
Merge pull request #23 from grafana/local-synk
codebien Jun 6, 2022
27e19ca
chore: fix this is not TTY
jwcastillo Jul 10, 2022
619ef93
chore: add new WIP dashboard and add support for all scenaio variable
jwcastillo Jul 10, 2022
23f9041
Merge pull request #29 from jwcastillo/new_dashboards
codebien Jul 12, 2022
48bf738
Send the trend count as a metric (#31)
robholland Jul 13, 2022
dd23b3c
Send trend sum as metric
pablochacin Aug 10, 2022
4ce693a
chore: set folder structure like k6 repo
jwcastillo Aug 13, 2022
22cf50d
chore: fix typo
jwcastillo Aug 13, 2022
a334561
chore: update grafana dashboard, add filter
jwcastillo Aug 13, 2022
b0581fe
Merge pull request #35 from jwcastillo/set_k6_repo_structure
javaducky Aug 15, 2022
d896b3e
Add test for MapTrend
pablochacin Aug 18, 2022
6d82624
Merge pull request #34 from pablochacin/send-trend-sem-metric
codebien Aug 18, 2022
253b51e
Sink by time series (#38)
codebien Sep 2, 2022
18a2efb
Refresh the CI steps
codebien Sep 27, 2022
0087358
Use golangci-lint action
codebien Sep 27, 2022
e2aa742
fix the env var definition in lint step
codebien Sep 27, 2022
e9a7f6b
Migrate to k6 v0.40.0
codebien Sep 27, 2022
f81cb1f
Merge pull request #42 from grafana/v040
javaducky Sep 27, 2022
817d382
Chore: minor correction to docker-compose command in readme
javaducky Sep 27, 2022
bc1906b
Bumped versions in Dockerfile
codebien Sep 29, 2022
f13288c
chore: replace dashboard, add new example
jwcastillo Oct 3, 2022
51d40a6
chore: panel improvements
jwcastillo Oct 8, 2022
08298f6
chore: panel improvements, add check wip, rps wip
jwcastillo Oct 11, 2022
786230d
Relicense for aligning to the same k6's license
codebien Nov 2, 2022
4a18a4d
config: Simplified and made k6 consistent
codebien Oct 20, 2022
fd893a1
Print the URL into description
codebien Oct 21, 2022
3743b65
Updated auth config example
codebien Oct 24, 2022
e33a841
Removed the prometheus/prometheus go library
codebien Sep 14, 2022
2595b16
Bump k6 version
codebien Nov 8, 2022
6c3f96a
Use directly metrics.TimeSeries
codebien Nov 8, 2022
626fcaa
Migrated to k6.metrics.TrendSink
codebien Nov 17, 2022
2948b3f
Bump k6 version to master
codebien Nov 17, 2022
7d31ae2
Sort labels with lexicographic order
codebien Nov 18, 2022
98cd4c6
Moved Prompb mapping to the TrendSink
codebien Nov 22, 2022
b72ddfb
Renamed to CacheNameIndex
codebien Nov 22, 2022
9577fd0
Relax response status code policy
codebien Nov 24, 2022
00146fe
Fixed expected names for Username option
codebien Nov 24, 2022
58e1a21
Migrated to helm/v3/strvals (#63)
arukiidou Nov 25, 2022
7ef3936
add links to k6
arukiidou Nov 25, 2022
50a1ded
Map Trend metrics as Native Histograms
codebien Nov 3, 2022
75e14af
Document the Native Histogram support
codebien Nov 23, 2022
667a4a4
Added Trend sinks benchmarks
codebien Nov 23, 2022
7ffa969
Add the name suffix convention for Native Histograms
codebien Nov 24, 2022
0cf39ad
Merge pull request #46 from jwcastillo/new_version_dashboard
javaducky Nov 28, 2022
715d57f
Polish from merge request #46
javaducky Nov 28, 2022
f3b7ba6
Configuration refactor for dropping Helm strvals
codebien Nov 29, 2022
391a493
Downgrade null for matching the k6 version
codebien Nov 29, 2022
e5aa4e6
completely drop options from cli argument
codebien Nov 29, 2022
0c8e448
chore: updates dashboard, update readme, clean samples
jwcastillo Nov 30, 2022
20c1153
Merge remote-tracking branch 'origin' into apdex_dashboard
jwcastillo Nov 30, 2022
de2dde1
chore: support native histogram
jwcastillo Nov 30, 2022
3a83317
chore: update prometheus
jwcastillo Nov 30, 2022
c2dc90a
chore: update dashboard
jwcastillo Nov 30, 2022
b34f0b1
Merge pull request #71 from jwcastillo/histogram-native-dashboard
javaducky Nov 30, 2022
bcf82f6
Merge pull request #70 from grafana/drop-cli-config
codebien Dec 1, 2022
7de5db5
Configurable Trend stats mapping
codebien Nov 25, 2022
fddb22f
Use seconds as time unit for Trend stats
codebien Nov 30, 2022
0b4cdbb
Merge pull request #72 from grafana/stats-time-seconds
codebien Dec 1, 2022
fa02497
Fixed mapping flaky test
codebien Dec 1, 2022
a7d3d0e
Force the naming convention
codebien Dec 1, 2022
3e03c29
docker-compose: Bump prometheus version
codebien Dec 1, 2022
446a103
Add dashboard instructions on Readme
ppcano Dec 2, 2022
6d77bf6
Mark all seen series as stale at the end of the test
codebien Nov 30, 2022
c39b472
fix: update dashboards to new naming convention
jwcastillo Dec 6, 2022
d4fecbf
Rename from prometheus-remote to prometheus-rw
codebien Dec 6, 2022
56960bd
Rename to K6_PROMETHEUS_RW_SERVER_URL
codebien Dec 6, 2022
06d4b3f
Renamed config.URL to config.ServerURL
codebien Dec 6, 2022
fb9c076
Drop mention of envconfig
codebien Dec 6, 2022
d7b9001
docs: Output renaming
codebien Dec 6, 2022
664a885
docs: Reverse dashboards order
codebien Dec 6, 2022
4dc5fee
Added Trend stats and dashboard details
codebien Dec 12, 2022
11479e3
Bump compose services' versions
codebien Dec 13, 2022
2ce6cb6
Add k6's linting rules and address detected issues (#83)
oleiade Dec 13, 2022
e4d9ee5
Moved stale marker in a dedicated pkg
codebien Dec 15, 2022
43e6aab
Stale marking process disabled by default
codebien Dec 15, 2022
c331f6a
Fix the default value for InsecureSkipTLS option.
codebien Dec 16, 2022
a2569a4
Bump compose versions
codebien Dec 16, 2022
1190621
Dashboard: Use instant query for counters (#89)
codebien Dec 16, 2022
7aba207
1ms to the stale marker to guarantee the accuracy
codebien Dec 13, 2022
e62cccf
Discard tags with empty fields
codebien Dec 19, 2022
721498a
Including note about availability as an experimental module
javaducky Jan 5, 2023
7d4b8d8
Change emoji for note
javaducky Jan 5, 2023
f32aa7d
dashboard: add code status rate and apdex, improvement checks, update…
jwcastillo Jan 16, 2023
89e9241
Update samples/error.js
jwcastillo Jan 16, 2023
506fd5a
Update samples/scenarios.js
jwcastillo Jan 16, 2023
d81741a
dashboard: resize image, add new image test result
jwcastillo Jan 16, 2023
e62dc06
dashboard: update readme
jwcastillo Jan 16, 2023
bc6cbf4
dashboard: split dashboard and support multiple select testid
jwcastillo Jan 19, 2023
88e422c
docs: update readme
jwcastillo Jan 19, 2023
0d0430f
docs: update readme
jwcastillo Jan 19, 2023
55fe9ae
Merge pull request #98 from jwcastillo/dashboard_apdex
codebien Jan 19, 2023
6e84662
Link the official doc instead of repeating
codebien Jan 19, 2023
c45d40d
dashboard: Various improvements, the instants are changed by range, t…
jwcastillo Feb 10, 2023
142a325
Merge pull request #102 from jwcastillo/main
javaducky Feb 14, 2023
f24df68
Bump k6 to the latest master version
codebien Mar 9, 2023
7891311
Update dockerfile services versions (#112)
jwcastillo Mar 9, 2023
51fc1fd
Minimal readme
codebien Mar 9, 2023
2073cf0
Link the Official dashboard from grafana.com
codebien Mar 9, 2023
16a372f
Add DS_PROMETHEUS variable
7olstoy Apr 1, 2023
6398dc0
fix and update dashboard
jwcastillo Mar 9, 2023
a1fc2d7
update apdex dashboard
jwcastillo Mar 9, 2023
9196383
Bump Grafana version
codebien Apr 12, 2023
ee93fd9
Renamed the datasource
codebien Apr 12, 2023
545ccb6
docker: unpin a ca-certs pkg
olegbespalov May 12, 2023
ea2353a
Update go.k6.io/[email protected]
codebien May 15, 2023
257930f
Bumped docker/docker-compose versions
codebien Jul 7, 2023
312c14d
Update the k6 dependency to the latest master version
na-- Jul 19, 2023
bbc2ad6
Pin the builder stage as alpine3.18
arukiidou Jul 31, 2023
3cc91e1
Migrate to Buf remote packages
pkwarren Jul 10, 2023
c3ad2de
Revert afero lib update
mstoykov Aug 4, 2023
2fff398
Create CODEOWNERS
codebien Aug 8, 2023
1336ff6
Removing in-repo golangci-lint's config
olegbespalov Aug 18, 2023
70ea0ba
chore: minor linter fixes
olegbespalov Aug 18, 2023
ec8bbab
Use re-usable workflow
olegbespalov Aug 22, 2023
1c35a64
Use issue auto-assigner
olegbespalov Aug 24, 2023
57a156f
Bump k6 version
codebien Sep 21, 2023
868c782
Bump k6 to latest master version
codebien Sep 21, 2023
3c2ffd4
Bump Prom client lib
codebien Sep 21, 2023
2c4617a
Bump Prom model lib
codebien Sep 21, 2023
e6cee88
New format for custom HTTP headers (#147)
codebien Sep 25, 2023
5b74564
Support Bearer Token
codebien Sep 22, 2023
5828247
Use klauspost/compress instead of google/snappy
codebien Sep 22, 2023
fcb9604
TLS certificates (#143)
codebien Sep 25, 2023
8aa5595
chore: Small refactor for fixing the linter
codebien Sep 25, 2023
d5e8e4e
Update Dashboards and Docker Compose setup (#148)
ppcano Sep 26, 2023
ae1a805
Update dashboards instructions (#150)
ppcano Sep 28, 2023
66cf3d2
Update readme: usage and development sections (#151)
ppcano Oct 2, 2023
4bf1ab2
Dashboards: fix `HTTP request failures` counter (#153)
ppcano Oct 6, 2023
ec4fa89
Fix the Bearer token parsing
codebien Oct 9, 2023
bffb0b4
Update k6 to v0.47.0
codebien Oct 19, 2023
ffe4dcf
Fix 32-bit build (#158)
mem Dec 19, 2023
7e799e8
Fix Peak RPS calculation, shared tooltip and fix VU display in the Pe…
Sea-you Apr 30, 2024
b42a6af
chore: minor linter fixes
olegbespalov Apr 30, 2024
a504173
Change to using sobek instead of goja
mstoykov Jun 6, 2024
fcf8211
histograms dashboard display avg VUs instead of sum
brunocascio Aug 15, 2024
b7fd687
feat: support sigv4 signing (#169)
obanby Oct 28, 2024
529ed44
Omit parseEnvs linter issue
olegbespalov Oct 28, 2024
a6423f9
Fix mismatch between Requests by URL quantile metrics and console out…
dylanjpaulson Oct 17, 2024
83420e0
do not use testutils
mstoykov Jan 20, 2025
dbe9412
Appease the linter
mstoykov Jan 20, 2025
de80291
Add 'internal/output/prometheusrw/' from commit 'dbe941223b10a8eecd5f…
codebien Feb 7, 2025
c070733
output/promrw: cleanup unnsecessary files
codebien Feb 7, 2025
a4e57aa
output/promrw: Move one package up then drop the useless pkg package
codebien Feb 7, 2025
51cba14
cmd: Import the new internal import for Prometheus output
codebien Feb 7, 2025
28f33c8
Drop the wrong snappy dependency
codebien Feb 7, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ require (
github.com/gorilla/websocket v1.5.3
github.com/grafana/xk6-dashboard v0.7.5
github.com/grafana/xk6-output-opentelemetry v0.3.0
github.com/grafana/xk6-output-prometheus-remote v0.5.1
github.com/grafana/xk6-redis v0.3.3
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/influxdata/influxdb1-client v0.0.0-20190402204710-8ff2fc3824fc
Expand Down Expand Up @@ -57,7 +56,7 @@ require (

require (
buf.build/gen/go/gogo/protobuf/protocolbuffers/go v1.31.0-20210810001428-4df00b267f94.1 // indirect
buf.build/gen/go/prometheus/prometheus/protocolbuffers/go v1.31.0-20230627135113-9a12bc2590d2.1 // indirect
buf.build/gen/go/prometheus/prometheus/protocolbuffers/go v1.31.0-20230627135113-9a12bc2590d2.1
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bufbuild/protocompile v0.14.1 // indirect
Expand All @@ -80,8 +79,8 @@ require (
github.com/nxadm/tail v1.4.11 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/client_golang v1.16.0
github.com/prometheus/client_model v0.4.0
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/r3labs/sse/v2 v2.10.0 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaW
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
Expand All @@ -91,8 +89,6 @@ github.com/grafana/xk6-dashboard v0.7.5 h1:TcILyffT/Ea/XD7xG1jMA5lwtusOPRbEQsQDH
github.com/grafana/xk6-dashboard v0.7.5/go.mod h1:Y75F8xmgCraKT+pBzFH6me9AyH5PkXD+Bxo1dm6Il/M=
github.com/grafana/xk6-output-opentelemetry v0.3.0 h1:dmclGBFtFVRJijqLncpu2dKVIIvx1GS3y6sQGg4Khl8=
github.com/grafana/xk6-output-opentelemetry v0.3.0/go.mod h1:5uUg0J2dxrw+DkC9fCsfiKv2jTpccn7nz6vGmRUr4f8=
github.com/grafana/xk6-output-prometheus-remote v0.5.1 h1:zsB0x/1kuArqnDNh419UWdMUYZwrzqtfjTUswWPIHhI=
github.com/grafana/xk6-output-prometheus-remote v0.5.1/go.mod h1:esXXthLoVp9JUdGkECRthESVYu0TQTR24wrx2nRM9ak=
github.com/grafana/xk6-redis v0.3.3 h1:9QiS4OUjYMzvriGzbqrhCn9i/kENmCkibZE+xCJSdPk=
github.com/grafana/xk6-redis v0.3.3/go.mod h1:YL1qfBZXW4o2aAF6/gRa/N9oGoumUg7cJzzCHLxoCOw=
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI=
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
"go.k6.io/k6/internal/output/cloud"
"go.k6.io/k6/internal/output/influxdb"
"go.k6.io/k6/internal/output/json"
"go.k6.io/k6/internal/output/prometheusrw/remotewrite"
"go.k6.io/k6/lib"
"go.k6.io/k6/output"
"go.k6.io/k6/output/csv"

"github.com/grafana/xk6-dashboard/dashboard"
"github.com/grafana/xk6-output-opentelemetry/pkg/opentelemetry"
"github.com/grafana/xk6-output-prometheus-remote/pkg/remotewrite"
)

// builtinOutput marks the available builtin outputs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"net/url"
"time"

"github.com/grafana/xk6-output-prometheus-remote/pkg/sigv4"
"go.k6.io/k6/internal/output/prometheusrw/sigv4"

prompb "buf.build/gen/go/prometheus/prometheus/protocolbuffers/go"
"github.com/klauspost/compress/snappy"
Expand Down
228 changes: 228 additions & 0 deletions internal/output/prometheusrw/remote/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
package remote

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

prompb "buf.build/gen/go/prometheus/prometheus/protocolbuffers/go"
"github.com/klauspost/compress/snappy"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/internal/output/prometheusrw/stale"
"google.golang.org/protobuf/proto"
)

func TestNewWriteClient(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,
cfg: &HTTPConfig{},
}
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, _ *http.Request) {
http.Error(w, "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,
cfg: &HTTPConfig{},
}
assert.Error(t, c.Store(context.Background(), nil))
}

func TestClientStoreHTTPBasic(t *testing.T) {
t.Parallel()
h := func(_ http.ResponseWriter, r *http.Request) {
u, pwd, ok := r.BasicAuth()
require.True(t, ok)
assert.Equal(t, "usertest", u)
assert.Equal(t, "pwdtest", pwd)
}
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,
cfg: &HTTPConfig{
BasicAuth: &BasicAuth{
Username: "usertest",
Password: "pwdtest",
},
},
}
assert.NoError(t, c.Store(context.Background(), nil))
}

func TestClientStoreHeaders(t *testing.T) {
t.Parallel()
h := func(_ http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Header.Get("X-Prometheus-Remote-Write-Version"), "0.1.0")
assert.Equal(t, r.Header.Get("X-MY-CUSTOM-HEADER"), "fake")
}
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,
cfg: &HTTPConfig{
Headers: http.Header(map[string][]string{
"X-MY-CUSTOM-HEADER": {"fake"},
// If the same key, of a mandatory protocol's header
// is provided, it will be overwritten.
"X-Prometheus-Remote-Write-Version": {"fake"},
}),
},
}
assert.NoError(t, c.Store(context.Background(), nil))
}

func TestNewWriteRequestBody(t *testing.T) {
t.Parallel()
ts := []*prompb.TimeSeries{
{
Labels: []*prompb.Label{{Name: "label1", Value: "val1"}},
Samples: []*prompb.Sample{{Value: 10.1, Timestamp: time.Unix(1, 0).Unix()}},
},
}
b, err := newWriteRequestBody(ts)
require.NoError(t, err)
require.NotEmpty(t, string(b))
assert.Contains(t, string(b), `label1`)
}

func TestNewWriteRequestBodyWithStaleMarker(t *testing.T) {
t.Parallel()

timestamp := time.Date(2022, time.December, 15, 11, 41, 18, 123, time.UTC)

ts := []*prompb.TimeSeries{
{
Labels: []*prompb.Label{{Name: "label1", Value: "val1"}},
Samples: []*prompb.Sample{{
Value: stale.Marker,
Timestamp: timestamp.UnixMilli(),
}},
},
}
b, err := newWriteRequestBody(ts)
require.NoError(t, err)
require.NotEmpty(t, b)

sb, err := snappy.Decode(nil, b)
require.NoError(t, err)

var series prompb.WriteRequest
err = proto.Unmarshal(sb, &series)
require.NoError(t, err)
require.NotEmpty(t, series.Timeseries[0])
require.NotEmpty(t, series.Timeseries[0].Samples)

assert.True(t, math.IsNaN(series.Timeseries[0].Samples[0].Value))
assert.Equal(t, timestamp.UnixMilli(), series.Timeseries[0].Samples[0].Timestamp)
}

func TestValidateStatusCode(t *testing.T) {
t.Parallel()
tests := []struct {
status int
expErr bool
}{
{status: http.StatusOK, expErr: false}, // Mimir
{status: http.StatusNoContent, expErr: false}, // Prometheus
{status: http.StatusBadRequest, expErr: true},
}
for _, tt := range tests {
err := validateResponseStatus(tt.status)
if tt.expErr {
assert.Error(t, err)
continue
}
assert.NoError(t, err)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"strings"
"time"

"github.com/grafana/xk6-output-prometheus-remote/pkg/sigv4"
"go.k6.io/k6/internal/output/prometheusrw/sigv4"

"github.com/grafana/xk6-output-prometheus-remote/pkg/remote"
"go.k6.io/k6/internal/output/prometheusrw/remote"
"go.k6.io/k6/lib/types"
"gopkg.in/guregu/null.v3"
)
Expand Down
Loading
Loading