2023-07-05 14:04:52 +00:00
|
|
|
package middleware
|
2023-04-07 14:28:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2023-04-07 14:28:21 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/metrics"
|
2024-03-01 14:07:08 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-07-05 14:04:52 +00:00
|
|
|
"go.uber.org/zap"
|
2023-04-07 14:28:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
readCounter struct {
|
|
|
|
io.ReadCloser
|
|
|
|
countBytes uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
writeCounter struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
countBytes uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
responseWrapper struct {
|
|
|
|
sync.Once
|
|
|
|
http.ResponseWriter
|
|
|
|
|
|
|
|
statusCode int
|
|
|
|
startTime time.Time
|
|
|
|
}
|
2023-07-05 14:04:52 +00:00
|
|
|
|
2023-11-30 08:25:05 +00:00
|
|
|
MetricsSettings interface {
|
2023-12-01 11:16:19 +00:00
|
|
|
ResolveNamespaceAlias(namespace string) string
|
|
|
|
}
|
|
|
|
|
2024-03-01 14:07:08 +00:00
|
|
|
// ContainerIDResolveFunc is a func to resolve container id by name.
|
|
|
|
ContainerIDResolveFunc func(ctx context.Context, bucket string) (cid.ID, error)
|
2023-07-05 14:04:52 +00:00
|
|
|
|
|
|
|
// cidResolveFunc is a func to resolve CID in Stats handler.
|
|
|
|
cidResolveFunc func(ctx context.Context, reqInfo *ReqInfo) (cnrID string)
|
2023-04-07 14:28:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const systemPath = "/system"
|
|
|
|
|
2023-07-05 14:04:52 +00:00
|
|
|
// Metrics wraps http handler for api with basic statistics collection.
|
2024-03-01 14:07:08 +00:00
|
|
|
func Metrics(log *zap.Logger, resolveBucket ContainerIDResolveFunc, appMetrics *metrics.AppMetrics, settings MetricsSettings) Func {
|
2023-07-05 14:04:52 +00:00
|
|
|
return func(h http.Handler) http.Handler {
|
2023-11-30 08:25:05 +00:00
|
|
|
return stats(h.ServeHTTP, resolveCID(log, resolveBucket), appMetrics, settings)
|
2023-07-05 14:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-07 14:28:21 +00:00
|
|
|
|
|
|
|
// Stats is a handler that update metrics.
|
2023-11-30 08:25:05 +00:00
|
|
|
func stats(f http.HandlerFunc, resolveCID cidResolveFunc, appMetrics *metrics.AppMetrics, settings MetricsSettings) http.HandlerFunc {
|
2023-04-07 14:28:21 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reqInfo := GetReqInfo(r.Context())
|
|
|
|
|
|
|
|
appMetrics.Statistic().CurrentS3RequestsInc(reqInfo.API)
|
|
|
|
defer appMetrics.Statistic().CurrentS3RequestsDec(reqInfo.API)
|
|
|
|
|
|
|
|
in := &readCounter{ReadCloser: r.Body}
|
|
|
|
out := &writeCounter{ResponseWriter: w}
|
|
|
|
|
|
|
|
r.Body = in
|
|
|
|
|
|
|
|
statsWriter := &responseWrapper{
|
|
|
|
ResponseWriter: out,
|
|
|
|
startTime: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
f(statsWriter, r)
|
|
|
|
|
|
|
|
// Time duration in secs since the call started.
|
|
|
|
// We don't need to do nanosecond precision here
|
|
|
|
// simply for the fact that it is not human-readable.
|
|
|
|
durationSecs := time.Since(statsWriter.startTime).Seconds()
|
|
|
|
|
|
|
|
cnrID := resolveCID(r.Context(), reqInfo)
|
2024-02-26 13:18:34 +00:00
|
|
|
appMetrics.UsersAPIStats().Update(reqInfo.User, reqInfo.BucketName, cnrID, settings.ResolveNamespaceAlias(reqInfo.Namespace),
|
2023-12-01 11:16:19 +00:00
|
|
|
requestTypeFromAPI(reqInfo.API), in.countBytes, out.countBytes)
|
2023-04-07 14:28:21 +00:00
|
|
|
|
|
|
|
code := statsWriter.statusCode
|
|
|
|
// A successful request has a 2xx response code
|
|
|
|
successReq := code >= http.StatusOK && code < http.StatusMultipleChoices
|
|
|
|
if !strings.HasSuffix(r.URL.Path, systemPath) {
|
|
|
|
appMetrics.Statistic().TotalS3RequestsInc(reqInfo.API)
|
|
|
|
if !successReq && code != 0 {
|
|
|
|
appMetrics.Statistic().TotalS3ErrorsInc(reqInfo.API)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 15:25:55 +00:00
|
|
|
// Increment the prometheus http request response histogram with appropriate label
|
|
|
|
appMetrics.Statistic().RequestDurationsUpdate(reqInfo.API, durationSecs)
|
2023-04-07 14:28:21 +00:00
|
|
|
|
|
|
|
appMetrics.Statistic().TotalInputBytesAdd(in.countBytes)
|
|
|
|
appMetrics.Statistic().TotalOutputBytesAdd(out.countBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:04:52 +00:00
|
|
|
func requestTypeFromAPI(api string) metrics.RequestType {
|
|
|
|
switch api {
|
2024-05-31 13:55:37 +00:00
|
|
|
case OptionsBucketOperation, OptionsObjectOperation, HeadObjectOperation, HeadBucketOperation:
|
2023-07-05 14:04:52 +00:00
|
|
|
return metrics.HEADRequest
|
2023-11-30 08:28:29 +00:00
|
|
|
case CreateMultipartUploadOperation, UploadPartCopyOperation, UploadPartOperation, CompleteMultipartUploadOperation,
|
|
|
|
PutObjectACLOperation, PutObjectTaggingOperation, CopyObjectOperation, PutObjectRetentionOperation, PutObjectLegalHoldOperation,
|
|
|
|
PutObjectOperation, PutBucketCorsOperation, PutBucketACLOperation, PutBucketLifecycleOperation, PutBucketEncryptionOperation,
|
|
|
|
PutBucketPolicyOperation, PutBucketObjectLockConfigOperation, PutBucketTaggingOperation, PutBucketVersioningOperation,
|
|
|
|
PutBucketNotificationOperation, CreateBucketOperation, PostObjectOperation:
|
2023-07-05 14:04:52 +00:00
|
|
|
return metrics.PUTRequest
|
2023-11-30 08:28:29 +00:00
|
|
|
case ListPartsOperation, ListMultipartUploadsOperation, ListObjectsV2MOperation, ListObjectsV2Operation,
|
|
|
|
ListObjectsV1Operation, ListBucketsOperation:
|
2023-07-05 14:04:52 +00:00
|
|
|
return metrics.LISTRequest
|
2023-11-30 08:28:29 +00:00
|
|
|
case GetObjectACLOperation, GetObjectTaggingOperation, SelectObjectContentOperation, GetObjectRetentionOperation, GetObjectLegalHoldOperation,
|
|
|
|
GetObjectAttributesOperation, GetObjectOperation, GetBucketLocationOperation, GetBucketPolicyOperation,
|
|
|
|
GetBucketLifecycleOperation, GetBucketEncryptionOperation, GetBucketCorsOperation, GetBucketACLOperation,
|
|
|
|
GetBucketWebsiteOperation, GetBucketAccelerateOperation, GetBucketRequestPaymentOperation, GetBucketLoggingOperation,
|
|
|
|
GetBucketReplicationOperation, GetBucketTaggingOperation, GetBucketObjectLockConfigOperation,
|
|
|
|
GetBucketVersioningOperation, GetBucketNotificationOperation, ListenBucketNotificationOperation:
|
2023-07-05 14:04:52 +00:00
|
|
|
return metrics.GETRequest
|
2023-11-30 08:28:29 +00:00
|
|
|
case AbortMultipartUploadOperation, DeleteObjectTaggingOperation, DeleteObjectOperation, DeleteBucketCorsOperation,
|
|
|
|
DeleteBucketWebsiteOperation, DeleteBucketTaggingOperation, DeleteMultipleObjectsOperation, DeleteBucketPolicyOperation,
|
|
|
|
DeleteBucketLifecycleOperation, DeleteBucketEncryptionOperation, DeleteBucketOperation:
|
2023-07-05 14:04:52 +00:00
|
|
|
return metrics.DELETERequest
|
|
|
|
default:
|
|
|
|
return metrics.UNKNOWNRequest
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolveCID forms CIDResolveFunc using BucketResolveFunc.
|
2024-03-01 14:07:08 +00:00
|
|
|
func resolveCID(log *zap.Logger, resolveContainerID ContainerIDResolveFunc) cidResolveFunc {
|
2023-07-05 14:04:52 +00:00
|
|
|
return func(ctx context.Context, reqInfo *ReqInfo) (cnrID string) {
|
2023-11-30 08:28:29 +00:00
|
|
|
if reqInfo.BucketName == "" || reqInfo.API == CreateBucketOperation || reqInfo.API == "" {
|
2023-07-05 14:04:52 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2024-03-01 14:07:08 +00:00
|
|
|
containerID, err := resolveContainerID(ctx, reqInfo.BucketName)
|
2023-07-05 14:04:52 +00:00
|
|
|
if err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
reqLogOrDefault(ctx, log).Debug(logs.FailedToResolveCID, zap.Error(err))
|
2023-07-05 14:04:52 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2024-03-01 14:07:08 +00:00
|
|
|
return containerID.EncodeToString()
|
2023-07-05 14:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 14:28:21 +00:00
|
|
|
// WriteHeader -- writes http status code.
|
|
|
|
func (w *responseWrapper) WriteHeader(code int) {
|
|
|
|
w.Do(func() {
|
|
|
|
w.statusCode = code
|
|
|
|
w.ResponseWriter.WriteHeader(code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush -- calls the underlying Flush.
|
|
|
|
func (w *responseWrapper) Flush() {
|
|
|
|
if f, ok := w.ResponseWriter.(http.Flusher); ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writeCounter) Flush() {
|
|
|
|
if f, ok := w.ResponseWriter.(http.Flusher); ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writeCounter) Write(p []byte) (int, error) {
|
|
|
|
n, err := w.ResponseWriter.Write(p)
|
|
|
|
atomic.AddUint64(&w.countBytes, uint64(n))
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *readCounter) Read(p []byte) (int, error) {
|
|
|
|
n, err := r.ReadCloser.Read(p)
|
|
|
|
atomic.AddUint64(&r.countBytes, uint64(n))
|
|
|
|
return n, err
|
|
|
|
}
|