@@ -14,9 +14,60 @@ import (
14
14
)
15
15
16
16
const (
17
- CommandFailed = "Command failed"
18
- CommandStarted = "Command started"
19
- CommandSucceeded = "Command succeeded"
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"
20
71
)
21
72
22
73
// Component is an enumeration representing the "components" which can be
@@ -87,31 +138,62 @@ type Command struct {
87
138
// structured logging.
88
139
func SerializeCommand (cmd Command , extraKeysAndValues ... interface {}) []interface {} {
89
140
// Initialize the boilerplate keys and values.
90
- keysAndValues := append ([]interface {}{
91
- "commandName" , cmd .Name ,
92
- "driverConnectionId" , cmd .DriverConnectionID ,
93
- "message" , cmd .Message ,
94
- "operationId" , cmd .OperationID ,
95
- "requestId" , cmd .RequestID ,
96
- "serverHost" , cmd .ServerHost ,
97
- }, extraKeysAndValues ... )
141
+ keysAndValues := KeyValues {
142
+ KeyCommandName , cmd .Name ,
143
+ KeyDriverConnectionID , cmd .DriverConnectionID ,
144
+ KeyMessage , cmd .Message ,
145
+ KeyOperationID , cmd .OperationID ,
146
+ KeyRequestID , cmd .RequestID ,
147
+ KeyServerHost , cmd .ServerHost ,
148
+ }
149
+
150
+ // Add the extra keys and values.
151
+ for i := 0 ; i < len (extraKeysAndValues ); i += 2 {
152
+ keysAndValues .Add (extraKeysAndValues [i ].(string ), extraKeysAndValues [i + 1 ])
153
+ }
98
154
99
- // Add the optional keys and values.
100
155
port , err := strconv .ParseInt (cmd .ServerPort , 0 , 32 )
101
156
if err == nil {
102
- keysAndValues = append ( keysAndValues , "serverPort" , port )
157
+ keysAndValues . Add ( KeyServerPort , port )
103
158
}
104
159
105
160
// Add the "serverConnectionId" if it is not nil.
106
161
if cmd .ServerConnectionID != nil {
107
- keysAndValues = append (keysAndValues ,
108
- "serverConnectionId" , * cmd .ServerConnectionID )
162
+ keysAndValues .Add (KeyServerConnectionID , * cmd .ServerConnectionID )
109
163
}
110
164
111
165
// Add the "serviceId" if it is not nil.
112
166
if cmd .ServiceID != nil {
113
- keysAndValues = append (keysAndValues ,
114
- "serviceId" , cmd .ServiceID .Hex ())
167
+ keysAndValues .Add (KeyServiceID , cmd .ServiceID .Hex ())
168
+ }
169
+
170
+ return keysAndValues
171
+ }
172
+
173
+ // Connection contains data that all connection log messages MUST contain.
174
+ type Connection struct {
175
+ Message string // Message associated with the connection
176
+ ServerHost string // Hostname or IP address for the server
177
+ ServerPort string // Port for the server
178
+ }
179
+
180
+ // SerializeConnection serializes a ConnectionMessage into a slice of keys
181
+ // and values that can be passed to a logger.
182
+ func SerializeConnection (conn Connection , extraKeysAndValues ... interface {}) []interface {} {
183
+ // Initialize the boilerplate keys and values.
184
+ keysAndValues := KeyValues {
185
+ KeyMessage , conn .Message ,
186
+ KeyServerHost , conn .ServerHost ,
187
+ }
188
+
189
+ // Add the optional keys and values.
190
+ for i := 0 ; i < len (extraKeysAndValues ); i += 2 {
191
+ keysAndValues .Add (extraKeysAndValues [i ].(string ), extraKeysAndValues [i + 1 ])
192
+ }
193
+
194
+ port , err := strconv .ParseInt (conn .ServerPort , 0 , 32 )
195
+ if err == nil {
196
+ keysAndValues .Add (KeyServerPort , port )
115
197
}
116
198
117
199
return keysAndValues
0 commit comments