MSSQL: Add connection timeout setting in configuration page (#58631)

* MSSQL add connection timeout

* add docs

* Update docs and add min value to the timeout setting
This commit is contained in:
Victor Marin 2022-11-11 21:04:08 +02:00 committed by GitHub
parent 78bb8c10ce
commit 6e776d0fec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 4 deletions

View File

@ -77,6 +77,10 @@ For example, use `1m` if Microsoft SQL Server writes data every minute.
You can also override this setting in a dashboard panel under its data source options.
### Connection timeout
The **Connection timeout** setting defines the maximum number of seconds to wait for a connection to the database before timing out. Default is 0 for no timeout.
### Database user permissions
Grafana doesn't validate that a query is safe, and could include any SQL statement.
@ -119,6 +123,7 @@ datasources:
maxOpenConns: 0 # Grafana v5.4+
maxIdleConns: 2 # Grafana v5.4+
connMaxLifetime: 14400 # Grafana v5.4+
connectionTimeout: 0 # Grafana v9.3+
secureJsonData:
password: 'Password!'
```

View File

@ -55,10 +55,11 @@ func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest)
func newInstanceSettings(cfg *setting.Cfg) datasource.InstanceFactoryFunc {
return func(settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
jsonData := sqleng.JsonData{
MaxOpenConns: 0,
MaxIdleConns: 2,
ConnMaxLifetime: 14400,
Encrypt: "false",
MaxOpenConns: 0,
MaxIdleConns: 2,
ConnMaxLifetime: 14400,
Encrypt: "false",
ConnectionTimeout: 0,
}
err := json.Unmarshal(settings.JSONData, &jsonData)
@ -171,6 +172,11 @@ func generateConnectionString(dsInfo sqleng.DataSourceInfo) (string, error) {
} else if encrypt == "disable" {
connStr += fmt.Sprintf("encrypt=%s;", dsInfo.JsonData.Encrypt)
}
if dsInfo.JsonData.ConnectionTimeout != 0 {
connStr += fmt.Sprintf("connection timeout=%d;", dsInfo.JsonData.ConnectionTimeout)
}
return connStr, nil
}

View File

@ -54,6 +54,7 @@ type JsonData struct {
MaxOpenConns int `json:"maxOpenConns"`
MaxIdleConns int `json:"maxIdleConns"`
ConnMaxLifetime int `json:"connMaxLifetime"`
ConnectionTimeout int `json:"connectionTimeout"`
Timescaledb bool `json:"timescaledb"`
Mode string `json:"sslmode"`
ConfigurationMethod string `json:"tlsConfigurationMethod"`

View File

@ -21,6 +21,7 @@ import {
Select,
useStyles2,
} from '@grafana/ui';
import { NumberInput } from 'app/core/components/OptionsUI/NumberInput';
import { ConnectionLimits } from 'app/features/plugins/sql/components/configuration/ConnectionLimits';
import { MSSQLAuthenticationType, MSSQLEncryptOptions, MssqlOptions } from '../types';
@ -60,6 +61,10 @@ export const ConfigurationEditor = (props: DataSourcePluginOptionsEditorProps<Ms
});
};
const onConnectionTimeoutChanged = (connectionTimeout?: number) => {
updateDatasourcePluginJsonDataOption(props, 'connectionTimeout', connectionTimeout ?? 0);
};
const authenticationOptions: Array<SelectableValue<MSSQLAuthenticationType>> = [
{ value: MSSQLAuthenticationType.sqlAuth, label: 'SQL Server Authentication' },
{ value: MSSQLAuthenticationType.windowsAuth, label: 'Windows Authentication' },
@ -74,6 +79,7 @@ export const ConfigurationEditor = (props: DataSourcePluginOptionsEditorProps<Ms
const shortWidth = 15;
const longWidth = 46;
const labelWidthSSL = 25;
const labelWidthDetails = 20;
return (
<>
@ -233,6 +239,7 @@ export const ConfigurationEditor = (props: DataSourcePluginOptionsEditorProps<Ms
</span>
}
label="Min time interval"
labelWidth={labelWidthDetails}
>
<Input
placeholder="1m"
@ -240,6 +247,23 @@ export const ConfigurationEditor = (props: DataSourcePluginOptionsEditorProps<Ms
onChange={onUpdateDatasourceJsonDataOption(props, 'timeInterval')}
></Input>
</InlineField>
<InlineField
tooltip={
<span>
The number of seconds to wait before canceling the request when connecting to the database. The default is{' '}
<code>0</code>, meaning no timeout.
</span>
}
label="Connection timeout"
labelWidth={labelWidthDetails}
>
<NumberInput
placeholder="60"
min={0}
value={jsonData.connectionTimeout}
onChange={onConnectionTimeoutChanged}
></NumberInput>
</InlineField>
</FieldSet>
<Alert title="User Permission" severity="info">

View File

@ -15,4 +15,5 @@ export interface MssqlOptions extends SQLOptions {
encrypt?: MSSQLEncryptOptions;
sslRootCertFile?: string;
serverName?: string;
connectionTimeout?: number;
}