Kubesense

Metrics Query Builder

A Metrics query charts one metric, narrowed by label filters and shaped by a chain of functions. The builder writes the PromQL for you and shows it under the query as you work, so you can build anything you would type by hand without leaving the builder.

Building a Query

  1. Open the Data Explorer and set the query's data source to Metrics
  2. Pick a metric from the metric selector
  3. Narrow it with label filters (see Filters)
  4. Click Add Function to shape the result (see Functions)
  5. Click Run

The PromQL preview under the functions updates on every change. It is the exact query the panel runs.

A metrics query: container_cpu_usage filtered to one namespace, then rate, then sum by container, with the PromQL preview below

In the example above, container_cpu_usage is filtered to the kubesense namespace, rate turns it into a per-second rate, and sum by (container_id, k8s_container_name) adds it up per container. The preview shows the PromQL those three steps produce.

Filters

Type filters into the search bar under the metric, as label:value. Several values for one label match any of them, and several excluded values exclude all of them.

Type in the barMatches series wherePromQL
pod:apipod is exactly apipod="api"
-pod:apipod is anything but apipod!="api"
pod:~api-.*pod matches the regular expressionpod=~"api-.*"
-pod:~api-.*pod does not match the regular expressionpod!~"api-.*"

Dashboard variables work in filter values, for example namespace:$namespace or pod:~$service-.*. When a variable has nothing selected, a filter that excludes it is left out rather than excluding everything.

note: A value that itself starts with - or ~ is read as an operator. To match such a value literally, switch to code mode and write the matcher yourself.

Functions

Functions apply in order, left to right: each one takes the result of the one before it. For example, rate then sum by (namespace) gives the per-second rate of each series, summed per namespace.

Adding a Function

Click Add Function and either browse or search:

  • Browse by category, then pick a function
  • Type part of a name to search the whole catalog at once, for example quantile lists quantile_over_time, histogram_quantile and the rest

Hover the ⓘ icon beside any function, in the menu or on a block, to see its signature and what it does.

Add Function searched for "quantile", with the tooltip open on Aggregate / quantile

CategoryWhat it doesExamples
RollupComputes one value per point from a window of past samplesrate, increase, avg_over_time, quantile_over_time
TransformChanges each value on its ownabs, clamp, histogram_quantile, round
LabelAdds, removes, renames or sorts by labelslabel_replace, label_keep, sort_by_label
AggregateCombines series into fewer seriessum, avg, topk, quantile, count_values
OperatorArithmetic and comparisons against a number+, *, >, default

Changing a Function

Click a function's name on its block to swap it for another. The same search opens in place, and the arguments the two functions share, such as the window, carry over.

Arguments

Each block shows its function's arguments as fields. The common ones:

ArgumentFound onNotes
overRollupsThe lookbehind window, such as 5m or $__rate_interval. Remove it to let the query engine pick one
stepRollupsEvaluates the rollup at this step instead of the panel's, like a PromQL subquery [5m:1m]
by / withoutAggregatesThe labels to keep, or to drop, when combining series
limitAggregatesCaps how many series the aggregate returns
keep_metric_namesRollups, transforms, operatorsKeeps the metric name, which these functions drop by default
boolComparisonsReturns 1 or 0 for every point instead of filtering
quantile / phiquantile_over_time, quantile, …A value from 0 to 1. 0.95 is the 95th percentile

Optional arguments are hidden until you add them with the + on the block, and each has its own × to remove it. The × at the end of the block removes the function.

Required Labels

Some label functions do nothing useful without labels, so the builder asks for them:

FunctionNeeds
labels_equalAt least 2 labels
label_keep, label_lowercase, label_uppercaseAt least 1 label
sort_by_label, sort_by_label_desc, sort_by_label_numeric, sort_by_label_numeric_descAt least 1 label

Until you fill them in, the block is outlined in red with Add at least N labels, and Run and saving the panel are refused. A dashboard imported from JSON with one of these functions missing its labels is rejected the same way.

Offset and @

Next to the metric are two optional modifiers for the series selector. Hover their ⓘ icons for a reminder in the product.

ModifierWhat it doesExample
offsetShifts the data back in time. Each point shows the value from that long before it, so the line keeps its shape, just lateroffset 1d compares with the same time yesterday
@Reads every point at one fixed time: start() or end() of the time range, or a Unix timestamptopk(5, x @ end()) picks the top 5 at the end of the range and keeps them fixed

info: On its own, @ draws a flat line: every point reads the same moment. It is useful combined with something that still changes over time, such as comparing against a baseline or choosing series at one moment.

Click the × on either modifier to clear it.

Code Mode

Click the Write PromQL icon in the query header to switch to code mode. The editor opens with the query the builder produced, and you can edit it freely, including variables such as $namespace and $__rate_interval.

Click Build a query to return to the builder. The builder keeps its own settings, so edits made in code mode are not carried back into it.

Duplicating a Query

Open a query's ⋯ menu and click Duplicate to add an exact copy right below it, under the next free letter. Change the copy to compare two variants side by side, for example the same metric with and without an offset.

Duplicate is not offered on panel types that hold a single query, and is disabled once the panel has 10 queries.