Project: APISIX (Orchestration & Management / API Gateway)
File: loki-logger.md

title: loki-logger keywords: Apache APISIX API Gateway Plugin Loki-logger Grafana Loki description: This document contains information about the Apache APISIX loki-logger Plugin. <!-- --> The `loki-logger` plugin is used to forward logs to for analysis and storage. When the Plugin is enabled, APISIX will serialize the request context information to and submit it to the batch queue. When the maximum batch size is exceeded, the data in the queue is pushed to Grafana Loki. See for more details. | Name | Type | Required | Default | Description | |||||| | endpoint_addrs | array[string] | True | | Loki API base URL, format like http://127.0.0.1:3100, supports HTTPS and domain names. If multiple endpoints are configured, they will be written randomly. | | endpoint_uri | string | False | /loki/api/v1/push | If you are using a log collection service that is compatible with the Loki Push API, you can use this configuration item to customize the API path. | | tenant_id | string | False | fake | Loki tenant ID. According to Loki's , its default value is set to the default value `fake` under single-tenancy. | | loglabels | object | False | {job = "apisix"} | Loki log label. and can be used by prefixing the string with `$`, both individual and combined, such as `$host` or `$remoteaddr:$remote_port`. | | ssl_verify | boolean | False | true | When set to `true`, verifies the SSL certificate. | | timeout | integer | False | 3000ms | Timeout for the Loki service HTTP call. Range from 1 to 60,000ms. | | keepalive | boolean | False | true | When set to `true`, keeps the connection alive for multiple requests. | | keepalive_timeout | integer | False | 60000ms | Idle time after which the connection is closed. Range greater than or equal than 1000ms. | | keepalive_pool | integer | False | 5 | Connection pool limit. Range greater than or equal than 1. | | log_format | object | False | | Log format declared as key value pairs in JSON format. Values only support strings. and can be used by prefixing the string with `$`. | | includereqbody | boolean | False | false | When set to `true` includes the request body in the log. If the request body is too big to be kept in the memory, it can't be logged due to Nginx's limitations. | | includereqbodyexpr | array | False | | Filter for when the `includereq_body` attribute is set to `true`. Request body is only logged when the expression set here evaluates to `true`. See for more. | | includerespbody | boolean | False | false | When set to `true` includes the response body in the log. | | includerespbodyexpr | array | False | | Filter for when the `includeresp_body` attribute is set to `true`. Response body is only logged when the expression set here evaluates to `true`. See for more. | This plugin supports using batch processors to aggregate and process entries (logs/data) in a batch. This avoids the need for frequently submitting the data. The batch processor submits data every `5` seconds or when the data in the queue reaches `1000`. See for more information or setting your custom configuration. ```json { "request": { "headers": { "connection": "close", "host": "localhost", "test-header": "only-for-test#1" }, "method": "GET", "uri": "/hello", "url": "http://localhost:1984/hello", "size": 89, "querystring": {} }, "client_ip": "127.0.0.1", "start_time": 1704525701293, "apisix_latency":

"response": { "headers": { "content-type": "text/plain", "server": "APISIX/3.7.0", "content-length": "12", "connection": "close" }, "status": 200, "size": 118 }, "route_id": "1", "lokilogtime": "1704525701293000000", "upstream_latency": 5, "latency": 105.99994659424, "upstream": "127.0.0.1:1980", "server": { "hostname": "localhost", "version": "3.7.0" }, "service_id": "" } ``` You can also set the format of the logs by configuring the Plugin metadata. The following configurations are available: | Name | Type | Required | Default | Description | |||-||-| | log_format | object | False | | Log format declared as key value pairs in JSON format. Values only support strings. and can be used by prefixing the string with `$`. | :::info IMPORTANT Configuring the plugin metadata is global in scope. This means that it will take effect on all Routes and Services which use the `loki-logger` plugin. ::: The example below shows how you can configure through the Admin API: :::note You can fetch the `admin_key` from `config.yaml` and save to an environment variable with the following command: ```bash adminkey=$(yq '.deployment.admin.adminkey[0].key' conf/config.yaml | sed 's/"//g') ``` ::: ```shell curl http://127.0.0.1:9180/apisix/admin/pluginmetadata/loki-logger -H "X-API-KEY: $adminkey" -X PUT -d ' { "log_format": { "host": "$host", "@timestamp": "$time_iso8601", "clientip": "$remoteaddr" } }' ``` With this configuration, your logs would be formatted as shown below: ```shell {"host":"localhost","@timestamp":"2020-09-23T19:05:05-04:00","clientip":"127.0.0.1","routeid":"1"} {"host":"localhost","@timestamp":"2020-09-23T19:05:05-04:00","clientip":"127.0.0.1","routeid":"1"} ``` The example below shows how you can enable the `loki-logger` plugin on a specific Route: ```shell curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d ' { "plugins": { "loki-logger": { "endpoint_addrs" : ["http://127.0.0.1:3100"] } }, "upstream": { "nodes": { "127.0.0.1:1980": 1 }, "type": "roundrobin" }, "uri": "/hello" }' ``` Now, if you make a request to APISIX, it will be logged in your Loki server: ```shell curl -i http://127.0.0.1:9080/hello ``` When you need to remove the `loki-logger` plugin, you can delete the corresponding JSON configuration with the following command and APISIX will automatically reload the relevant configuration without restarting the service: ```shell curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d ' { "methods": ["GET"], "uri": "/hello", "plugins": {}, "upstream": { "type": "roundrobin", "nodes": { "127.0.0.1:1980": 1 } } }' ``` Look at `error.log` for such a log. ```text 2023/04/30 13:45:46 [error] 19381#19381: *1075673 [lua] batch-processor.lua:95: Batch Processor[loki logger] failed to process entries: loki server returned status: 401, body: no org id, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:9081 ``` The error can be diagnosed based on the error code in the `failed to process entries: loki server returned status: 401, body: no org id` and the response body of the loki server. Make sure to `keepalive` related configuration is set properly. See for more information. Check the logs in `error.log`, look for such a log. ```text 2023/04/30 13:49:34 [error] 19381#19381: *1082680 [lua] batch-processor.lua:95: Batch Processor[loki logger] failed to process entries: loki server returned status: 429, body: Ingestion rate limit exceeded for user tenant_1 (limit: 4194304 bytes/sec) while attempting to ingest '1000' lines totaling '616307' bytes, reduce log volume or contact your Loki administrator to see if the limit can be increased, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:9081 ``` The logs usually associated with high QPS look like the above. The error is: `Ingestion rate limit exceeded for user tenant_1 (limit: 4194304 bytes/sec) while attempting to ingest '1000' lines totaling '616307' bytes, reduce log volume or contact your Loki administrator to see if the limit can be increased`. Refer to to add limits on the amount of default and burst logs, such as `ingestionratemb` and `ingestionburstsize_mb`. As the test during development, setting the `ingestionburstsize_mb` to 100 allows APISIX to push the logs correctly at least at 10000 RPS.