Filtering in the API
How the REST API, Terraform and MCP return and accept filters, where the `kql` field goes on each object, and what scripts written before the filter bar can ignore.
This page is for developers who call the KubeSense REST API, Terraform or MCP. It shows what a read returns, where to put kql when you write, and what an existing script can ignore.
The rule
Every logs, traces, events and RUM filter you read has a kql you can read. To change a filter, send kql. Everything else that filters add to an answer is read-only, and you can ignore it.
- A script that sends the old Basic or Advanced filter keeps working.
- A script that sends a read back unchanged keeps working.
- You never need to send
kql_flags,kql_computed,kql_sigorlegacy.
Reading
A read adds filter members beside the filter. This is a logs query of an alert rule, from GET /api/alerts/rules/{id}. The read shape of an alert is in query_config, with the filter in unified_filter, as Fetch one alert rule describes. The filter is trimmed here.
{
"label": "A",
"selectedMode": "logs",
"value_operation": "row_count",
"unified_filter": {
"type": "common",
"common_filter": [
{ "field": "workload", "operation": "IN", "values": ["payment"], "type": "string" }
]
},
"kql": "workload:payment",
"kql_version": 1,
"kql_computed": true,
"kql_sig": "7e9ebb7a4c8e.612949c1fc0a"
}When the saved filter could not be written in the filter syntax, the query has legacy and no kql. See When a filter cannot be converted.
{
"label": "A",
"selectedMode": "logs",
"value_operation": "row_count",
"unified_filter": {
"type": "common",
"common_filter": [
{ "field": "nosuchfield", "operation": "IN", "values": ["x"], "type": "string" }
]
},
"legacy": {
"reason": "unknown_field",
"detail": "no logs field reaches column \"nosuchfield\""
},
"kql_computed": true
}| Member | What it means | Do you send it? |
|---|---|---|
kql | The filter text. | Yes. It is the one member to send when you change a filter. |
kql_version | The version of the filter syntax that the text uses, an integer. It is 1 today. | Only to pin a version. Leave it out for the latest. See Versions. |
kql_flags | A list of notice names, present only when the conversion needs a note. Each one is a heading on What changed when your filter was converted, such as body_exact_to_token. | No. |
kql_computed | true when KubeSense wrote the kql from the saved filter, and no one typed it. | No. A write drops it. |
kql_sig | A fingerprint of the kql and of the filter beside it. A write uses it to tell which of the two you changed. It is never stored. | No. Send the read back with it, or leave it out. |
legacy | reason and detail for a filter that cannot be written in the filter syntax. The reasons are in the table under When a filter cannot be converted. | No. |
The members sit on the object that holds the filter: a query of an alert, a good or total filter of an SLO, a query of a dashboard panel, a saved view, a pipeline rule's config, and a log-to-metric query. A metrics, SPL or SQL query has none of them.
Writing
Send kql on the object that holds the filter, and leave out the filter tree and the chips. KubeSense compiles the text and stores the filter it compiles to.
An alert rule
POST /api/alerts/rules and PUT /api/alerts/rules/{id} take kql on each logs, traces or RUM entry of query. The entry names its signal with selectedMode. A RUM entry also names its event type with rumEventType.
curl -s -X POST "https://<your-kubesense-host>/api/alerts/rules" \
-H "X-API-Key: $KUBESENSE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "Payment errors",
"severity": "critical",
"query": [
{
"label": "A",
"selectedMode": "logs",
"value_operation": "row_count",
"kql": "workload:payment type:ERROR",
"groupBy": [{ "field": "workload", "type": "string", "is_attribute": false }]
}
],
"metric_query_label": "A",
"threshold": { "operator": "greater_than", "value": 5, "frequency": "at_least_once" },
"time_window_prometheus_format": "5m",
"evaluation_interval_prometheus_format": "1m"
}'The rule stores kql and kql_version beside the filter it compiled to, and evaluates the filter. An entry with selectedMode of metrics or formula and a kql returns 400 unsupported_signal. So does a RUM entry with no rumEventType, or with one that is not session, view, action, error, resource or long_task.
A dashboard panel
A dashboard filter lives inside preset, which is a JSON string. See Create a dashboard. In a panel query of that preset, send kql and leave filters empty or out.
{
"selectedMode": "logs",
"label": "A",
"kql": "workload:payment type:ERROR"
}KubeSense stores the query in filter mode, and a read gives it back that way. The text is in filters.kql, in a list of one, and the version is the camelCase kqlVersion.
{
"selectedMode": "logs",
"label": "A",
"filterMode": "KQL",
"filters": { "kql": ["workload:payment type:ERROR"] },
"kqlVersion": 1
}The text can name the dashboard's variables, as in workload:$svc or $$svc. A name the preset does not define is accepted, and its term is ignored when the panel runs. In every other filter text, such as an explorer search, an alert, an SLO, a pipeline or an MCP search, a $name, ${name} or $$name is ordinary text, searched as typed.
A query that is still a Basic or Advanced filter reads back with kql, kql_version and kql_computed, as in Reading. If the query has chips in filters, a kql beside them returns 400 kql_with_filters.
Other objects
| Object | Where to send kql | Notes |
|---|---|---|
| Alert rules | query[].kql on create, update and bulk. query_config[].kql on the /definition routes. | Logs, traces and RUM entries. A RUM entry needs rumEventType. |
| SLOs | good_events_filter.kql and total_events_filter.kql on POST and PUT /api/slo. | Logs and traces filters. Set selectedMode on the filter. |
| Saved views | kql on POST /api/views and PUT /api/views/{id}, with source set. | Stored in raw_filters as {"kql", "kql_version"}. A read gives kql and kql_version on the view. |
| Log-to-metric | query.kql on create and update. | Stored in query.raw_filters as {"kql", "kql_version"}. |
| Log pipelines | rules[].config.kql on create and update of block and replace rules. | Stored in config.raw_conditions as {"kql", "kql_version"}. A /regex/ term works only here and in log-to-metric. |
| Dashboard panels, variables and event overlays | kql on the query, the variable's meta or the overlay, inside preset. | Stored as filterMode: "KQL" with the text in filters.kql. |
| Workflows | kql on a step's query_config[] entry, or on the step's config. | A $NAME in the text takes the workflow input at run time. |
| List endpoints | kql in the body of the SLO, dashboard, workload, audit, issue, anomaly and cloud list calls, and ?kql= on GET /api/alerts/rules. | A kql beside filters returns 400 kql_with_filters. |
The fields each screen accepts are in Filtering on each screen.
Existing scripts keep working
A script written before the filter bar needs no change.
- The old filter. A body with the Basic chips or the Advanced tree and no
kqlreaches the handler as it was sent. Nothing is converted or stored on your behalf. - A read sent back unchanged. KubeSense drops
kql_sig,kql_computed,kql_flagsandlegacy, and does not store akqlit wrote itself. - Only the filter changed. If you edit the tree or the chips of a read and send it back, the text is out of date. KubeSense drops it, and the new filter is stored as you sent it.
- Only
kqlchanged. If you edit the text of a read and leave the tree as read, KubeSense compiles the text and stores the filter it gives. - Both changed, and they disagree. You get
400 tree_differs_from_kql. Sendkqlalone, or the tree alone.
These rules need kql_sig, which tells KubeSense which side you edited. A body you build from scratch has none. If it holds a kql and a tree that do not match, it returns 400 tree_differs_from_kql.
On a server where the filter bar is switched off, a read has no filter members. A read sent back has its stale text dropped. A write that sends kql returns 400 kql_not_enabled, unless it is a read sent back with its filter tree.
Errors
A refused write returns 400 with the problems in data.errors. Each has the path of the member, a code, a message, and start and end, the characters of the text it marks. This is a typo in query[0].kql:
{
"error": true,
"message": "query[0].kql: Unknown field workloadx on Logs. Did you mean workload? Attributes take @, as in @workloadx.",
"data": {
"errors": [
{
"path": "query[0].kql",
"code": "unknown_field",
"message": "Unknown field workloadx on Logs. Did you mean workload? Attributes take @, as in @workloadx.",
"start": 0,
"end": 9
}
]
}
}| Code | When |
|---|---|
A filter error code, such as unknown_field, missing_field, sql_operator, unsupported_operator, text_too_long, bad_variable or regex_not_allowed | The text is not a valid filter. See Errors. |
bad_kql | kql is not a string. |
bad_kql_version | kql_version is not an integer in the supported range. The message names the range. See Versions. |
tree_differs_from_kql | You changed both the kql and the filter beside it, and they differ. |
unsupported_signal | The query is not a logs, traces, events or RUM query, or its signal is not one that filters support. |
kql_with_filters | A dashboard query or a list body holds kql and chips. |
kql_not_supported | The object cannot take a filter, such as a dashboard query in SPL or SQL mode, or a pipeline condition the pipeline cannot run. |
kql_not_enabled | Filters are switched off on this server. |
A body with a filter in it must be at most 8 MB and sent with Content-Type: application/json. Otherwise you get 413 kql_body_too_large or 415 kql_body_not_json.
Terraform and MCP
Terraform
- On
kubesense_alert_rule,kqlis a plain optional string in aquery_configblock.kql_versionis optional and computed, so a plan shows no change when the server stamps a version you did not name. - An import adopts a
kqlthat someone wrote. Akqlthat KubeSense rendered from the saved filter is not adopted. kql_flags,kql_sig,kql_computedandlegacystay out of the query'sextra_json, so they never show in a plan.kubesense_dashboard_jsonremoves the members a read adds before it compares, so a dashboard you wrote as chips shows no false diff. Akqlthat someone wrote stays.
MCP
search-logs,search-traces,search-rum,analyze-logs,analyze-tracesandcreate-alerttake the filter in theirqueryargument, andanalyze-telemetryin each entry ofqueries, with an optional integerkql_version.- A filter in an older argument, such as
whereorfilters, is refused. - The log pipeline tools take
kqlin a rule'sconfig.create-dashboardandupdate-dashboardtakekqlon a panel query. get-alert-details,get-dashboard-detailsandget-log-pipelinereturnkql, orlegacywhen the filter cannot be written in the filter syntax.- A value of
kql_versionoutside the supported range is an error that names the range.
See MCP server for the tools.
Storage
Some objects store the filter text and some keep the original filter until someone edits it. A read looks the same either way, apart from the dashboard form shown above.