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
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-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
|
2022-11-24 11:36:49 +00:00
|
|
|
enabled bool
|
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 {
|
2023-06-14 07:05:51 +00:00
|
|
|
AddRequestDuration(string, time.Duration, bool)
|
|
|
|
AddPayloadSize(string, int)
|
2021-03-16 08:14:56 +00:00
|
|
|
}
|
2021-03-15 13:07:20 +00:00
|
|
|
)
|
|
|
|
|
2022-11-24 11:36:49 +00:00
|
|
|
func NewMetricCollector(next ServiceServer, register MetricRegister, enabled bool) *MetricCollector {
|
2021-03-15 13:01:35 +00:00
|
|
|
return &MetricCollector{
|
2021-03-16 08:14:56 +00:00
|
|
|
next: next,
|
|
|
|
metrics: register,
|
2022-11-24 11:36:49 +00:00
|
|
|
enabled: enabled,
|
2021-03-15 13:01:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 11:51:19 +00:00
|
|
|
func (m MetricCollector) Get(req *object.GetRequest, stream GetObjectStream) (err error) {
|
2022-11-24 11:36:49 +00:00
|
|
|
if m.enabled {
|
|
|
|
t := time.Now()
|
|
|
|
defer func() {
|
2023-06-14 07:05:51 +00:00
|
|
|
m.metrics.AddRequestDuration("Get", time.Since(t), err == nil)
|
2022-11-24 11:36:49 +00:00
|
|
|
}()
|
|
|
|
err = m.next.Get(req, &getStreamMetric{
|
|
|
|
ServerStream: stream,
|
|
|
|
stream: stream,
|
|
|
|
metrics: m.metrics,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
err = m.next.Get(req, stream)
|
|
|
|
}
|
2022-11-29 11:51:19 +00:00
|
|
|
return
|
2021-03-15 13:01:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
func (m MetricCollector) Put() (PutObjectStream, error) {
|
2022-11-24 11:36:49 +00:00
|
|
|
if m.enabled {
|
|
|
|
t := time.Now()
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
stream, err := m.next.Put()
|
2022-11-24 11:36:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &putStreamMetric{
|
|
|
|
stream: stream,
|
|
|
|
metrics: m.metrics,
|
|
|
|
start: t,
|
|
|
|
}, nil
|
2021-03-15 13:07:20 +00:00
|
|
|
}
|
2023-04-03 11:23:53 +00:00
|
|
|
return m.next.Put()
|
2021-03-15 13:01:35 +00:00
|
|
|
}
|
|
|
|
|
2023-07-03 08:36:20 +00:00
|
|
|
func (m MetricCollector) PutSingle(ctx context.Context, request *object.PutSingleRequest) (*object.PutSingleResponse, error) {
|
|
|
|
if m.enabled {
|
|
|
|
t := time.Now()
|
|
|
|
|
|
|
|
res, err := m.next.PutSingle(ctx, request)
|
|
|
|
|
|
|
|
m.metrics.AddRequestDuration("PutSingle", time.Since(t), err == nil)
|
|
|
|
if err == nil {
|
|
|
|
m.metrics.AddPayloadSize("PutSingle", len(request.GetBody().GetObject().GetPayload()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
return m.next.PutSingle(ctx, request)
|
|
|
|
}
|
|
|
|
|
2021-03-15 13:01:35 +00:00
|
|
|
func (m MetricCollector) Head(ctx context.Context, request *object.HeadRequest) (*object.HeadResponse, error) {
|
2022-11-24 11:36:49 +00:00
|
|
|
if m.enabled {
|
|
|
|
t := time.Now()
|
2021-03-15 13:01:35 +00:00
|
|
|
|
2022-11-24 11:36:49 +00:00
|
|
|
res, err := m.next.Head(ctx, request)
|
2022-11-29 11:51:19 +00:00
|
|
|
|
2023-06-14 07:05:51 +00:00
|
|
|
m.metrics.AddRequestDuration("Head", time.Since(t), err == nil)
|
2022-11-29 11:51:19 +00:00
|
|
|
|
2022-11-24 11:36:49 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
return m.next.Head(ctx, request)
|
2021-03-15 13:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m MetricCollector) Search(req *object.SearchRequest, stream SearchStream) error {
|
2022-11-24 11:36:49 +00:00
|
|
|
if m.enabled {
|
|
|
|
t := time.Now()
|
2021-03-15 13:01:35 +00:00
|
|
|
|
2022-11-24 11:36:49 +00:00
|
|
|
err := m.next.Search(req, stream)
|
2022-11-29 11:51:19 +00:00
|
|
|
|
2023-06-14 07:05:51 +00:00
|
|
|
m.metrics.AddRequestDuration("Search", time.Since(t), err == nil)
|
2022-11-29 11:51:19 +00:00
|
|
|
|
2022-11-24 11:36:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return m.next.Search(req, stream)
|
2021-03-15 13:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m MetricCollector) Delete(ctx context.Context, request *object.DeleteRequest) (*object.DeleteResponse, error) {
|
2022-11-24 11:36:49 +00:00
|
|
|
if m.enabled {
|
|
|
|
t := time.Now()
|
2021-03-15 13:01:35 +00:00
|
|
|
|
2022-11-24 11:36:49 +00:00
|
|
|
res, err := m.next.Delete(ctx, request)
|
2022-11-29 11:51:19 +00:00
|
|
|
|
2023-06-14 07:05:51 +00:00
|
|
|
m.metrics.AddRequestDuration("Delete", time.Since(t), err == nil)
|
2022-11-24 11:36:49 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
return m.next.Delete(ctx, request)
|
2021-03-15 13:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m MetricCollector) GetRange(req *object.GetRangeRequest, stream GetObjectRangeStream) error {
|
2022-11-24 11:36:49 +00:00
|
|
|
if m.enabled {
|
|
|
|
t := time.Now()
|
2021-03-15 13:01:35 +00:00
|
|
|
|
2022-11-24 11:36:49 +00:00
|
|
|
err := m.next.GetRange(req, stream)
|
2022-11-29 11:51:19 +00:00
|
|
|
|
2023-06-14 07:05:51 +00:00
|
|
|
m.metrics.AddRequestDuration("GetRange", time.Since(t), err == nil)
|
2022-11-29 11:51:19 +00:00
|
|
|
|
2022-11-24 11:36:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return m.next.GetRange(req, stream)
|
2021-03-15 13:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m MetricCollector) GetRangeHash(ctx context.Context, request *object.GetRangeHashRequest) (*object.GetRangeHashResponse, error) {
|
2022-11-24 11:36:49 +00:00
|
|
|
if m.enabled {
|
|
|
|
t := time.Now()
|
2021-03-15 13:01:35 +00:00
|
|
|
|
2022-11-24 11:36:49 +00:00
|
|
|
res, err := m.next.GetRangeHash(ctx, request)
|
2022-11-29 11:51:19 +00:00
|
|
|
|
2023-06-14 07:05:51 +00:00
|
|
|
m.metrics.AddRequestDuration("GetRangeHash", time.Since(t), err == nil)
|
2022-11-29 11:51:19 +00:00
|
|
|
|
2022-11-24 11:36:49 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
return m.next.GetRangeHash(ctx, request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MetricCollector) Enable() {
|
|
|
|
m.enabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MetricCollector) Disable() {
|
|
|
|
m.enabled = false
|
2021-03-15 13:01:35 +00:00
|
|
|
}
|
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 {
|
2023-06-14 07:05:51 +00:00
|
|
|
s.metrics.AddPayloadSize("Get", len(chunk.GetChunk()))
|
2021-03-15 13:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.stream.Send(resp)
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
func (s putStreamMetric) Send(ctx context.Context, req *object.PutRequest) error {
|
2021-03-15 13:07:20 +00:00
|
|
|
chunk, ok := req.GetBody().GetObjectPart().(*object.PutObjectPartChunk)
|
|
|
|
if ok {
|
2023-06-14 07:05:51 +00:00
|
|
|
s.metrics.AddPayloadSize("Put", len(chunk.GetChunk()))
|
2021-03-15 13:07:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
return s.stream.Send(ctx, req)
|
2021-03-15 13:07:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
func (s putStreamMetric) CloseAndRecv(ctx context.Context) (*object.PutResponse, error) {
|
|
|
|
res, err := s.stream.CloseAndRecv(ctx)
|
2022-11-29 11:51:19 +00:00
|
|
|
|
2023-06-14 07:05:51 +00:00
|
|
|
s.metrics.AddRequestDuration("Put", time.Since(s.start), err == nil)
|
2021-03-16 10:58:38 +00:00
|
|
|
|
2022-11-29 11:51:19 +00:00
|
|
|
return res, err
|
2021-03-15 13:07:20 +00:00
|
|
|
}
|