Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

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 8be7459
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 7 deletions.
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module zenit
go 1.21

require (
github.com/aws/aws-sdk-go v1.46.6
github.com/debeando/go-common v0.4.4
github.com/aws/aws-sdk-go v1.47.3
github.com/debeando/go-common v0.4.5
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c
github.com/kardianos/service v1.2.2
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/spf13/cobra v1.7.0
github.com/spf13/cobra v1.8.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -35,7 +35,7 @@ require (
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.mongodb.org/mongo-driver v1.12.1 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ github.com/aws/aws-sdk-go v1.46.2 h1:XZbOmjtN1VCfEtQq7QNFsbxIqO+bB+bRhiOBjp6AzWc
github.com/aws/aws-sdk-go v1.46.2/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go v1.46.6 h1:6wFnNC9hETIZLMf6SOTN7IcclrOGwp/n9SLp8Pjt6E8=
github.com/aws/aws-sdk-go v1.46.6/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go v1.47.3 h1:e0H6NFXiniCpR8Lu3lTphVdRaeRCDLAeRyTHd1tJSd8=
github.com/aws/aws-sdk-go v1.47.3/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -14,6 +17,8 @@ github.com/debeando/go-common v0.4.3 h1:JUDvmSZONsNS8DvyJ/rrJBbRvTuFBqqjtBVUkEp4
github.com/debeando/go-common v0.4.3/go.mod h1:0u1RTk8umumGN+i+2V07g0itNWfDIUU19bsxEeFLMG4=
github.com/debeando/go-common v0.4.4 h1:uDYGYC+I+/ZlOZ27w8FHUKiaGzfsMaxzASJnPGai4SE=
github.com/debeando/go-common v0.4.4/go.mod h1:0u1RTk8umumGN+i+2V07g0itNWfDIUU19bsxEeFLMG4=
github.com/debeando/go-common v0.4.5 h1:XVE8thhXVdNoLuCOQokq3TZePxbPKv2vRqwBeKeK8Ds=
github.com/debeando/go-common v0.4.5/go.mod h1:0u1RTk8umumGN+i+2V07g0itNWfDIUU19bsxEeFLMG4=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
Expand Down Expand Up @@ -62,6 +67,8 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -103,6 +110,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -120,6 +129,8 @@ golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand All @@ -132,6 +143,8 @@ golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
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
97 changes: 97 additions & 0 deletions mysql/digest/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
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 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
})

queries.FilterByQueryTime(0.100)

fmt.Println(
fmt.Sprintf("Slow query total: %d, Filtered: %d, Analyzed %d, Unique: %d.",
queries.Count(),
queries.Filtered(),
queries.Analyzed(),
queries.Unique()))

tbl := table.New(
"DIGEST ID", "SCORE", "COUNT", "Q. TIME",
"L. TIME", "R. SENT", "R. EXAMINED",
)

queries.Clean()
queries.SortByScore()

min := queries.ScoreMin()
max := queries.ScoreMax()

for index := range *queries {
tbl.AddRow(
(*queries)[index].ID,
formatScore(min, max, (*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(min, max, score float64) string {
return fmt.Sprintf("%03d", int(((score-min)/(max-min))*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 8be7459

Please sign in to comment.