Skip to content

Commit

Permalink
Added collection explain for mysql8 for query optimization (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
kochetovd authored Jul 31, 2024
1 parent cde50ec commit b617e6a
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 105 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ARG RELEEM_ENV
ARG RELEEM_DEBUG
ARG RELEEM_HOSTNAME
ARG RELEEM_INTERVAL_COLLECT_ALL_METRICS
ARG RELEEM_QUERY_OPTIMIZATION

RUN apt update \
&& apt install -y \
Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

const (
ReleemAgentVersion = "1.17.4.1"
ReleemAgentVersion = "1.18.0"
)

var (
Expand Down Expand Up @@ -38,6 +38,7 @@ type Config struct {
InstanceType string `hcl:"instance_type"`
AwsRegion string `hcl:"aws_region"`
AwsRDSDB string `hcl:"aws_rds_db"`
CollectExplain bool `hcl:"collect_explain"`
}

func LoadConfig(filename string, logger logging.Logger) (*Config, error) {
Expand Down
2 changes: 1 addition & 1 deletion current_version_agent
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.17.4.1
1.18.0
4 changes: 3 additions & 1 deletion docker/releem.conf.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,6 @@ env="${RELEEM_ENV:-prod}"

# Debug string `hcl:"debug"`
# Releem Debug messages
debug=${RELEEM_DEBUG:-false}
debug=${RELEEM_DEBUG:-false}

collect_explain=${RELEEM_QUERY_OPTIMIZATION:-false}
16 changes: 12 additions & 4 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/bash
# install.sh - Version 1.17.4.1
# install.sh - Version 1.18.0
# (C) Releem, Inc 2022
# All rights reserved

# Releem installation script: install and set up the Releem Agent on supported Linux distributions
# using the package manager.

set -e
install_script_version=1.17.4.1
install_script_version=1.18.0
logfile="releem-install.log"

WORKDIR="/opt/releem"
Expand Down Expand Up @@ -300,8 +300,8 @@ else
mysql ${root_connection_string} --user=root --password=${RELEEM_MYSQL_ROOT_PASSWORD} -Be "CREATE USER '${RELEEM_MYSQL_LOGIN}'@'${mysql_user_host}' identified by '${RELEEM_MYSQL_PASSWORD}';"
mysql ${root_connection_string} --user=root --password=${RELEEM_MYSQL_ROOT_PASSWORD} -Be "GRANT PROCESS ON *.* TO '${RELEEM_MYSQL_LOGIN}'@'${mysql_user_host}';"
mysql ${root_connection_string} --user=root --password=${RELEEM_MYSQL_ROOT_PASSWORD} -Be "GRANT REPLICATION CLIENT ON *.* TO '${RELEEM_MYSQL_LOGIN}'@'${mysql_user_host}';"
mysql ${root_connection_string} --user=root --password=${RELEEM_MYSQL_ROOT_PASSWORD} -Be "GRANT SHOW VIEW ON *.* TO '${RELEEM_MYSQL_LOGIN}'@'${mysql_user_host}';"
mysql ${root_connection_string} --user=root --password=${RELEEM_MYSQL_ROOT_PASSWORD} -Be "GRANT SHOW VIEW ON *.* TO '${RELEEM_MYSQL_LOGIN}'@'${mysql_user_host}';"

if mysql ${root_connection_string} --user=root --password=${RELEEM_MYSQL_ROOT_PASSWORD} -Be "GRANT SELECT ON performance_schema.events_statements_summary_by_digest TO '${RELEEM_MYSQL_LOGIN}'@'${mysql_user_host}';"
then
echo "Successfully GRANT" > /dev/null
Expand All @@ -321,6 +321,11 @@ else
else
printf "\033[31m\n This database version is too old.\033[0m\n"
fi
if [ -n $RELEEM_QUERY_OPTIMIZATION ];
then
mysql ${root_connection_string} --user=root --password=${RELEEM_MYSQL_ROOT_PASSWORD} -Be "GRANT SELECT ON *.* TO '${RELEEM_MYSQL_LOGIN}'@'${mysql_user_host}';"
mysql ${root_connection_string} --user=root --password=${RELEEM_MYSQL_ROOT_PASSWORD} -Be "GRANT ALL ON releem.* TO '${RELEEM_MYSQL_LOGIN}'@'${mysql_user_host}';"
fi

#mysql ${root_connection_string} --user=root --password=${RELEEM_MYSQL_ROOT_PASSWORD} -Be "GRANT SELECT, PROCESS,EXECUTE, REPLICATION CLIENT,SHOW DATABASES,SHOW VIEW ON *.* TO '${RELEEM_MYSQL_LOGIN}'@'${mysql_user_host}';"
printf "\033[32m\n Created new user \`${RELEEM_MYSQL_LOGIN}\`\033[0m\n"
Expand Down Expand Up @@ -443,6 +448,9 @@ fi
if [ -n "$RELEEM_MYSQL_SSL_MODE" ]; then
echo "mysql_ssl_mode=$RELEEM_MYSQL_SSL_MODE" | $sudo_cmd tee -a $CONF >/dev/null
fi
if [ -n "$RELEEM_QUERY_OPTIMIZATION" ]; then
echo "collect_explain=$RELEEM_QUERY_OPTIMIZATION" | $sudo_cmd tee -a $CONF >/dev/null
fi
echo "interval_seconds=60" | $sudo_cmd tee -a $CONF >/dev/null
echo "interval_read_config_seconds=3600" | $sudo_cmd tee -a $CONF >/dev/null

Expand Down
59 changes: 9 additions & 50 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package main

import (
"context"
"database/sql"
"flag"
"fmt"
"os"
"strings"

"github.com/Releem/daemon"
"github.com/Releem/mysqlconfigurer/config"
m "github.com/Releem/mysqlconfigurer/metrics"
r "github.com/Releem/mysqlconfigurer/repeater"
u "github.com/Releem/mysqlconfigurer/utils"

"github.com/advantageous/go-logback/logging"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
Expand Down Expand Up @@ -45,15 +45,6 @@ type Service struct {
// return fileInfo.Mode().Type() == fs.ModeSocket
// }

func IsPath(path string, logger logging.Logger) bool {
result_path := strings.Index(path, "/")
if result_path == 0 {
return true
} else {
return false
}
}

// Manage by daemon commands or run the daemon
func (service *Service) Manage(logger logging.Logger, configFile string, command []string, TypeConfiguration string, AgentEvent string, AgentTask string) (string, error) {
var gatherers, gatherers_configuration []m.MetricsGatherer
Expand Down Expand Up @@ -160,41 +151,9 @@ func (service *Service) Manage(logger logging.Logger, configFile string, command
gatherers = append(gatherers, m.NewOSMetricsGatherer(nil, configuration))

}
// }

// Init connection DB
var db *sql.DB
var TypeConnection, MysqlSslMode string

if configuration.MysqlSslMode {
MysqlSslMode = "?tls=skip-verify"
} else {
MysqlSslMode = ""
}
if IsPath(configuration.MysqlHost, logger) {
db, err = sql.Open("mysql", configuration.MysqlUser+":"+configuration.MysqlPassword+"@unix("+configuration.MysqlHost+")/mysql"+MysqlSslMode)
TypeConnection = "unix"

} else {
db, err = sql.Open("mysql", configuration.MysqlUser+":"+configuration.MysqlPassword+"@tcp("+configuration.MysqlHost+":"+configuration.MysqlPort+")/mysql"+MysqlSslMode)
TypeConnection = "tcp"
}
if err != nil {
logger.PrintError("Connection opening to failed", err)
}

err = db.Ping()
if err != nil {
logger.PrintError("Connection failed", err)
} else {
if TypeConnection == "unix" {
logger.Println("Connect Success to DB via unix socket", configuration.MysqlHost)
} else if TypeConnection == "tcp" {
logger.Println("Connect Success to DB via tcp", configuration.MysqlHost)
}
}
config.DB = db
defer db.Close()
config.DB = u.ConnectionDatabase(configuration, logger, "mysql")
defer config.DB.Close()

//Init repeaters
repeaters := make(map[string]m.MetricsRepeater)
Expand All @@ -208,13 +167,13 @@ func (service *Service) Manage(logger logging.Logger, configFile string, command

//Init gatherers
gatherers = append(gatherers,
m.NewDbConfGatherer(nil, db, configuration),
m.NewDbInfoGatherer(nil, db, configuration),
m.NewDbMetricsBaseGatherer(nil, db, configuration),
m.NewDbConfGatherer(nil, configuration),
m.NewDbInfoGatherer(nil, configuration),
m.NewDbMetricsBaseGatherer(nil, configuration),
m.NewAgentMetricsGatherer(nil, configuration))
gatherers_configuration = append(gatherers_configuration, m.NewDbMetricsGatherer(nil, db, configuration))
gatherers_configuration = append(gatherers_configuration, m.NewDbMetricsGatherer(nil, configuration))
if Mode.Name == "TaskSet" && Mode.ModeType == "collect_queries" {
gatherers = append(gatherers, m.NewDbCollectQueries(nil, db, configuration))
gatherers = append(gatherers, m.NewDbCollectQueries(nil, configuration))
}
m.RunWorker(gatherers, gatherers_configuration, repeaters, nil, configuration, configFile, Mode)

Expand Down
Loading

0 comments on commit b617e6a

Please sign in to comment.