Skip to content
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

feat: handle peer count metric #25

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

35 changes: 35 additions & 0 deletions telemetry/peer_count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package telemetry

import (
"database/sql"
"time"
)

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

func (r *PeerCount) put(db *sql.DB) error {
stmt, err := db.Prepare("INSERT INTO peerCount (timestamp, nodeName, nodeKeyUid, peerCount, 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.PeerCount, 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 @@ -66,6 +66,7 @@ const (
UpdateEnvelopeMetric TelemetryType = "UpdateEnvelope"
ReceivedMessagesMetric TelemetryType = "ReceivedMessages"
ErrorSendingEnvelopeMetric TelemetryType = "ErrorSendingEnvelope"
PeerCountMetric TelemetryType = "PeerCount"
)

type TelemetryRequest struct {
Expand Down Expand Up @@ -128,6 +129,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 error sending envelope: %v", err)})
continue
}
case PeerCountMetric:
var peerCount PeerCount
if err := json.Unmarshal(*data.TelemetryData, &peerCount); err != nil {
errorDetails = append(errorDetails, map[string]interface{}{"Id": data.Id, "Error": fmt.Sprintf("Error decoding peer count: %v", err)})
continue
}
if err := peerCount.put(s.DB); err != nil {
errorDetails = append(errorDetails, map[string]interface{}{"Id": data.Id, "Error": fmt.Sprintf("Error saving peer count: %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/000009_peer_count.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS peerCount (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would we have table per metric?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is what we have now - each metric type gets own table - receivedEnvelopes, pushedEnvelopes...

https://github.com/status-im/telemetry/blob/master/telemetry/sql/000005_pushed_envelope.up.sql

id SERIAL PRIMARY KEY,
createdAt INTEGER NOT NULL,
peerCount INTEGER NOT NULL,
nodeName VARCHAR(255) NOT NULL,
nodeKeyUid VARCHAR(255) NOT NULL,
timestamp INTEGER NOT NULL,
statusVersion VARCHAR(31),
CONSTRAINT peerCount_unique unique(timestamp, nodeName, nodeKeyUid, statusVersion)
);
Loading