Loki’s HTTP API

Loki exposes an HTTP API for pushing, querying, and tailing log data.Note that authenticating against the API isout of scope for Loki.

The HTTP API includes the following endpoints:

Microservices Mode

When deploying Loki in microservices mode, the set of endpoints exposed by eachcomponent is different.

These endpoints are exposed by all components:

These endpoints are exposed by just the querier:

While these endpoints are exposed by just the distributor:

And these endpoints are exposed by just the ingester:

The API endpoints starting with /loki/ are Prometheus API-compatible and the result formats can be used interchangeably.

A list of clients can be found in the clients documentation.

Matrix, Vector, And Streams

Some Loki API endpoints return a result of a matrix, a vector, or a stream:

  • Matrix: a table of values where each row represents a different label setand the columns are each sample value for that row over the queried time.Matrix types are only returned when running a query that computes some value.

  • Instant Vector: denoted in the type as just vector, an Instant Vectorrepresents the latest value of a calculation for a given labelset. InstantVectors are only returned when doing a query against a single point intime.

  • Stream: a Stream is a set of all values (logs) for a given label set over thequeried time range. Streams are the only type that will result in log linesbeing returned.

GET /loki/api/v1/query

/loki/api/v1/query allows for doing queries against a single point in time. The URLquery parameters support the following values:

  • query: The LogQL query to perform
  • limit: The max number of entries to return
  • time: The evaluation time for the query as a nanosecond Unix epoch. Defaults to now.
  • direction: Determines the sort order of logs. Supported values are forward or backward. Defaults to backward.

In microservices mode, /loki/api/v1/query is exposed by the querier.

Response:

  1. {
  2. "status": "success",
  3. "data": {
  4. "resultType": "vector" | "streams",
  5. "result": [<vector value>] | [<stream value>]
  6. }
  7. }

Where <vector value> is:

  1. {
  2. "metric": {
  3. <label key-value pairs>
  4. },
  5. "value": [
  6. <number: nanosecond unix epoch>,
  7. <string: value>
  8. ]
  9. }

And <stream value> is:

  1. {
  2. "stream": {
  3. <label key-value pairs>
  4. },
  5. "values": [
  6. [
  7. <string: nanosecond unix epoch>,
  8. <string: log line>
  9. ],
  10. ...
  11. ]
  12. }

Examples

  1. $ curl -G -s "http://localhost:3100/loki/api/v1/query" --data-urlencode 'query=sum(rate({job="varlogs"}[10m])) by (level)' | jq
  2. {
  3. "status": "success",
  4. "data": {
  5. "resultType": "vector",
  6. "result": [
  7. {
  8. "metric": {},
  9. "value": [
  10. 1559848867745737,
  11. "1267.1266666666666"
  12. ]
  13. },
  14. {
  15. "metric": {
  16. "level": "warn"
  17. },
  18. "value": [
  19. 1559848867745737,
  20. "37.77166666666667"
  21. ]
  22. },
  23. {
  24. "metric": {
  25. "level": "info"
  26. },
  27. "value": [
  28. 1559848867745737,
  29. "37.69"
  30. ]
  31. }
  32. ]
  33. }
  34. }
  1. $ curl -G -s "http://localhost:3100/loki/api/v1/query" --data-urlencode 'query={job="varlogs"}' | jq
  2. {
  3. "status": "success",
  4. "data": {
  5. "resultType": "streams",
  6. "result": [
  7. {
  8. "stream": {
  9. "filename": "/var/log/myproject.log",
  10. "job": "varlogs",
  11. "level": "info"
  12. },
  13. "values": [
  14. [
  15. "1568234281726420425",
  16. "foo"
  17. ],
  18. [
  19. "1568234269716526880",
  20. "bar"
  21. ]
  22. ]
  23. }
  24. ]
  25. }
  26. }

GET /loki/api/v1/query_range

/loki/api/v1/query_range is used to do a query over a range of time andaccepts the following query parameters in the URL:

  • query: The LogQL query to perform
  • limit: The max number of entries to return
  • start: The start time for the query as a nanosecond Unix epoch. Defaults to one hour ago.
  • end: The start time for the query as a nanosecond Unix epoch. Defaults to now.
  • step: Query resolution step width in seconds. Defaults to a dynamic value based on start and end.
  • direction: Determines the sort order of logs. Supported values are forward or backward. Defaults to backward.

Requests against this endpoint require Loki to query the index store in order tofind log streams for particular labels. Because the index store is spread out bytime, the time span covered by start and end, if large, may cause additionalload against the index server and result in a slow query.

In microservices mode, /loki/api/v1/query_range is exposed by the querier.

Response:

  1. {
  2. "status": "success",
  3. "data": {
  4. "resultType": "matrix" | "streams",
  5. "result": [<matrix value>] | [<stream value>]
  6. }
  7. }

Where <matrix value> is:

  1. {
  2. "metric": {
  3. <label key-value pairs>
  4. },
  5. "values": [
  6. <number: nanosecond unix epoch>,
  7. <string: value>
  8. ]
  9. }

