Skip to content

Commit

Permalink
Better error handling for 'Peers' metric in all groups
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-ssvlabs committed Oct 2, 2024
1 parent 3e722e1 commit b8f4421
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 24 deletions.
49 changes: 41 additions & 8 deletions internal/benchmark/metrics/consensus/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package consensus
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"strconv"
Expand Down Expand Up @@ -78,14 +80,7 @@ func (p *PeerMetric) measure(ctx context.Context) {
p.AddDataPoint(map[string]uint32{
PeerCountMeasurement: 0,
})

var errorResponse any
_ = json.NewDecoder(res.Body).Decode(&errorResponse)
jsonErrResponse, _ := json.Marshal(errorResponse)
logger.WriteError(
metric.ConsensusGroup,
p.Name,
fmt.Errorf("received unsuccessful status code. Code: '%s'. Response: '%s'", res.Status, jsonErrResponse))
p.logErrorResponse(res)
return
}

Expand All @@ -109,6 +104,44 @@ func (p *PeerMetric) measure(ctx context.Context) {
p.writeMetric(peerNr)
}

func (p *PeerMetric) logErrorResponse(res *http.Response) {
var responseString string
if res.Header.Get("Content-Type") == "application/json" {
var errorResponse any
if err := json.NewDecoder(res.Body).Decode(&errorResponse); err != nil {
logger.WriteError(
metric.ConsensusGroup,
p.Name,
errors.Join(err, fmt.Errorf("received unsuccessful status code. Code: '%s'. Failed to JSON decode response", res.Status)))
return
}
jsonErrResponse, err := json.Marshal(errorResponse)
if err != nil {
logger.WriteError(
metric.ConsensusGroup,
p.Name,
errors.Join(err, fmt.Errorf("received unsuccessful status code. Code: '%s'. Failed to marshal response", res.Status)))
return
}
responseString = string(jsonErrResponse)
} else {
body, err := io.ReadAll(res.Body)
if err != nil {
logger.WriteError(
metric.ConsensusGroup,
p.Name,
errors.Join(err, fmt.Errorf("received unsuccessful status code. Code: '%s'. Failed to decode response", res.Status)))
return
}
responseString = string(body)
}

logger.WriteError(
metric.ConsensusGroup,
p.Name,
fmt.Errorf("received unsuccessful status code. Code: '%s'. Response: '%s'", res.Status, responseString))
}

func (p *PeerMetric) writeMetric(peerNr int) {
p.AddDataPoint(map[string]uint32{
PeerCountMeasurement: uint32(peerNr),
Expand Down
48 changes: 40 additions & 8 deletions internal/benchmark/metrics/execution/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"strconv"
Expand Down Expand Up @@ -95,14 +96,7 @@ func (p *PeerMetric) measure(ctx context.Context) {

if res.StatusCode != http.StatusOK {
p.writeMetric(0)

var errorResponse any
_ = json.NewDecoder(res.Body).Decode(&errorResponse)
jsonErrResponse, _ := json.Marshal(errorResponse)
logger.WriteError(
metric.ExecutionGroup,
p.Name,
fmt.Errorf("received unsuccessful status code. Code: '%s'. Response: '%s'", res.Status, jsonErrResponse))
p.logErrorResponse(res)
return
}

Expand All @@ -129,6 +123,44 @@ func (p *PeerMetric) measure(ctx context.Context) {
p.writeMetric(peerCount)
}

func (p *PeerMetric) logErrorResponse(res *http.Response) {
var responseString string
if res.Header.Get("Content-Type") == "application/json" {
var errorResponse any
if err := json.NewDecoder(res.Body).Decode(&errorResponse); err != nil {
logger.WriteError(
metric.ExecutionGroup,
p.Name,
errors.Join(err, fmt.Errorf("received unsuccessful status code. Code: '%s'. Failed to JSON decode response", res.Status)))
return
}
jsonErrResponse, err := json.Marshal(errorResponse)
if err != nil {
logger.WriteError(
metric.ExecutionGroup,
p.Name,
errors.Join(err, fmt.Errorf("received unsuccessful status code. Code: '%s'. Failed to marshal response", res.Status)))
return
}
responseString = string(jsonErrResponse)
} else {
body, err := io.ReadAll(res.Body)
if err != nil {
logger.WriteError(
metric.ExecutionGroup,
p.Name,
errors.Join(err, fmt.Errorf("received unsuccessful status code. Code: '%s'. Failed to decode response", res.Status)))
return
}
responseString = string(body)
}

logger.WriteError(
metric.ExecutionGroup,
p.Name,
fmt.Errorf("received unsuccessful status code. Code: '%s'. Response: '%s'", res.Status, responseString))
}

func (p *PeerMetric) writeMetric(value int64) {
p.AddDataPoint(map[string]uint32{
PeerCountMeasurement: uint32(value),
Expand Down
49 changes: 41 additions & 8 deletions internal/benchmark/metrics/ssv/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package ssv
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"time"
Expand Down Expand Up @@ -73,14 +75,7 @@ func (p *PeerMetric) measure(ctx context.Context) {

if res.StatusCode != http.StatusOK {
p.writeMetric(0)

var errorResponse any
_ = json.NewDecoder(res.Body).Decode(&errorResponse)
jsonErrResponse, _ := json.Marshal(errorResponse)
logger.WriteError(
metric.SSVGroup,
p.Name,
fmt.Errorf("received unsuccessful status code. Code: '%s'. Response: '%s'", res.Status, jsonErrResponse))
p.logErrorResponse(res)
return
}

Expand All @@ -93,6 +88,44 @@ func (p *PeerMetric) measure(ctx context.Context) {
p.writeMetric(resp.Advanced.Peers)
}

func (p *PeerMetric) logErrorResponse(res *http.Response) {
var responseString string
if res.Header.Get("Content-Type") == "application/json" {
var errorResponse any
if err := json.NewDecoder(res.Body).Decode(&errorResponse); err != nil {
logger.WriteError(
metric.SSVGroup,
p.Name,
errors.Join(err, fmt.Errorf("received unsuccessful status code. Code: '%s'. Failed to JSON decode response", res.Status)))
return
}
jsonErrResponse, err := json.Marshal(errorResponse)
if err != nil {
logger.WriteError(
metric.SSVGroup,
p.Name,
errors.Join(err, fmt.Errorf("received unsuccessful status code. Code: '%s'. Failed to marshal response", res.Status)))
return
}
responseString = string(jsonErrResponse)
} else {
body, err := io.ReadAll(res.Body)
if err != nil {
logger.WriteError(
metric.SSVGroup,
p.Name,
errors.Join(err, fmt.Errorf("received unsuccessful status code. Code: '%s'. Failed to decode response", res.Status)))
return
}
responseString = string(body)
}

logger.WriteError(
metric.SSVGroup,
p.Name,
fmt.Errorf("received unsuccessful status code. Code: '%s'. Response: '%s'", res.Status, responseString))
}

func (p *PeerMetric) writeMetric(value uint32) {
p.AddDataPoint(map[string]uint32{
PeerCountMeasurement: value,
Expand Down

0 comments on commit b8f4421

Please sign in to comment.