2021-03-15 13:01:35 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-15 13:04:13 +00:00
|
|
|
"time"
|
2021-03-15 13:01:35 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/object"
|
2021-03-15 13:07:20 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/util"
|
2021-03-15 13:01:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
MetricCollector struct {
|
2021-03-16 08:14:56 +00:00
|
|
|
next ServiceServer
|
|
|
|
metrics MetricRegister
|
2021-03-15 13:01:35 +00:00
|
|
|
}
|
2021-03-15 13:07:20 +00:00
|
|
|
|
|
|
|
getStreamMetric struct {
|
|
|
|
util.ServerStream
|
2021-03-16 08:14:56 +00:00
|
|
|
stream GetObjectStream
|
|
|
|
metrics MetricRegister
|
2021-03-15 13:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
putStreamMetric struct {
|
2021-03-15 10:53:08 +00:00
|
|
|
stream PutObjectStream
|
2021-03-16 08:14:56 +00:00
|
|
|
metrics MetricRegister
|
2021-03-16 10:58:38 +00:00
|
|
|
start time.Time
|
2021-03-15 13:07:20 +00:00
|
|
|
}
|
2021-03-15 13:04:13 +00:00
|
|
|
|
2021-03-16 08:14:56 +00:00
|
|
|
MetricRegister interface {
|
|
|
|
IncGetReqCounter()
|
|
|
|
IncPutReqCounter()
|
|
|
|
IncHeadReqCounter()
|
|
|
|
IncSearchReqCounter()
|
|
|
|
IncDeleteReqCounter()
|
|
|
|
IncRangeReqCounter()
|
|
|
|
IncRangeHashReqCounter()
|
|
|
|
|
|
|
|
AddGetReqDuration(time.Duration)
|
|
|
|
AddPutReqDuration(time.Duration)
|
|
|
|
AddHeadReqDuration(time.Duration)
|
|
|
|
AddSearchReqDuration(time.Duration)
|
|
|
|
AddDeleteReqDuration(time.Duration)
|
|
|
|
AddRangeReqDuration(time.Duration)
|
|
|
|
AddRangeHashReqDuration(time.Duration)
|
|
|
|
|
|
|
|
AddPutPayload(int)
|
|
|
|
AddGetPayload(int)
|
|
|
|
}
|
2021-03-15 13:07:20 +00:00
|
|
|
)
|
|
|
|
|
2021-03-16 08:14:56 +00:00
|
|
|
func NewMetricCollector(next ServiceServer, register MetricRegister) *MetricCollector {
|
2021-03-15 13:01:35 +00:00
|
|
|
return &MetricCollector{
|
2021-03-16 08:14:56 +00:00
|
|
|
next: next,
|
|
|
|
metrics: register,
|
2021-03-15 13:01:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MetricCollector) Get(req *object.GetRequest, stream GetObjectStream) error {
|
2021-03-15 13:04:13 +00:00
|
|
|
t := time.Now()
|
2021-03-15 13:01:35 +00:00
|
|
|
defer func() {
|
2021-03-16 08:14:56 +00:00
|
|
|
m.metrics.IncGetReqCounter()
|
|
|
|
m.metrics.AddGetReqDuration(time.Since(t))
|
2021-03-15 13:01:35 +00:00
|
|
|
}()
|
|
|
|
|
2021-03-15 13:07:20 +00:00
|
|
|
return m.next.Get(req, &getStreamMetric{
|
|
|
|
ServerStream: stream,
|
|
|
|
stream: stream,
|
2021-03-16 08:14:56 +00:00
|
|
|
metrics: m.metrics,
|
2021-03-15 13:07:20 +00:00
|
|
|
})
|
2021-03-15 13:01:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 10:53:08 +00:00
|
|
|
func (m MetricCollector) Put(ctx context.Context) (PutObjectStream, error) {
|
2021-03-15 13:04:13 +00:00
|
|
|
t := time.Now()
|
2021-03-15 13:01:35 +00:00
|
|
|
|
2021-03-15 13:07:20 +00:00
|
|
|
stream, err := m.next.Put(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-16 08:14:56 +00:00
|
|
|
return &putStreamMetric{
|
|
|
|
stream: stream,
|
|
|
|
metrics: m.metrics,
|
2021-03-16 10:58:38 +00:00
|
|
|
start: t,
|
2021-03-16 08:14:56 +00:00
|
|
|
}, nil
|
2021-03-15 13:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m MetricCollector) Head(ctx context.Context, request *object.HeadRequest) (*object.HeadResponse, error) {
|
2021-03-15 13:04:13 +00:00
|
|
|
t := time.Now()
|
2021-03-15 13:01:35 +00:00
|
|
|
defer func() {
|
2021-03-16 08:14:56 +00:00
|
|
|
m.metrics.IncHeadReqCounter()
|
|
|
|
m.metrics.AddHeadReqDuration(time.Since(t))
|
2021-03-15 13:01:35 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return m.next.Head(ctx, request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MetricCollector) Search(req *object.SearchRequest, stream SearchStream) error {
|
2021-03-15 13:04:13 +00:00
|
|
|
t := time.Now()
|
2021-03-15 13:01:35 +00:00
|
|
|
defer func() {
|
2021-03-16 08:14:56 +00:00
|
|
|
m.metrics.IncSearchReqCounter()
|
|
|
|
m.metrics.AddSearchReqDuration(time.Since(t))
|
2021-03-15 13:01:35 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return m.next.Search(req, stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MetricCollector) Delete(ctx context.Context, request *object.DeleteRequest) (*object.DeleteResponse, error) {
|
2021-03-15 13:04:13 +00:00
|
|
|
t := time.Now()
|
2021-03-15 13:01:35 +00:00
|
|
|
defer func() {
|
2021-03-16 08:14:56 +00:00
|
|
|
m.metrics.IncDeleteReqCounter()
|
|
|
|
m.metrics.AddDeleteReqDuration(time.Since(t))
|
2021-03-15 13:01:35 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return m.next.Delete(ctx, request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MetricCollector) GetRange(req *object.GetRangeRequest, stream GetObjectRangeStream) error {
|
2021-03-15 13:04:13 +00:00
|
|
|
t := time.Now()
|
2021-03-15 13:01:35 +00:00
|
|
|
defer func() {
|
2021-03-16 08:14:56 +00:00
|
|
|
m.metrics.IncRangeReqCounter()
|
|
|
|
m.metrics.AddRangeReqDuration(time.Since(t))
|
2021-03-15 13:01:35 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return m.next.GetRange(req, stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MetricCollector) GetRangeHash(ctx context.Context, request *object.GetRangeHashRequest) (*object.GetRangeHashResponse, error) {
|
2021-03-15 13:04:13 +00:00
|
|
|
t := time.Now()
|
2021-03-15 13:01:35 +00:00
|
|
|
defer func() {
|
2021-03-16 08:14:56 +00:00
|
|
|
m.metrics.IncRangeHashReqCounter()
|
|
|
|
m.metrics.AddRangeHashReqDuration(time.Since(t))
|
2021-03-15 13:01:35 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return m.next.GetRangeHash(ctx, request)
|
|
|
|
}
|
2021-03-15 13:07:20 +00:00
|
|
|
|
|
|
|
func (s getStreamMetric) Send(resp *object.GetResponse) error {
|
|
|
|
chunk, ok := resp.GetBody().GetObjectPart().(*object.GetObjectPartChunk)
|
|
|
|
if ok {
|
2021-03-16 08:14:56 +00:00
|
|
|
s.metrics.AddGetPayload(len(chunk.GetChunk()))
|
2021-03-15 13:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.stream.Send(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s putStreamMetric) Send(req *object.PutRequest) error {
|
|
|
|
chunk, ok := req.GetBody().GetObjectPart().(*object.PutObjectPartChunk)
|
|
|
|
if ok {
|
2021-03-16 08:14:56 +00:00
|
|
|
s.metrics.AddPutPayload(len(chunk.GetChunk()))
|
2021-03-15 13:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.stream.Send(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s putStreamMetric) CloseAndRecv() (*object.PutResponse, error) {
|
2021-03-16 10:58:38 +00:00
|
|
|
defer func() {
|
2022-11-29 11:47:00 +00:00
|
|
|
s.metrics.IncPutReqCounter()
|
2021-03-16 10:58:38 +00:00
|
|
|
s.metrics.AddPutReqDuration(time.Since(s.start))
|
|
|
|
}()
|
|
|
|
|
2021-03-15 13:07:20 +00:00
|
|
|
return s.stream.CloseAndRecv()
|
|
|
|
}
|