prometheus

package module
v0.0.0-...-369679f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

Prometheus reader k6 extension

K6 extension to read data from prometheus

Build

To build a custom k6 binary with this extension, first ensure you have the prerequisites:

  1. Download xk6:

    go install go.k6.io/xk6/cmd/xk6@latest
    
  2. Build the k6 binary:

    xk6 build --with  github.com/JorTurFer/xk6-input-prometheus
    

    This will create a k6 binary that includes the xk6-input-prometheus extension in your local folder. This k6 binary can now run a k6 test.

Development

To make development a little smoother, use the Makefile in the root folder. The default target will format your code, run tests, and create a k6 binary with your local code rather than from GitHub.

git clone git@github.com/JorTurFer/xk6-input-prometheus.git
cd xk6-input-prometheus
make build

Using the k6 binary with xk6-input-prometheus, run the k6 test as usual:

./k6 run example.js

Examples:

Document Insertion Test
import prometheus from 'k6/x/prometheusread';

export default function () {
    query()
    queryRange()
}

function query (){
    console.log("Query Sample")
    var client = prometheus.newPrometheusClient("http://demo.robustperception.io:9090", "user", "password")
    var response = client.query("alertmanager_notifications_total")
    
    response.forEach(item => {   
      var jsonBytes = item.marshalJSON()
      var {metric, value} = JSON.parse(String.fromCharCode(...jsonBytes))
  
      console.log(metric.__name__) // show metric name
      console.log(value[1]) // show metric value
    });
}

function queryRange (){
    console.log("QueryRange Sample")
    var client = prometheus.newPrometheusClient("http://demo.robustperception.io:9090", "user", "password")
    var end = new Date()
    var start = new Date(end.getTime() - 5 * 60000)
    var period = "minute"

    var response = client.queryRange("rate(prometheus_tsdb_head_samples_appended_total[5m])", start.toISOString(), end.toISOString(), period)

    response.forEach(item => {   
        var jsonBytes = item.marshalJSON()
        var {metric, values} = JSON.parse(String.fromCharCode(...jsonBytes))
    
        console.log(metric) // show metric
        console.log(values) // show metric values
    });
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func (*Client) Query

func (c *Client) Query(query string) (model.Value, error)

func (*Client) QueryRange

func (c *Client) QueryRange(query, start, end, period string) (model.Value, error)

type Prometheus

type Prometheus struct{}

func (*Prometheus) NewPrometheusClient

func (*Prometheus) NewPrometheusClient(url, username string, password config.Secret) Client

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL