Query Log

When your Prometheus server is experiencing a high query load, it can be useful to get more insight into the specific PromQL queries that your Prometheus server is running. Prometheus allows you to turn on an optional query log file that logs all PromQL queries, along with key execution statistics. Since this log file is configured in Prometheus' main configuration file (which can be live-reloaded), you can turn it off and on at run-time as needed.

You can configure the query log file using the query_log_file field in the global section of the main Prometheus configuration file. For example, to write all PromQL queries to a file named /tmp/prometheus-queries.log, the global section could read:

global:
  query_log_file: /tmp/prometheus-queries.log

After configuring this setting, Prometheus will write every completed query to the log file in a JSON-based format.

Here is an example of a 1-hour range query for the PromQL expression sum by(mode) (rate(demo_cpu_usage_seconds_total[5m])):

{
   "spanID" : "0000000000000000",
   "params" : {
      "end" : "2022-08-20T15:31:36.161Z",
      "start" : "2022-08-20T14:31:36.161Z",
      "query" : "sum by(mode) (rate(demo_cpu_usage_seconds_total[5m]))",
      "step" : 14
   },
   "ts" : "2022-08-20T15:31:36.169Z",
   "stats" : {
      "samples" : {
         "totalQueryableSamples" : 258942,
         "peakSamples" : 5181
      },
      "timings" : {
         "resultSortTime" : 2.103e-06,
         "execTotalTime" : 0.002680069,
         "execQueueTime" : 7.788e-06,
         "queryPreparationTime" : 6.5491e-05,
         "innerEvalTime" : 0.002585578,
         "evalTotalTime" : 0.002665138
      }
   },
   "httpRequest" : {
      "path" : "/api/v1/query_range",
      "clientIP" : "::1",
      "method" : "GET"
   }
}

The fields mean the following:

  • spanID: The trace span ID, when using request tracing.
  • params: The query parameters like start and end time, resolution step, and query expression.
  • ts: The timestamp of when the query was executed.
  • stats: This contains a range of internal statistics that can give you an idea about the size and duration of the query. Most importantly, the timings.evalTotalTime field tells you the total query execution time.
  • httpRequest: For queries that were triggered over HTTP, this contains the HTTP path and method, as well as the triggering client's IP address. This is helpful for tracking down where queries are coming from.

This query log is very useful for tracking down slow or expensive queries that are overloading your Prometheus servers. Note however, that the query log file only includes queries that have already completed. If an expensive query causes your Prometheus to crash due to an out-of-memory (OOM) error, you will not be able to find out the query causing the OOM from this log file. We will learn how to deal with this kind of situation in the next section.