# Module 05: Local Observability (Labs 06-07) Streaming Results to InfluxDB, Grafana, and Prometheus
Navigate: [All Slides](../index.html) | [Prev: Advanced Scripting](../04_Advanced_Scripting/index.html) | [Next: Cloud Integration](../06_Cloud_Integration/index.html)
## What You'll Learn - How to use `--out` flag to send metrics to InfluxDB - How to navigate the bundled Grafana k6 dashboard - Which panels to watch during a run (VUs, request rate, p95/p99, error rate) - How to configure Prometheus remote write output - How to query k6 metrics in the Prometheus expression browser - How data persists for post-test analysis
## The Problem: Terminal-Only Results ```bash k6 run script.js ``` Results appear only in the terminal: - Summary disappears when you close the window - No real-time visualization during the run - No historical comparison across runs - Hard to share with team members
## Enter InfluxDB: Time-Series Storage InfluxDB is a time-series database optimized for metrics: - Stores every k6 data point with timestamps - Supports high-write throughput (thousands of metrics/sec) - Integrates with Grafana for visualization k6 has **built-in** InfluxDB output support.
## Verify Services Are Running ```bash # InfluxDB curl http://localhost:8086/ping # Grafana curl http://localhost:3030/api/health # Prometheus curl http://localhost:9090/-/healthy ``` All should return successful responses.
## Run k6 with InfluxDB Output ```bash k6 run --out influxdb=http://localhost:8086/k6 scripts/solutions/lab-06-solution.js ``` Output flag syntax: ``` --out
=
``` - `influxdb` — built-in output module - `http://localhost:8086/k6` — InfluxDB URL + database name k6 creates the database automatically if it doesn't exist.
## Open Grafana Dashboard Navigate to: **http://localhost:3030** Login credentials: - Username: `admin` - Password: `admin` Browse to: **Dashboards → k6 / k6 Overview**
## Key Grafana Panels | Panel | What It Shows | |-------|---------------| | **Virtual Users** | Active VU count across the test | | **Request Rate** | HTTP requests per second | | **Response Time (p95 / p99)** | Latency percentiles for all requests | | **Error Rate** | Fraction of requests returning non-2xx status | | **Checks Passed / Failed** | Pass rate of named k6 checks |
## Watch Metrics in Real-Time With the test running, observe panels refreshing every few seconds: - **VU count** rises during ramp-up, holds during sustain - **Request rate** increases proportionally to VU count - **p95/p99 latency** reveals performance degradation - **Error rate** flags when thresholds are breached
## Exploring the InfluxDB Datasource In Grafana: 1. Go to **Configuration → Data Sources** 2. Click the **InfluxDB** datasource 3. Note the URL: `http://influxdb:8086` 4. Note the database name: `k6` This is how Grafana knows where to query for k6 metrics.
## Examining Panel Queries Click any panel title → **Edit** to see the underlying query. Example p95 response time query: ```sql SELECT percentile("value", 95) FROM "http_req_duration" WHERE $timeFilter GROUP BY time($__interval) fill(none) ``` This is InfluxQL syntax.
## Data Persists After the Run After the test completes: - All data remains in InfluxDB - Use Grafana time picker to review the full test window - Compare results across multiple runs - Share Grafana URLs with team members
## Enter Prometheus: Alternative Backend Prometheus is a metrics scraping and storage system: - Pull-based model (default) or push-based (remote write) - First-class support for labels and filtering - PromQL query language - Widely adopted in Kubernetes environments k6 supports **Prometheus remote write** output.
## Prometheus Remote Write Configuration Set the environment variable: ```bash export K6_PROMETHEUS_RW_SERVER_URL=http://localhost:9090/api/v1/write ``` Run k6 with Prometheus output: ```bash k6 run --out experimental-prometheus-rw scripts/solutions/lab-07-solution.js ``` Watch the CLI output for confirmation: ``` output: Prometheus remote write (http://localhost:9090/api/v1/write) ```
## Prometheus Must Enable Remote Write Prometheus does not accept remote write by default. The workshop environment sets this flag in `docker-compose.yml`: ```yaml prometheus: command: - --config.file=/etc/prometheus/prometheus.yml - --web.enable-remote-write-receiver ``` If you deploy Prometheus yourself, this flag is required.
## Query k6 Metrics in Prometheus Navigate to: **http://localhost:9090** Click **Graph** tab and enter: ```promql k6_http_req_duration_p95 ``` Try additional queries: ```promql # Request rate rate(k6_http_reqs_total[1m]) # Active VUs k6_vus # Check pass rate k6_checks_total ```
## k6 to Prometheus Metric Mapping | k6 metric | Prometheus metric | |-----------|-------------------| | `http_req_duration` (p95) | `k6_http_req_duration_p95` | | `http_reqs` | `k6_http_reqs_total` | | `vus` | `k6_vus` | | `checks` | `k6_checks_total` | Browse all k6 metrics by filtering on `k6_` prefix in Metrics Explorer.
## InfluxDB vs. Prometheus: When to Use Each | Feature | InfluxDB | Prometheus | |---|---|---| | k6 integration | Built-in | Experimental (but stable) | | Query language | InfluxQL | PromQL | | Storage model | Push-based | Pull or Push (remote write) | | Grafana support | Excellent | Excellent | | Best for | Dedicated k6 stack | Existing Prometheus infrastructure | Both work well — choose based on your existing stack.
## Key Takeaways - `--out influxdb=
/
` streams metrics to InfluxDB during the run - Grafana queries InfluxDB in near real-time for live monitoring - The k6 InfluxDB output is built-in — no extension required - `--out experimental-prometheus-rw` enables push-based metrics to Prometheus - Prometheus must have `--web.enable-remote-write-receiver` flag enabled - Data persists in both backends for post-test analysis and historical comparison - Both InfluxDB and Prometheus integrate seamlessly with Grafana
# Lab Complete! Ready to explore cloud integration and JSON output
Navigate: [All Slides](../index.html) | [Prev: Advanced Scripting](../04_Advanced_Scripting/index.html) | [Next: Cloud Integration](../06_Cloud_Integration/index.html)