storj/satellite/metrics/counter.go
Michal Niewrzal 6cc2052f47 satellite: fix segment loop observers metrics
We made optimization for segment loop observers to avoid
heavy monkit initialization on each call. It was applied to very
often executed methods. Unfortunately we used wrong monkit
method to track function times. Instead mon.Task we used
mon.Func().

https://github.com/spacemonkeygo/monkit#how-it-works

Change-Id: I9ca454dbd828c6b43ba09ca75c341991d2fd73a8
2022-08-10 14:13:16 +00:00

88 lines
2.1 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package metrics
import (
"context"
"storj.io/common/uuid"
"storj.io/storj/satellite/metabase/segmentloop"
)
var (
remoteSegmentFunc = mon.Task()
inlineSegmentFunc = mon.Task()
)
// Counter implements the segment loop observer interface for data science metrics collection.
//
// architecture: Observer
type Counter struct {
// number of objects that has at least one remote segment
RemoteObjects int64
// number of objects that has all inline segments
InlineObjects int64
// encrypted size
TotalInlineBytes int64
// encrypted size
TotalRemoteBytes int64
TotalInlineSegments int64
TotalRemoteSegments int64
lastStreamID uuid.UUID
onlyInline bool
}
// NewCounter instantiates a new counter to be subscribed to the metainfo loop.
func NewCounter() *Counter {
return &Counter{
onlyInline: true,
}
}
// LoopStarted is called at each start of a loop.
func (counter *Counter) LoopStarted(context.Context, segmentloop.LoopInfo) (err error) {
return nil
}
// RemoteSegment increments the count for objects with remote segments.
func (counter *Counter) RemoteSegment(ctx context.Context, segment *segmentloop.Segment) error {
defer remoteSegmentFunc(&ctx)(nil) // method always returns nil
counter.onlyInline = false
counter.TotalRemoteBytes += int64(segment.EncryptedSize)
counter.TotalRemoteSegments++
if counter.lastStreamID.Compare(segment.StreamID) != 0 {
counter.RemoteObjects++
counter.lastStreamID = segment.StreamID
counter.onlyInline = true
}
return nil
}
// InlineSegment increments the count for inline objects.
func (counter *Counter) InlineSegment(ctx context.Context, segment *segmentloop.Segment) error {
defer inlineSegmentFunc(&ctx)(nil) // method always returns nil
counter.TotalInlineBytes += int64(segment.EncryptedSize)
counter.TotalInlineSegments++
if counter.lastStreamID.Compare(segment.StreamID) != 0 {
if counter.onlyInline {
counter.InlineObjects++
} else {
counter.RemoteObjects++
}
counter.lastStreamID = segment.StreamID
counter.onlyInline = true
}
return nil
}