Skip to content

Commit

Permalink
metrics,sqlite,rhp: add RHP designation for future-proofing
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Dec 14, 2023
1 parent cdbd511 commit 7e004f9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
5 changes: 5 additions & 0 deletions host/metrics/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ type (

// DataMetrics is a collection of metrics related to data usage.
DataMetrics struct {
RHP RHPData `json:"rhp"`
}

// RHPData is a collection of data metrics related to the RHP.
RHPData struct {
// Ingress returns the number of bytes received by the host.
Ingress uint64 `json:"ingress"`
// Egress returns the number of bytes sent by the host.
Expand Down
28 changes: 14 additions & 14 deletions persist/sqlite/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ const (
metricRegistryWrites = "registryWrites"

// bandwidth
metricDataIngress = "dataIngress"
metricDataEgress = "dataEgress"
metricDataRHPIngress = "dataIngress"
metricDataRHPEgress = "dataEgress"

// metricRHP2Ingress
// Deprecated: combined into metricDataIngress
// Deprecated: combined into metricDataRHPIngress
metricRHP2Ingress = "rhp2Ingress"
// metricRHP2Egress
// Deprecated: combined into metricDataEgress
// Deprecated: combined into metricDataRHPEgress
metricRHP2Egress = "rhp2Egress"
// metricRHP3Ingress
// Deprecated: combined into metricDataIngress
// Deprecated: combined into metricDataRHPIngress
metricRHP3Ingress = "rhp3Ingress"
// metricRHP3Egress
// Deprecated: combined into metricDataEgress
// Deprecated: combined into metricDataRHPEgress
metricRHP3Egress = "rhp3Egress"

// pricing
Expand Down Expand Up @@ -238,16 +238,16 @@ JOIN (
return
}

// IncrementRHP3DataUsage increments the RHP3 ingress and egress metrics.
func (s *Store) IncrementDataUsage(ingress, egress uint64) error {
// IncrementRHPDataUsage increments the RHP3 ingress and egress metrics.
func (s *Store) IncrementRHPDataUsage(ingress, egress uint64) error {
return s.transaction(func(tx txn) error {
if ingress > 0 {
if err := incrementNumericStat(tx, metricDataIngress, int(ingress), time.Now()); err != nil {
if err := incrementNumericStat(tx, metricDataRHPIngress, int(ingress), time.Now()); err != nil {
return fmt.Errorf("failed to track ingress: %w", err)
}
}
if egress > 0 {
if err := incrementNumericStat(tx, metricDataEgress, int(egress), time.Now()); err != nil {
if err := incrementNumericStat(tx, metricDataRHPEgress, int(egress), time.Now()); err != nil {
return fmt.Errorf("failed to track egress: %w", err)
}
}
Expand Down Expand Up @@ -384,10 +384,10 @@ func mustParseMetricValue(stat string, buf []byte, m *metrics.Metrics) {
case metricRegistryWrites:
m.Registry.Writes = mustScanUint64(buf)
// bandwidth
case metricDataIngress:
m.Data.Ingress = mustScanUint64(buf)
case metricDataEgress:
m.Data.Egress = mustScanUint64(buf)
case metricDataRHPIngress:
m.Data.RHP.Ingress = mustScanUint64(buf)
case metricDataRHPEgress:
m.Data.RHP.Egress = mustScanUint64(buf)
// potential revenue
case metricPotentialRPCRevenue:
m.Revenue.Potential.RPC = mustScanCurrency(buf)
Expand Down
16 changes: 8 additions & 8 deletions persist/sqlite/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,26 @@ func migrateVersion24(tx txn, log *zap.Logger) error {
switch stat {
case metricRHP2Ingress:
lastRHP2Ingress = value
stat = metricDataIngress
stat = metricDataRHPIngress
case metricRHP2Egress:
lastRHP2Egress = value
stat = metricDataEgress
stat = metricDataRHPEgress
case metricRHP3Ingress:
lastRHP3Ingress = value
stat = metricDataIngress
stat = metricDataRHPIngress
case metricRHP3Egress:
lastRHP3Egress = value
stat = metricDataEgress
stat = metricDataRHPEgress
}

switch stat {
case metricDataIngress:
case metricDataRHPIngress:
if len(dataPointsIngress) == 0 || !dataPointsIngress[len(dataPointsIngress)-1].timestamp.Equal(timestamp) {
dataPointsIngress = append(dataPointsIngress, combinedStat{timestamp, lastRHP2Ingress + lastRHP3Ingress})
} else {
dataPointsIngress[len(dataPointsIngress)-1].value = lastRHP2Ingress + lastRHP3Ingress
}
case metricDataEgress:
case metricDataRHPEgress:
if len(dataPointsEgress) == 0 || !dataPointsEgress[len(dataPointsEgress)-1].timestamp.Equal(timestamp) {
dataPointsEgress = append(dataPointsEgress, combinedStat{timestamp, lastRHP2Egress + lastRHP3Egress})
} else {
Expand All @@ -73,14 +73,14 @@ func migrateVersion24(tx txn, log *zap.Logger) error {

// add the new data points
for _, dataPoint := range dataPointsIngress {
if err := setNumericStat(tx, metricDataIngress, dataPoint.value, dataPoint.timestamp); err != nil {
if err := setNumericStat(tx, metricDataRHPIngress, dataPoint.value, dataPoint.timestamp); err != nil {
return fmt.Errorf("failed to set data ingress metric: %w", err)
}
log.Debug("added ingress datapoint", zap.Time("timestamp", dataPoint.timestamp), zap.Uint64("value", dataPoint.value))
}

for _, dataPoint := range dataPointsEgress {
if err := setNumericStat(tx, metricDataEgress, dataPoint.value, dataPoint.timestamp); err != nil {
if err := setNumericStat(tx, metricDataRHPEgress, dataPoint.value, dataPoint.timestamp); err != nil {
return fmt.Errorf("failed to set data egress metric: %w", err)
}
log.Debug("added egress datapoint", zap.Time("timestamp", dataPoint.timestamp), zap.Uint64("value", dataPoint.value))
Expand Down
4 changes: 2 additions & 2 deletions rhp/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const persistInterval = time.Minute
type (
// A DataRecorderStore persists data usage
DataRecorderStore interface {
IncrementDataUsage(ingress, egress uint64) error
IncrementRHPDataUsage(ingress, egress uint64) error
}

// A DataRecorder records the amount of data read and written across
Expand Down Expand Up @@ -59,7 +59,7 @@ func (dr *DataRecorder) persistUsage() {
return
}

if err := dr.store.IncrementDataUsage(r, w); err != nil {
if err := dr.store.IncrementRHPDataUsage(r, w); err != nil {
dr.log.Error("failed to persist data usage", zap.Error(err))
return
}
Expand Down

0 comments on commit 7e004f9

Please sign in to comment.