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

Moving Package Variable to Monitor #93

Merged
merged 3 commits into from
Feb 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions metrics/repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type Repl struct {
errPolicy map[string]*errors.Policy
stop bool
dropNotAReplica map[string]bool
statusQuery string
newTerms bool
}

var _ blip.Collector = &Repl{}
Expand All @@ -47,6 +49,8 @@ func NewRepl(db *sql.DB) *Repl {
ERR_NO_ACCESS: errors.NewPolicy(""),
},
dropNotAReplica: map[string]bool{},
statusQuery: "SHOW SLAVE STATUS", // SHOW REPLICA STATUS as of 8.022
newTerms: false,
}
}

Expand Down Expand Up @@ -86,9 +90,6 @@ func (c *Repl) Help() blip.CollectorHelp {
}
}

var statusQuery = "SHOW SLAVE STATUS" // SHOW REPLICA STATUS as of 8.022
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put that comment back, i.e. (few lines up in the diff):

		statusQuery:     "SHOW SLAVE STATUS",  // SHOW REPLICA STATUS as of 8.022

When working with MySQL, you'll find that such "as of " comments are common and helpful, especially when your career with MySQL begins to span decades. Lots and lots of changes, and who can remember them all?

var newTerms = false

func (c *Repl) Prepare(ctx context.Context, plan blip.Plan) (func(), error) {
haveVersion := false

Expand Down Expand Up @@ -134,10 +135,10 @@ LEVEL:
}
haveVersion = true
if major == 8 && patch >= 22 {
statusQuery = "SHOW REPLICA STATUS"
newTerms = true
c.statusQuery = "SHOW REPLICA STATUS"
c.newTerms = true
}
blip.Debug("mysql %d.x.%d %s", major, patch, statusQuery)
blip.Debug("mysql %d.x.%d %s", major, patch, c.statusQuery)
}
return nil, nil
}
Expand All @@ -155,7 +156,7 @@ func (c *Repl) Collect(ctx context.Context, levelName string) ([]blip.MetricValu

// Return SHOW SLAVE|REPLICA STATUS as map[string]string, which can be nil
// if MySQL is not a replica
replStatus, err := sqlutil.RowToMap(ctx, c.db, statusQuery)
replStatus, err := sqlutil.RowToMap(ctx, c.db, c.statusQuery)
if err != nil {
return c.collectError(err)
}
Expand Down Expand Up @@ -189,10 +190,13 @@ func (c *Repl) Collect(ctx context.Context, levelName string) ([]blip.MetricValu
Value: running,
Meta: map[string]string{"source": ""},
}
if newTerms {
m.Meta["source"] = replStatus["Source_Host"]
} else {
m.Meta["source"] = replStatus["Master_Host"]
// Make sure we have results.
if len(replStatus) != 0 {
if c.newTerms {
m.Meta["source"] = replStatus["Source_Host"]
} else {
m.Meta["source"] = replStatus["Master_Host"]
}
}
metrics = append(metrics, m)
}
Expand Down