[manager,collector]mariadb,postgresql支持timeout,fix jdbc解析异常 (#36)

* [manager,collector]fix jdbc解析异常,mariadb支持timeout

* [manager]postgresql支持timeout设置
This commit is contained in:
tomsun28
2022-03-17 15:42:36 +08:00
committed by GitHub
parent f710795f0f
commit ddb290bba3
8 changed files with 44 additions and 14 deletions

View File

@@ -52,11 +52,17 @@ public class JdbcCommonCollect extends AbstractCollect {
} }
JdbcProtocol jdbcProtocol = metrics.getJdbc(); JdbcProtocol jdbcProtocol = metrics.getJdbc();
String databaseUrl = constructDatabaseUrl(jdbcProtocol); String databaseUrl = constructDatabaseUrl(jdbcProtocol);
//获取查询语句/获取链接最大超时时间 // 查询超时时间默认3000毫秒
int timeout = metrics.getJdbc().getTimeout().intValue(); int timeout = 3000;
try {
// 获取查询语句超时时间
timeout = Integer.parseInt(jdbcProtocol.getTimeout());
} catch (Exception e) {
log.warn(e.getMessage());
}
try { try {
Statement statement = getConnection(jdbcProtocol.getUsername(), Statement statement = getConnection(jdbcProtocol.getUsername(),
jdbcProtocol.getPassword(), databaseUrl,timeout); jdbcProtocol.getPassword(), databaseUrl, timeout);
switch (jdbcProtocol.getQueryType()) { switch (jdbcProtocol.getQueryType()) {
case QUERY_TYPE_ONE_ROW: case QUERY_TYPE_ONE_ROW:
queryOneRow(statement, jdbcProtocol.getSql(), metrics.getAliasFields(), builder, startTime); queryOneRow(statement, jdbcProtocol.getSql(), metrics.getAliasFields(), builder, startTime);
@@ -108,7 +114,9 @@ public class JdbcCommonCollect extends AbstractCollect {
try { try {
statement = jdbcConnect.getConnection().createStatement(); statement = jdbcConnect.getConnection().createStatement();
// 设置查询超时时间10秒 // 设置查询超时时间10秒
statement.setQueryTimeout(timeout); int timeoutSecond = timeout / 1000;
timeoutSecond = timeoutSecond <= 0 ? 1 : timeoutSecond;
statement.setQueryTimeout(timeoutSecond);
// 设置查询最大行数1000行 // 设置查询最大行数1000行
statement.setMaxRows(1000); statement.setMaxRows(1000);
} catch (Exception e) { } catch (Exception e) {

View File

@@ -38,7 +38,7 @@ public class JdbcProtocol {
/** /**
* 超时时间 * 超时时间
*/ */
private Long timeout; private String timeout;
/** /**
* 数据库类型 mysql oracle ... * 数据库类型 mysql oracle ...
*/ */

View File

@@ -16,6 +16,8 @@ configmap:
type: 2 type: 2
- key: database - key: database
type: 1 type: 1
- key: timeout
type: 0
- key: url - key: url
type: 1 type: 1
# 指标组列表 # 指标组列表
@@ -61,6 +63,7 @@ metrics:
username: ^_^username^_^ username: ^_^username^_^
password: ^_^password^_^ password: ^_^password^_^
database: ^_^database^_^ database: ^_^database^_^
timeout: ^_^timeout^_^
# SQL查询方式 oneRow, multiRow, columns # SQL查询方式 oneRow, multiRow, columns
queryType: columns queryType: columns
# sql # sql
@@ -102,6 +105,7 @@ metrics:
username: ^_^username^_^ username: ^_^username^_^
password: ^_^password^_^ password: ^_^password^_^
database: ^_^database^_^ database: ^_^database^_^
timeout: ^_^timeout^_^
# SQL查询方式 oneRow, multiRow, columns # SQL查询方式 oneRow, multiRow, columns
queryType: columns queryType: columns
# sql # sql
@@ -134,6 +138,7 @@ metrics:
username: ^_^username^_^ username: ^_^username^_^
password: ^_^password^_^ password: ^_^password^_^
database: ^_^database^_^ database: ^_^database^_^
timeout: ^_^timeout^_^
# SQL查询方式 oneRow, multiRow, columns # SQL查询方式 oneRow, multiRow, columns
queryType: columns queryType: columns
# sql # sql

View File

@@ -18,8 +18,6 @@ configmap:
type: 1 type: 1
- key: timeout - key: timeout
type: 0 type: 0
required: false
defaultValue: 3
- key: url - key: url
type: 1 type: 1
# 指标组列表 # 指标组列表
@@ -107,6 +105,7 @@ metrics:
username: ^_^username^_^ username: ^_^username^_^
password: ^_^password^_^ password: ^_^password^_^
database: ^_^database^_^ database: ^_^database^_^
timeout: ^_^timeout^_^
# SQL查询方式 oneRow, multiRow, columns # SQL查询方式 oneRow, multiRow, columns
queryType: columns queryType: columns
# sql # sql
@@ -139,6 +138,7 @@ metrics:
username: ^_^username^_^ username: ^_^username^_^
password: ^_^password^_^ password: ^_^password^_^
database: ^_^database^_^ database: ^_^database^_^
timeout: ^_^timeout^_^
# SQL查询方式 oneRow, multiRow, columns # SQL查询方式 oneRow, multiRow, columns
queryType: columns queryType: columns
# sql # sql

View File

@@ -18,6 +18,8 @@ configmap:
type: 1 type: 1
- key: url - key: url
type: 1 type: 1
- key: timeout
type: 0
# 指标组列表 # 指标组列表
metrics: metrics:
- name: basic - name: basic
@@ -45,6 +47,7 @@ metrics:
host: ^_^host^_^ host: ^_^host^_^
# 端口 # 端口
port: ^_^port^_^ port: ^_^port^_^
timeout: ^_^timeout^_^
platform: postgresql platform: postgresql
username: ^_^username^_^ username: ^_^username^_^
password: ^_^password^_^ password: ^_^password^_^
@@ -87,6 +90,7 @@ metrics:
host: ^_^host^_^ host: ^_^host^_^
# 端口 # 端口
port: ^_^port^_^ port: ^_^port^_^
timeout: ^_^timeout^_^
platform: postgresql platform: postgresql
username: ^_^username^_^ username: ^_^username^_^
password: ^_^password^_^ password: ^_^password^_^
@@ -110,6 +114,7 @@ metrics:
host: ^_^host^_^ host: ^_^host^_^
# 端口 # 端口
port: ^_^port^_^ port: ^_^port^_^
timeout: ^_^timeout^_^
platform: postgresql platform: postgresql
username: ^_^username^_^ username: ^_^username^_^
password: ^_^password^_^ password: ^_^password^_^

View File

@@ -11,6 +11,12 @@ param:
required: true required: true
defaultValue: 80 defaultValue: 80
placeholder: '请输入端口' placeholder: '请输入端口'
- field: timeout
name: 查询超时时间
type: number
required: false
defaultValue: 3000
placeholder: '查询超时时间'
- field: database - field: database
name: 数据库名称 name: 数据库名称
type: text type: text

View File

@@ -11,6 +11,12 @@ param:
required: true required: true
defaultValue: 80 defaultValue: 80
placeholder: '请输入端口' placeholder: '请输入端口'
- field: timeout
name: 查询超时时间
type: number
required: false
defaultValue: 3000
placeholder: '查询超时时间'
- field: database - field: database
name: 数据库名称 name: 数据库名称
type: text type: text
@@ -28,9 +34,3 @@ param:
name: URL name: URL
type: text type: text
required: false required: false
- field: timeout
name: 超时时间
type: number
required: false
defaultValue: 3
placeholder: '请输入mysql可接受的超时时间'

View File

@@ -11,6 +11,12 @@ param:
required: true required: true
defaultValue: 5432 defaultValue: 5432
placeholder: '请输入端口' placeholder: '请输入端口'
- field: timeout
name: 查询超时时间
type: number
required: false
defaultValue: 3000
placeholder: '查询超时时间'
- field: database - field: database
name: 数据库名称 name: 数据库名称
type: text type: text