storj/storagenode/bandwidth/service.go
Jeff Wendling 7999d24f81 all: use monkit v3
this commit updates our monkit dependency to the v3 version where
it outputs in an influx style. this makes discovery much easier
as many tools are built to look at it this way.

graphite and rothko will suffer some due to no longer being a tree
based on dots. hopefully time will exist to update rothko to
index based on the new metric format.

it adds an influx output for the statreceiver so that we can
write to influxdb v1 or v2 directly.

Change-Id: Iae9f9494a6d29cfbd1f932a5e71a891b490415ff
2020-02-05 23:53:17 +00:00

65 lines
1.5 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
// Package bandwidth implements bandwidth usage rollup loop.
package bandwidth
import (
"context"
"time"
"github.com/spacemonkeygo/monkit/v3"
"go.uber.org/zap"
"storj.io/common/sync2"
)
var mon = monkit.Package()
// Config defines parameters for storage node Collector.
type Config struct {
Interval time.Duration `help:"how frequently bandwidth usage rollups are calculated" default:"1h0m0s"`
}
// Service implements
//
// architecture: Chore
type Service struct {
log *zap.Logger
db DB
Loop *sync2.Cycle
}
// NewService creates a new bandwidth service.
func NewService(log *zap.Logger, db DB, config Config) *Service {
return &Service{
log: log,
db: db,
Loop: sync2.NewCycle(config.Interval),
}
}
// Run starts the background process for rollups of bandwidth usage
func (service *Service) Run(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
return service.Loop.Run(ctx, service.Rollup)
}
// Rollup calls bandwidth DB Rollup method and logs any errors
func (service *Service) Rollup(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
service.log.Info("Performing bandwidth usage rollups")
err = service.db.Rollup(ctx)
if err != nil {
service.log.Error("Could not rollup bandwidth usage", zap.Error(err))
}
return nil
}
// Close stops the background process for rollups of bandwidth usage
func (service *Service) Close() (err error) {
service.Loop.Close()
return nil
}