github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/internal/logger/component.go (about) 1 // Copyright (C) MongoDB, Inc. 2023-present. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 6 7 package logger 8 9 import ( 10 "os" 11 "strconv" 12 13 "go.mongodb.org/mongo-driver/bson/primitive" 14 ) 15 16 const ( 17 CommandFailed = "Command failed" 18 CommandStarted = "Command started" 19 CommandSucceeded = "Command succeeded" 20 ConnectionPoolCreated = "Connection pool created" 21 ConnectionPoolReady = "Connection pool ready" 22 ConnectionPoolCleared = "Connection pool cleared" 23 ConnectionPoolClosed = "Connection pool closed" 24 ConnectionCreated = "Connection created" 25 ConnectionReady = "Connection ready" 26 ConnectionClosed = "Connection closed" 27 ConnectionCheckoutStarted = "Connection checkout started" 28 ConnectionCheckoutFailed = "Connection checkout failed" 29 ConnectionCheckedOut = "Connection checked out" 30 ConnectionCheckedIn = "Connection checked in" 31 ) 32 33 const ( 34 KeyCommand = "command" 35 KeyCommandName = "commandName" 36 KeyDatabaseName = "databaseName" 37 KeyDriverConnectionID = "driverConnectionId" 38 KeyDurationMS = "durationMS" 39 KeyError = "error" 40 KeyFailure = "failure" 41 KeyMaxConnecting = "maxConnecting" 42 KeyMaxIdleTimeMS = "maxIdleTimeMS" 43 KeyMaxPoolSize = "maxPoolSize" 44 KeyMessage = "message" 45 KeyMinPoolSize = "minPoolSize" 46 KeyOperationID = "operationId" 47 KeyReason = "reason" 48 KeyReply = "reply" 49 KeyRequestID = "requestId" 50 KeyServerConnectionID = "serverConnectionId" 51 KeyServerHost = "serverHost" 52 KeyServerPort = "serverPort" 53 KeyServiceID = "serviceId" 54 KeyTimestamp = "timestamp" 55 ) 56 57 type KeyValues []interface{} 58 59 func (kvs *KeyValues) Add(key string, value interface{}) { 60 *kvs = append(*kvs, key, value) 61 } 62 63 const ( 64 ReasonConnClosedStale = "Connection became stale because the pool was cleared" 65 ReasonConnClosedIdle = "Connection has been available but unused for longer than the configured max idle time" 66 ReasonConnClosedError = "An error occurred while using the connection" 67 ReasonConnClosedPoolClosed = "Connection pool was closed" 68 ReasonConnCheckoutFailedTimout = "Wait queue timeout elapsed without a connection becoming available" 69 ReasonConnCheckoutFailedError = "An error occurred while trying to establish a new connection" 70 ReasonConnCheckoutFailedPoolClosed = "Connection pool was closed" 71 ) 72 73 // Component is an enumeration representing the "components" which can be 74 // logged against. A LogLevel can be configured on a per-component basis. 75 type Component int 76 77 const ( 78 // ComponentAll enables logging for all components. 79 ComponentAll Component = iota 80 81 // ComponentCommand enables command monitor logging. 82 ComponentCommand 83 84 // ComponentTopology enables topology logging. 85 ComponentTopology 86 87 // ComponentServerSelection enables server selection logging. 88 ComponentServerSelection 89 90 // ComponentConnection enables connection services logging. 91 ComponentConnection 92 ) 93 94 const ( 95 mongoDBLogAllEnvVar = "MONGODB_LOG_ALL" 96 mongoDBLogCommandEnvVar = "MONGODB_LOG_COMMAND" 97 mongoDBLogTopologyEnvVar = "MONGODB_LOG_TOPOLOGY" 98 mongoDBLogServerSelectionEnvVar = "MONGODB_LOG_SERVER_SELECTION" 99 mongoDBLogConnectionEnvVar = "MONGODB_LOG_CONNECTION" 100 ) 101 102 var componentEnvVarMap = map[string]Component{ 103 mongoDBLogAllEnvVar: ComponentAll, 104 mongoDBLogCommandEnvVar: ComponentCommand, 105 mongoDBLogTopologyEnvVar: ComponentTopology, 106 mongoDBLogServerSelectionEnvVar: ComponentServerSelection, 107 mongoDBLogConnectionEnvVar: ComponentConnection, 108 } 109 110 // EnvHasComponentVariables returns true if the environment contains any of the 111 // component environment variables. 112 func EnvHasComponentVariables() bool { 113 for envVar := range componentEnvVarMap { 114 if os.Getenv(envVar) != "" { 115 return true 116 } 117 } 118 119 return false 120 } 121 122 // Command is a struct defining common fields that must be included in all 123 // commands. 124 type Command struct { 125 // TODO(GODRIVER-2824): change the DriverConnectionID type to int64. 126 DriverConnectionID uint64 // Driver's ID for the connection 127 Name string // Command name 128 Message string // Message associated with the command 129 OperationID int32 // Driver-generated operation ID 130 RequestID int64 // Driver-generated request ID 131 ServerConnectionID *int64 // Server's ID for the connection used for the command 132 ServerHost string // Hostname or IP address for the server 133 ServerPort string // Port for the server 134 ServiceID *primitive.ObjectID // ID for the command in load balancer mode 135 } 136 137 // SerializeCommand takes a command and a variable number of key-value pairs and 138 // returns a slice of interface{} that can be passed to the logger for 139 // structured logging. 140 func SerializeCommand(cmd Command, extraKeysAndValues ...interface{}) []interface{} { 141 // Initialize the boilerplate keys and values. 142 keysAndValues := KeyValues{ 143 KeyCommandName, cmd.Name, 144 KeyDriverConnectionID, cmd.DriverConnectionID, 145 KeyMessage, cmd.Message, 146 KeyOperationID, cmd.OperationID, 147 KeyRequestID, cmd.RequestID, 148 KeyServerHost, cmd.ServerHost, 149 } 150 151 // Add the extra keys and values. 152 for i := 0; i < len(extraKeysAndValues); i += 2 { 153 keysAndValues.Add(extraKeysAndValues[i].(string), extraKeysAndValues[i+1]) 154 } 155 156 port, err := strconv.ParseInt(cmd.ServerPort, 0, 32) 157 if err == nil { 158 keysAndValues.Add(KeyServerPort, port) 159 } 160 161 // Add the "serverConnectionId" if it is not nil. 162 if cmd.ServerConnectionID != nil { 163 keysAndValues.Add(KeyServerConnectionID, *cmd.ServerConnectionID) 164 } 165 166 // Add the "serviceId" if it is not nil. 167 if cmd.ServiceID != nil { 168 keysAndValues.Add(KeyServiceID, cmd.ServiceID.Hex()) 169 } 170 171 return keysAndValues 172 } 173 174 // Connection contains data that all connection log messages MUST contain. 175 type Connection struct { 176 Message string // Message associated with the connection 177 ServerHost string // Hostname or IP address for the server 178 ServerPort string // Port for the server 179 } 180 181 // SerializeConnection serializes a ConnectionMessage into a slice of keys 182 // and values that can be passed to a logger. 183 func SerializeConnection(conn Connection, extraKeysAndValues ...interface{}) []interface{} { 184 // Initialize the boilerplate keys and values. 185 keysAndValues := KeyValues{ 186 KeyMessage, conn.Message, 187 KeyServerHost, conn.ServerHost, 188 } 189 190 // Add the optional keys and values. 191 for i := 0; i < len(extraKeysAndValues); i += 2 { 192 keysAndValues.Add(extraKeysAndValues[i].(string), extraKeysAndValues[i+1]) 193 } 194 195 port, err := strconv.ParseInt(conn.ServerPort, 0, 32) 196 if err == nil { 197 keysAndValues.Add(KeyServerPort, port) 198 } 199 200 return keysAndValues 201 }