github.com/minio/madmin-go/v2@v2.2.1/trace.go (about)

     1  //
     2  // Copyright (c) 2015-2022 MinIO, Inc.
     3  //
     4  // This file is part of MinIO Object Storage stack
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU Affero General Public License as
     8  // published by the Free Software Foundation, either version 3 of the
     9  // License, or (at your option) any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU Affero General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU Affero General Public License
    17  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    18  //
    19  
    20  package madmin
    21  
    22  import (
    23  	"math/bits"
    24  	"net/http"
    25  	"time"
    26  )
    27  
    28  //go:generate stringer -type=TraceType -trimprefix=Trace $GOFILE
    29  
    30  // TraceType indicates the type of the tracing Info
    31  type TraceType uint64
    32  
    33  const (
    34  	// TraceOS tracing (Golang os package calls)
    35  	TraceOS TraceType = 1 << iota
    36  	// TraceStorage tracing (MinIO Storage Layer)
    37  	TraceStorage
    38  	// TraceS3 provides tracing of S3 API calls
    39  	TraceS3
    40  	// TraceInternal tracing internal (.minio.sys/...) HTTP calls
    41  	TraceInternal
    42  	// TraceScanner will trace scan operations.
    43  	TraceScanner
    44  	// TraceDecommission will trace decommission operations.
    45  	TraceDecommission
    46  	// TraceHealing will trace healing operations.
    47  	TraceHealing
    48  	// TraceBatchReplication will trace batch replication operations.
    49  	TraceBatchReplication
    50  	// TraceBatchKeyRotation will trace batch keyrotation operations.
    51  	TraceBatchKeyRotation
    52  	// TraceRebalance will trace rebalance operations
    53  	TraceRebalance
    54  	// TraceReplicationResync will trace replication resync operations.
    55  	TraceReplicationResync
    56  	// TraceBootstrap will trace events during MinIO cluster bootstrap
    57  	TraceBootstrap
    58  	// TraceFTP will trace events from MinIO FTP Server
    59  	TraceFTP
    60  	// TraceILM will trace events during MinIO ILM operations
    61  	TraceILM
    62  	// Add more here...
    63  
    64  	// TraceAll contains all valid trace modes.
    65  	// This *must* be the last entry.
    66  	TraceAll TraceType = (1 << iota) - 1
    67  )
    68  
    69  // Contains returns whether all flags in other is present in t.
    70  func (t TraceType) Contains(other TraceType) bool {
    71  	return t&other == other
    72  }
    73  
    74  // Overlaps returns whether any flags in t overlaps with other.
    75  func (t TraceType) Overlaps(other TraceType) bool {
    76  	return t&other != 0
    77  }
    78  
    79  // SingleType returns whether t has a single type set.
    80  func (t TraceType) SingleType() bool {
    81  	// Include
    82  	return bits.OnesCount64(uint64(t)) == 1
    83  }
    84  
    85  // Merge will merge other into t.
    86  func (t *TraceType) Merge(other TraceType) {
    87  	*t = *t | other
    88  }
    89  
    90  // SetIf will add other if b is true.
    91  func (t *TraceType) SetIf(b bool, other TraceType) {
    92  	if b {
    93  		*t = *t | other
    94  	}
    95  }
    96  
    97  // Mask returns the trace type as uint32.
    98  func (t TraceType) Mask() uint64 {
    99  	return uint64(t)
   100  }
   101  
   102  // TraceInfo - represents a trace record, additionally
   103  // also reports errors if any while listening on trace.
   104  type TraceInfo struct {
   105  	TraceType TraceType `json:"type"`
   106  
   107  	NodeName string        `json:"nodename"`
   108  	FuncName string        `json:"funcname"`
   109  	Time     time.Time     `json:"time"`
   110  	Path     string        `json:"path"`
   111  	Duration time.Duration `json:"dur"`
   112  
   113  	Message    string            `json:"msg,omitempty"`
   114  	Error      string            `json:"error,omitempty"`
   115  	Custom     map[string]string `json:"custom,omitempty"`
   116  	HTTP       *TraceHTTPStats   `json:"http,omitempty"`
   117  	HealResult *HealResultItem   `json:"healResult,omitempty"`
   118  }
   119  
   120  // Mask returns the trace type as uint32.
   121  func (t TraceInfo) Mask() uint64 {
   122  	return t.TraceType.Mask()
   123  }
   124  
   125  // traceInfoLegacy - represents a trace record, additionally
   126  // also reports errors if any while listening on trace.
   127  // For minio versions before July 2022.
   128  type traceInfoLegacy struct {
   129  	TraceInfo
   130  
   131  	ReqInfo   *TraceRequestInfo  `json:"request"`
   132  	RespInfo  *TraceResponseInfo `json:"response"`
   133  	CallStats *TraceCallStats    `json:"stats"`
   134  
   135  	StorageStats *struct {
   136  		Path     string        `json:"path"`
   137  		Duration time.Duration `json:"duration"`
   138  	} `json:"storageStats"`
   139  	OSStats *struct {
   140  		Path     string        `json:"path"`
   141  		Duration time.Duration `json:"duration"`
   142  	} `json:"osStats"`
   143  }
   144  
   145  type TraceHTTPStats struct {
   146  	ReqInfo   TraceRequestInfo  `json:"request"`
   147  	RespInfo  TraceResponseInfo `json:"response"`
   148  	CallStats TraceCallStats    `json:"stats"`
   149  }
   150  
   151  // TraceCallStats records request stats
   152  type TraceCallStats struct {
   153  	InputBytes  int `json:"inputbytes"`
   154  	OutputBytes int `json:"outputbytes"`
   155  	// Deprecated: Use TraceInfo.Duration (June 2022)
   156  	Latency         time.Duration `json:"latency"`
   157  	TimeToFirstByte time.Duration `json:"timetofirstbyte"`
   158  }
   159  
   160  // TraceRequestInfo represents trace of http request
   161  type TraceRequestInfo struct {
   162  	Time     time.Time   `json:"time"`
   163  	Proto    string      `json:"proto"`
   164  	Method   string      `json:"method"`
   165  	Path     string      `json:"path,omitempty"`
   166  	RawQuery string      `json:"rawquery,omitempty"`
   167  	Headers  http.Header `json:"headers,omitempty"`
   168  	Body     []byte      `json:"body,omitempty"`
   169  	Client   string      `json:"client"`
   170  }
   171  
   172  // TraceResponseInfo represents trace of http request
   173  type TraceResponseInfo struct {
   174  	Time       time.Time   `json:"time"`
   175  	Headers    http.Header `json:"headers,omitempty"`
   176  	Body       []byte      `json:"body,omitempty"`
   177  	StatusCode int         `json:"statuscode,omitempty"`
   178  }