Variables use the syntax ${{variable name|default value}} or ${{variable name|default value|prefix}} to dynamically adjust placeholders in query parameters. By setting these variables, you can filter charts or an entire dashboard without directly modifying query analysis statements. This topic describes how to filter an entire dashboard and individual charts.
Add a filter to filter data on a dashboard
Filter types
Each chart displays the query and analysis results of a query statement. Add a filter to modify search conditions or replace placeholder variables for all charts on a dashboard to filter data on the dashboard. Simple Log Service (SLS) provides the following types of filters:
Filters of the Filter type: This type of filter uses the key-value pairs of log fields as filter conditions. A filter of the Filter type searches the results of the specified query statement for logs that contain or do not contain the specified
key-valuepair. For example, add a filter of the Filter type to query the number of page views (PVs) for each request method by using dynamic list items.Filters of the Variable Replacement type: This type of filter uses variables as filter conditions. Before you add a filter of the Variable Replacement type, you must specify a variable for the query statements of related charts on your dashboard. After you add the filter, the variable in the query statements of the charts is replaced with the value that you specify for the variable. For example, query the numbers of PVs per 10 seconds, 60 seconds, or 600 seconds.
Filters of the Metric Filter type: This type of filter uses labels and values of metrics as filter conditions. After you collect metrics, add a filter of the Metric Filter type. You can add multiple filter conditions. The filter conditions are evaluated by using a logical AND.
Filters of the Filter type
Add a filter of the Variable Replacement type
Filters of the Metric Filter type
Configure settings for variable replacement to filter data on a chart
Configure settings for variable replacement for a chart. After you configure the settings, SLS adds a filter in the upper-right corner of the chart. Use the filter to filter data on the chart. The following section describes the operations that you must perform to query the numbers of PVs within different time ranges in a table.
1. Configure settings for variable replacement
On the left side of the Edit Chart page, configure the query time range, Logstore, and query statement for which you want to specify a variable.
The following query statement is executed to query the numbers of PVs within different time ranges.
${{date|60}}specifies that the name of the variable isdateand the default value of the variable is 60.* | select __time__ - __time__ % ${{date | 60}} as time, COUNT(*) as pv, avg(request_time) as duration, request_method GROUP BY time, request_method order by time limit 1000
Configure settings for variable replacement
On the right side of the Edit Chart page, select Table Pro in the Chart Types section and choose in the Variable Replacement section.

In the Variable Replacement dialog box, set the Variable Key parameter to data, the Display Name parameter to time, the Display Name parameter below the Variable Values parameter to min and hour, and the Replacement Value parameter to 60 and 3600.

In the upper-right corner of the Edit Chart page, click OK. Then, in the upper-right corner of the dashboard page, click Save. In the Save Dashboard dialog box, click OK.
2. Filter data on the chart
After you configure the settings, a filter appears in the upper-left corner of the chart. If you select a value from the filter drop-down list, SLS performs a query and analysis operation based on the value that you select. For example, if you select hour, SLS executes the following query statement:
(*)| select __time__ - __time__ % 3600 as time, COUNT(*) as pv, avg(request_time) as duration, request_method GROUP BY time, request_method order by time limit 1000 
Use variables together with interaction occurrences
When you configure an interaction occurrence for a chart, you can also specify a variable. When you click a value on a chart to trigger an interaction occurrence, the system automatically replaces the specified variable with the value. This facilitates data query and analysis. For more information, see Configuration examples.

Advanced use of variables (using prefixes)
When you filter a dashboard or a chart, you can also use the ${{variable_name|default_value|prefix}} syntax with a prefix.
If you do not set a value for the variable, the query uses the default value.
If you set a value for the variable, the query statement combines the prefix and the variable value.
Filter a dashboard: HTTP method (http_method) filtering
Variable description
Variable format:
${{method_filter|http_method in ('GET','POST')|http_method =}}Query statement:
(*)|select status, count(1) as err_count where ${{method_filter|request_method in ('GET','POST')|request_method =}} and status>=400 group by statusIf the variable is not set: By default, the query analyzes the distribution of errors with a status code of 400 or higher for GET or POST requests. The query statement is:
(*)|select status, count(1) as err_count where request_method in ('GET','POST') and status>=400 group by statusIf the variable is set to
'DELETE': The query switches to analyze the error status for theDELETEmethod. The query statement is:(*)| select status, count(1) as err_count where request_method ='DELETE' and status>=400 group by status
Procedure
Configure the chart: In the panel on the left, set the time range, select the logstore, and enter the query statement that contains the variable. In the panel on the right, select Table. Click Apply at the top of the page to preview the chart. Then, click OK to save the chart.

Configure a variable filter: In the Filter panel, add values to the drop-down list for the
method_filtervariable, as shown in the following figure.
Filter the dashboard: Click the filter and select
DELETE. The dashboard displays statistics based on the(*)| select status, count(1) as err_count where request_method ='DELETE' and status>=400 group by statusquery statement.
Filter a chart: Request time (request_time) threshold filtering
Variable description
Variable format:
${{slow_query|request_time>50|request_time>}}Query statement:
(*)| select request_uri, avg(request_time) as avg_time where ${{slow_query|request_time>50|request_time>}} group by request_uri order by avg_time descIf the variable is not set: By default, the query filters for APIs with a request time greater than 50 ms and sorts them by average request time in descending order. The query statement is:
(*)| select request_uri, avg(request_time) as avg_time where request_time>50 group by request_uri order by avg_time descIf the variable is set to
1000 ms: The query is adjusted to analyze slow APIs that take longer than 1,000 ms. The query statement is:(*)| select request_uri, avg(request_time) as avg_time where request_time>1000 group by request_uri order by avg_time desc
Procedure
Configure the chart: In the left panel of the Edit Chart page, set the time range, select the logstore, and enter the query statement that contains the variable.

Configure variable replacement: In the right panel of the Edit Chart page, click Variable Replacement. In the Variable Replacement dialog box, add values to the drop-down list for the
slow_queryvariable, as shown in the following figure.
Filter the chart: Click the filter box in the upper-left corner of the chart. When you select
1000 ms, the chart displays statistics based on the(*)| select request_uri, avg(request_time) as avg_time where request_time>1000 group by request_uri order by avg_time descquery statement.