And <stream value> is:

  1. {
  2. "stream": {
  3. <label key-value pairs>
  4. },
  5. "values": [
  6. [
  7. <string: nanosecond unix epoch>,
  8. <string: log line>
  9. ],
  10. ...
  11. ]
  12. }

Examples

  1. $ curl -G -s "http://localhost:3100/loki/api/v1/query_range" --data-urlencode 'query=sum(rate({job="varlogs"}[10m])) by (level)' --data-urlencode 'step=300' | jq
  2. {
  3. "status": "success",
  4. "data": {
  5. "resultType": "matrix",
  6. "result": [
  7. {
  8. "metric": {
  9. "level": "info"
  10. },
  11. "values": [
  12. [
  13. 1559848958663735,
  14. "137.95"
  15. ],
  16. [
  17. 1559849258663735,
  18. "467.115"
  19. ],
  20. [
  21. 1559849558663735,
  22. "658.8516666666667"
  23. ]
  24. ]
  25. },
  26. {
  27. "metric": {
  28. "level": "warn"
  29. },
  30. "values": [
  31. [
  32. 1559848958663735,
  33. "137.27833333333334"
  34. ],
  35. [
  36. 1559849258663735,
  37. "467.69"
  38. ],
  39. [
  40. 1559849558663735,
  41. "660.6933333333334"
  42. ]
  43. ]
  44. }
  45. ]
  46. }
  47. }
  1. $ curl -G -s "http://localhost:3100/loki/api/v1/query_range" --data-urlencode 'query={job="varlogs"}' | jq
  2. {
  3. "status": "success",
  4. "data": {
  5. "resultType": "streams",
  6. "result": [
  7. {
  8. "stream": {
  9. "filename": "/var/log/myproject.log",
  10. "job": "varlogs",
  11. "level": "info"
  12. },
  13. "values": [
  14. {
  15. "1569266497240578000",
  16. "foo"
  17. },
  18. {
  19. "1569266492548155000",
  20. "bar"
  21. }
  22. ]
  23. }
  24. ]
  25. }
  26. }

GET /loki/api/v1/label

/loki/api/v1/label retrieves the list of known labels within a given time span. Itaccepts the following query parameters in the URL:

  • start: The start time for the query as a nanosecond Unix epoch. Defaults to 6 hours ago.
  • end: The start time for the query as a nanosecond Unix epoch. Defaults to now.

In microservices mode, /loki/api/v1/label is exposed by the querier.

Response:

  1. {
  2. "values": [
  3. <label string>,
  4. ...
  5. ]
  6. }

Examples

  1. $ curl -G -s "http://localhost:3100/loki/api/v1/label" | jq
  2. {
  3. "values": [
  4. "foo",
  5. "bar",
  6. "baz"
  7. ]
  8. }

GET /loki/api/v1/label/<name>/values

/loki/api/v1/label/<name>/values retrieves the list of known values for a givenlabel within a given time span. It accepts the following query parameters inthe URL:

  • start: The start time for the query as a nanosecond Unix epoch. Defaults to 6 hours ago.
  • end: The start time for the query as a nanosecond Unix epoch. Defaults to now.

In microservices mode, /loki/api/v1/label/<name>/values is exposed by the querier.

Response:

  1. {
  2. "values": [
  3. <label value>,
  4. ...
  5. ]
  6. }

Examples

  1. $ curl -G -s "http://localhost:3100/loki/api/v1/label/foo/values" | jq
  2. {
  3. "values": [
  4. "cat",
  5. "dog",
  6. "axolotl"
  7. ]
  8. }

GET /loki/api/v1/tail

/loki/api/v1/tail is a WebSocket endpoint that will stream log messages based ona query. It accepts the following query parameters in the URL:

  • query: The LogQL query to perform
  • delay_for: The number of seconds to delay retrieving logs to let slow loggers catch up. Defaults to 0 and cannot be larger than 5.
  • limit: The max number of entries to return
  • start: The start time for the query as a nanosecond Unix epoch. Defaults to one hour ago.

In microservices mode, /loki/api/v1/tail is exposed by the querier.

Response (streamed):

  1. {
  2. "streams": [
  3. {
  4. "stream": {
  5. <label key-value pairs>
  6. },
  7. "values": [
  8. [
  9. <string: nanosecond unix epoch>,
  10. <string: log line>
  11. ]
  12. ]
  13. }
  14. ],
  15. "dropped_entries": [
  16. {
  17. "labels": {
  18. <label key-value pairs>
  19. },
  20. "timestamp": "<nanosecond unix epoch>"
  21. }
  22. ]
  23. }

POST /loki/api/v1/push

Alias (DEPRECATED): POST /api/prom/push

/loki/api/v1/push is the endpoint used to send log entries to Loki. The defaultbehavior is for the POST body to be a snappy-compressed protobuf messsage:

