Skip to content

Commit

Permalink
Remove rotation for error log
Browse files Browse the repository at this point in the history
  • Loading branch information
yamatcha committed Nov 27, 2024
1 parent 267f415 commit cefe7d7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 95 deletions.
2 changes: 1 addition & 1 deletion cmd/moco-agent/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ var rootCmd = &cobra.Command{
metrics.Init(registry, clusterName, index)

c := cron.New(cron.WithLogger(rLogger.WithName("cron")))
if _, err := c.AddFunc(config.logRotationSchedule, func() { agent.RotateErrorLog(); agent.RotateSlowLog() }); err != nil {
if _, err := c.AddFunc(config.logRotationSchedule, func() { agent.RotateLog() }); err != nil {
rLogger.Error(err, "failed to parse the cron spec", "spec", config.logRotationSchedule)
return err
}
Expand Down
3 changes: 0 additions & 3 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ const (
// MySQLAdminPort is a port number for MySQL Admin
MySQLAdminPort = 33062

// MySQLErrorLogName is a filekey of error log for MySQL.
MySQLErrorLogName = "mysql.err"

// MySQLSlowLogName is a filekey of slow query log for MySQL.
MySQLSlowLogName = "mysql.slow"
)
45 changes: 5 additions & 40 deletions server/rotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,8 @@ import (
"github.com/cybozu-go/moco-agent/metrics"
)

// RotateLog rotates error log files
func (a *Agent) RotateErrorLog() {
ctx := context.Background()

metrics.LogRotationCount.Inc()
startTime := time.Now()

errFile := filepath.Join(a.logDir, mocoagent.MySQLErrorLogName)
err := os.Rename(errFile, errFile+".0")
if err != nil && !os.IsNotExist(err) {
a.logger.Error(err, "failed to rotate err log file")
metrics.LogRotationFailureCount.Inc()
return
}

if _, err := a.db.ExecContext(ctx, "FLUSH LOCAL ERROR LOGS"); err != nil {
a.logger.Error(err, "failed to exec FLUSH LOCAL ERROR LOGS")
metrics.LogRotationFailureCount.Inc()
return
}

durationSeconds := time.Since(startTime).Seconds()
metrics.LogRotationDurationSeconds.Observe(durationSeconds)
}

// RotateLog rotates slow log files
func (a *Agent) RotateSlowLog() {
func (a *Agent) RotateLog() {
ctx := context.Background()

metrics.LogRotationCount.Inc()
Expand All @@ -62,23 +37,13 @@ func (a *Agent) RotateSlowLog() {

// RotateLogIfSizeExceeded rotates log files if it exceeds rotationSize
func (a *Agent) RotateLogIfSizeExceeded(rotationSize int64) {
errFile := filepath.Join(a.logDir, mocoagent.MySQLErrorLogName)
errFileStat, err := os.Stat(errFile)
if err != nil {
a.logger.Error(err, "failed to get stat of error log file")
return
}
if errFileStat.Size() > rotationSize {
a.RotateErrorLog()
}

slowFile := filepath.Join(a.logDir, mocoagent.MySQLSlowLogName)
slowFileStat, err := os.Stat(slowFile)
file := filepath.Join(a.logDir, mocoagent.MySQLSlowLogName)
fileStat, err := os.Stat(file)
if err != nil {
a.logger.Error(err, "failed to get stat of slow query log file")
return
}
if slowFileStat.Size() > rotationSize {
a.RotateSlowLog()
if fileStat.Size() > rotationSize {
a.RotateLog()
}
}
84 changes: 33 additions & 51 deletions server/rotate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,29 @@ var _ = Describe("log rotation", Ordered, func() {
Expect(err).ShouldNot(HaveOccurred())
defer agent.CloseDB()

By("preparing log files for testing")
slowFile := filepath.Join(tmpDir, mocoagent.MySQLSlowLogName)
errFile := filepath.Join(tmpDir, mocoagent.MySQLErrorLogName)
logFiles := []string{slowFile, errFile}

for _, file := range logFiles {
_, err := os.Create(file)
Expect(err).ShouldNot(HaveOccurred())
}
By("preparing log file for testing")
logFile := filepath.Join(tmpDir, mocoagent.MySQLSlowLogName)

agent.RotateErrorLog()
agent.RotateSlowLog()
_, err = os.Create(logFile)
Expect(err).ShouldNot(HaveOccurred())

for _, file := range logFiles {
_, err := os.Stat(file + ".0")
Expect(err).ShouldNot(HaveOccurred())
}
Expect(testutil.ToFloat64(metrics.LogRotationCount)).To(BeNumerically("==", 2))
agent.RotateLog()

_, err = os.Stat(logFile + ".0")
Expect(err).ShouldNot(HaveOccurred())
Expect(testutil.ToFloat64(metrics.LogRotationCount)).To(BeNumerically("==", 1))
Expect(testutil.ToFloat64(metrics.LogRotationFailureCount)).To(BeNumerically("==", 0))

By("creating the same name directory")
for _, file := range logFiles {
err := os.Rename(file+".0", file)
Expect(err).ShouldNot(HaveOccurred())
err = os.Mkdir(file+".0", 0777)
Expect(err).ShouldNot(HaveOccurred())
}
err = os.Rename(logFile+".0", logFile)
Expect(err).ShouldNot(HaveOccurred())
err = os.Mkdir(logFile+".0", 0777)
Expect(err).ShouldNot(HaveOccurred())

agent.RotateErrorLog()
agent.RotateSlowLog()
agent.RotateLog()

Expect(testutil.ToFloat64(metrics.LogRotationCount)).To(BeNumerically("==", 4))
Expect(testutil.ToFloat64(metrics.LogRotationFailureCount)).To(BeNumerically("==", 2))
Expect(testutil.ToFloat64(metrics.LogRotationCount)).To(BeNumerically("==", 2))
Expect(testutil.ToFloat64(metrics.LogRotationFailureCount)).To(BeNumerically("==", 1))
})

It("should rotate logs by RotateLogIfSizeExceeded if size exceeds", func() {
Expand All @@ -93,44 +83,36 @@ var _ = Describe("log rotation", Ordered, func() {
Expect(err).ShouldNot(HaveOccurred())
defer agent.CloseDB()

By("preparing log files for testing")
slowFile := filepath.Join(tmpDir, mocoagent.MySQLSlowLogName)
errFile := filepath.Join(tmpDir, mocoagent.MySQLErrorLogName)
logFiles := []string{slowFile, errFile}
By("preparing log file for testing")
logFile := filepath.Join(tmpDir, mocoagent.MySQLSlowLogName)

logDataSize := 512
data := bytes.Repeat([]byte("a"), logDataSize)
for _, file := range logFiles {
f, err := os.Create(file)
Expect(err).ShouldNot(HaveOccurred())
f.Write(data)
}
f, err := os.Create(logFile)
Expect(err).ShouldNot(HaveOccurred())
f.Write(data)

agent.RotateLogIfSizeExceeded(int64(logDataSize) + 1)

Expect(testutil.ToFloat64(metrics.LogRotationCount)).To(BeNumerically("==", 4))
Expect(testutil.ToFloat64(metrics.LogRotationFailureCount)).To(BeNumerically("==", 2))
Expect(testutil.ToFloat64(metrics.LogRotationCount)).To(BeNumerically("==", 2))
Expect(testutil.ToFloat64(metrics.LogRotationFailureCount)).To(BeNumerically("==", 1))

agent.RotateLogIfSizeExceeded(int64(logDataSize) - 1)

for _, file := range logFiles {
_, err := os.Stat(file + ".0")
Expect(err).ShouldNot(HaveOccurred())
}
Expect(testutil.ToFloat64(metrics.LogRotationCount)).To(BeNumerically("==", 6))
Expect(testutil.ToFloat64(metrics.LogRotationFailureCount)).To(BeNumerically("==", 2))
_, err = os.Stat(logFile + ".0")
Expect(err).ShouldNot(HaveOccurred())
Expect(testutil.ToFloat64(metrics.LogRotationCount)).To(BeNumerically("==", 3))
Expect(testutil.ToFloat64(metrics.LogRotationFailureCount)).To(BeNumerically("==", 1))

By("creating the same name directory")
for _, file := range logFiles {
err := os.Rename(file+".0", file)
Expect(err).ShouldNot(HaveOccurred())
err = os.Mkdir(file+".0", 0777)
Expect(err).ShouldNot(HaveOccurred())
}
err = os.Rename(logFile+".0", logFile)
Expect(err).ShouldNot(HaveOccurred())
err = os.Mkdir(logFile+".0", 0777)
Expect(err).ShouldNot(HaveOccurred())

agent.RotateLogIfSizeExceeded(int64(logDataSize) - 1)

Expect(testutil.ToFloat64(metrics.LogRotationCount)).To(BeNumerically("==", 8))
Expect(testutil.ToFloat64(metrics.LogRotationFailureCount)).To(BeNumerically("==", 4))
Expect(testutil.ToFloat64(metrics.LogRotationCount)).To(BeNumerically("==", 4))
Expect(testutil.ToFloat64(metrics.LogRotationFailureCount)).To(BeNumerically("==", 2))
})
})

0 comments on commit cefe7d7

Please sign in to comment.