Skip to content

Commit

Permalink
feat: track store confirmation failures
Browse files Browse the repository at this point in the history
  • Loading branch information
adklempner committed Sep 28, 2024
1 parent 171275d commit 2a29886
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 59 deletions.
1 change: 1 addition & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func main() {
server.RegisterMetric(types.SentEnvelopeMetric, &metrics.SentEnvelope{})
server.RegisterMetric(types.PeerCountByShardMetric, &metrics.PeerCountByShard{})
server.RegisterMetric(types.PeerCountByOriginMetric, &metrics.PeerCountByOrigin{})
server.RegisterMetric(types.StoreConfirmationFailedMetric, &metrics.StoreConfirmationFailed{})

server.RegisterMetric(types.MessageCheckSuccessMetric, &metrics.MessageCheckSuccess{})
server.RegisterMetric(types.MessageCheckFailureMetric, &metrics.MessageCheckFailure{})
Expand Down
6 changes: 6 additions & 0 deletions lib/common/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func DropTables(db *sql.DB) {
"messageCheckSuccess",
"messageCheckFailure",
"dialFailure",
"storeconfirmationfailed",
"schema_migrations",
}

Expand Down Expand Up @@ -94,6 +95,11 @@ func DropTables(db *sql.DB) {
log.Fatalf("an error '%s' was not expected when dropping the index", err)
}

_, err = tx.Exec("DROP INDEX IF EXISTS storeConfirmationFailed_unique")
if err != nil {
log.Fatalf("an error '%s' was not expected when dropping the index", err)
}

_, err = tx.Exec("DROP INDEX IF EXISTS peerCountByOrigin_unique")
if err != nil {
log.Fatalf("an error '%s' was not expected when dropping the index", err)
Expand Down
115 changes: 69 additions & 46 deletions lib/database/bindata.go

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

15 changes: 15 additions & 0 deletions lib/database/sql/000017_store_confirmation_failure.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE IF NOT EXISTS storeConfirmationFailed (
id SERIAL PRIMARY KEY,
messageHash TEXT NOT NULL,
recordId INTEGER NOT NULL
);

ALTER TABLE storeConfirmationFailed ADD CONSTRAINT fk_storeConfirmationFailed_telemetryRecord
FOREIGN KEY (recordId) REFERENCES telemetryRecord(id);

ALTER TABLE storeConfirmationFailed
ADD CONSTRAINT storeConfirmationFailed_unique
UNIQUE (
recordId,
messageHash
);
59 changes: 59 additions & 0 deletions lib/metrics/store_confirmation_failure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package metrics

import (
"context"
"database/sql"
"encoding/json"
"fmt"
"log"

"github.com/status-im/telemetry/lib/common"
"github.com/status-im/telemetry/pkg/types"
)

type StoreConfirmationFailed struct {
types.StoreConfirmationFailed
}

func (s *StoreConfirmationFailed) Process(ctx context.Context, db *sql.DB, errs *common.MetricErrors, data *types.TelemetryRequest) error {
if err := json.Unmarshal(*data.TelemetryData, &s); err != nil {
errs.Append(data.ID, fmt.Sprintf("Error decoding store confirmation failed: %v", err))
return err
}

if err := s.Put(ctx, db); err != nil {
errs.Append(data.ID, fmt.Sprintf("Error saving store confirmation failed: %v", err))
return err
}

log.Printf("AK: store confirmation metric saved: %v", s)
return nil
}

func (s *StoreConfirmationFailed) Put(ctx context.Context, db *sql.DB) error {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()

recordId, err := InsertTelemetryRecord(tx, &s.TelemetryRecord)
if err != nil {
return err
}
result := tx.QueryRow("INSERT INTO storeConfirmationFailed (recordId, messageHash) VALUES ($1, $2) RETURNING id;", recordId, s.MessageHash)
if result.Err() != nil {
return result.Err()
}

err = result.Scan(&s.ID)
if err != nil {
return err
}

return nil
}

func (s *StoreConfirmationFailed) Clean(db *sql.DB, before int64) (int64, error) {
return common.Cleanup(db, "storeConfirmationFailed", before)
}
33 changes: 20 additions & 13 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import "encoding/json"
type TelemetryType string

const (
ProtocolStatsMetric TelemetryType = "ProtocolStats"
ReceivedEnvelopeMetric TelemetryType = "ReceivedEnvelope"
SentEnvelopeMetric TelemetryType = "SentEnvelope"
UpdateEnvelopeMetric TelemetryType = "UpdateEnvelope"
ReceivedMessagesMetric TelemetryType = "ReceivedMessages"
ErrorSendingEnvelopeMetric TelemetryType = "ErrorSendingEnvelope"
PeerCountMetric TelemetryType = "PeerCount"
PeerConnFailureMetric TelemetryType = "PeerConnFailure"
PeerCountByShardMetric TelemetryType = "PeerCountByShard"
PeerCountByOriginMetric TelemetryType = "PeerCountByOrigin"
MessageCheckSuccessMetric TelemetryType = "MessageCheckSuccess"
MessageCheckFailureMetric TelemetryType = "MessageCheckFailure"
DialFailureMetric TelemetryType = "DialFailure"
ProtocolStatsMetric TelemetryType = "ProtocolStats"
ReceivedEnvelopeMetric TelemetryType = "ReceivedEnvelope"
SentEnvelopeMetric TelemetryType = "SentEnvelope"
UpdateEnvelopeMetric TelemetryType = "UpdateEnvelope"
ReceivedMessagesMetric TelemetryType = "ReceivedMessages"
ErrorSendingEnvelopeMetric TelemetryType = "ErrorSendingEnvelope"
PeerCountMetric TelemetryType = "PeerCount"
PeerConnFailureMetric TelemetryType = "PeerConnFailure"
PeerCountByShardMetric TelemetryType = "PeerCountByShard"
PeerCountByOriginMetric TelemetryType = "PeerCountByOrigin"
MessageCheckSuccessMetric TelemetryType = "MessageCheckSuccess"
MessageCheckFailureMetric TelemetryType = "MessageCheckFailure"
DialFailureMetric TelemetryType = "DialFailure"
StoreConfirmationFailedMetric TelemetryType = "StoreConfirmationFailed"
)

type Origin int64
Expand Down Expand Up @@ -168,3 +169,9 @@ type DialFailure struct {
Protocols string `json:"protocols"`
Timestamp int64 `json:"timestamp"`
}
type StoreConfirmationFailed struct {
TelemetryRecord
ID int `json:"id"`
MessageHash string `json:"messageHash"`
Timestamp int64 `json:"timestamp"`
}

0 comments on commit 2a29886

Please sign in to comment.