Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit a133ad2

Browse files
committed
Adding build with docker and updating docker-compose
1 parent f5c3d42 commit a133ad2

File tree

9 files changed

+156
-22
lines changed

9 files changed

+156
-22
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
k6
1+
.DS_Store
2+
k6

Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Multi-stage build to generate custom k6 with extension
2+
FROM golang:1.17-alpine as builder
3+
WORKDIR $GOPATH/src/go.k6.io/k6
4+
ADD . .
5+
RUN apk --no-cache add git
6+
RUN CGO_ENABLED=0 go install go.k6.io/xk6/cmd/xk6@latest
7+
RUN CGO_ENABLED=0 xk6 build \
8+
--with github.com/grafana/xk6-output-prometheus-remote=. \
9+
--output /tmp/k6
10+
11+
# Create image for running k6 with output for Prometheus Remote Write
12+
FROM alpine:3.15
13+
RUN apk add --no-cache ca-certificates \
14+
&& adduser -D -u 12345 -g 12345 k6
15+
COPY --from=builder /tmp/k6 /usr/bin/k6
16+
17+
USER 12345
18+
WORKDIR /home/k6
19+
20+
# By default, the image is setup for running js scripts.
21+
ENTRYPOINT ["k6", "run"]

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,33 @@ If remote endpoint responds too slowly or the k6 test run generates too many met
4949
### Prometheus as remote-write agent
5050

