Skip to content

Commit

Permalink
BUG/MEDIUM: runtime: fix runtime stats parsing when in master-worker
Browse files Browse the repository at this point in the history
  • Loading branch information
mjuraga committed Apr 19, 2024
1 parent 39af58b commit 5bf2114
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion runtime/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

// GetInfo fetches HAProxy info from runtime API
func (s *SingleRuntime) GetInfo() models.ProcessInfo {
dataStr, err := s.ExecuteRaw("show info typed")
dataStr, err := s.ExecuteWithResponse("show info typed")
data := models.ProcessInfo{RuntimeAPI: s.socketPath}
if err != nil {
data.Error = err.Error()
Expand Down
20 changes: 9 additions & 11 deletions runtime/runtime_single_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (s *SingleRuntime) readFromSocket(command string, socket socketType) (strin
case statsSocket:
fullCommand = fmt.Sprintf("set severity-output number;%s\n", command)
if s.worker > 0 {
fullCommand = fmt.Sprintf("@%v set severity-output number;@%v %s;quit\n", s.worker, s.worker, command)
fullCommand = fmt.Sprintf("set severity-output number;@%v %s;quit\n", s.worker, command)
}
case masterSocket:
fullCommand = fmt.Sprintf("%s;quit", command)
Expand Down Expand Up @@ -160,6 +160,7 @@ func (s *SingleRuntime) readFromSocket(command string, socket socketType) (strin

result := strings.TrimSuffix(data.String(), "\n> ")
result = strings.TrimSuffix(result, "\n")
result = strings.TrimSpace(result)
return result, nil //nolint:nilerr
}

Expand All @@ -175,10 +176,10 @@ func (s *SingleRuntime) Execute(command string) error {
if err != nil {
return fmt.Errorf("%w [%s]", err, command)
}
if len(rawdata) > 5 {
switch rawdata[1:5] {
if len(rawdata) > 4 {
switch rawdata[0:4] {
case "[3]:", "[2]:", "[1]:", "[0]:":
return fmt.Errorf("[%c] %s [%s]", rawdata[2], rawdata[5:], command)
return fmt.Errorf("[%c] %s [%s]", rawdata[1], rawdata[4:], command)
}
}
return nil
Expand All @@ -189,16 +190,13 @@ func (s *SingleRuntime) ExecuteWithResponse(command string) (string, error) {
if err != nil {
return "", fmt.Errorf("%w [%s]", err, command)
}
if len(rawdata) > 5 {
switch rawdata[1:5] {
if len(rawdata) > 4 {
switch rawdata[0:4] {
case "[3]:", "[2]:", "[1]:", "[0]:":
return "", fmt.Errorf("[%c] %s [%s]", rawdata[2], rawdata[5:], command)
return "", fmt.Errorf("[%c] %s [%s]", rawdata[1], rawdata[4:], command)
}
}
if len(rawdata) > 1 {
return rawdata[1:], nil
}
return "", nil
return rawdata, nil
}

func (s *SingleRuntime) ExecuteMaster(command string) (string, error) {
Expand Down
33 changes: 17 additions & 16 deletions runtime/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *SingleRuntime) GetStats() *models.NativeStatsCollection {
rAPI = s.socketPath
}
result := &models.NativeStatsCollection{RuntimeAPI: rAPI}
rawdata, err := s.ExecuteRaw("show stat")
rawdata, err := s.ExecuteWithResponse("show stat")
if err != nil {
result.Error = err.Error()
return result
Expand All @@ -47,26 +47,27 @@ func (s *SingleRuntime) GetStats() *models.NativeStatsCollection {
if len(line) < len(keys) {
continue
}
oneLineData := &models.NativeStat{}
for index, key := range keys {
if len(line[index]) > 0 {
data[key] = line[index]
}
if key == "type" {
switch line[index] {
case "0", "1":
oneLineData.Name = line[0]
oneLineData.Type = strings.ToLower(line[1])
case "2":
oneLineData.Name = line[1]
oneLineData.Type = "server"
oneLineData.BackendName = line[0]
case "3":
// we ignore listener
default:
// add logging when available fmt.Printf("unexpected stat type: %s", line[32])
}
}
}
oneLineData := &models.NativeStat{}
switch line[32] { // 32. type [LFBS]: (0=frontend, 1=backend, 2=server, 3=socket/listener)
case "0", "1":
oneLineData.Name = line[0]
oneLineData.Type = strings.ToLower(line[1])
case "2":
oneLineData.Name = line[1]
oneLineData.Type = "server"
oneLineData.BackendName = line[0]
case "3":
// we ignore socket/listener
default:
// add logging when available fmt.Printf("unexpected stat type: %s", line[32])
}

var st models.NativeStatStats
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: &st,
Expand Down

0 comments on commit 5bf2114

Please sign in to comment.