storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/gateway-metrics.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2019 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cmd
    18  
    19  import (
    20  	"net/http"
    21  	"sync/atomic"
    22  )
    23  
    24  // RequestStats - counts for Get and Head requests
    25  type RequestStats struct {
    26  	Get  uint64 `json:"Get"`
    27  	Head uint64 `json:"Head"`
    28  	Put  uint64 `json:"Put"`
    29  	Post uint64 `json:"Post"`
    30  }
    31  
    32  // IncBytesReceived - Increase total bytes received from gateway backend
    33  func (s *BackendMetrics) IncBytesReceived(n uint64) {
    34  	atomic.AddUint64(&s.bytesReceived, n)
    35  }
    36  
    37  // GetBytesReceived - Get total bytes received from gateway backend
    38  func (s *BackendMetrics) GetBytesReceived() uint64 {
    39  	return atomic.LoadUint64(&s.bytesReceived)
    40  }
    41  
    42  // IncBytesSent - Increase total bytes sent to gateway backend
    43  func (s *BackendMetrics) IncBytesSent(n uint64) {
    44  	atomic.AddUint64(&s.bytesSent, n)
    45  }
    46  
    47  // GetBytesSent - Get total bytes received from gateway backend
    48  func (s *BackendMetrics) GetBytesSent() uint64 {
    49  	return atomic.LoadUint64(&s.bytesSent)
    50  }
    51  
    52  // IncRequests - Increase request count sent to gateway backend by 1
    53  func (s *BackendMetrics) IncRequests(method string) {
    54  	// Only increment for Head & Get requests, else no op
    55  	if method == http.MethodGet {
    56  		atomic.AddUint64(&s.requestStats.Get, 1)
    57  	} else if method == http.MethodHead {
    58  		atomic.AddUint64(&s.requestStats.Head, 1)
    59  	} else if method == http.MethodPut {
    60  		atomic.AddUint64(&s.requestStats.Put, 1)
    61  	} else if method == http.MethodPost {
    62  		atomic.AddUint64(&s.requestStats.Post, 1)
    63  	}
    64  }
    65  
    66  // GetRequests - Get total number of Get & Headrequests sent to gateway backend
    67  func (s *BackendMetrics) GetRequests() RequestStats {
    68  	return s.requestStats
    69  }
    70  
    71  // NewMetrics - Prepare new BackendMetrics structure
    72  func NewMetrics() *BackendMetrics {
    73  	return &BackendMetrics{}
    74  }