2022-12-23 12:28:30 +00:00
|
|
|
package api
|
2020-07-16 12:42:06 +00:00
|
|
|
|
|
|
|
import (
|
2022-12-27 12:30:11 +00:00
|
|
|
"context"
|
2020-07-16 12:42:06 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/accessbox"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
2020-07-16 12:42:06 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
2022-12-23 12:28:30 +00:00
|
|
|
type RequestType int
|
|
|
|
|
|
|
|
const (
|
2022-12-27 12:30:11 +00:00
|
|
|
UNKNOWNRequest RequestType = iota
|
|
|
|
HEADRequest RequestType = iota
|
|
|
|
PUTRequest RequestType = iota
|
|
|
|
LISTRequest RequestType = iota
|
|
|
|
GETRequest RequestType = iota
|
|
|
|
DELETERequest RequestType = iota
|
2022-12-23 12:28:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (t RequestType) String() string {
|
|
|
|
switch t {
|
|
|
|
case 1:
|
2022-12-27 12:30:11 +00:00
|
|
|
return "HEAD"
|
2022-12-23 12:28:30 +00:00
|
|
|
case 2:
|
2022-12-27 12:30:11 +00:00
|
|
|
return "PUT"
|
2022-12-23 12:28:30 +00:00
|
|
|
case 3:
|
2022-12-27 12:30:11 +00:00
|
|
|
return "LIST"
|
2022-12-23 12:28:30 +00:00
|
|
|
case 4:
|
2022-12-27 12:30:11 +00:00
|
|
|
return "GET"
|
|
|
|
case 5:
|
2022-12-23 12:28:30 +00:00
|
|
|
return "DELETE"
|
|
|
|
default:
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RequestTypeFromAPI(api string) RequestType {
|
|
|
|
switch api {
|
2022-12-27 12:30:11 +00:00
|
|
|
case "Options", "HeadObject", "HeadBucket":
|
2022-12-23 12:28:30 +00:00
|
|
|
return HEADRequest
|
2022-12-27 12:30:11 +00:00
|
|
|
case "CreateMultipartUpload", "UploadPartCopy", "UploadPart", "CompleteMultipartUpload",
|
|
|
|
"PutObjectACL", "PutObjectTagging", "CopyObject", "PutObjectRetention", "PutObjectLegalHold",
|
|
|
|
"PutObject", "PutBucketCors", "PutBucketACL", "PutBucketLifecycle", "PutBucketEncryption",
|
|
|
|
"PutBucketPolicy", "PutBucketObjectLockConfig", "PutBucketTagging", "PutBucketVersioning",
|
|
|
|
"PutBucketNotification", "CreateBucket", "PostObject":
|
2022-12-23 12:28:30 +00:00
|
|
|
return PUTRequest
|
2022-12-27 12:30:11 +00:00
|
|
|
case "ListObjectParts", "ListMultipartUploads", "ListObjectsV2M", "ListObjectsV2", "ListBucketVersions",
|
|
|
|
"ListObjectsV1", "ListBuckets":
|
2022-12-23 12:28:30 +00:00
|
|
|
return LISTRequest
|
2022-12-27 12:30:11 +00:00
|
|
|
case "GetObjectACL", "GetObjectTagging", "SelectObjectContent", "GetObjectRetention", "getobjectlegalhold",
|
|
|
|
"GetObjectAttributes", "GetObject", "GetBucketLocation", "GetBucketPolicy",
|
|
|
|
"GetBucketLifecycle", "GetBucketEncryption", "GetBucketCors", "GetBucketACL",
|
|
|
|
"GetBucketWebsite", "GetBucketAccelerate", "GetBucketRequestPayment", "GetBucketLogging",
|
|
|
|
"GetBucketReplication", "GetBucketTagging", "GetBucketObjectLockConfig",
|
|
|
|
"GetBucketVersioning", "GetBucketNotification", "ListenBucketNotification":
|
2022-12-23 12:28:30 +00:00
|
|
|
return GETRequest
|
2022-12-27 12:30:11 +00:00
|
|
|
case "AbortMultipartUpload", "DeleteObjectTagging", "DeleteObject", "DeleteBucketCors",
|
|
|
|
"DeleteBucketWebsite", "DeleteBucketTagging", "DeleteMultipleObjects", "DeleteBucketPolicy",
|
|
|
|
"DeleteBucketLifecycle", "DeleteBucketEncryption", "DeleteBucket":
|
2022-12-23 12:28:30 +00:00
|
|
|
return DELETERequest
|
|
|
|
default:
|
2022-12-27 12:30:11 +00:00
|
|
|
return UNKNOWNRequest
|
2022-12-23 12:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 12:42:06 +00:00
|
|
|
type (
|
|
|
|
// HTTPAPIStats holds statistics information about
|
2022-04-13 16:56:58 +00:00
|
|
|
// the API given in the requests.
|
2020-07-16 12:42:06 +00:00
|
|
|
HTTPAPIStats struct {
|
|
|
|
apiStats map[string]int
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2023-02-09 08:59:31 +00:00
|
|
|
UsersStat interface {
|
|
|
|
Update(user, bucket, cnrID string, reqType RequestType, in, out uint64)
|
2022-12-26 13:13:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 12:42:06 +00:00
|
|
|
// HTTPStats holds statistics information about
|
2021-05-13 19:25:32 +00:00
|
|
|
// HTTP requests made by all clients.
|
2020-07-16 12:42:06 +00:00
|
|
|
HTTPStats struct {
|
|
|
|
currentS3Requests HTTPAPIStats
|
|
|
|
totalS3Requests HTTPAPIStats
|
|
|
|
totalS3Errors HTTPAPIStats
|
|
|
|
|
|
|
|
totalInputBytes uint64
|
|
|
|
totalOutputBytes uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
readCounter struct {
|
|
|
|
io.ReadCloser
|
|
|
|
countBytes uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
writeCounter struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
countBytes uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
responseWrapper struct {
|
2020-11-27 12:28:27 +00:00
|
|
|
sync.Once
|
2020-07-16 12:42:06 +00:00
|
|
|
http.ResponseWriter
|
|
|
|
|
2020-11-27 12:28:27 +00:00
|
|
|
statusCode int
|
|
|
|
startTime time.Time
|
2020-07-16 12:42:06 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-08-06 12:02:13 +00:00
|
|
|
const systemPath = "/system"
|
2020-07-16 12:42:06 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
httpStatsMetric = new(HTTPStats)
|
|
|
|
httpRequestsDuration = prometheus.NewHistogramVec(
|
|
|
|
prometheus.HistogramOpts{
|
2022-12-20 08:38:58 +00:00
|
|
|
Name: "frostfs_s3_request_seconds",
|
|
|
|
Help: "Time taken by requests served by current FrostFS S3 Gate instance",
|
2020-07-16 12:42:06 +00:00
|
|
|
Buckets: []float64{.05, .1, .25, .5, 1, 2.5, 5, 10},
|
|
|
|
},
|
|
|
|
[]string{"api"},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// Collects HTTP metrics for FrostFS S3 Gate in Prometheus specific format
|
2022-04-13 16:56:58 +00:00
|
|
|
// and sends to the given channel.
|
2020-07-16 12:42:06 +00:00
|
|
|
func collectHTTPMetrics(ch chan<- prometheus.Metric) {
|
|
|
|
for api, value := range httpStatsMetric.currentS3Requests.Load() {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
2022-12-20 08:38:58 +00:00
|
|
|
prometheus.BuildFQName("frostfs_s3", "requests", "current"),
|
|
|
|
"Total number of running s3 requests in current FrostFS S3 Gate instance",
|
2020-07-16 12:42:06 +00:00
|
|
|
[]string{"api"}, nil),
|
|
|
|
prometheus.CounterValue,
|
|
|
|
float64(value),
|
|
|
|
api,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
for api, value := range httpStatsMetric.totalS3Requests.Load() {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
2022-12-20 08:38:58 +00:00
|
|
|
prometheus.BuildFQName("frostfs_s3", "requests", "total"),
|
|
|
|
"Total number of s3 requests in current FrostFS S3 Gate instance",
|
2020-07-16 12:42:06 +00:00
|
|
|
[]string{"api"}, nil),
|
|
|
|
prometheus.CounterValue,
|
|
|
|
float64(value),
|
|
|
|
api,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
for api, value := range httpStatsMetric.totalS3Errors.Load() {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
2022-12-20 08:38:58 +00:00
|
|
|
prometheus.BuildFQName("frostfs_s3", "errors", "total"),
|
|
|
|
"Total number of s3 errors in current FrostFS S3 Gate instance",
|
2020-07-16 12:42:06 +00:00
|
|
|
[]string{"api"}, nil),
|
|
|
|
prometheus.CounterValue,
|
|
|
|
float64(value),
|
|
|
|
api,
|
|
|
|
)
|
|
|
|
}
|
2022-12-26 13:13:24 +00:00
|
|
|
}
|
|
|
|
|
2022-12-27 12:30:11 +00:00
|
|
|
// CIDResolveFunc is a func to resolve CID in Stats handler.
|
|
|
|
type CIDResolveFunc func(ctx context.Context, reqInfo *ReqInfo) (cnrID string)
|
|
|
|
|
|
|
|
// Stats is a handler that update metrics.
|
2023-02-09 08:59:31 +00:00
|
|
|
func Stats(f http.HandlerFunc, resolveCID CIDResolveFunc, usersStat UsersStat) http.HandlerFunc {
|
2020-07-16 12:42:06 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-12-27 12:30:11 +00:00
|
|
|
reqInfo := GetReqInfo(r.Context())
|
|
|
|
|
|
|
|
httpStatsMetric.currentS3Requests.Inc(reqInfo.API)
|
|
|
|
defer httpStatsMetric.currentS3Requests.Dec(reqInfo.API)
|
2020-07-16 12:42:06 +00:00
|
|
|
|
|
|
|
in := &readCounter{ReadCloser: r.Body}
|
|
|
|
out := &writeCounter{ResponseWriter: w}
|
|
|
|
|
|
|
|
r.Body = in
|
|
|
|
|
|
|
|
statsWriter := &responseWrapper{
|
|
|
|
ResponseWriter: out,
|
|
|
|
startTime: time.Now(),
|
|
|
|
}
|
|
|
|
|
2022-12-27 12:30:11 +00:00
|
|
|
f(statsWriter, r)
|
2020-07-16 12:42:06 +00:00
|
|
|
|
|
|
|
// Time duration in secs since the call started.
|
2022-04-13 16:56:58 +00:00
|
|
|
// We don't need to do nanosecond precision here
|
2022-12-27 12:30:11 +00:00
|
|
|
// simply for the fact that it is not human-readable.
|
2020-07-16 12:42:06 +00:00
|
|
|
durationSecs := time.Since(statsWriter.startTime).Seconds()
|
|
|
|
|
2022-12-27 12:30:11 +00:00
|
|
|
user := resolveUser(r.Context())
|
|
|
|
cnrID := resolveCID(r.Context(), reqInfo)
|
2023-02-09 08:59:31 +00:00
|
|
|
usersStat.Update(user, reqInfo.BucketName, cnrID, RequestTypeFromAPI(reqInfo.API), in.countBytes, out.countBytes)
|
2022-12-27 12:30:11 +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) {
|
|
|
|
httpStatsMetric.totalS3Requests.Inc(reqInfo.API)
|
|
|
|
if !successReq && code != 0 {
|
|
|
|
httpStatsMetric.totalS3Errors.Inc(reqInfo.API)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
// Increment the prometheus http request response histogram with appropriate label
|
|
|
|
httpRequestsDuration.With(prometheus.Labels{"api": reqInfo.API}).Observe(durationSecs)
|
|
|
|
}
|
2020-07-16 12:42:06 +00:00
|
|
|
|
|
|
|
atomic.AddUint64(&httpStatsMetric.totalInputBytes, in.countBytes)
|
|
|
|
atomic.AddUint64(&httpStatsMetric.totalOutputBytes, out.countBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 12:30:11 +00:00
|
|
|
func resolveUser(ctx context.Context) string {
|
|
|
|
user := "anon"
|
|
|
|
if bd, ok := ctx.Value(BoxData).(*accessbox.Box); ok && bd != nil && bd.Gate != nil && bd.Gate.BearerToken != nil {
|
|
|
|
user = bearer.ResolveIssuer(*bd.Gate.BearerToken).String()
|
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
2020-07-16 12:42:06 +00:00
|
|
|
// Inc increments the api stats counter.
|
|
|
|
func (stats *HTTPAPIStats) Inc(api string) {
|
|
|
|
if stats == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stats.Lock()
|
|
|
|
defer stats.Unlock()
|
|
|
|
if stats.apiStats == nil {
|
|
|
|
stats.apiStats = make(map[string]int)
|
|
|
|
}
|
|
|
|
stats.apiStats[api]++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dec increments the api stats counter.
|
|
|
|
func (stats *HTTPAPIStats) Dec(api string) {
|
|
|
|
if stats == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stats.Lock()
|
|
|
|
defer stats.Unlock()
|
|
|
|
if val, ok := stats.apiStats[api]; ok && val > 0 {
|
|
|
|
stats.apiStats[api]--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load returns the recorded stats.
|
|
|
|
func (stats *HTTPAPIStats) Load() map[string]int {
|
|
|
|
stats.Lock()
|
|
|
|
defer stats.Unlock()
|
|
|
|
var apiStats = make(map[string]int, len(stats.apiStats))
|
|
|
|
for k, v := range stats.apiStats {
|
|
|
|
apiStats[k] = v
|
|
|
|
}
|
|
|
|
return apiStats
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *HTTPStats) getInputBytes() uint64 {
|
|
|
|
return atomic.LoadUint64(&st.totalInputBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *HTTPStats) getOutputBytes() uint64 {
|
|
|
|
return atomic.LoadUint64(&st.totalOutputBytes)
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// WriteHeader -- writes http status code.
|
2020-07-16 12:42:06 +00:00
|
|
|
func (w *responseWrapper) WriteHeader(code int) {
|
2020-11-27 12:28:27 +00:00
|
|
|
w.Do(func() {
|
2020-07-16 12:42:06 +00:00
|
|
|
w.statusCode = code
|
|
|
|
w.ResponseWriter.WriteHeader(code)
|
2020-11-27 12:28:27 +00:00
|
|
|
})
|
2020-07-16 12:42:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// Flush -- calls the underlying Flush.
|
2020-07-16 12:42:06 +00:00
|
|
|
func (w *responseWrapper) Flush() {
|
|
|
|
if f, ok := w.ResponseWriter.(http.Flusher); ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 08:17:31 +00:00
|
|
|
func (w *writeCounter) Flush() {
|
|
|
|
if f, ok := w.ResponseWriter.(http.Flusher); ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 12:42:06 +00:00
|
|
|
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
|
|
|
|
}
|