Grafana Loki
Synopsis
Creates a Grafana Loki target that sends logs to Grafana Loki through its push API. It works with a Loki that you run yourself and with Grafana Cloud Logs.
Schema
- name: <string>
description: <string>
type: loki
pipelines: <pipeline[]>
status: <boolean>
properties:
url: <string>
format: <string>
compression: <boolean>
tenant_id: <string>
headers:
<key>: <value>
authentication:
type: <string>
username: <string>
password: <string>
token: <string>
labels:
<label name>: <string>
label_fields:
<label name>: <string>
structured_metadata:
- <string>
batch_size: <numeric>
max_bytes: <numeric>
timeout: <numeric>
max_retries: <numeric>
retry_delay: <numeric>
disable_retries: <boolean>
keep_alive: <boolean>
follow_redirects: <boolean>
connect_timeout: <numeric>
socket_timeout: <numeric>
pool_max: <numeric>
pool_max_per_route: <numeric>
tls:
status: <boolean>
verify: <boolean>
ca_name: <string>
cert_name: <string>
key_name: <string>
passphrase: <string>
server_name: <string>
min_tls_version: <string>
max_tls_version: <string>
debug:
status: <boolean>
dont_send_logs: <boolean>
Configuration
The following fields are used to define the target:
| Field | Required | Default | Description |
|---|---|---|---|
name | Y | Target name | |
description | N | - | Optional description |
type | Y | Must be loki | |
pipelines | N | - | Optional post-processor pipelines |
status | N | true | Enable/disable the target |
Connection
| Field | Required | Default | Description |
|---|---|---|---|
url | Y | - | The address of Loki, or of the gateway in front of it, such as https://loki.example.com. It must be an http or https URL with a host. A URL with no path, or with only /, gets the path /loki/api/v1/push. A URL with any other path is used as it is set. The target keeps the query of the URL in both cases. In the console: |
format | N | protobuf | protobuf or json. With protobuf, the body is a Snappy-compressed protobuf message, and the content type is application/x-protobuf. With json, the body is a JSON document, and the content type is application/json. YAML only |
compression | N | true | Applies only to the json format. The target compresses the body with gzip and sends Content-Encoding: gzip. The protobuf format is always compressed with Snappy and has no Content-Encoding. YAML only |
tenant_id | N | - | The tenant. The target sends it in the X-Scope-OrgID header. Set it when your Loki has more than one tenant. In the console: |
headers | N | - | A map of header names to values. The target sends each one on every request, as it is set. A header can hold a key, so the console withholds the values when it shows the target. The target sets some headers itself, and a header of the same name does not replace them (see Headers). YAML only |
Authentication
| Field | Required | Default | Description |
|---|---|---|---|
authentication.type | N | none | The sign-in: none, basic or bearer. The header type of the HTTP target is not available. In the console: |
authentication.username | N | - | The user name for basic. It is required with basic. In the console: |
authentication.password | N | - | The password for basic. It is required with basic. In the console: |
authentication.token | N | - | The token for bearer. It is required with bearer. In the console: |
username, password and token can each be plain text, an environment variable or a secret.
Labels
| Field | Required | Default | Description |
|---|---|---|---|
labels | N | - | The static labels, as a map from the label name to its value. In the console: |
label_fields | N | - | The labels whose value the target reads from the record, as a map from the label name to the name of a field. In the console: |
structured_metadata | N | - | A list of field names. The target sends each field it finds as structured metadata of the entry. The list has at most 128 fields. In the console: |
How the Labels Choose a Stream describes how the target works out the labels of each record.
Performance
| Field | Required | Default | Description |
|---|---|---|---|
batch_size | N | 1000 | The number of records in one request. In the console: |
max_bytes | N | 1048576 | The size, in bytes, at which the target sends a request. It is the batch size that Promtail uses. The length of each line plus 64 bytes counts toward it for each record. Director refuses a value below zero. YAML only |
timeout | N | 60 | As on the HTTP page. In the console: |
max_retries | N | 0 | The number of tries after the first one. -1 tries without limit. The target tries again only when the request never left the host (see How the Target Sends). In the console: |
retry_delay | N | 1 | The number of seconds before the first retry. The later waits grow. In the console: |
disable_retries | N | false | As on the HTTP page. In the console: |
HTTP Client
keep_alive (true), follow_redirects (true), connect_timeout (10), socket_timeout (10), pool_max (50) and pool_max_per_route (25) work as on the HTTP page's Request Settings and Connection Pool, with the same defaults. They are YAML only.
TLS
The tls settings work as on the HTTP page. In the console: the
Scheduling
See Scheduling and Pool Behavior for interval and cron fields shared by all targets.
Debug Options
The debug options work as on the HTTP page.
The Console Form
The form has four steps:
Grafana Loki settings . UnderConnection settings :URL andTenant ID (Optional) . UnderPerformance :Batch size (Optional) andRequest timeout (Optional) , then theDisable retries switch, thenMaximum retries (Optional) andRetry delay (Optional) . The form hides the last two whileDisable retries is on.Authentication .Authentication method isNone (the default),Basic orBearer .Basic showsUsername andPassword .Bearer showsToken . The form sends only the fields of the chosen method.Stream labels :- A notice,
Every stream has two labels already . It says that the target labels each stream withservice_name(the device's name) anddevice_type, that a label you add with one of these names replaces it, and that Loki takes at most 15 labels, these two included. Labels , the labels with the same value on every line. It is a list with the columnsLabel name andValue , and anAdd label button.Labels from record fields , the labels whose value Director reads from the record. A field name with a dot reads a nested field, such ashost.name. It is a list with the columnsLabel name andRecord field , and anAdd label from field button.Structured metadata fields (Optional) , which takes one tag for each record field.
- A notice,
TLS settings . The same fields as in the TLS settings step of the HTTP target.
The form checks the <name> stands for the label name and N for a count:
- A name must start with a letter from A to Z or an underscore, and use only letters from A to Z, digits and underscores. The messages are
Label name is required,Label name must start with a letter from A to Z or an underscore, and use only letters from A to Z, digits and underscoresandLabel name cannot start with two underscores. A label also needs its value:Value is requiredunderLabels , andRecord field is requiredunderLabels from record fields . - A name in both lists:
"<name>" is already a label with a fixed value. Use each label name once. - A name twice in one list:
"<name>" is in this list more than once. Use each label name once. - More than 15 labels:
Loki takes at most 15 labels, service_name and device_type included. These settings add up to N. - More than 128 metadata fields:
Loki takes at most 128 structured metadata fields. These settings list N.
The form has no fields for format, compression, max_bytes, headers, keep_alive, follow_redirects, connect_timeout, socket_timeout, pool_max and pool_max_per_route. They are YAML only. A target that has one of them in its YAML keeps it when you edit or clone the target in the console.
Details
Where to Find the Values
- The URL: the address of your Loki, or of the gateway in front of it. For Grafana Cloud Logs, use the address of your Loki instance.
- The tenant: set
tenant_idwhen your Loki has more than one tenant. The target sends it in theX-Scope-OrgIDheader. - The sign-in: Grafana Cloud Logs takes
basic. The user name is the user ID of your Loki instance. The password is an access policy token that is allowed to write logs. Keep the token in a secret.
How the Target Sends
- Each record becomes one line in a stream. The line is the whole record, as text.
- The target sends the lines of a batch in one request. It sends the request when the batch holds
batch_sizerecords or reachesmax_bytes. - The target tries a request again only when the request never left the host, such as when the connection could not be opened. This keeps one attempt from putting a batch in Loki twice.
- When Loki answers with the status 400, 413 or 422, it has refused the batch for good. The target reports the batch as rejected. The HTTP page describes what Director does with a rejected batch.
- Any other status outside 200 to 299 is an error. A wrong credential and a Loki that is down are among the causes. Director delivers the batch again later.
How the Labels Choose a Stream
Loki keeps lines in streams. Records with the same labels go to the same stream. A record with different labels goes to another stream. The target works out the labels of each record like this:
- Every record gets two labels by default.
service_nameis the name of its device.device_typeis the type of its device. labelsadds static labels. The value is the same on every record.label_fieldsadds labels whose value comes from the record. The target reads the field from the JSON of the record. It looks for the whole field name at the top level first. If that finds nothing, it follows a dotted name through nested objects, such ashost.name. A string is the value of the label as it is. A number or a boolean gives its JSON text.- The target leaves a label out of a record when its value is empty. It also leaves the label out when the field is missing, is null, or is an object or an array.
- A label of yours with the name of a default label replaces the default. The default does not come back for a record where your label has no value. That record has no label of that name.
- The same name in both
labelsandlabel_fieldsis a configuration error. - A label name starts with a letter or an underscore, and holds only letters, digits and underscores. Loki keeps the names that start with
__for itself, so the target refuses them. - A stream has at most 15 labels. The count includes
service_nameanddevice_typeunless you replace them.
A Record with No Label
Loki refuses a stream with no labels, and it refuses the whole request with it. So a record that ends up with no label at all gets the label service_name="unknown_service". This is the name that Loki gives to a stream without a service. It can happen when you replace both default labels and the fields of your labels are missing from a record.
Why Few Different Label Values Matter
Each different set of label values is one stream. A label whose value changes often makes many streams. Loki refuses new streams past its limit, which is 5,000 streams a tenant by default. The target logs a warning when it has sent more than 1,000 different streams in the last hour.
Use a label for a field that has few different values, such as an environment, a host or a level. Put every other field in structured metadata.
When to Use Structured Metadata
Use structured metadata for a value that changes often, such as a trace ID. Structured metadata is not part of the stream, so its values can change without making new streams. In LogQL, you select a stream by its labels. You can then filter its lines on the structured metadata.
structured_metadatais a list of field names. The target reads each field as it does forlabel_fields. It sends the fields it finds as structured metadata of the entry.- The name of an entry is the field name with every character outside
A-Z,a-z,0-9and_replaced by_. Souser.namebecomesuser_name. A name that starts with a digit gets an underscore in front. - Two fields that give the same name, such as
trace.idandtrace-id, are a configuration error. - Structured metadata needs Loki 3, or Loki 2.9 with it turned on. Older versions refuse it.
The Line, the Time and the Text
- The line of an entry is the whole record, as text.
- The target replaces each run of bytes that is not valid UTF-8 with the character U+FFFD. It does this in the line, in the label values and in the structured metadata values, so that both formats send the same text. The target checks the limits on the cleaned text, because that is the text Loki gets.
- The time of the entry is the time of the record. A record has no time when its epoch (the seconds since 1 January 1970) is zero or less. The target also treats a time that does not fit in a count of nanoseconds in 64 bits as no time. That is a time before 1678 or after 2262.
- A record with no time gets the time at which the target gets the record. Both formats send the same time.
Limits
The target keeps to the limits that Loki has by default:
- A stream has at most 15 labels, counting
service_nameanddevice_typeunless you replace them. The target refuses a configuration with more. - A label value has at most 2,048 bytes. The target refuses a configuration with a longer value in
labels. It leaves a longer value that a field gives out of the labels of that record, and logs a warning at most once a minute. - A line has at most 256 KB. The target drops a longer record and counts it as dropped. It logs a warning at most once a minute.
structured_metadatalists at most 128 fields. The target refuses a configuration that lists more.- The names and values of the structured metadata of one entry total at most 64 KB. For a larger entry, the target sends the entry without its structured metadata, and logs a warning at most once a minute.
- A tenant can have 5,000 streams by default. The target logs a warning at most once a minute when it has sent more than 1,000 different streams in the last hour. Labels whose values change often cause this. Move them to
structured_metadata. - Loki refuses entries that are older than a week by default. The target does not change the time of a record, so Loki refuses a record that is older than that.
Sign-in, TLS and the HTTP Client
The sign-in, the TLS settings and the HTTP client settings work as on the HTTP page, with these differences:
- The sign-in takes only
none,basicandbearer.basicsends the user name and the password.bearersends the token in theAuthorizationheader asBearer <token>. - The target uses the
tlssettings only whentls.statusistrue. A client certificate needs its key, so setcert_nameandkey_nametogether or leave both out. - An
httpsURL needs notlssettings when the host of Director trusts the certificate of Loki.
Headers
The target sends each header in headers on every request, as it is set.
- It sets
X-Scope-OrgIDfromtenant_idandAuthorizationfromauthenticationafter the headers, so a header of the same name does not replace them. It setsX-Scope-OrgIDonly whentenant_idis set, andAuthorizationonly forbasicandbearer. - The content type and the content encoding follow from
formatandcompression, and a header does not replace them either. - A header can hold a key, so the console withholds the values when it shows the target.
Examples
A Loki with Tenants
A Loki that has more than one tenant. Each stream gets the labels | |
Grafana Cloud Logs
Grafana Cloud Logs. The user name is the user ID of the Loki instance. The password is an access policy token, read from a secret... | |
Labels and Structured Metadata
Labels from fields, and a field that changes often as structured metadata. Each stream gets | |
Troubleshooting
Configuration Errors
Director refuses a configuration it cannot use, with one of these messages. <name> stands for the name in your configuration and N for a number:
url is required for Lokiurl for Loki must be an http or https URL with a host, such as https://loki.example.com: the URL has another scheme or no host. When the URL cannot be read at all, the same message continues with the reason.format for Loki must be protobuf or jsonlabel name "<name>" for Loki must start with a letter or an underscore and hold only letters, digits and underscores: the name breaks the rule. The same message appears for a name that starts with two underscores, which Loki keeps for itself.label "<name>" for Loki is set in both labels and label_fieldslabel "<name>" for Loki has a value over 2048 bytes, which Loki refuses: the value is inlabels. A value that a field gives is left out of that record instead, with a warning (see below).the target has N labels for Loki, counting service_name and device_type, but Loki takes at most 15structured_metadata lists N fields for Loki, but Loki takes at most 128 structured metadata entriesstructured_metadata fields "<field>" and "<field>" for Loki both give the name "<name>": two fields give the same name for the entry, such astrace.idandtrace-id, which both givetrace_id.batch_size must be greater than 0, got Nmax_bytes must not be negative, got Nmax_retries must be -1 (unlimited on transient errors) or >= 0, got Nusername and password are required for basic authenticationtoken is required for bearer authenticationauthentication type for Loki must be one of: none, basic, bearerboth tls.cert_name and tls.key_name must be provided together or omitted together
The two messages about basic and bearer appear when a credential is not set. Director reads each credential itself when the target starts.
Errors When the Target Starts
failed to build TLS config: ...: the TLS settings could not be used. The rest of the message gives the reason.failed to resolve http authentication <setting>: ...: the environment variable or the secret that a credential names could not be read.<setting>isusername,passwordortoken. The target does not start.
Warnings While It Runs
These appear in the Director log. In the log, each warning ends with target= and the name of the target. That ending is left out below. N is the count since Director last logged that warning. Director logs each of these warnings at most once a minute:
[Loki] dropped N records in the last minute: Loki refuses a line over 262144 bytes: the target dropped records whose line was over 256 KB, and counted them as dropped.[Loki] left out N label values over 2048 bytes in the last minute: Loki refuses them: a field gave a label value over 2,048 bytes. The target left that label out of the record.[Loki] sent N entries without their structured metadata in the last minute: it was over 65536 bytes: the names and values of the structured metadata of an entry were over 64 KB. The target sent the entry without its structured metadata.[Loki] wrote more than 1000 streams in the last hour: Loki refuses new streams past its limit, 5000 a tenant by default, so check the labels for values that change often: the labels make many streams. Move the fields whose values change often tostructured_metadata.
Errors While It Runs
Director logs one of these messages when a request fails. A message never holds the URL, because a URL can carry a credential. The first part wraps a reason, as in failed to send logs to Loki (not retried): received status code 401 from Loki: ....
failed to send logs to Loki after N retries: ...: the request never left the host on any try, such as when the connection could not be opened.Nismax_retries. The rest of the message gives the last reason. Director delivers the batch again later.failed to send logs to Loki (not retried): ...: the request may have reached Loki, so the target does not send it again itself. The rest of the message gives the reason. Director delivers the batch again later.failed to send logs to Loki: shutdown during retry (attempt N): ...: the target or Director stopped while the target waited to try again. The rest of the message gives the last reason.failed to create request: ...: the target could not build the request. The rest of the message gives the reason.failed to send request: ...: the request failed on its way to Loki, such as a connection that could not be opened or an answer that did not arrive in time. The rest of the message gives the reason.received status code N from Loki: ...: Loki answered with a status outside 200 to 299. After the colon comes the start of Loki's answer, at most 512 bytes. A wrong credential and a Loki that is down are among the causes.record rejected by target: received status code N from Loki: ...:Nis 400, 413 or 422. Loki refused the batch for good, and the target reports the batch as rejected.