storj/storagenode/inspector/inspector.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

130 lines
3.7 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package inspector
import (
"context"
"net"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/pb"
"storj.io/common/rpc/rpcstatus"
"storj.io/storj/storagenode/bandwidth"
"storj.io/storj/storagenode/contact"
"storj.io/storj/storagenode/pieces"
"storj.io/storj/storagenode/piecestore"
)
var (
mon = monkit.Package()
// Error is the default error class for piecestore monitor errors
Error = errs.Class("piecestore inspector")
)
// Endpoint does inspectory things
//
// architecture: Endpoint
type Endpoint struct {
log *zap.Logger
pieceStore *pieces.Store
contact *contact.Service
pingStats *contact.PingStats
usageDB bandwidth.DB
startTime time.Time
pieceStoreConfig piecestore.OldConfig
dashboardAddress net.Addr
externalAddress string
}
// NewEndpoint creates piecestore inspector instance
func NewEndpoint(
log *zap.Logger,
pieceStore *pieces.Store,
contact *contact.Service,
pingStats *contact.PingStats,
usageDB bandwidth.DB,
pieceStoreConfig piecestore.OldConfig,
dashboardAddress net.Addr,
externalAddress string) *Endpoint {
return &Endpoint{
log: log,
pieceStore: pieceStore,
contact: contact,
pingStats: pingStats,
usageDB: usageDB,
pieceStoreConfig: pieceStoreConfig,
dashboardAddress: dashboardAddress,
startTime: time.Now(),
externalAddress: externalAddress,
}
}
// Stats returns current statistics about the storage node
func (inspector *Endpoint) Stats(ctx context.Context, in *pb.StatsRequest) (out *pb.StatSummaryResponse, err error) {
defer mon.Task()(&ctx)(&err)
return inspector.retrieveStats(ctx)
}
func (inspector *Endpoint) retrieveStats(ctx context.Context) (_ *pb.StatSummaryResponse, err error) {
defer mon.Task()(&ctx)(&err)
// Space Usage
_, piecesContentSize, err := inspector.pieceStore.SpaceUsedForPieces(ctx)
if err != nil {
return nil, rpcstatus.Error(rpcstatus.Internal, err.Error())
}
usage, err := bandwidth.TotalMonthlySummary(ctx, inspector.usageDB)
if err != nil {
return nil, rpcstatus.Error(rpcstatus.Internal, err.Error())
}
ingress := usage.Put + usage.PutRepair
egress := usage.Get + usage.GetAudit + usage.GetRepair
totalUsedBandwidth := usage.Total()
return &pb.StatSummaryResponse{
UsedSpace: piecesContentSize,
AvailableSpace: inspector.pieceStoreConfig.AllocatedDiskSpace.Int64() - piecesContentSize,
UsedIngress: ingress,
UsedEgress: egress,
UsedBandwidth: totalUsedBandwidth,
AvailableBandwidth: inspector.pieceStoreConfig.AllocatedBandwidth.Int64() - totalUsedBandwidth,
}, nil
}
// Dashboard returns dashboard information
func (inspector *Endpoint) Dashboard(ctx context.Context, in *pb.DashboardRequest) (out *pb.DashboardResponse, err error) {
defer mon.Task()(&ctx)(&err)
return inspector.getDashboardData(ctx)
}
func (inspector *Endpoint) getDashboardData(ctx context.Context) (_ *pb.DashboardResponse, err error) {
defer mon.Task()(&ctx)(&err)
statsSummary, err := inspector.retrieveStats(ctx)
if err != nil {
return nil, rpcstatus.Error(rpcstatus.Internal, err.Error())
}
lastPingedAt := inspector.pingStats.WhenLastPinged()
return &pb.DashboardResponse{
NodeId: inspector.contact.Local().Id,
InternalAddress: "",
ExternalAddress: inspector.contact.Local().Address.Address,
LastPinged: lastPingedAt,
DashboardAddress: inspector.dashboardAddress.String(),
Uptime: ptypes.DurationProto(time.Since(inspector.startTime)),
Stats: statsSummary,
}, nil
}