github.com/minio/console@v1.4.1/pkg/logger/reqinfo.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2022 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package logger
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"sync"
    23  
    24  	"github.com/minio/console/pkg/utils"
    25  )
    26  
    27  // KeyVal - appended to ReqInfo.Tags
    28  type KeyVal struct {
    29  	Key string
    30  	Val interface{}
    31  }
    32  
    33  // ObjectVersion object version key/versionId
    34  type ObjectVersion struct {
    35  	ObjectName string
    36  	VersionID  string `json:"VersionId,omitempty"`
    37  }
    38  
    39  // ReqInfo stores the request info.
    40  type ReqInfo struct {
    41  	RemoteHost   string          // Client Host/IP
    42  	Host         string          // Node Host/IP
    43  	UserAgent    string          // User Agent
    44  	DeploymentID string          // x-minio-deployment-id
    45  	RequestID    string          // x-amz-request-id
    46  	SessionID    string          // custom session id
    47  	API          string          // API name - GetObject PutObject NewMultipartUpload etc.
    48  	BucketName   string          `json:",omitempty"` // Bucket name
    49  	ObjectName   string          `json:",omitempty"` // Object name
    50  	VersionID    string          `json:",omitempty"` // corresponding versionID for the object
    51  	Objects      []ObjectVersion `json:",omitempty"` // Only set during MultiObject delete handler.
    52  	AccessKey    string          // Access Key
    53  	tags         []KeyVal        // Any additional info not accommodated by above fields
    54  	sync.RWMutex
    55  }
    56  
    57  // GetTags - returns the user defined tags
    58  func (r *ReqInfo) GetTags() []KeyVal {
    59  	if r == nil {
    60  		return nil
    61  	}
    62  	r.RLock()
    63  	defer r.RUnlock()
    64  	return append([]KeyVal(nil), r.tags...)
    65  }
    66  
    67  // GetTagsMap - returns the user defined tags in a map structure
    68  func (r *ReqInfo) GetTagsMap() map[string]interface{} {
    69  	if r == nil {
    70  		return nil
    71  	}
    72  	r.RLock()
    73  	defer r.RUnlock()
    74  	m := make(map[string]interface{}, len(r.tags))
    75  	for _, t := range r.tags {
    76  		m[t.Key] = t.Val
    77  	}
    78  	return m
    79  }
    80  
    81  // SetReqInfo sets ReqInfo in the context.
    82  func SetReqInfo(ctx context.Context, req *ReqInfo) context.Context {
    83  	if ctx == nil {
    84  		LogIf(context.Background(), fmt.Errorf("context is nil"))
    85  		return nil
    86  	}
    87  	return context.WithValue(ctx, utils.ContextLogKey, req)
    88  }
    89  
    90  // GetReqInfo returns ReqInfo if set.
    91  func GetReqInfo(ctx context.Context) *ReqInfo {
    92  	if ctx != nil {
    93  		r, ok := ctx.Value(utils.ContextLogKey).(*ReqInfo)
    94  		if ok {
    95  			return r
    96  		}
    97  		r = &ReqInfo{}
    98  		if val, o := ctx.Value(utils.ContextRequestID).(string); o {
    99  			r.RequestID = val
   100  		}
   101  		if val, o := ctx.Value(utils.ContextRequestUserID).(string); o {
   102  			r.SessionID = val
   103  		}
   104  		if val, o := ctx.Value(utils.ContextRequestUserAgent).(string); o {
   105  			r.UserAgent = val
   106  		}
   107  		if val, o := ctx.Value(utils.ContextRequestHost).(string); o {
   108  			r.Host = val
   109  		}
   110  		if val, o := ctx.Value(utils.ContextRequestRemoteAddr).(string); o {
   111  			r.RemoteHost = val
   112  		}
   113  		SetReqInfo(ctx, r)
   114  		return r
   115  	}
   116  	return nil
   117  }