Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 10
Add ProxySQL topology query API#37
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -42,6 +42,7 @@ import ( | ||
| "github.com/proxysql/orchestrator/go/logic" | ||
| "github.com/proxysql/orchestrator/go/metrics/query" | ||
| "github.com/proxysql/orchestrator/go/process" | ||
| "github.com/proxysql/orchestrator/go/proxysql" | ||
| orcraft "github.com/proxysql/orchestrator/go/raft" | ||
| ) | ||
| @@ -3762,6 +3763,41 @@ func (this *HttpAPI) registerSingleAPIRequest(m *martini.ClassicMartini, path st | ||
| } | ||
| } | ||
| // ProxySQLServers returns all servers from ProxySQL's runtime_mysql_servers table. | ||
| func (this *HttpAPI) ProxySQLServers(params martini.Params, r render.Render, req *http.Request) { | ||
| hook := proxysql.GetHook() | ||
| if hook == nil || !hook.IsConfigured() { | ||
| Respond(r, &APIResponse{Code: ERROR, Message: "ProxySQL is not configured"}) | ||
| return | ||
| } | ||
Comment on lines
+3768
to
+3772
CopilotAI | ||
| servers, err := hook.GetClient().GetServers() | ||
| if err != nil { | ||
| Respond(r, &APIResponse{Code: ERROR, Message: fmt.Sprintf("Failed to query ProxySQL servers: %v", err)}) | ||
| return | ||
| } | ||
| Respond(r, &APIResponse{Code: OK, Message: fmt.Sprintf("Found %d servers", len(servers)), Details: servers}) | ||
| } | ||
| // ProxySQLServersByHostgroup returns servers for a specific hostgroup from ProxySQL. | ||
| func (this *HttpAPI) ProxySQLServersByHostgroup(params martini.Params, r render.Render, req *http.Request) { | ||
| hook := proxysql.GetHook() | ||
| if hook == nil || !hook.IsConfigured() { | ||
| Respond(r, &APIResponse{Code: ERROR, Message: "ProxySQL is not configured"}) | ||
| return | ||
| } | ||
| hostgroupID, err := strconv.Atoi(params["hostgroup"]) | ||
| if err != nil { | ||
| Respond(r, &APIResponse{Code: ERROR, Message: fmt.Sprintf("Invalid hostgroup ID: %v", err)}) | ||
| return | ||
| } | ||
| servers, err := hook.GetClient().GetServersByHostgroup(hostgroupID) | ||
| if err != nil { | ||
| Respond(r, &APIResponse{Code: ERROR, Message: fmt.Sprintf("Failed to query ProxySQL servers: %v", err)}) | ||
| return | ||
| } | ||
| Respond(r, &APIResponse{Code: OK, Message: fmt.Sprintf("Found %d servers in hostgroup %d", len(servers), hostgroupID), Details: servers}) | ||
| } | ||
| func (this *HttpAPI) registerAPIRequestInternal(m *martini.ClassicMartini, path string, handler martini.Handler, allowProxy bool) { | ||
| this.registerSingleAPIRequest(m, path, handler, allowProxy) | ||
| @@ -4048,6 +4084,10 @@ func (this *HttpAPI) RegisterRequests(m *martini.ClassicMartini) { | ||
| this.registerAPIRequest(m, "agent-custom-command/:host/:command", this.AgentCustomCommand) | ||
| this.registerAPIRequest(m, "seeds", this.Seeds) | ||
| // ProxySQL topology | ||
| this.registerAPIRequest(m, "proxysql/servers", this.ProxySQLServers) | ||
| this.registerAPIRequest(m, "proxysql/servers/:hostgroup", this.ProxySQLServersByHostgroup) | ||
| // Configurable status check endpoint | ||
| if config.Config.StatusEndpoint == config.DefaultStatusAPIEndpoint { | ||
| this.registerAPIRequestNoProxy(m, "status", this.StatusCheck) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -29,6 +29,14 @@ func (h *Hook) IsConfigured() bool { | ||
| return h.client != nil && h.writerHostgroup > 0 | ||
| } | ||
| // GetClient returns the underlying ProxySQL client, or nil if the hook is not configured. | ||
| func (h *Hook) GetClient() *Client { | ||
| if h == nil { | ||
| return nil | ||
| } | ||
| return h.client | ||
| } | ||
Comment on lines
+32
to
+38
CopilotAI | ||
| func (h *Hook) PreFailover(oldMasterHost string, oldMasterPort int) error { | ||
| if !h.IsConfigured() { | ||
| return nil | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,80 @@ | ||||||||
| package proxysql | ||||||||
| import ( | ||||||||
| "fmt" | ||||||||
| "github.com/proxysql/golib/log" | ||||||||
| ) | ||||||||
| // ServerEntry represents a row from ProxySQL's mysql_servers table. | ||||||||
CopilotAI | ||||||||
| // ServerEntry represents a row from ProxySQL's mysql_servers table. | |
| // ServerEntry represents a row from ProxySQL's runtime_mysql_servers table | |
| // (the runtime view of the mysql_servers configuration). |
CopilotAIMar 24, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetServers/GetServersByHostgroup log at Info level on every call. Since these methods may be invoked frequently via the new HTTP endpoints (polling/monitoring), this can create noisy logs in production. Consider lowering this to a debug-level log or removing it (or rate-limiting) to avoid log volume issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for iterating through *sql.Rows and scanning them into ServerEntry structs is duplicated in GetServers and GetServersByHostgroup. You can extract this into a helper function to avoid repetition and make the code more modular. This will improve the maintainability of this new file.
func (c*Client) GetServers() ([]ServerEntry, error) {
ifc==nil {
return []ServerEntry{}, nil
}
query:="SELECT hostgroup_id, hostname, port, status, weight, max_connections, comment FROM runtime_mysql_servers ORDER BY hostgroup_id, hostname, port"rows, db, err:=c.Query(query)
iferr!=nil {
returnnil, fmt.Errorf("proxysql: GetServers: %v", err)
}
deferdb.Close()
deferrows.Close()
servers, err:=scanServerEntries(rows)
iferr!=nil {
returnnil, fmt.Errorf("proxysql: GetServers: failed to process rows: %v", err)
}
log.Infof("proxysql: GetServers returned %d entries", len(servers))
returnservers, nil
}
// GetServersByHostgroup returns servers filtered by hostgroup ID.// Returns an empty slice (no error) if the client is nil.func (c*Client) GetServersByHostgroup(hostgroupIDint) ([]ServerEntry, error) {
ifc==nil {
return []ServerEntry{}, nil
}
query:="SELECT hostgroup_id, hostname, port, status, weight, max_connections, comment FROM runtime_mysql_servers WHERE hostgroup_id = ? ORDER BY hostname, port"rows, db, err:=c.Query(query, hostgroupID)
iferr!=nil {
returnnil, fmt.Errorf("proxysql: GetServersByHostgroup(%d): %v", hostgroupID, err)
}
deferdb.Close()
deferrows.Close()
servers, err:=scanServerEntries(rows)
iferr!=nil {
returnnil, fmt.Errorf("proxysql: GetServersByHostgroup: failed to process rows: %v", err)
}
log.Infof("proxysql: GetServersByHostgroup(%d) returned %d entries", hostgroupID, len(servers))
returnservers, nil
}
// scanServerEntries is a helper to scan rows into a slice of ServerEntry.funcscanServerEntries(rows*sql.Rows) ([]ServerEntry, error) {
varservers []ServerEntryforrows.Next() {
varsServerEntryiferr:=rows.Scan(&s.HostgroupID, &s.Hostname, &s.Port, &s.Status, &s.Weight, &s.MaxConns, &s.Comment); err!=nil {
returnnil, err
}
servers=append(servers, s)
}
returnservers, rows.Err()
}| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package proxysql | ||
| import ( | ||
| "testing" | ||
| ) | ||
| func TestServerEntryStruct(t *testing.T) { | ||
| s := ServerEntry{ | ||
| HostgroupID: 10, | ||
| Hostname: "db1.example.com", | ||
| Port: 3306, | ||
| Status: "ONLINE", | ||
| Weight: 1000, | ||
| MaxConns: 100, | ||
| Comment: "primary writer", | ||
| } | ||
| if s.HostgroupID != 10 { | ||
| t.Errorf("expected HostgroupID=10, got %d", s.HostgroupID) | ||
| } | ||
| if s.Hostname != "db1.example.com" { | ||
| t.Errorf("expected Hostname=db1.example.com, got %s", s.Hostname) | ||
| } | ||
| if s.Port != 3306 { | ||
| t.Errorf("expected Port=3306, got %d", s.Port) | ||
| } | ||
| if s.Status != "ONLINE" { | ||
| t.Errorf("expected Status=ONLINE, got %s", s.Status) | ||
| } | ||
| if s.Weight != 1000 { | ||
| t.Errorf("expected Weight=1000, got %d", s.Weight) | ||
| } | ||
| if s.MaxConns != 100 { | ||
| t.Errorf("expected MaxConns=100, got %d", s.MaxConns) | ||
| } | ||
| if s.Comment != "primary writer" { | ||
| t.Errorf("expected Comment='primary writer', got '%s'", s.Comment) | ||
| } | ||
| } | ||
| func TestGetServersNilClient(t *testing.T) { | ||
| var c *Client | ||
| servers, err := c.GetServers() | ||
| if err != nil { | ||
| t.Errorf("expected no error for nil client, got %v", err) | ||
| } | ||
| if servers == nil { | ||
| t.Error("expected non-nil empty slice, got nil") | ||
| } | ||
| if len(servers) != 0 { | ||
| t.Errorf("expected empty slice, got %d entries", len(servers)) | ||
| } | ||
| } | ||
| func TestGetServersByHostgroupNilClient(t *testing.T) { | ||
| var c *Client | ||
| servers, err := c.GetServersByHostgroup(10) | ||
| if err != nil { | ||
| t.Errorf("expected no error for nil client, got %v", err) | ||
| } | ||
| if servers == nil { | ||
| t.Error("expected non-nil empty slice, got nil") | ||
| } | ||
| if len(servers) != 0 { | ||
| t.Errorf("expected empty slice, got %d entries", len(servers)) | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic to get the ProxySQL hook and check if it's configured is duplicated here and in the
ProxySQLServersByHostgroupfunction. To improve maintainability and reduce code duplication, consider extracting this logic into a private helper method onHttpAPI.For example, you could create a helper like this:
Then, both
ProxySQLServersandProxySQLServersByHostgroupcan call this helper at the beginning: