mod grafana reference

This commit is contained in:
sheyanjie-qq 2024-07-10 18:46:29 +08:00
parent c8aa963417
commit bdc0b5c194
8 changed files with 460 additions and 51 deletions

View File

@ -378,7 +378,7 @@ The configuration parameters in properties are as follows.
- TSDBDriver.PROPERTY_KEY_DISABLE_SSL_CERT_VALIDATION: Whether to disable SSL certification validation. It only takes effect when using Websocket connections. true: enabled, false: disabled. The default is false.
For JDBC native connections, you can specify other parameters, such as log level, SQL length, etc., by specifying URL and Properties. For more detailed configuration, please refer to [Client Configuration](../../reference/config/#configuration-file-on-client-side).
For JDBC native connections, you can specify other parameters, such as log level, SQL length, etc., by specifying URL and Properties. For more detailed configuration, please refer to [Client Configuration](../../reference/config/).
### Priority of configuration parameters

View File

@ -22,9 +22,7 @@ Record these values:
- TDengine REST API url: `http://tdengine.local:6041`.
- TDengine cluster authorization, with user + password.
## Configuring Grafana
### Install Grafana Plugin and Configure Data Source
## Install Grafana Plugin and Configure Data Source
<Tabs defaultValue="script">
<TabItem value="gui" label="With GUI">
@ -34,8 +32,6 @@ Under Grafana 8, plugin catalog allows you to [browse and manage plugins within
Installation may cost some minutes, you can **Create a TDengine data source** when installation finished.
Then you can add a TDengine data source by filling up the configuration options.
![TDengine Database Grafana plugin add data source](./grafana/add_datasource3.webp)
- Host: IP address of the server where the components of the TDengine cluster provide REST service and the port number of the TDengine REST service (6041), by default use `http://localhost:6041`.
- User: TDengine user name.
- Password: TDengine user password.
@ -99,8 +95,6 @@ Now users can log in to the Grafana server (username/password: admin/admin) dire
Click `Add data source` to enter the Add data source page, and enter TDengine in the query box to add it.
Enter the datasource configuration page, and follow the default prompts to modify the corresponding configuration.
![TDengine Database TDinsight plugin add database 3](./grafana/add_datasource3.webp)
- Host: IP address of the server where the components of the TDengine cluster provide REST service and the port number of the TDengine REST service (6041), by default use `http://localhost:6041`.
- User: TDengine user name.
- Password: TDengine user password.
@ -177,37 +171,112 @@ Open Grafana (http://localhost:3000), and you can add dashboard with TDengine no
</TabItem>
</Tabs>
### Create Dashboard
:::info
Go back to the main interface to create a dashboard and click Add Query to enter the panel query page:
In the following introduction, we take Grafana v11.0.0 as an example. Other versions may have different features, please refer to [Grafana's official website](https://grafana.com/docs/grafana/latest/).
:::
## Built-in Variables and Custom Variables
The Variable feature in Grafana is very powerful. It can be used in queries, panel titles, labels, etc., to create more dynamic and interactive Dashboards, improving user experience and efficiency.
The main functions and characteristics of variables include:
- Dynamic data query: Variables can be used in query statements, allowing users to dynamically change query conditions by selecting different variable values, thus viewing different data views. This is very useful for scenarios that need to dynamically display data based on user input.
- Improved reusability: By defining variables, the same configuration or query logic can be reused in multiple places without the need to rewrite the same code. This makes the maintenance and updating of Dashboards simpler and more efficient.
- Flexible configuration options: Variables offer a variety of configuration options, such as predefined static value lists, dynamic value querying from data sources, regular expression filtering, etc., making the application of variables more flexible and powerful.
Grafana provides both built-in variables and custom variables, which can be referenced in SQL writing. We can use `$variableName` to reference the variable, where `variableName` is the name of the variable. For detailed reference, please refer to [Variable reference](https://grafana.com/docs/grafana/latest/dashboards/variables/variable-syntax/).
### Built-in Variables
Grafana has built-in variables such as `from`, `to`, and `interval`, all derived from Grafana plugin panels. Their meanings are as follows:
- `from` is the start time of the query range
- `to` is the end time of the query range
- `interval` represent time spans
It is recommended to set the start and end times of the query range for each query, which can effectively reduce the amount of data scanned by the TDengine server during query execution. `interval` is the size of the window split, which in Grafana version 11, is calculated based on the time range and the number of return points.
In addition to the above three common variables, Grafana also provides variables such as `__timezone`, `__org`, `__user`, etc. For details, please refer to [Built-in Variables](https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables/#global-variables).
### Custom Variables
We can add custom variables in the Dashboard. The usage of custom variables is no different from that of built-in variables; they are referenced in SQL with `$variableName`.
Custom variables support multiple types, such as `Query`, `Constant`, `Interval`, `Data source`, etc.
Custom variables can reference other custom variables, for example, one variable represents a region, and another variable can reference the value of the region to query devices in that region.
#### Adding Query Type Variables
In the Dashboard configuration, select `Variables`, then click `New variable`:
1. In the `Name` field, enter your variable name, here we set the variable name as `selected_groups`.
2. In the `Select variable type` dropdown menu, select `Query`.
Depending on the selected variable type, configure the corresponding options. For example, if you choose `Query`, you need to specify the data source and the query statement for obtaining variable values. Here, taking smart meters as an example, we set the query type, select the data source, and configure the SQL as `select distinct(groupid) from power.meters where groupid < 3 and ts > $from and ts < $to;`
3. After clicking `Run Query` at the bottom, you can see the variable values generated based on your configuration in the `Preview of values` section.
4. Other configurations are not detailed here. After completing the configuration, click the `Apply` button at the bottom of the page, then click `Save dashboard` in the top right corner to save.
After completing the above steps, we have successfully added a new custom variable `$selected_groups` to the Dashboard. We can later reference this variable in the Dashboard's queries with `$selected_groups`.
We can also add another custom variable to reference this `selected_groups` variable, for example, we add a query variable named `tbname_max_current`, with the SQL as `select tbname from power.meters where groupid = $selected_groups and ts > $from and ts < $to;`
#### Adding Interval Type Variables
We can customize the time window interval to better fit business needs.
1. In the `Name` field, enter the variable name as `interval`.
2. In the `Select variable type` dropdown menu, select `Interval`.
3. In the `Interval options` enter `1s,2s,5s,10s,15s,30s,1m`.
4. Other configurations are not detailed here. After completing the configuration, click the `Apply` button at the bottom of the page, then click `Save dashboard` in the top right corner to save.
After completing the above steps, we have successfully added a new custom variable `$interval` to the Dashboard. We can later reference this variable in the Dashboard's queries with `$interval`.
## TDengine Time Series Query Support
On top of supporting standard SQL, TDengine also provides a series of special query syntaxes that meet the needs of time series business scenarios, bringing great convenience to the development of applications in time series scenarios.
- `partition by` this clause can split data by certain dimensions and then perform a series of calculations within the split data space, which can replace `group by` in most cases.
- `interval` this clause is used to generate time windows of the same time interval.
- `fill` this clause is used to specify how to fill when there is data missing in any window.
- `Window Pseudocolumns` If you need to output the time window information corresponding to the aggregation result in the results, you need to use window pseudocolumns in the SELECT clause: the start time of the time window (_wstart), the end time of the time window (_wend), etc.
For a detailed introduction to these features, please refer to [Time-Series Extensions](../../taos-sql/distinguished/).
## Create Dashboard
Return to the main interface to create a Dashboard, click Add Query to enter the panel query page:
![TDengine Database TDinsight plugin create dashboard 1](./grafana/create_dashboard1.webp)
As shown above, select the `TDengine` data source in the `Query` and enter the corresponding SQL in the query box below for query. We will continue to use power meters as an example. In order to demonstrate the beautiful curves, **virtual data** is used here.
- INPUT SQL: Enter the desired query (the results being two columns and multiple rows), such as `select _wstart as ts, avg(current) as current from power.meters where ts > $from and ts < $to interval($interval) fill(null)`. In this statement, `$from`, `$to`, and `$interval` are variables that Grafana replaces with the query time range and interval. In addition to the built-in variables, custom template variables are also supported.
- ALIAS BY: This allows you to set the current query alias.
- GENERATE SQL: Clicking this button will automatically replace the corresponding variables and generate the final executed statement.
- Group by column(s): `group by` or `partition by` columns name split by comma. By setting `Group by column(s)`, it can show multi-dimension data if Sql is `group by` or `partition by`. Such as, it can show data by `groupid` if sql is `select _wstart as ts, groupid, avg(current) as current from power.meters where ts > $from and ts < $to partition by groupid interval($interval) fill(null)` and `Group by column(s)` is `groupid`.
- Group By Format: format legend for `group by` or `partition by`. For example, in the above Input SQL, set `Group By Format` to `groupid-{{groupid}}`, and display the legend name as the formatted group name.
## Time Series Data Display
Suppose we want to query the average current size over a period of time, with the time window divided by $interval, and fill with null if data is missing in any time window interval.
- INPUT SQL: Enter the statement to be queried (the result set of this SQL statement should be two columns and multiple rows), here enter: `select _wstart as ts, avg(current) as current from power.meters where groupid in ($selected_groups) and ts > $from and ts < $to interval($interval) fill(null)`, where from, to, and interval are built-in variables of the Grafana, selected_groups is a custom variable.
- ALIAS BY: You can set the current query alias.
- GENERATE SQL: Clicking this button will automatically replace the corresponding variables and generate the final execution statement.
In the custom variables at the top, if the value of `selected_groups` is selected as 1, then the query for the average value change of all device currents in the `meters` supertable where `groupid` is 1 is as shown in the following figure:
![TDengine Database Grafana plugin create dashboard](./grafana/create_dashboard2.webp)
:::note
Since the REST connection because is stateless. Grafana plugin can use &lt;db_name&gt;.&lt;table_name&gt; in the SQL command to specify the database name.
Since the REST interface is stateless, it is not possible to use the `use db` statement to switch databases. In the SQL statement in the Grafana plugin, you can use \<db_name>.\<table_name> to specify the database.
:::
Query the average current changes of all devices in the `meters` stable as shown in the following figure:
## Time Series Data Group Display
Suppose we want to query the average current value over a period of time, displayed by `groupid` grouping, we can modify the previous SQL to `select _wstart as ts, groupid, avg(current) as current from power.meters where ts > $from and ts < $to partition by groupid interval($interval) fill(null)`
![TDengine Database TDinsight plugin create dashboard 2](./grafana/create_dashboard2.webp)
- Group by column(s): **Half-width** comma-separated `group by` or `partition by` column names. If it is a `group by` or `partition by` query statement, setting the `Group by` column can display multidimensional data. Here, set the Group by column name to `groupid`, which can display data grouped by `groupid`.
- Group By Format: Legend formatting format for multidimensional data in Group by or Partition by scenarios. For example, the above INPUT SQL, setting `Group By Format` to `groupid-{{groupid}}`, the displayed legend name is the formatted group name.
Query the average current value of all devices in the 'meters' stable and display it in groups according to the `groupid` as shown in the following figure:
After completing the settings, the data is displayed grouped by `groupid` as shown in the following figure:
![TDengine Database TDinsight plugin create dashboard 2](./grafana/create_dashboard3.webp)
![TDengine Database Grafana plugin create dashboard](./grafana/create_dashboard3.webp)
> For more information on how to use Grafana to create the appropriate monitoring interface and for more details on using Grafana, refer to the official Grafana [documentation](https://grafana.com/docs/).
### Importing the Dashboard
## Performance Suggestions
- **Include time range in all queries**, in time series databases, if the time range is not included in the query, it will lead to table scanning and poor performance. A common SQL writing example is `select column_name from db.table where ts > $from and ts < $to;`
- For queries of the latest status type, we generally recommend **enabling cache when creating the database** (`CACHEMODEL` set to last_row or both), a common SQL writing example is `select last(column_name) from db.table where ts > $from and ts < $to;`
## Importing the Dashboard
You can install TDinsight dashboard in data source configuration page (like `http://localhost:3000/datasources/edit/1/dashboards`) as a monitoring visualization tool for TDengine cluster. Ensure that you use TDinsight for 3.x. Please note TDinsight for 3.x needs to configure and run taoskeeper correctly.
@ -221,3 +290,137 @@ For more dashboards using TDengine data source, [search here in Grafana](https:/
- [15155](https://grafana.com/grafana/dashboards/15155): TDengine alert demo.
- [15167](https://grafana.com/grafana/dashboards/15167): TDinsight.
- [16388](https://grafana.com/grafana/dashboards/16388): Telegraf node metrics dashboard using TDengine data source.
## Alert Configuration Introduction
### Alert Configuration Steps
The TDengine Grafana plugin supports alerts. To configure alerts, the following steps are required:
1. Configure Contact Points: Set up notification channels, including DingDing, Email, Slack, WebHook, Prometheus Alertmanager, etc.
2. Configure Notification Policies: Set up routing for which channel to send alerts to, as well as the timing and frequency of notifications.
3. Configure "Alert rules": Set up detailed alert rules.
3.1 Configure alert name.
3.2 Configure query and alert trigger conditions.
3.3 Configure evaluation behavior.
3.4 Configure labels and notifications.
3.5 Configure annotations.
### Alert Configuration Web UI
In Grafana 11, the alert Web UI has 6 tabs: "Alert rules", "Contact points", "Notification policies", "Silences", "Groups", and "Settings".
- "Alert rules" displays and configures alert rules.
- "Contact points" support notification channels such as DingDing, Email, Slack, WebHook, Prometheus Alertmanager, etc.
- "Notification policies" sets up routing for which channel to send alerts to, as well as the timing and frequency of notifications.
- "Silences" configures silent periods for alerts.
- "Groups" displays grouped alerts after they are triggered.
- "Admin" allows modifying alert configurations through JSON.
## Configuring Email Contact Point
### Modifying Grafana Server Configuration File
Add SMTP/Emailing and Alerting modules to the Grafana service configuration file. For Linux systems, the configuration file is usually located at `/etc/grafana/grafana.ini`.
Add the following content to the configuration file:
```ini
#################################### SMTP / Emailing ##########################
[smtp]
enabled = true
host = smtp.qq.com:465 #Email service used
user = receiver@foxmail.com
password = *********** #Use mail authorization code
skip_verify = true
from_address = sender@foxmail.com
```
Then restart the Grafana service. For example, on a Linux system, execute `systemctl restart grafana-server.service`
### Grafana Configuration for Email Contact Point
Find "Home" -> "Alerting" -> "Contact points" on the Grafana page to create a new contact point
"Name": Email Contact Point
"Integration": Select the contact type, here choose Email, fill in the email receiving address, and save the contact point after completion
![TDengine Database Grafana plugin alert email](./grafana/alert-email.webp)
## Configuring Feishu Contact Point
### Feishu Robot Configuration
1. "Feishu Workspace" -> "Get Apps" -> "Search for Feishu Robot Assistant" -> "Create Command"
2. Choose Trigger: Grafana
3. Choose Action: Send a message through the official robot, fill in the recipient and message content
![TDengine Database Grafana plugin feishu robot](./grafana/alert-feishu1.webp)
### Grafana Configuration for Feishu Contact Point
Find "Home" -> "Alerting" -> "Contact points" on the Grafana page to create a new contact point
"Name": Feishu Contact Point
"Integration": Select the contact type, here choose Webhook, and fill in the URL (the Grafana trigger Webhook address in Feishu Robot Assistant), then save the contact point
![TDengine Database Grafana plugin feishu contact point](./grafana/alert-feishu2.webp)
## Notification Policy
After configuring the contact points, you can see there is a Default Policy
![TDengine Database Grafana plugin Notification default policy](./grafana/alert-notification1.webp)
Click on the "..." on the right -> "Edit", then edit the default notification policy, a configuration window pops up:
![TDengine Database Grafana plugin Notification](./grafana/alert-notification2.webp)
Configure the parameters as shown in the screenshot above.
## Configuring Alert Rules
### Define Query and Alert Conditions
Select "Edit" -> "Alert" -> "New alert rule" in the panel where you want to configure the alert.
1. "Enter alert rule name": Here, enter `power meters alert` as an example for smart meters.
2. "Define query and alert condition":
2.1 Choose data source: `TDengine Datasource`
2.2 Query statement:
```sql
select _wstart as ts, groupid, avg(current) as current from power.meters where ts > $from and ts < $to partition by groupid interval($interval) fill(null)
```
2.3 Set "Expression": `Threshold is above 100`
2.4 Click "Set as alert condition"
2.5 "Preview": View the results of the set rules
After completing the settings, you can see the image displayed below:
![TDengine Database Grafana plugin Alert Rules](./grafana/alert-rules1.webp)
### Configuring Expressions and Calculation Rules
Grafana's "Expression" supports various operations and calculations on data, which are divided into:
1. "Reduce": Aggregates the values of a time series within the selected time range into a single value
1.1 "Function" is used to set the aggregation method, supporting Min, Max, Last, Mean, Sum, and Count.
1.2 "Mode" supports the following three:
- "Strict": If no data is queried, the data will be assigned NaN.
- "Drop Non-numeric Value": Remove illegal data results.
- "Replace Non-numeric Value": If it is illegal data, replace it with a constant value.
2. "Threshold": Checks whether the time series data meets the threshold judgment condition. Returns 0 when the condition is false, and 1 when true. Supports the following methods:
- Is above (x > y)
- Is below (x < y)
- Is within range (x > y1 AND x < y2)
- Is outside range (x < y1 AND x > y2)
3. "Math": Performs mathematical operations on the data of the time series.
4. "Resample": Changes the timestamps in each time series to have a consistent interval, so that mathematical operations can be performed between them.
5. "Classic condition (legacy)": Multiple logical conditions can be configured to determine whether to trigger an alert.
As shown in the screenshot above, here we set the alert to trigger when the maximum value exceeds 100.
### Configuring Evaluation behavior
![TDengine Database Grafana plugin Alert Evaluation Behavior](./grafana/alert-evaluation.webp)
Configure the parameters as shown in the screenshot above.
### Configuring Labels and Notifications
![TDengine Database Grafana plugin Alert Labels and Notifications](./grafana/alert-labels.webp)
Configure the parameters as shown in the screenshot above.
### Configuring annotations
![TDengine Database Grafana plugin Alert Labels and Notifications](./grafana/alert-annotations.webp)
After setting "Summary" and "Description", you will receive an alert notification if the alert is triggered.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 124 KiB

After

Width:  |  Height:  |  Size: 154 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 122 KiB

After

Width:  |  Height:  |  Size: 157 KiB

View File

@ -379,7 +379,7 @@ properties 中的配置参数如下:
- TSDBDriver.PROPERTY_KEY_DISABLE_SSL_CERT_VALIDATION: 关闭 SSL 证书验证 。仅在使用 Websocket 连接时生效。true: 启用false: 不启用。默认为 false。
此外对 JDBC 原生连接,通过指定 URL 和 Properties 还可以指定其他参数比如日志级别、SQL 长度等。更多详细配置请参考[客户端配置](../../reference/config/#仅客户端适用)。
此外对 JDBC 原生连接,通过指定 URL 和 Properties 还可以指定其他参数比如日志级别、SQL 长度等。更多详细配置请参考[客户端配置](../../reference/config/)。
### 配置参数的优先级

View File

@ -22,26 +22,19 @@ TDengine 能够与开源数据可视化系统 [Grafana](https://www.grafana.com/
- TDengine 集群 REST API 地址,如:`http://tdengine.local:6041`。
- TDengine 集群认证信息,可使用用户名及密码。
## 配置 Grafana
### 安装 Grafana Plugin 并配置数据源
## 安装 Grafana Plugin 并配置数据源
<Tabs defaultValue="script">
<TabItem value="gui" label="图形化界面安装">
使用 Grafana 最新版本8.5+),您可以在 Grafana 中[浏览和管理插件](https://grafana.com/docs/grafana/next/administration/plugin-management/#plugin-catalog)(对于 7.x 版本,请采用 **安装脚本** 或 **手动安装** 方式)。在 Grafana 管理界面中的 **Configurations > Plugins** 页面直接搜索 `TDengine` 并按照提示安装。
安装完毕后,按照指示 **Create a TDengine data source** 添加数据源。
输入 TDengine 相关配置,如下图所示:
![TDengine Database Grafana plugin add data source](./add_datasource3.webp)
安装完毕后,按照指示 **Create a TDengine data source** 添加数据源,输入 TDengine 相关配置:
- Host TDengine 集群中提供 REST 服务的 IP 地址与端口号,默认 \<http://localhost:6041>。
- UserTDengine 用户名。
- PasswordTDengine 用户密码。
点击 `Save & Test` 进行测试,成功会提示:`TDengine Data source is working`。
配置完毕,现在可以使用 TDengine 创建 Dashboard 了。
</TabItem>
<TabItem value="script" label="安装脚本">
@ -95,9 +88,7 @@ GF_INSTALL_PLUGINS=tdengine-datasource
之后,用户可以直接通过 \<http://localhost:3000> 的网址,登录 Grafana 服务器(用户名/密码admin/admin通过左侧 `Configuration -> Data Sources` 可以添加数据源,
点击 `Add data source` 可进入新增数据源页面,在查询框中输入 TDengine 然后点击 `select` 选择添加后会进入数据源配置页面,按照默认提示修改相应配置即可:
![TDengine Database Grafana plugin add data source](./add_datasource3.webp)
点击 `Add data source` 可进入新增数据源页面,在查询框中输入 TDengine 然后点击 `select` 选择添加后会进入数据源配置页面,按照默认提示修改相应配置:
- Host TDengine 集群中提供 REST 服务的 IP 地址与端口号,默认 \<http://localhost:6041>。
- UserTDengine 用户名。
@ -175,20 +166,87 @@ docker run -d \
</TabItem>
</Tabs>
### 创建 Dashboard
回到主界面创建 Dashboard点击 Add Query 进入面板查询页面:
:::info
下文介绍中,都以 Grafana v11.0.0 版本为例,其他版本功能可能有差异,请参考 [Grafana 官网](https://grafana.com/docs/grafana/latest/)。
:::
## 内置变量和自定义变量
Grafana 中的 Variable变量功能非常强大可以在 Dashboard 的查询、面板标题、标签等地方使用,用来创建更加动态和交互式的 Dashbord提高用户体验和效率。
变量的主要作用和特点包括:
- 动态数据查询:变量可以用于查询语句中,使得用户可以通过选择不同的变量值来动态更改查询条件,从而查看不同的数据视图。这对于需要根据用户输入动态展示数据的场景非常有用。
- 提高可重用性:通过定义变量,可以在多个地方重用相同的配置或查询逻辑,而不需要重复编写相同的代码。这使得 Dashboard 的维护和更新变得更加简单高效。
- 灵活的配置选项:变量提供了多种配置选项,如预定义的静态值列表、从数据源动态查询值、正则表达式过滤等,使得变量的应用更加灵活和强大。
Grafana 提供了内置变量和自定义变量,它们都可以可以在编写 SQL 时引用,引用的方式是 `$variableName``variableName` 是变量的名字,其他引用方式请参考 [引用方式](https://grafana.com/docs/grafana/latest/dashboards/variables/variable-syntax/)。
### 内置变量
Grafana 内置了 `from`、`to` 和 `interval` 等变量,都取自于 Grafana 插件面板。其含义如下:
- `from` 查询范围的起始时间
- `to` 查询范围的结束时间
- `interval` 窗口切分间隔
对于每个查询都建议设置查询范围的起始时间和结束时间,可以有效的减少 TDengine 服务端执行查询扫描的数据量。`internal` 是窗口切分的大小,在 Grafana 11 版本中,其大小为时间间隔和返回点数计算而得。
除了上述三个常用变量Grafana 还提供了如 `__timezone`, `__org`, `__user` 等变量,详情请参考 [内置变量](https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables/#global-variables)。
### 自定义变量
我们可以在 Dashbord 中增加自定义变量。自定义变量和内置变量的使用方式没有区别,都是在 SQL 中用 `$variableName` 进行引用。
自定义变量支持多种类型,常见的类型包括 `Query`(查询)、`Constant`(常量)、`Interval`(间隔)、`Data source`(数据源)等。
自定义变量可以引用其他自定义变量,比如一个变量表示区域,另一个变量可以引用区域的值,来查询这个区域的设备。
#### 添加查询类型变量
在 Dashbord 的配置中,选择 【Variables】然后点击 【New variable】
1. 在 “Name“ 字段中,输入你的变量名,此处我们设置变量名为 `selected_groups`。
2. 在 【Select variable type】下拉菜单中选择 ”Query“查询
根据选择的变量类型,配置相应的选项。例如,如果选择了 “Query” 类型,你需要指定数据源和用于获取变量值的查询语句。此处我们还以智能电表为例,设置查询类型,选择数据源后,配置 SQL 为 `select distinct(groupid) from power.meters where groupid < 3 and ts > $from and ts < $to;`
3. 点击底部的【Run Query】后可以在 “Preview of values”值预览部分查看到根据你的配置生成的变量值。
4. 还有其他配置不再赘述完成配置后点击页面底部的【Apply】应用按钮然后点击右上角的【Save dashboard】保存。
完成以上步骤后,我们就成功在 Dashboard 中添加了一个新的自定义变量 `$selected_groups`。我们可以后面在 Dashboard 的查询中通过 `$selected_groups` 的方式引用这个变量。
我们还可以再新增自定义变量来引用这个 `selected_groups` 变量,比如我们新增一个名为 `tbname_max_current` 的查询变量,其 SQL 为 `select tbname from power.meters where groupid = $selected_groups and ts > $from and ts < $to;`
#### 添加间隔类型变量
我们可以自定义时间窗口间隔,可以更加贴合业务需求。
1. 在 “Name“ 字段中,输入变量名为 `interval`。
2. 在 【Select variable type】下拉菜单中选择 “Interval”间隔
3. 在 【Interval options】选项中输入 `1s,2s,5s,10s,15s,30s,1m`。
4. 还有其他配置不再赘述完成配置后点击页面底部的【Apply】应用按钮然后点击右上角的【Save dashboard】保存。
完成以上步骤后,我们就成功在 Dashboard 中添加了一个新的自定义变量 `$interval`。我们可以后面在 Dashboard 的查询中通过 `$interval` 的方式引用这个变量。
## TDengine 时间序列查询支持
TDengine 在支持标准 SQL 的基础之上,还提供了一系列满足时序业务场景需求的特色查询语法,这些语法能够为时序场景的应用的开发带来极大的便利。
- `partition by` 子句可以按一定的维度对数据进行切分,然后在切分出的数据空间内再进行一系列的计算。绝大多数情况可以替代 `group by`。
- `interval` 子句用于产生相等时间周期的窗口
- `fill` 语句指定某一窗口区间数据缺失的情况下的填充模式
- `时间戳伪列` 如果需要在结果中输出聚合结果所对应的时间窗口信息,需要在 SELECT 子句中使用时间戳相关的伪列: 时间窗口起始时间 (_wstart), 时间窗口结束时间 (_wend) 等
上述特性详细介绍可以参考 [特色查询](../../taos-sql/distinguished/)。
## 创建 Dashboard
回到主界面创建 Dashboard点击【Add Query】进入面板查询页面
![TDengine Database Grafana plugin create dashboard](./create_dashboard1.webp)
如上图所示,在 Query 中选中 `TDengine` 数据源,在下方查询框可输入相应 SQL 进行查询。 我们继续用智能电表来举例,为了展示曲线美观,此处**用了虚拟数据**。
具体说明如下:
如上图所示,在 ”Query“ 中选中 `TDengine` 数据源,在下方查询框可输入相应 SQL 进行查询。 我们继续用智能电表来举例,为了展示曲线美观,此处**用了虚拟数据**。
- INPUT SQL输入要查询的语句该 SQL 语句的结果集应为两列多行),例如:`select _wstart as ts, avg(current) as current from power.meters where ts > $from and ts < $to interval($interval) fill(null)` 其中from、to 和 interval 为 TDengine 插件的内置变量,表示从 Grafana 插件面板获取的时间查询范围和窗口切分间隔。除了内置变量外,也支持使用自定义模板变量。
- ALIAS BY可设置当前查询别名。
- GENERATE SQL 点击该按钮会自动替换相应变量,并生成最终执行的语句。
- Group by column(s) **半角**逗号分隔的 `group by` 或 `partition by` 列名。如果是 `group by` or `partition by` 查询语句,设置 `Group by` 列可以展示多维数据。例如INPUT SQL 为 `select _wstart as ts, groupid, avg(current) as current from power.meters where ts > $from and ts < $to partition by groupid interval($interval) fill(null)`,设置 Group by 列名为 `groupid`,可以按 `groupid` 展示数据。
- Group By Format Group by 或 Partition by 场景下多维数据 legend 格式化格式。例如上述 INPUT SQL将 `Group By Format` 设置为 `groupid-{{groupid}}`,展示的 legend 名字为格式化的分组名。
## 时间序列数据展示
假设我们想查询一段时间内的平均电流大小,时间窗口按 `$interval` 切分,若某一时间窗口区间数据缺失,填充 null。
- “INPUT SQL“输入要查询的语句该 SQL 语句的结果集应为两列多行),此处输入:`select _wstart as ts, avg(current) as current from power.meters where groupid in ($selected_groups) and ts > $from and ts < $to interval($interval) fill(null)` 其中from、to 和 interval 为 Grafana 内置变量selected_groups 为自定义变量。
- “ALIAS BY“可设置当前查询别名。
- “GENERATE SQL“ 点击该按钮会自动替换相应变量,并生成最终执行的语句。
在顶部的自定义变量中,若选择 `selected_groups` 的值为 1则查询 `meters` 超级表中 `groupid` 为 1 的所有设备电流平均值变化如下图:
![TDengine Database Grafana plugin create dashboard](./create_dashboard2.webp)
:::note
@ -196,17 +254,24 @@ docker run -d \
:::
查询 `meters` 超级表所有设备电流平均值变化如下图:
## 时间序列数据分组展示
假设我们想查询一段时间内的平均电流大小,按 `groupid` 分组展示,我们可以修改之前的 SQL 为 `select _wstart as ts, groupid, avg(current) as current from power.meters where ts > $from and ts < $to partition by groupid interval($interval) fill(null)`
![TDengine Database Grafana plugin create dashboard](./create_dashboard2.webp)
- “Group by column(s)“: **半角**逗号分隔的 `group by` 或 `partition by` 列名。如果是 `group by` 或 `partition by` 查询语句,设置 “Group by“ 列,可以展示多维数据。此处设置 “Group by“ 列名为 `groupid`,可以按 `groupid` 分组展示数据。
- “Group By Format“ `Group by` 或 `Partition by` 场景下多维数据 legend 格式化格式。例如上述 INPUT SQL将 “Group By Format“ 设置为 `groupid-{{groupid}}`,展示的 legend 名字为格式化的分组名。
查询 `meters` 超级表所有设备电流平均值,并按照 `groupid` 分组展示如下图:
完成设置后,按照 `groupid` 分组展示如下图:
![TDengine Database Grafana plugin create dashboard](./create_dashboard3.webp)
> 关于如何使用 Grafana 创建相应的监测界面以及更多有关使用 Grafana 的信息,请参考 Grafana 官方的[文档](https://grafana.com/docs/)。
### 导入 Dashboard
## 性能建议
- **所有查询加上时间范围**,在时序数据库中,如果不加查询的时间范围,会扫表导致性能低下。常见的 SQL 写法是 `select column_name from db.table where ts > $from and ts < $to;`
- 对于最新状态类型的查询,我们一般建议在**创建数据库的时候打开缓存**`CACHEMODEL` 设置为 last_row 或者 both常见的 SQL 写法是 `select last(column_name) from db.table where ts > $from and ts < $to;`
## 导入 Dashboard
在数据源配置页面,您可以为该数据源导入 TDinsight 面板,作为 TDengine 集群的监控可视化工具。如果 TDengine 服务端为 3.0 版本请选择 `TDinsight for 3.x` 导入。注意 TDinsight for 3.x 需要运行和配置 taoskeeper。
@ -216,7 +281,148 @@ docker run -d \
使用 TDengine 作为数据源的其他面板,可以[在此搜索](https://grafana.com/grafana/dashboards/?dataSource=tdengine-datasource)。以下是一份不完全列表:
- [15146](https://grafana.com/grafana/dashboards/15146): 监控多个 TDengine 集群
- [15155](https://grafana.com/grafana/dashboards/15155): TDengine 告警示例
- [15167](https://grafana.com/grafana/dashboards/15167): TDinsight
- [16388](https://grafana.com/grafana/dashboards/16388): Telegraf 采集节点信息的数据展示
- [15146](https://grafana.com/grafana/dashboards/15146) 监控多个 TDengine 集群
- [15155](https://grafana.com/grafana/dashboards/15155) TDengine 告警示例
- [15167](https://grafana.com/grafana/dashboards/15167) TDinsight
- [16388](https://grafana.com/grafana/dashboards/16388) Telegraf 采集节点信息的数据展示
## 告警配置简介
### 告警配置流程
TDengine Grafana 插件支持告警,如果要配置告警,需要以下几个步骤:
1. 配置联络点“Contact points“配置通知渠道包括 DingDing、Email、Slack、WebHook、Prometheus Alertmanager 等
2. 配置告警通知策略“Notification policies“配置告警发送到哪个通道的路由以及发送通知的时间和重复频率
3. 配置 “Alert rules“配置详细的告警规则
3.1 配置告警名称
3.2 配置查询及告警触发条件
3.3 配置规则评估策略
3.4 配置标签和告警通道
3.5 配置通知文案
### 告警配置界面
在Grafana 11 告警界面一共有 6 个 Tab分别是 “Alert rules“、“Contact points“、“Notification policies“、“Silences“、 “Groups“ 和 “Settings“。
- “Alert rules“ 告警规则列表,用于展示和配置告警规则
- “Contact points“ 通知渠道,包括 DingDing、Email、Slack、WebHook、Prometheus Alertmanager 等
- “Notification policies“ 配置告警发送到哪个通道的路由,以及发送通知的时间和重复频率
- “Silences“ 配置告警静默时间段
- “Groups“ 告警组,配置的告警触发后会在这里分组显示
- “Settings“ 提供通过 JSON 方式修改告警配置
## 配置邮件联络点
### Grafana Server 配置文件修改
在 Grafana 服务的配置文件中添加 SMTP/Emailing 和 Alerting 模块,以 Linux 系统为例,其配置文件一般位于 `/etc/grafana/grafana.ini`
在配置文件中增加下面内容:
```ini
#################################### SMTP / Emailing ##########################
[smtp]
enabled = true
host = smtp.qq.com:465 #使用的邮箱
user = receiver@foxmail.com
password = *********** #使用mail授权码
skip_verify = true
from_address = sender@foxmail.com
```
然后重启 Grafana 服务即可, 以 Linux 系统为例,执行 `systemctl restart grafana-server.service`
### Grafana 页面创建新联络点
在 Grafana 页面找到 “Home“ -> “Alerting“ -> “Contact points“创建新联络点
”Name“ Email Contact Point
“Integration“选择联络类型这里选择 Email填写邮件接收地址完成后保存联络点
![TDengine Database Grafana plugin alert email](./alert-email.webp)
## 配置飞书联络点
### 飞书机器人配置
1. “飞书工作台“ -> “获取应用“ -> “搜索飞书机器人助手“ -> “新建指令“
2. 选择触发器Grafana
3. 选择操作:通过官方机器人发送消息,填写发送对象和发送内容
![TDengine Database Grafana plugin feishu robot](./alert-feishu1.webp)
### Grafana 配置飞书联络点
在 Grafana 页面找到 “Home“ -> “Alerting“ -> “Contact points“ 创建新联络点
“Name“Feishu Contact Point
“Integration“选择联络类型这里选择 Webhook并填写 URL (在飞书机器人助手的 Grafana 触发器 Webhook 地址),完成后保存联络点
![TDengine Database Grafana plugin feishu contact point](./alert-feishu2.webp)
## 通知策略
配置好联络点后可以看到已有一个Default Policy
![TDengine Database Grafana plugin Notification default policy](./alert-notification1.webp)
点击右侧 ”...“ -> ”Edit“然后编辑默认通知策略弹出配置窗口
![TDengine Database Grafana plugin Notification](./alert-notification2.webp)
然后配置下列参数:
- “Group wait“ 发送首次告警之前的等待时间。
- “Group interval“ 发送第一个告警后,为该组发送下一批新告警的等待时间。
- “Repeat interval“ 成功发送告警后再次重复发送告警的等待时间。
## 配置告警规则
### 配置查询和告警触发条件
在需要配置告警的面板中选择 “Edit“ -> “Alert“ -> “New alert rule“。
1. “Enter alert rule name“ (输入告警规则名称):此处以智能电表为例输入 `power meters alert`
2. “Define query and alert condition“ (定义查询和告警触发条件)
2.1 选择数据源:`TDengine Datasource`
2.2 查询语句:
```sql
select _wstart as ts, groupid, avg(current) as current from power.meters where ts > $from and ts < $to partition by groupid interval($interval) fill(null)
```
2.3 设置 ”Expression“表达式`Threshold is above 100`
2.4 点击【Set as alert condition】
2.5 “Preview“查看设置的规则的结果
完成设置后可以看到下面图片展示:
![TDengine Database Grafana plugin Alert Rules](./alert-rules1.webp)
### 配置表达式和计算规则
Grafana 的 “Expression“表达式支持对数据做各种操作和计算其类型分为
1. “Reduce“将所选时间范围内的时间序列值聚合为单个值
1.1 “Function“ 用来设置聚合方法,支持 Min、Max、Last、Mean、Sum 和 Count。
1.2 “Mode“ 支持下面三种:
- “Strict“如果查询不到数据数据会赋值为 NaN。
- “Drop Non-numeric Value“去掉非法数据结果。
- “Replace Non-numeric Value“如果是非法数据使用固定值进行替换。
2. “Threshold“检查时间序列数据是否符合阈值判断条件。当条件为假时返回 0为真则返回1。支持下列方式
- Is above (x > y)
- Is below (x < y)
- Is within range (x > y1 AND x < y2)
- Is outside range (x < y1 AND x > y2)
3. “Math“对时间序列的数据进行数学运算。
4. “Resample“更改每个时间序列中的时间戳使其具有一致的时间间隔以便在它们之间执行数学运算。
5. “Classic condition (legacy)“: 可配置多个逻辑条件,判断是否触发告警。
如上节截图显示,此处我们设置最大值超过 100 触发告警。
### 配置评估策略
![TDengine Database Grafana plugin Alert Evaluation Behavior](./alert-evaluation.webp)
完成下面配置:
- “Folder“设置告警规则所属目录。
- “Evaluation group“设置告警规则评估组。“Evaluation group“ 可以选择已有组或者新建组,新建组可以设置组名和评估时间间隔。
- “Pending period“在告警规则的阈值被触发后异常值持续多长时间可以触发告警合理设置可以避免误报。
### 配置标签和告警通道
![TDengine Database Grafana plugin Alert Labels and Notifications](./alert-labels.webp)
完成下面配置:
- “Labels“ 将标签添加到规则中,以便进行搜索、静默或路由到通知策略。
- “Contact point“ 选择联络点,当告警发生时通过设置的联络点进行通知。
### 配置通知文案
![TDengine Database Grafana plugin Alert Labels and Notifications](./alert-annotations.webp)
设置 “Summary” 和 ”Description” 后,若告警触发,将会收到告警通知。

Binary file not shown.

Before

Width:  |  Height:  |  Size: 124 KiB

After

Width:  |  Height:  |  Size: 154 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 122 KiB

After

Width:  |  Height:  |  Size: 157 KiB