Variables are placeholders for values. You can use variables in query statements. You can use variables to create dashboards that are more interactive and dynamic.
Entry point
If you want to execute a query statement on the query and analysis page of a Logstore, you cannot specify variables in the query statement.
Log on to the Simple Log Service console.
Go to the Dashboard page.
In the Projects section, click the project that you want to manage.
In the left-side navigation pane, choose
.In the Dashboard list, click the required dashboard.
On the dashboard page that appears, click Edit.
Click Create Chart or find the chart that you want to manage and choose to go to the edit page.
Specify variables in the query statement.
Configuration description
If you specify a variable in a drill-down event or a filter, you must add the variable to the query statement. The variable is in the ${{Variable name|Default value}}
format. For example, you can specify host=~"${{host|^.*}}"
for host=~"^.*"
.
Drill-down events
For example, you can specify a variable in the query statement of Chart A and add Chart A to Dashboard A. You can also configure a drill-down event to open Dashboard A for Chart B and specify the variable in the drill-down event. When you click a value in Chart B to trigger the drill-down event, the system replaces the variable in the query statement of Chart A with the value in Chart B and re-executes the query statement. Then, Chart A is updated. For more information, see Drill-down events.
Filters
If you add a filter of the Replace Variable type on a dashboard and the dashboard contains a chart whose query statement includes the variable specified for the filter, the system automatically replaces the variable in the query statement for the chart with the value that you select for the filter. The variable configuration applies to all charts whose query statements include the variable on the dashboard. For more information, see Add a filter of the Replace Variable type.
Variable replacement
This method adds a filter of the Replace Variable type to a single chart. After you configure the settings of variable replacement on the Common Settings tab, Simple Log Service adds a filter in the upper-left corner of the chart. You can select a value from the filter drop-down list. After you select a value, Simple Log Service automatically replaces the variable in the query statement of the chart with the variable value indicated by the value that you select, and performs a query and analysis operation. For more information, see Example 2: Configure variable replacement.
Configuration examples
Example 1: Specify variables in a query statement
In the following query statement, change the value of the request_method field to the ${{method|PUT}}
variable:
request_method: * | SELECT __time__ - __time__ %60 AS time, COUNT(1) AS PV GROUP BY time ORDER BY time
Example 2: Configure variable replacement
In the following query statement, change the value 60 to the
${{date | 60}}
variable:* | select __time__ - __time__ % 60 as time, COUNT(*) as pv, avg(request_time) as duration, request_method GROUP BY time, request_method order by time limit 1000
Configure the settings of variable replacement on the Common Settings tab.
Set Variable Key to date and Display Name below Variable Key to time. Set Display Name below Variable Values to min and hour, and set Replaced Value to 60 for min and 3600 for hour.
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, Simple Log Service performs a query and analysis operation based on the value that you select. For example, if you select hour, Simple Log Service executes the
* | SELECT __time__ - __time__ %3600 AS time, COUNT(*) AS pv, approx_distinct(remote_addr) AS uv GROUP BY time ORDER BY time LIMIT 10000
query statement.