Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

k6 extension for Loki

License

NotificationsYou must be signed in to change notification settings

grafana/xk6-loki

A k6 extension for pushing logs to Loki.

Getting started

  1. Installxk6

    go install go.k6.io/xk6/cmd/xk6@latest
  2. Checkoutgrafana/xk6-loki

    git clone https://github.com/grafana/xk6-lokicd xk6-loki
  3. Build the extension

    make k6

Javascript API

Modulek6/x/loki

Thek6/x/loki module contains the Loki extension to interact with the Loki API.To import the module add

importlokifrom'k6/x/loki';

on top of your test file.

ClassConfig(url, [timeout, ratio, cardinality, labels])

The classConfig holds configuration for the Loki client. The constructortakes the following arguments:

argumenttypedescriptiondefault
urlstringThe full URL to Loki in format[${scheme}://][${tenant}[:${token}]]@${host}[:${port}]-
timeoutintegerRequest timeout in milliseconds, e.g.10000 for 10s10000
ratiofloatThe ratio between JSON and Protobuf encoded batch requests for pushing logs
Must be a number between (including) 0 (100% JSON) and 1 (100% Protobuf)
This is only relevant for write scenarios.
0.9
cardinalityobjectThe cardinality (unique values) forlabels, where the object key is the name of the label and the value is the maximum amount of different values it may have.null
labelsLabelsA Labels object that contains custom label definitions.null

Example:

importlokifrom'k6/x/loki';letconf=loki.Config("localhost:3100");

ClassLabels(labels)

The classLabels allows the definition of custom labels that can be usedinstead of the built-in labels. An instance of this class can be passed asfifth argument to theloki.Config constructor.

argumenttypedescriptiondefault
labelsobjectAn object of type string (label name) to list of strings (possibel label values).-

Example:

importlokifrom'k6/x/loki';letlabels=loki.Labels({"format":["json","logfmt"],// must contain at least one of the supported log formats"cluster":["prod-us-east-0","prod-eu-west-1"],"namespace":["dev","staging","prod"],"container":["nginx","app-1","app-2","app-3"]});

ClassClient(config)

The classClient is a Loki client that can read from and write to a Loki instance.The constructor takes the following arguments:

argumenttypedescriptiondefault
configobjectAn instance ofConfig which holds the configuration for the client.-

Example:

importlokifrom'k6/x/loki';letconf=loki.Config("localhost:3100");letclient=loki.Client(conf);

Methodclient.push()

This function is a shortcut forclient.pushParameterized(5, 800*1024, 1024*1024).

Methodclient.pushParameterized(streams, minSize, maxSize)

Execute a push request (POST /loki/api/v1/push).

The functionpushParameterized generates batch of logs and pushes it to the Loki instance.A batch consists of one or more streams which hold multiple log lines.A stream is a set of log lines with a unique set of labels.

argumenttypedescriptiondefault
streamsintegerThe amount of streams the pushed batch should contain.-
minSizeintegerMinimum size in bytes of the raw log lines of the batch.-
maxSizeintegerMaximum size in bytes of the raw log lines of the batch.-

minSize andmaxSize define the boundaries for a random value of the actual batch size.

Methodclient.instantQuery(query, limit)

This function is a shortcut forclient.instantQueryAt(query, limit, time.Now()) wheretime.Now() is the current nanosecond.

Methodclient.instantQueryAt(query, limit, instant)

Execute an instant query (GET /loki/api/v1/query).

argumenttypedescriptiondefault
querystringThe LogQL query to perform.-
limitintegerMaxiumum number of entries to return.-
instantintegerNanosecond at which to execute query.-

Methodclient.rangeQuery(query, duration, limit)

This function is a shortcut forclient.rangeQueryAt(query, duration, limit, time.Now()) wheretime.Now() is the current nanosecond.

Methodclient.rangeQueryAt(query, duration, limit, instant)

Execute a range query (GET /loki/api/v1/query_range).

argumenttypedescriptiondefault
querystringThe LogQL query to perform.-
durationstringThe time span of the range, e.g.15m,1h, or7d.-
limitintegerMaxiumum number of entries to return.-
instantintegerNanosecond at which to execute query.-

duration defines the range for the query and uses the current timestamp as end and current timestamp - duration as start.

Methodclient.labelsQuery(duration)

This function is a shortcut forclient.labelsQueryAt(duration, time.Now()) wheretime.Now() is the current nanosecond.

Methodclient.labelsQueryAt(duration, instant)

Execute a labels query (GET /loki/api/v1/labels).

argumenttypedescriptiondefault
durationstringThe time span for which labels should be returned, e.g.15m,1h, or7d.-
instantintegerNanosecond at which to execute query.-

duration defines the range for the query and uses the current timestamp as end and current timestamp - duration as start.

Methodclient.labelValuesQuery(label, duration)

This function is a shortcut forclient.labelValuesQueryAt(label, duration, time.Now()) wheretime.Now() is the current nanosecond.

Methodclient.labelValuesQueryAt(label, duration, instant)

Execute a label values query (GET /loki/api/v1/label//values).

argumenttypedescriptiondefault
labelstringThe label name for which to query the values.-
durationstringThe time span for which label values should be returned, e.g.15m,1h, or7d.-
instantintegerNanosecond at which to execute query.-

duration defines the range for the query and uses the current timestamp as end and current timestamp - duration as start.

Methodclient.seriesQuery(matchers, duration)

This function is a shortcut forclient.seriesQueryAt(matchers, duration, time.Now()) wheretime.Now() is the current nanosecond.

Methodclient.seriesQueryAt(matchers, duration, instant)

Execute a series query (GET /loki/api/v1/series).

argumenttypedescriptiondefault
matcherslistA list of label matchers used for the query.-
durationstringThe time span for which the matching series should be returned, e.g.15m,1h, or7d.-
instantintegerNanosecond at which to execute query.-

duration defines the range for the query and uses the current timestamp as end and current timestamp - duration as start.

Labels

xk6-loki uses the following built-in label names for generating streams:

namevaluesnotes
instancefixed: 1 per k6 worker
formatfixed: apache_common, apache_combined, apache_error, rfc3164, rfc5424, json, logfmtThis label defines how the log lines of a stream are formatted.
osfixed: darwin, linux, windows-
namespacevariable1
appvariable1
podvariable1
languagevariable1
wordvariable1

The total amount of different streams is defined by the carthesian product of all label values. Keep in mind that high cardinality impacts the performance of the Loki instance.

Custom labels

Additionally,xk6-loki also supports custom labels that can be used insteadof the built-in labels.

Seeexamples/custom-labels.js for a full example with custom labels.

Metrics

The extension collects metrics that are printed in theend-of-test summary in addition to the built-in metrics.

Query metrics

These metrics are collected only for instant and range queries.

namedescription
loki_bytes_processed_per_secondamount of bytes processed by Loki per second
loki_bytes_processed_totaltotal amount of bytes processed by Loki
loki_lines_processed_per_secondamount of lines processed by Loki per second
loki_lines_processed_totaltotal amount of lines processed by Loki

Write metrics

namedescription
loki_client_uncompressed_bytesthe quantity of uncompressed log data pushed to Loki, in bytes
loki_client_linesthe number of log lines pushed to Loki

Example

importsleepfrom'k6';importlokifrom'k6/x/loki';/** * URL used for push and query requests * Path is automatically appended by the client *@constant {string} */constBASE_URL=`http://localhost:3100`;/** * Client timeout for read and write in milliseconds *@constant {number} */consttimeout=5000;/** * Ratio between Protobuf and JSON encoded payloads when pushing logs to Loki *@constant {number} */constratio=0.5;/** * Cardinality for labels *@constant {object} */constcardinality={"app":5,"namespace":5};/** * Execution options */exportconstoptions={vus:10,iterations:10,};/** * Create configuration object */constconf=newloki.Config(BASE_URL,timeout,ratio,cardinality);/** * Create Loki client */constclient=newloki.Client(conf);exportdefault()=>{// Push a batch of 2 streams with a payload size between 500KB and 1MBletres=client.pushParameterized(2,512*1024,1024*1024);// A successful push request returns HTTP status 204check(res,{'successful write':(res)=>res.status==204});sleep(1);}
./k6 run examples/simple.js

You can find more examples in theexamples/ folder.

Footnotes

  1. The amount of values can be defined incardinality argument of the client configuration.2345


[8]ページ先頭

©2009-2025 Movatter.jp