Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go.d: mysql: disable session query log and slow query log #17219

Merged
merged 1 commit into from Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/go/collectors/go.d.plugin/modules/mysql/collect.go
Expand Up @@ -28,6 +28,8 @@ func (m *MySQL) collect() (map[string]int64, error) {
m.doUserStatistics = m.isPercona || m.isMariaDB && m.version.GTE(semver.Version{Major: 10, Minor: 1, Patch: 1})
}

m.disableSessionQueryLog()

mx := make(map[string]int64)

if err := m.collectGlobalStatus(mx); err != nil {
Expand Down
58 changes: 58 additions & 0 deletions src/go/collectors/go.d.plugin/modules/mysql/disable_logging.go
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: GPL-3.0-or-later

package mysql

const (
queryShowSessionVariables = `
SHOW SESSION VARIABLES
WHERE
Variable_name LIKE 'sql_log_off'
OR Variable_name LIKE 'slow_query_log';`
)

const (
queryDisableSessionQueryLog = "SET SESSION sql_log_off='ON';"
queryDisableSessionSlowQueryLog = "SET SESSION slow_query_log='OFF';"
)

func (m *MySQL) disableSessionQueryLog() {
q := queryShowSessionVariables
m.Debugf("executing query: '%s'", q)

var sqlLogOff, slowQueryLog string
var name string
_, err := m.collectQuery(q, func(column, value string, _ bool) {
switch column {
case "Variable_name":
name = value
case "Value":
switch name {
case "sql_log_off":
sqlLogOff = value
case "slow_query_log":
slowQueryLog = value
}
}
})
if err != nil {
m.Debug(err)
return
}

if sqlLogOff == "OFF" && m.doDisableSessionQueryLog {
// requires SUPER privileges
q = queryDisableSessionQueryLog
m.Debugf("executing query: '%s'", q)
if _, err := m.collectQuery(q, func(_, _ string, _ bool) {}); err != nil {
m.Infof("failed to disable session query log (sql_log_off): %v", err)
m.doDisableSessionQueryLog = false
}
}
if slowQueryLog == "ON" {
q = queryDisableSessionSlowQueryLog
m.Debugf("executing query: '%s'", q)
if _, err := m.collectQuery(q, func(_, _ string, _ bool) {}); err != nil {
m.Debugf("failed to disable session slow query log (slow_query_log): %v", err)
}
}
}
3 changes: 3 additions & 0 deletions src/go/collectors/go.d.plugin/modules/mysql/mysql.go
Expand Up @@ -43,6 +43,7 @@ func New() *MySQL {
addGaleraOnce: &sync.Once{},
addQCacheOnce: &sync.Once{},
addTableOpenCacheOverflowsOnce: &sync.Once{},
doDisableSessionQueryLog: true,
doSlaveStatus: true,
doUserStatistics: true,
collectedReplConns: make(map[string]bool),
Expand Down Expand Up @@ -79,6 +80,8 @@ type MySQL struct {
isMariaDB bool
isPercona bool

doDisableSessionQueryLog bool

doSlaveStatus bool
collectedReplConns map[string]bool
doUserStatistics bool
Expand Down
45 changes: 45 additions & 0 deletions src/go/collectors/go.d.plugin/modules/mysql/mysql_test.go
Expand Up @@ -24,6 +24,8 @@ var (
dataConfigJSON, _ = os.ReadFile("testdata/config.json")
dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")

dataSessionVariables, _ = os.ReadFile("testdata/session_variables.txt")

dataMySQLVer8030Version, _ = os.ReadFile("testdata/mysql/v8.0.30/version.txt")
dataMySQLVer8030GlobalStatus, _ = os.ReadFile("testdata/mysql/v8.0.30/global_status.txt")
dataMySQLVer8030GlobalVariables, _ = os.ReadFile("testdata/mysql/v8.0.30/global_variables.txt")
Expand Down Expand Up @@ -60,6 +62,7 @@ func Test_testDataIsValid(t *testing.T) {
for name, data := range map[string][]byte{
"dataConfigJSON": dataConfigJSON,
"dataConfigYAML": dataConfigYAML,
"dataSessionVariables": dataSessionVariables,
"dataMySQLVer8030Version": dataMySQLVer8030Version,
"dataMySQLVer8030GlobalStatus": dataMySQLVer8030GlobalStatus,
"dataMySQLVer8030GlobalVariables": dataMySQLVer8030GlobalVariables,
Expand Down Expand Up @@ -164,6 +167,9 @@ func TestMySQL_Check(t *testing.T) {
wantFail: false,
prepareMock: func(t *testing.T, m sqlmock.Sqlmock) {
mockExpect(t, m, queryShowVersion, dataMariaVer1084Version)
mockExpect(t, m, queryShowSessionVariables, dataSessionVariables)
mockExpect(t, m, queryDisableSessionQueryLog, nil)
mockExpect(t, m, queryDisableSessionSlowQueryLog, nil)
mockExpect(t, m, queryShowGlobalStatus, dataMariaVer1084GlobalStatus)
mockExpect(t, m, queryShowGlobalVariables, dataMariaVer1084GlobalVariables)
mockExpect(t, m, queryShowAllSlavesStatus, dataMariaVer1084AllSlavesStatusMultiSource)
Expand All @@ -181,20 +187,29 @@ func TestMySQL_Check(t *testing.T) {
wantFail: true,
prepareMock: func(t *testing.T, m sqlmock.Sqlmock) {
mockExpect(t, m, queryShowVersion, dataMariaVer1084Version)
mockExpect(t, m, queryShowSessionVariables, dataSessionVariables)
mockExpect(t, m, queryDisableSessionQueryLog, nil)
mockExpect(t, m, queryDisableSessionSlowQueryLog, nil)
mockExpectErr(m, queryShowGlobalStatus)
},
},
"fails when error on querying global variables": {
wantFail: true,
prepareMock: func(t *testing.T, m sqlmock.Sqlmock) {
mockExpect(t, m, queryShowVersion, dataMariaVer1084Version)
mockExpect(t, m, queryShowSessionVariables, dataSessionVariables)
mockExpect(t, m, queryDisableSessionQueryLog, nil)
mockExpect(t, m, queryDisableSessionSlowQueryLog, nil)
mockExpectErr(m, queryShowGlobalStatus)
},
},
"success when error on querying slave status": {
wantFail: false,
prepareMock: func(t *testing.T, m sqlmock.Sqlmock) {
mockExpect(t, m, queryShowVersion, dataMariaVer1084Version)
mockExpect(t, m, queryShowSessionVariables, dataSessionVariables)
mockExpect(t, m, queryDisableSessionQueryLog, nil)
mockExpect(t, m, queryDisableSessionSlowQueryLog, nil)
mockExpect(t, m, queryShowGlobalStatus, dataMariaVer1084GlobalStatus)
mockExpect(t, m, queryShowGlobalVariables, dataMariaVer1084GlobalVariables)
mockExpectErr(m, queryShowAllSlavesStatus)
Expand All @@ -206,6 +221,9 @@ func TestMySQL_Check(t *testing.T) {
wantFail: false,
prepareMock: func(t *testing.T, m sqlmock.Sqlmock) {
mockExpect(t, m, queryShowVersion, dataMariaVer1084Version)
mockExpect(t, m, queryShowSessionVariables, dataSessionVariables)
mockExpect(t, m, queryDisableSessionQueryLog, nil)
mockExpect(t, m, queryDisableSessionSlowQueryLog, nil)
mockExpect(t, m, queryShowGlobalStatus, dataMariaVer1084GlobalStatus)
mockExpect(t, m, queryShowGlobalVariables, dataMariaVer1084GlobalVariables)
mockExpect(t, m, queryShowAllSlavesStatus, dataMariaVer1084AllSlavesStatusMultiSource)
Expand All @@ -217,6 +235,9 @@ func TestMySQL_Check(t *testing.T) {
wantFail: false,
prepareMock: func(t *testing.T, m sqlmock.Sqlmock) {
mockExpect(t, m, queryShowVersion, dataMariaVer1084Version)
mockExpect(t, m, queryShowSessionVariables, dataSessionVariables)
mockExpect(t, m, queryDisableSessionQueryLog, nil)
mockExpect(t, m, queryDisableSessionSlowQueryLog, nil)
mockExpect(t, m, queryShowGlobalStatus, dataMariaVer1084GlobalStatus)
mockExpect(t, m, queryShowGlobalVariables, dataMariaVer1084GlobalVariables)
mockExpect(t, m, queryShowAllSlavesStatus, dataMariaVer1084AllSlavesStatusMultiSource)
Expand Down Expand Up @@ -260,6 +281,9 @@ func TestMySQL_Collect(t *testing.T) {
{
prepareMock: func(t *testing.T, m sqlmock.Sqlmock) {
mockExpect(t, m, queryShowVersion, dataMariaVer5564Version)
mockExpect(t, m, queryShowSessionVariables, dataSessionVariables)
mockExpect(t, m, queryDisableSessionQueryLog, nil)
mockExpect(t, m, queryDisableSessionSlowQueryLog, nil)
mockExpect(t, m, queryShowGlobalStatus, dataMariaVer5564GlobalStatus)
mockExpect(t, m, queryShowGlobalVariables, dataMariaVer5564GlobalVariables)
mockExpect(t, m, queryShowSlaveStatus, nil)
Expand Down Expand Up @@ -391,6 +415,9 @@ func TestMySQL_Collect(t *testing.T) {
{
prepareMock: func(t *testing.T, m sqlmock.Sqlmock) {
mockExpect(t, m, queryShowVersion, dataMariaVer1084Version)
mockExpect(t, m, queryShowSessionVariables, dataSessionVariables)
mockExpect(t, m, queryDisableSessionQueryLog, nil)
mockExpect(t, m, queryDisableSessionSlowQueryLog, nil)
mockExpect(t, m, queryShowGlobalStatus, dataMariaVer1084GlobalStatus)
mockExpect(t, m, queryShowGlobalVariables, dataMariaVer1084GlobalVariables)
mockExpect(t, m, queryShowAllSlavesStatus, nil)
Expand Down Expand Up @@ -570,6 +597,9 @@ func TestMySQL_Collect(t *testing.T) {
{
prepareMock: func(t *testing.T, m sqlmock.Sqlmock) {
mockExpect(t, m, queryShowVersion, dataMariaVer1084Version)
mockExpect(t, m, queryShowSessionVariables, dataSessionVariables)
mockExpect(t, m, queryDisableSessionQueryLog, nil)
mockExpect(t, m, queryDisableSessionSlowQueryLog, nil)
mockExpect(t, m, queryShowGlobalStatus, dataMariaVer1084GlobalStatus)
mockExpect(t, m, queryShowGlobalVariables, dataMariaVer1084GlobalVariables)
mockExpect(t, m, queryShowAllSlavesStatus, dataMariaVer1084AllSlavesStatusSingleSource)
Expand Down Expand Up @@ -751,6 +781,9 @@ func TestMySQL_Collect(t *testing.T) {
{
prepareMock: func(t *testing.T, m sqlmock.Sqlmock) {
mockExpect(t, m, queryShowVersion, dataMariaVer1084Version)
mockExpect(t, m, queryShowSessionVariables, dataSessionVariables)
mockExpect(t, m, queryDisableSessionQueryLog, nil)
mockExpect(t, m, queryDisableSessionSlowQueryLog, nil)
mockExpect(t, m, queryShowGlobalStatus, dataMariaVer1084GlobalStatus)
mockExpect(t, m, queryShowGlobalVariables, dataMariaVer1084GlobalVariables)
mockExpect(t, m, queryShowAllSlavesStatus, dataMariaVer1084AllSlavesStatusMultiSource)
Expand Down Expand Up @@ -935,6 +968,9 @@ func TestMySQL_Collect(t *testing.T) {
{
prepareMock: func(t *testing.T, m sqlmock.Sqlmock) {
mockExpect(t, m, queryShowVersion, dataMariaVer1084Version)
mockExpect(t, m, queryShowSessionVariables, dataSessionVariables)
mockExpect(t, m, queryDisableSessionQueryLog, nil)
mockExpect(t, m, queryDisableSessionSlowQueryLog, nil)
mockExpect(t, m, queryShowGlobalStatus, dataMariaVer1084GlobalStatus)
mockExpect(t, m, queryShowGlobalVariables, dataMariaVer1084GlobalVariables)
mockExpectErr(m, queryShowAllSlavesStatus)
Expand Down Expand Up @@ -1113,6 +1149,9 @@ func TestMySQL_Collect(t *testing.T) {
{
prepareMock: func(t *testing.T, m sqlmock.Sqlmock) {
mockExpect(t, m, queryShowVersion, dataMariaGaleraClusterVer1084Version)
mockExpect(t, m, queryShowSessionVariables, dataSessionVariables)
mockExpect(t, m, queryDisableSessionQueryLog, nil)
mockExpect(t, m, queryDisableSessionSlowQueryLog, nil)
mockExpect(t, m, queryShowGlobalStatus, dataMariaGaleraClusterVer1084GlobalStatus)
mockExpect(t, m, queryShowGlobalVariables, dataMariaGaleraClusterVer1084GlobalVariables)
mockExpect(t, m, queryShowAllSlavesStatus, nil)
Expand Down Expand Up @@ -1307,6 +1346,9 @@ func TestMySQL_Collect(t *testing.T) {
{
prepareMock: func(t *testing.T, m sqlmock.Sqlmock) {
mockExpect(t, m, queryShowVersion, dataMySQLVer8030Version)
mockExpect(t, m, queryShowSessionVariables, dataSessionVariables)
mockExpect(t, m, queryDisableSessionQueryLog, nil)
mockExpect(t, m, queryDisableSessionSlowQueryLog, nil)
mockExpect(t, m, queryShowGlobalStatus, dataMySQLVer8030GlobalStatus)
mockExpect(t, m, queryShowGlobalVariables, dataMySQLVer8030GlobalVariables)
mockExpect(t, m, queryShowReplicaStatus, dataMySQLVer8030ReplicaStatusMultiSource)
Expand Down Expand Up @@ -1442,6 +1484,9 @@ func TestMySQL_Collect(t *testing.T) {
{
prepareMock: func(t *testing.T, m sqlmock.Sqlmock) {
mockExpect(t, m, queryShowVersion, dataPerconaVer8029Version)
mockExpect(t, m, queryShowSessionVariables, dataSessionVariables)
mockExpect(t, m, queryDisableSessionQueryLog, nil)
mockExpect(t, m, queryDisableSessionSlowQueryLog, nil)
mockExpect(t, m, queryShowGlobalStatus, dataPerconaVer8029GlobalStatus)
mockExpect(t, m, queryShowGlobalVariables, dataPerconaVer8029GlobalVariables)
mockExpect(t, m, queryShowReplicaStatus, nil)
Expand Down
@@ -0,0 +1,6 @@
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| sql_log_off | OFF |
| slow_query_log | ON |
+----------------+-------+