Alternatively, if the Content-Type header is set to application/json, aJSON post body can be sent in the following format:

  1. {
  2. "streams": [
  3. {
  4. "labels": "<LogQL label key-value pairs>",
  5. "entries": [
  6. {
  7. "ts": "<RFC3339Nano string>",
  8. "line": "<log line>"
  9. }
  10. ]
  11. }
  12. ]
  13. }

NOTE: logs sent to Loki for every stream must be in timestamp-ascendingorder, meaning each log line must be more recent than the one last received.If logs do not follow this order, Loki will reject the log with an out oforder error.

In microservices mode, /loki/api/v1/push is exposed by the distributor.

Examples

  1. $ curl -H "Content-Type: application/json" -XPOST -s "https://localhost:3100/loki/api/v1/push" --data-raw \
  2. '{"streams": [{ "labels": "{foo=\"bar\"}", "entries": [{ "ts": "2018-12-18T08:28:06.801064-04:00", "line": "fizzbuzz" }] }]}'

GET /api/prom/tail

DEPRECATED: /api/prom/tail is deprecated. Use /loki/api/v1/tailinstead.

/api/prom/tail is a WebSocket endpoint that will stream log messages based ona query. It accepts the following query parameters in the URL:

  • query: The LogQL query to perform
  • delay_for: The number of seconds to delay retrieving logs to let slow loggers catch up. Defaults to 0 and cannot be larger than 5.
  • limit: The max number of entries to return
  • start: The start time for the query as a nanosecond Unix epoch. Defaults to one hour ago.

In microservices mode, /api/prom/tail is exposed by the querier.

Response (streamed):

  1. {
  2. "streams": [
  3. {
  4. "labels": "<LogQL label key-value pairs>",
  5. "entries": [
  6. {
  7. "ts": "<RFC3339Nano timestamp>",
  8. "line": "<log line>"
  9. }
  10. ]
  11. }
  12. ],
  13. "dropped_entries": [
  14. {
  15. "Timestamp": "<RFC3339Nano timestamp>",
  16. "Labels": "<LogQL label key-value pairs>"
  17. }
  18. ]
  19. }

dropped_entries will be populated when the tailer could not keep up with theamount of traffic in Loki. When present, it indicates that the entries receivedin the streams is not the full amount of logs that are present in Loki. Notethat the keys in dropped_entries will be sent as uppercase Timestampand Labels instead of labels and ts like in the entries for the stream.

As the response is streamed, the object defined by the response format abovewill be sent over the WebSocket multiple times.

GET /api/prom/query

WARNING: /api/prom/query is DEPRECATED; use /loki/api/v1/query_rangeinstead.

/api/prom/query supports doing general queries. The URL query parameterssupport the following values:

  • query: The LogQL query to perform
  • limit: The max number of entries to return
  • start: The start time for the query as a nanosecond Unix epoch. Defaults to one hour ago.
  • end: The start time for the query as a nanosecond Unix epoch. Defaults to now.
  • direction: Determines the sort order of logs. Supported values are forward or backward. Defaults to backward.
  • regexp: a regex to filter the returned results

In microservices mode, /api/prom/query is exposed by the querier.

Note that the larger the time span between start and end will causeadditional load on Loki and the index store, resulting in slower queries.

Response:

  1. {
  2. "streams": [
  3. {
  4. "labels": "<LogQL label key-value pairs>",
  5. "entries": [
  6. {
  7. "ts": "<RFC3339Nano string>",
  8. "line": "<log line>"
  9. },
  10. ...
  11. ],
  12. },
  13. ...
  14. ]
  15. }

Examples

  1. $ curl -G -s "http://localhost:3100/api/prom/query" --data-urlencode '{foo="bar"}' | jq
  2. {
  3. "streams": [
  4. {
  5. "labels": "{filename=\"/var/log/myproject.log\", job=\"varlogs\", level=\"info\"}",
  6. "entries": [
  7. {
  8. "ts": "2019-06-06T19:25:41.972739Z",
  9. "line": "foo"
  10. },
  11. {
  12. "ts": "2019-06-06T19:25:41.972722Z",
  13. "line": "bar"
  14. }
  15. ]
  16. }
  17. ]
  18. }

Examples

  1. $ curl -H "Content-Type: application/json" -XPOST -s "https://localhost:3100/loki/api/v1/push" --data-raw \
  2. '{"streams": [{ "labels": "{foo=\"bar\"}", "entries": [{ "ts": "2018-12-18T08:28:06.801064-04:00", "line": "fizzbuzz" }] }]}'

GET /ready

/ready returns HTTP 200 when the Loki ingester is ready to accept traffic. Ifrunning Loki on Kubernetes, /ready can be used as a readiness probe.

In microservices mode, the /ready endpoint is exposed by all components.

POST /flush

/flush triggers a flush of all in-memory chunks held by the ingesters to thebacking store. Mainly used for local testing.

In microservices mode, the /flush endpoint is exposed by the ingester.

GET /metrics

/metrics exposes Prometheus metrics. SeeObserving Lokifor a list of exported metrics.

In microservices mode, the /metrics endpoint is exposed by all components.