Skip to content

Commit

Permalink
feat: support to send the metrics to prometheus
Browse files Browse the repository at this point in the history
Experimental support to send the performance metrics to the prometheus,
powerred by the k6 plugin xk6-output-prometheus-remote.

Signed-off-by: chlins <[email protected]>
  • Loading branch information
chlins committed Aug 22, 2023
1 parent 47682fc commit 2390911
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Then:
go run mage.go
```

## The environment variables and targets
## The environment variables and targets

The environment variables in the table are the configurations for the performance testing. Use `VAR1=value1 VAR2=value2 go run mage.go target` format to apply the variables to the testing

Expand All @@ -53,6 +53,16 @@ The environment variables in the table are the configurations for the performanc
| K6_JSON_OUTPUT | Make k6 output detailed statistics in JSON format | false |
| HARBOR_REPORT | Whether generate testing report | false |

**Experimental** to send the metrics to the prometheus. (Refer to <https://k6.io/docs/results-output/real-time/prometheus-remote-write> for more details)
| Variable | Description | Default value |
| ----------------- | ------------------------------------------------------------ | ------------- |
| K6_PROMETHEUS_RW_SERVER_URL | URL of the Prometheus remote write implementation's endpoint | |
| K6_PROMETHEUS_RW_USERNAME | User for the HTTP Basic authentication at the Prometheus remote write endpoint | |
| K6_PROMETHEUS_RW_PASSWORD | Password for the HTTP Basic authentication at the Prometheus remote write endpoint| |
| K6_PROMETHEUS_RW_TREND_AS_NATIVE_HISTOGRAM | If true, it maps the all defined trend metrics as Native Histograms | false |
| K6_PROMETHEUS_RW_TREND_STATS | It's a comma-separated list of stats functions | min,p(90),p(95),p(99),max |
| K6_PROMETHEUS_RW_INSECURE_SKIP_TLS_VERIFY | If true, the HTTP client skips TLS verification on the endpoint | false |


The following table includes the targets.

Expand Down
50 changes: 50 additions & 0 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ const (

K6CsvOutputEnvKey = "K6_CSV_OUTPUT"
K6JsonOutputEnvKey = "K6_JSON_OUTPUT"

// xk6-output-prometheus-remote related configurations
K6Out = "K6_OUT"
K6PrometheusRwInsecureSkipTlsVerify = "K6_PROMETHEUS_RW_INSECURE_SKIP_TLS_VERIFY"
K6PrometheusRwServerURL = "K6_PROMETHEUS_RW_SERVER_URL"
K6PrometheusRwUsername = "K6_PROMETHEUS_RW_USERNAME"
K6PrometheusRwPassword = "K6_PROMETHEUS_RW_PASSWORD"
K6PrometheusRwTrendAsNativeHistogram = "K6_PROMETHEUS_RW_TREND_AS_NATIVE_HISTOGRAM"
K6PrometheusRwTrendStats = "K6_PROMETHEUS_RW_TREND_STATS"
)

// Default target to run when none is specified
Expand Down Expand Up @@ -226,6 +235,34 @@ func addHarborEnv(env map[string]string) map[string]string {
env["HARBOR_PASSWORD"], _ = u.User.Password()
}

// send metrics to prometheus
if promURL := os.Getenv(K6PrometheusRwServerURL); promURL != "" {
env[K6PrometheusRwServerURL] = promURL
env[K6Out] = "xk6-prometheus-rw"

if username := os.Getenv(K6PrometheusRwUsername); username != "" {
env[K6PrometheusRwUsername] = username
}
if password := os.Getenv(K6PrometheusRwPassword); password != "" {
env[K6PrometheusRwPassword] = password
}

if getEnvBool(K6PrometheusRwTrendAsNativeHistogram) {
env[K6PrometheusRwTrendAsNativeHistogram] = "true"
}

if getEnvBool(K6PrometheusRwInsecureSkipTlsVerify) {
env[K6PrometheusRwInsecureSkipTlsVerify] = "true"
}

if stats := os.Getenv(K6PrometheusRwTrendStats); stats != "" {
env[K6PrometheusRwTrendStats] = stats
} else {
// set the following trend stats by default
env[K6PrometheusRwTrendStats] = "min,p(90),p(95),p(99),max"
}
}

return env
}

Expand Down Expand Up @@ -271,6 +308,19 @@ func getK6RunArgs(script string) []string {
if hasOutputs {
args = append(args, "--tag", fmt.Sprintf("script=%s", scriptName))
}
// append testid as tag if send metrics to prometheus
if os.Getenv(K6PrometheusRwServerURL) != "" {
size := os.Getenv(HarborSizeEnvKey)
vus := os.Getenv(HarborVusEnvKey)
if vus == "" {
// the default VUS in script is 500 if user not specified
vus = "500"
}
// these tags will be the custom label for prom metrics
args = append(args, "--tag", fmt.Sprintf("testid=%s", scriptName))
args = append(args, "--tag", fmt.Sprintf("size=%s", size))
args = append(args, "--tag", fmt.Sprintf("vus=%s", vus))
}

return args
}
Expand Down

0 comments on commit 2390911

Please sign in to comment.