5151
To enable remote write in Prometheus 2.x use `--enable-feature=remote-write-receiver` option. See docker-compose samples in `example/`. Options for remote write storage can be found [here](https://prometheus.io/docs/operating/integrations/).
52+
53+
54+
# Docker Compose
55+
56+
This repo includes a [docker-compose.yml](./docker-compose.yml) file that starts _Prometheus_, _Grafana_, and a custom build of _k6_ having the `xk6-output-prometheus-remote` extension.
57+
58+
> This is just a quick setup to show the usage. For a real use case, you will want to deploy outside of docker.
59+
60+
Clone the repo to get started and follow these steps:
61+
62+
1. Start the docker compose environment.
63+
```shell
64+
docker-compose up -d
65+
```
66+
67+
> Some users have encountered failures for the k6 build portion. A workaround may be to disable the _"Use Docker Compose V2"_ checkbox in the _General_ section of Docker Desktop settings.
68+
69+
```shell
70+
# Output
71+
Creating xk6-output-prometheus-remote_grafana_1 ... done
72+
Creating xk6-output-prometheus-remote_prometheus_1 ... done
73+
Creating xk6-output-prometheus-remote_k6_1 ... done
74+
```
75+
76+
2. Use the k6 Docker image to run the k6 script and send metrics to the Prometheus container started on the previous step. The [test.js](./example/test.js) sets a [test-wide tag](https://k6.io/docs/using-k6/tags-and-groups/#test-wide-tags) with a unique identifier to segment the metrics into discrete test runs for Grafana dashboards.
77+
```shell
78+
docker-compose run --rm k6 -<example/test.js
79+
```
80+
81+
3. Visit http://localhost:3000/ to view results in Grafana.

docker-compose.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
version: '3.8'
2+
3+
networks:
4+
k6:
5+
grafana:
6+
prometheus:
7+
8+
services:
9+
prometheus:
10+
image: prom/prometheus:v2.33.5
11+
command: --web.enable-remote-write-receiver --config.file=/etc/prometheus/prometheus.yml
12+
networks:
13+
- k6
14+
- grafana
15+
- prometheus
16+
ports:
17+
- "9090:9090"
18+
19+
grafana:
20+
image: grafana/grafana:8.2.6
21+
networks:
22+
- grafana
23+
- prometheus
24+
ports:
25+
- "3000:3000"
26+
environment:
27+
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
28+
- GF_AUTH_ANONYMOUS_ENABLED=true
29+
- GF_AUTH_BASIC_ENABLED=false
30+
volumes:
31+
- ./example/config-datasources.yaml:/etc/grafana/provisioning/datasources/datasource.yaml
32+
- ./example/config-dashboards.yaml:/etc/grafana/provisioning/dashboards/dashboards.yaml
33+
- ./example/dashboards:/var/lib/grafana/dashboards
34+
- ./example/grafana.dashboard.json:/var/lib/grafana/dashboards/grafana.dashboard.json
35+
36+
k6:
37+
build: .
38+
networks:
39+
- k6
40+
ports:
41+
- "6565:6565"
42+
environment:
43+
- K6_PROMETHEUS_REMOTE_URL=http://prometheus:9090/api/v1/write
44+
- K6_OUT=output-prometheus-remote

example/config-dashboards.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# For configuration options, see
2+
# https://grafana.com/docs/grafana/latest/administration/provisioning/#dashboards
3+
4+
apiVersion: 1
5+
6+
providers:
7+
# We're defining a directory from which to load file-based dashboards
8+
- name: 'a unique provider name'
9+
type: file
10+
disableDeletion: false
11+
updateIntervalSeconds: 10
12+
allowUiUpdates: false
13+
options:
14+
path: /var/lib/grafana/dashboards
15+
foldersFromFilesStructure: true

example/config-datasources.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# For configuration options, see
2+
# https://grafana.com/docs/grafana/latest/administration/provisioning/#example-data-source-config-file
3+
4+
apiVersion: 1
5+
6+
datasources:
7+
- name: prometheus-in-docker
8+
type: prometheus
9+
access: proxy
10+
url: prometheus:9090
11+
basicAuth: false
12+
isDefault: true
13+
jsonData:
14+
tlsAuth: false
15+
tlsAuthWithCACert: false
16+
editable: false

example/dashboards/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Dashboards
2+
3+
Add custom dashboards here, then start the [docker-compose](../../docker-compose.yml) to have the dashboards provisioned at startup. Use folders to group related dashboards.

example/grafana.dashboard.json

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"__inputs": [
33
{
44
"name": "DATASOURCE",
5-
"label": "rwprometheus",
5+
"label": "Prometheus in Docker",
66
"description": "",
77
"type": "datasource",
88
"pluginId": "prometheus",
9-
"pluginName": "Prometheus"
9+
"pluginName": "prometheus-in-docker"
1010
}
1111
],
1212
"__requires": [
@@ -108,7 +108,7 @@
108108
"type": "row"
109109
},
110110
{
111-
"datasource": "${DATASOURCE}",
111+
"datasource": "prometheus-in-docker",
112112
"fieldConfig": {
113113
"defaults": {
114114
"color": {
@@ -178,7 +178,7 @@
178178
"type": "table"
179179
},
180180
{
181-
"datasource": "${DATASOURCE}",
181+
"datasource": "prometheus-in-docker",
182182
"fieldConfig": {
183183
"defaults": {
184184
"color": {
@@ -264,7 +264,7 @@
264264
"bars": false,
265265
"dashLength": 10,
266266
"dashes": false,
267-
"datasource": "${DATASOURCE}",
267+
"datasource": "prometheus-in-docker",
268268
"fill": 1,
269269
"fillGradient": 0,
270270
"gridPos": {
@@ -373,7 +373,7 @@
373373
"id": 35,
374374
"panels": [
375375
{
376-
"datasource": "${DATASOURCE}",
376+
"datasource": "prometheus-in-docker",
377377
"fieldConfig": {
378378
"defaults": {
379379
"color": {
@@ -463,7 +463,7 @@
463463
"type": "timeseries"
464464
},
465465
{
466-
"datasource": "${DATASOURCE}",
466+
"datasource": "prometheus-in-docker",
467467
"fieldConfig": {
468468
"defaults": {
469469
"color": {
@@ -535,7 +535,7 @@
535535
"mode": "spectrum"
536536
},
537537
"dataFormat": "timeseries",
538-
"datasource": "${DATASOURCE}",
538+
"datasource": "prometheus-in-docker",
539539
"gridPos": {
540540
"h": 8,
541541
"w": 12,
@@ -599,7 +599,7 @@
599599
"mode": "spectrum"
600600
},
601601
"dataFormat": "timeseries",
602-
"datasource": "${DATASOURCE}",
602+
"datasource": "prometheus-in-docker",
603603
"gridPos": {
604604
"h": 8,
605605
"w": 12,
@@ -654,7 +654,7 @@
654654
"bars": false,
655655
"dashLength": 10,
656656
"dashes": false,
657-
"datasource": "${DATASOURCE}",
657+
"datasource": "prometheus-in-docker",
658658
"fieldConfig": {
659659
"defaults": {
660660
"unit": "ms"
@@ -810,7 +810,7 @@
810810
"id": 32,
811811
"panels": [
812812
{
813-
"datasource": "${DATASOURCE}",
813+
"datasource": "prometheus-in-docker",
814814
"fieldConfig": {
815815
"defaults": {
816816
"color": {
@@ -895,7 +895,7 @@
895895
"type": "timeseries"
896896
},
897897
{
898-
"datasource": "${DATASOURCE}",
898+
"datasource": "prometheus-in-docker",
899899
"fieldConfig": {
900900
"defaults": {
901901
"color": {
@@ -975,7 +975,7 @@
975975
"type": "timeseries"
976976
},
977977
{
978-
"datasource": "${DATASOURCE}",
978+
"datasource": "prometheus-in-docker",
979979
"fieldConfig": {
980980
"defaults": {
981981
"color": {
@@ -1057,7 +1057,7 @@
10571057
"type": "gauge"
10581058
},
10591059
{
1060-
"datasource": "${DATASOURCE}",
1060+
"datasource": "prometheus-in-docker",
10611061
"fieldConfig": {
10621062
"defaults": {
10631063
"color": {
@@ -1143,7 +1143,7 @@
11431143
"bars": false,
11441144
"dashLength": 10,
11451145
"dashes": false,
1146-
"datasource": "${DATASOURCE}",
1146+
"datasource": "prometheus-in-docker",
11471147
"fill": 1,
11481148
"fillGradient": 0,
11491149
"gridPos": {
@@ -1262,7 +1262,7 @@
12621262
}
12631263
},
12641264
{
1265-
"datasource": "${DATASOURCE}",
1265+
"datasource": "prometheus-in-docker",
12661266
"fieldConfig": {
12671267
"defaults": {
12681268
"color": {
@@ -1338,7 +1338,7 @@
13381338
"type": "table"
13391339
},
13401340
{
1341-
"datasource": "${DATASOURCE}",
1341+
"datasource": "prometheus-in-docker",
13421342
"fieldConfig": {
13431343
"defaults": {
13441344
"color": {
@@ -1424,7 +1424,7 @@
14241424
{
14251425
"allValue": "http.*",
14261426
"current": {},
1427-
"datasource": "${DATASOURCE}",
1427+
"datasource": "prometheus-in-docker",
14281428
"definition": "label_values(url)",
14291429
"description": null,
14301430
"error": null,
@@ -1447,7 +1447,7 @@
14471447
{
14481448
"allValue": null,
14491449
"current": {},
1450-
"datasource": "${DATASOURCE}",
1450+
"datasource": "prometheus-in-docker",
14511451
"definition": "label_values(scenario)",
14521452
"description": null,
14531453
"error": null,
@@ -1557,7 +1557,7 @@
15571557
{
15581558
"allValue": ".*",
15591559
"current": {},
1560-
"datasource": "${DATASOURCE}",
1560+
"datasource": "prometheus-in-docker",
15611561
"definition": "label_values(method)",
15621562
"description": null,
15631563
"error": null,

example/test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ export const options = {
66
thresholds: {
77
'http_reqs{expected_response:true}': ['rate>10'],
88
},
9+
// Adding a tag to distinguish discrete test runs
10+
tags: {
11+
testid: `testrun-${Date.now()}`,
12+
}
913
};
1014

1115
export default function () {
12-
http.get('https://google.com');
16+
http.get('https://test.k6.io/');
1317
}

0 commit comments

Comments
 (0)