Skip to content

Commit

Permalink
feat - mysql slow query digest
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicola Strappazzon C committed Nov 5, 2023
1 parent 9fdc6ea commit a48153a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ require (
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
)

replace github.com/debeando/go-common => /Users/nsc/go/src/github.com/debeando/go-common
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"zenit/agent"
"zenit/aws"
"zenit/mysql"
"zenit/service"
"zenit/version"

Expand Down Expand Up @@ -30,8 +31,9 @@ Find more information at: https://github.com/debeando/zenit`,

rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output")

rootCmd.AddCommand(aws.NewCommand())
rootCmd.AddCommand(agent.NewCommand())
rootCmd.AddCommand(aws.NewCommand())
rootCmd.AddCommand(mysql.NewCommand())
rootCmd.AddCommand(service.NewCommand())
rootCmd.AddCommand(version.NewCommand())
rootCmd.Execute()
Expand Down
84 changes: 84 additions & 0 deletions mysql/digest/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package digest

import (
"fmt"

"github.com/debeando/go-common/file"
"github.com/debeando/go-common/mysql/sql/digest"
"github.com/debeando/go-common/mysql/sql/parser/slow"
"github.com/debeando/go-common/table"

"github.com/spf13/cobra"
)

func NewCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "digest [FILENAME]",
Short: "Analyze MySQL queries from slow query log.",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
cmd.Help()
return
}

if !file.Exist(args[0]) {
fmt.Println(fmt.Sprintf("File not exist: %s", args[0]))
return
}

queries := &digest.List{}
channelIn := make(chan string)
channelOut := make(chan string)

defer close(channelIn)

go slow.LogsParser(channelIn, channelOut)

go func() {
defer close(channelOut)
for log := range channelOut {
queries.Add(slow.QueryParser(log))
}
}()

file.ReadLineByLine(args[0], func(line string){
channelIn <- line
})

tbl := table.New("DIGEST_ID", "SCORE", "COUNT", "Q. TIME", "L. TIME", "R. SENT", "R. EXAMINED")
tbl.SetFirstColumnAlignment(table.Right)

fmt.Println(fmt.Sprintf("Summary:\n- Analizded: %d\n- Unique: %d\n- Total: %d", 0, queries.Unique(), queries.Count()))

queries.SortByScore()

for index := range *queries {
tbl.AddRow(
(*queries)[index].ID,
formatScore((*queries)[index].Score),
(*queries)[index].Count,
formatTime((*queries)[index].Time.Query),
formatTime((*queries)[index].Time.Lock),
(*queries)[index].Rows.Sent,
(*queries)[index].Rows.Examined,
)
}
tbl.Print()
},
}

return cmd
}

func formatTime(seconds float64) string {
if seconds < 1e-6 {
return fmt.Sprintf("%06.3f", seconds*1e6)
} else if seconds < 1e-3 {
return fmt.Sprintf("%06.3f", seconds*1e3)
}
return fmt.Sprintf("%06.3f", seconds)
}

func formatScore(score float64) string {
return fmt.Sprintf("%03d", int(((score - 0.5) / (517550 - 0.5)) * 99 + 1))
}
21 changes: 21 additions & 0 deletions mysql/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package mysql

import (
"zenit/mysql/digest"

"github.com/spf13/cobra"
)

func NewCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "mysql",
Short: "MySQL commands to facilitate administration.",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

cmd.AddCommand(digest.NewCommand())

return cmd
}

0 comments on commit a48153a

Please sign in to comment.