Skip to content

Commit

Permalink
feat: handle metric for peer connection failure
Browse files Browse the repository at this point in the history
  • Loading branch information
adklempner committed Jul 15, 2024
1 parent 7ed524e commit f7f96f2
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
25 changes: 24 additions & 1 deletion telemetry/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions telemetry/peer_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,32 @@ func (r *PeerCount) put(db *sql.DB) error {

return nil
}

type PeerConnFailure struct {
ID int `json:"id"`
CreatedAt int64 `json:"createdAt"`
Timestamp int64 `json:"timestamp"`
NodeName string `json:"nodeName"`
NodeKeyUid string `json:"nodeKeyUid"`
PeerId string `json:"peerId"`
StatusVersion string `json:"statusVersion"`
}

func (r *PeerConnFailure) put(db *sql.DB) error {
stmt, err := db.Prepare("INSERT INTO peerConnFailure (timestamp, nodeName, nodeKeyUid, peerId, statusVersion, createdAt) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id;")
if err != nil {
return err
}

defer stmt.Close()

r.CreatedAt = time.Now().Unix()
lastInsertId := 0
err = stmt.QueryRow(r.Timestamp, r.NodeName, r.NodeKeyUid, r.PeerId, r.StatusVersion, r.CreatedAt).Scan(&lastInsertId)
if err != nil {
return err
}
r.ID = lastInsertId

return nil
}
11 changes: 11 additions & 0 deletions telemetry/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const (
ReceivedMessagesMetric TelemetryType = "ReceivedMessages"
ErrorSendingEnvelopeMetric TelemetryType = "ErrorSendingEnvelope"
PeerCountMetric TelemetryType = "PeerCount"
PeerConnFailureMetric TelemetryType = "PeerConnFailure"
)

type TelemetryRequest struct {
Expand Down Expand Up @@ -139,6 +140,16 @@ func (s *Server) createTelemetryData(w http.ResponseWriter, r *http.Request) {
errorDetails = append(errorDetails, map[string]interface{}{"Id": data.Id, "Error": fmt.Sprintf("Error saving peer count: %v", err)})
continue
}
case PeerConnFailureMetric:
var peerConnFailure PeerConnFailure
if err := json.Unmarshal(*data.TelemetryData, &peerConnFailure); err != nil {
errorDetails = append(errorDetails, map[string]interface{}{"Id": data.Id, "Error": fmt.Sprintf("Error decoding peer connection failure: %v", err)})
continue
}
if err := peerConnFailure.put(s.DB); err != nil {
errorDetails = append(errorDetails, map[string]interface{}{"Id": data.Id, "Error": fmt.Sprintf("Error saving peer connection failure: %v", err)})
continue
}
default:
errorDetails = append(errorDetails, map[string]interface{}{"Id": data.Id, "Error": fmt.Sprintf("Unknown telemetry type: %s", data.TelemetryType)})
}
Expand Down
10 changes: 10 additions & 0 deletions telemetry/sql/000010_peer_conn_failure.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS peerConnFailure (
id SERIAL PRIMARY KEY,
createdAt INTEGER NOT NULL,
peerId VARCHAR(255) NOT NULL,
nodeName VARCHAR(255) NOT NULL,
nodeKeyUid VARCHAR(255) NOT NULL,
timestamp INTEGER NOT NULL,
statusVersion VARCHAR(31),
CONSTRAINT peerConnFailure_unique unique(timestamp, nodeName, nodeKeyUid, peerId, statusVersion)
);

0 comments on commit f7f96f2

Please sign in to comment.