Mstdlib-1.24.0
m_sql_trace.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2017 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_SQL_TRACE_H__
25#define __M_SQL_TRACE_H__
26
27/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
28
29#include <mstdlib/base/m_defs.h>
30#include <mstdlib/base/m_types.h>
31#include <mstdlib/sql/m_sql.h>
32#include <mstdlib/sql/m_sql_stmt.h>
33
34/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
35
36__BEGIN_DECLS
37
38/*! \addtogroup m_sql_trace SQL Pool Tracing
39 * \ingroup m_sql_conn
40 *
41 * SQL Pool Tracing
42 *
43 * @{
44 */
45
46/*! Event types for SQL pool tracing */
47typedef enum {
48 M_SQL_TRACE_CONNECTING = 1, /*!< Starting a new connection */
49 M_SQL_TRACE_CONNECTED = 2, /*!< New connection was successfully started */
50 M_SQL_TRACE_CONNECT_FAILED = 3, /*!< New connection failed */
51 M_SQL_TRACE_DISCONNECTING = 4, /*!< Starting graceful disconnect from server (idle, etc) */
52 M_SQL_TRACE_DISCONNECTED = 5, /*!< Graceful disconnect has completed */
53 M_SQL_TRACE_BEGIN_START = 6, /*!< SQL Transaction Begin starting. This may internally also
54 * call #M_SQL_TRACE_EXECUTE_START and #M_SQL_TRACE_EXECUTE_FINISH
55 * depending on how the driver handles transactions. */
56 M_SQL_TRACE_BEGIN_FINISH = 7, /*!< SQL Transaction Begin completed (possibly failed) */
57 M_SQL_TRACE_ROLLBACK_START = 8, /*!< SQL Transaction Rollback starting. This may internally also
58 * call #M_SQL_TRACE_EXECUTE_START and #M_SQL_TRACE_EXECUTE_FINISH
59 * depending on how the driver handles transactions. */
60 M_SQL_TRACE_ROLLBACK_FINISH = 9, /*!< SQL Transaction Rollback completed (possibly failed) */
61 M_SQL_TRACE_COMMIT_START = 10, /*!< SQL Transaction Commit starting. This may internally also
62 * call #M_SQL_TRACE_EXECUTE_START and #M_SQL_TRACE_EXECUTE_FINISH
63 * depending on how the driver handles transactions. On failure,
64 * this may also flow through #M_SQL_TRACE_ROLLBACK_START and
65 * #M_SQL_TRACE_ROLLBACK_FINISH */
66 M_SQL_TRACE_COMMIT_FINISH = 11, /*!< SQL Transaction Commit completed (possibly failed) */
67 M_SQL_TRACE_EXECUTE_START = 12, /*!< SQL Statement Execution started */
68 M_SQL_TRACE_EXECUTE_FINISH = 13, /*!< SQL Statement Execution finished (possibly failed) */
69 M_SQL_TRACE_FETCH_START = 14, /*!< SQL Statement Fetching result data started */
70 M_SQL_TRACE_FETCH_FINISH = 15, /*!< SQL Statement Fetching result data finished (possibly failed or canceled) */
71 M_SQL_TRACE_CONNFAIL = 16, /*!< Connection to SQL server failed unexpectedly */
72 M_SQL_TRACE_TRANFAIL = 17, /*!< SQL Transaction failed (duplicative of M_SQL_TRACE_EXECUTE_FINISH),
73 * but only on fatal (non-retryable) failure */
74 M_SQL_TRACE_DRIVER_DEBUG = 18, /*!< Debug/Informational message generated by driver */
75 M_SQL_TRACE_DRIVER_ERROR = 19, /*!< Error message generated by driver */
76 M_SQL_TRACE_STALL_QUERY = 20, /*!< Possibly stalled query. Only generated via M_sql_connpool_trace_stalls() */
77 M_SQL_TRACE_STALL_TRANS_IDLE = 21, /*!< Possibly stalled transaction that is not executing any queries. Only generated via M_sql_connpool_trace_stalls() */
78 M_SQL_TRACE_STALL_TRANS_LONG = 22 /*!< Possibly stalled transaction due to long run time. Only generated via M_sql_connpool_trace_stalls() */
80
81
82/*! Connection type */
83typedef enum {
84 M_SQL_CONN_TYPE_UNKNOWN = 0, /*!< Unknown, probably misuse */
85 M_SQL_CONN_TYPE_PRIMARY = 1, /*!< Primary (read/write) sub-pool */
86 M_SQL_CONN_TYPE_READONLY = 2 /*!< Read Only sub-pool */
88
89
90/*! Trace data */
91struct M_sql_trace_data;
92/*! Typedef for trace data */
93typedef struct M_sql_trace_data M_sql_trace_data_t;
94
95
96/*! Callback prototype used for tracing SQL subsystem events.
97 *
98 * \param[in] event_type The event type
99 * \param[in] data The metadata about the event, use the M_sql_trace_*() functions to get details.
100 * \param[in] arg User-supplied argument passed to the trace callback.
101 */
102typedef void (*M_sql_trace_cb_t)(M_sql_trace_t event_type, const M_sql_trace_data_t *data, void *arg);
103
104
105/*! Add a trace callback to the SQL subsystem.
106 *
107 * Only one trace callback can be registered per pool. If one is already registered,
108 * it will be replaced.
109 *
110 * \note This must be called prior to M_sql_connpool_start()
111 *
112 * \param[in] pool Initialized pool object by M_sql_connpool_create().
113 * \param[in] cb Callback to register
114 * \param[in] cb_arg User-supplied argument to pass to callback.
115 * \return M_TRUE on success, M_FALSE on misuse
116 */
117M_API M_bool M_sql_connpool_add_trace(M_sql_connpool_t *pool, M_sql_trace_cb_t cb, void *cb_arg);
118
119
120/*! Run a check to see if there are any stalled connections as per the provided criteria.
121 *
122 * This function is used to help identify if a connection or transaction appear to be
123 * stalled, whether due to user error, a server issue, or a bug in the client side
124 * library.
125 *
126 * It depends on M_sql_connpool_add_trace() having already been called and will
127 * iterate across all non-idle connections looking for stalls. Output from this function
128 * will use the registered trace callback.
129 *
130 * Since SQL is not implemented asynchronously this must be called from another thread
131 * periodically to look for stalls.
132 *
133 * \param[in] pool Initialized pool object by M_sql_connpool_create(), that has had M_sql_connpool_add_trace()
134 * called.
135 * \param[in] max_query_s Maximum query run time before it is considered stalled in seconds. A good starting place may
136 * be 60s. Use 0 to ignore this metric.
137 * \param[in] max_trans_idle_s Maximum transaction time where a connection is held but the transaction is *idle*, helps
138 * identify user error such as leaving a transaction open or a long-running operation while
139 * holding a connection. A good starting place can be fairly short, like 10s. Use 0 to
140 * ignore this metric.
141 * \param[in] max_trans_s Maximum transaction overall time, regardless if queries are actively executing. Can help
142 * identify transactions that might need to be split into smaller operations. A good starting
143 * place should be fairly long, such as 5minutes/300s. Use 0 to ignore this metric.
144 */
145M_API void M_sql_connpool_trace_stalls(M_sql_connpool_t *pool, M_uint64 max_query_s, M_uint64 max_trans_idle_s, M_uint64 max_trans_s);
146
147
148/*! Set a flag on the statement to ensure a #M_SQL_TRACE_TRANFAIL is not triggered
149 * in the event of a failure.
150 *
151 * This is used to silence warnings in the trace system for failures that may
152 * be expected. For instance, this is used internally by M_sql_table_exists()
153 * otherwise a warning might be emitted when the table does not exist.
154 *
155 * \param[in] stmt Initialized statement handle to apply flag
156 */
158
159
160/*! Retrieve the error string containing the most recent error condition.
161 *
162 * Only Valid on:
163 * - #M_SQL_TRACE_CONNECT_FAILED
164 * - #M_SQL_TRACE_BEGIN_FINISH
165 * - #M_SQL_TRACE_ROLLBACK_FINISH
166 * - #M_SQL_TRACE_COMMIT_FINISH
167 * - #M_SQL_TRACE_EXECUTE_FINISH
168 * - #M_SQL_TRACE_FETCH_FINISH
169 * - #M_SQL_TRACE_CONNFAIL
170 * - #M_SQL_TRACE_TRANFAIL
171 * - #M_SQL_TRACE_DRIVER_DEBUG
172 * - #M_SQL_TRACE_DRIVER_ERROR
173 *
174 * \param[in] data Trace Data structure passed to trace callback
175 * \return Pointer to error string, or NULL if no error string available.
176 */
178
179
180/*! Retrieve the most recent error condition identifier.
181 *
182 * Only valid on:
183 * - #M_SQL_TRACE_CONNECT_FAILED
184 * - #M_SQL_TRACE_BEGIN_FINISH
185 * - #M_SQL_TRACE_ROLLBACK_FINISH
186 * - #M_SQL_TRACE_COMMIT_FINISH
187 * - #M_SQL_TRACE_EXECUTE_FINISH
188 * - #M_SQL_TRACE_FETCH_FINISH
189 * - #M_SQL_TRACE_CONNFAIL
190 * - #M_SQL_TRACE_TRANFAIL
191 *
192 * \param[in] data Trace Data structure passed to trace callback
193 * \return Most recent error code, possibly #M_SQL_ERROR_SUCCESS if no error.
194 */
196
197
198/*! Retrieve the duration, in milliseconds of operation
199 *
200 * Only valid on:
201 * - #M_SQL_TRACE_CONNECTED - Time to establish connection
202 * - #M_SQL_TRACE_DISCONNECTING - Time connection was up before disconnect was attempted.
203 * - #M_SQL_TRACE_DISCONNECTED - Time connection took to disconnect (from start of disconnect)
204 * - #M_SQL_TRACE_CONNECT_FAILED - Time it took for connection to fail.
205 * - #M_SQL_TRACE_BEGIN_FINISH - Time it took to begin a transaction.
206 * - #M_SQL_TRACE_ROLLBACK_FINISH - Time it took to rollback.
207 * - #M_SQL_TRACE_COMMIT_FINISH - Time it took to commit a transaction.
208 * - #M_SQL_TRACE_EXECUTE_FINISH - Time it took to execute the transaction.
209 * - #M_SQL_TRACE_FETCH_FINISH - Time it took to retrieve the rows after execution.
210 * - #M_SQL_TRACE_CONNFAIL - Time connection was up before a failure was detected.
211 * - #M_SQL_TRACE_TRANFAIL - Time query execution took before failure was returned.
212 * - #M_SQL_TRACE_STALL_QUERY - Current runtime of query.
213 * - #M_SQL_TRACE_STALL_TRANS_IDLE - Time since last query was performed.
214 * - #M_SQL_TRACE_STALL_TRANS_LONG - Time since transaction was started.
215
216 *
217 * \param[in] data Trace Data structure passed to trace callback
218 * \return Time in milliseconds.
219 */
221
222
223/*! Retrieve the total duration of a sequence of events, for a limited set of
224 * events.
225 *
226 * Only valid on:
227 * - #M_SQL_TRACE_FETCH_FINISH - Total time of query execution plus row fetch time.
228 * - #M_SQL_TRACE_DISCONNECTED - Total time from connection establishment to disconnect end.
229 * - #M_SQL_TRACE_STALL_TRANS_IDLE - Total time since transaction was started.
230 *
231 * \param[in] data Trace Data structure passed to trace callback
232 * \return Time in milliseconds.
233 */
235
236
237/*! Retreive type of connection (Primary vs ReadOnly)
238 *
239 * Available on all
240 *
241 * \param[in] data Trace Data structure passed to trace callback
242 * \return Type of connection
243 */
245
246
247/*! Retrieve the internal connection id, enumerated from 0 - max_conns for each
248 * primary and readonly member pool.
249 *
250 * Available on all
251 *
252 * \param[in] data Trace Data structure passed to trace callback
253 * \return Identifier
254 */
256
257
258/*! Retreive the user-supplied query being executed.
259 *
260 * Only available on:
261 * - #M_SQL_TRACE_EXECUTE_START
262 * - #M_SQL_TRACE_EXECUTE_FINISH
263 * - #M_SQL_TRACE_FETCH_START
264 * - #M_SQL_TRACE_FETCH_FINISH
265 * - #M_SQL_TRACE_TRANFAIL
266 * - #M_SQL_TRACE_STALL_QUERY
267 * - #M_SQL_TRACE_STALL_TRANS_LONG (possibly)
268 *
269 * \param[in] data Trace Data structure passed to trace callback
270 * \return User-supplied query.
271 */
272M_API const char *M_sql_trace_get_query_user(const M_sql_trace_data_t *data);
273
274
275/*! Retrieve string for prepared query (rewritten by driver) that has been
276 * executed by the server.
277 *
278 * Only available on:
279 * - #M_SQL_TRACE_EXECUTE_FINISH
280 * - #M_SQL_TRACE_FETCH_START
281 * - #M_SQL_TRACE_FETCH_FINISH
282 * - #M_SQL_TRACE_TRANFAIL
283 * - #M_SQL_TRACE_STALL_QUERY
284 * - #M_SQL_TRACE_STALL_TRANS_LONG (possibly)
285 *
286 * \param[in] data Trace Data structure passed to trace callback
287 * \return Rewritten user-supplied query.
288 */
290
291/*! Retrieve a fake formatted string with substituted data instead of seeing
292 * prepared statement place holders. This can be useful for debugging but is
293 * quite heavy.
294 *
295 * Only available on:
296 * - #M_SQL_TRACE_EXECUTE_FINISH
297 * - #M_SQL_TRACE_FETCH_START
298 * - #M_SQL_TRACE_FETCH_FINISH
299 * - #M_SQL_TRACE_TRANFAIL
300 * - #M_SQL_TRACE_STALL_QUERY
301 * - #M_SQL_TRACE_STALL_TRANS_LONG (possibly)
302 *
303 * \param[in] data Trace Data structure passed to trace callback
304 * \return Rewritten query. Must be M_free()'d by caller.
305 */
307
308/*! Retrieve the number of request columns bound to the query
309 *
310 * Only available on:
311 * - #M_SQL_TRACE_EXECUTE_START
312 * - #M_SQL_TRACE_EXECUTE_FINISH
313 * - #M_SQL_TRACE_FETCH_START
314 * - #M_SQL_TRACE_FETCH_FINISH
315 * - #M_SQL_TRACE_TRANFAIL
316 * - #M_SQL_TRACE_STALL_QUERY
317 * - #M_SQL_TRACE_STALL_TRANS_LONG (possibly)
318 *
319 * \param[in] data Trace Data structure passed to trace callback
320 * \return Number of columns bound to the query by the caller.
321 */
323
324
325/*! Retrieve the number of request rows (total) bound to the query
326 *
327 * Only available on:
328 * - #M_SQL_TRACE_EXECUTE_START
329 * - #M_SQL_TRACE_EXECUTE_FINISH
330 * - #M_SQL_TRACE_FETCH_START
331 * - #M_SQL_TRACE_FETCH_FINISH
332 * - #M_SQL_TRACE_TRANFAIL
333 * - #M_SQL_TRACE_STALL_QUERY
334 * - #M_SQL_TRACE_STALL_TRANS_LONG (possibly)
335 *
336 * \param[in] data Trace Data structure passed to trace callback
337 * \return Total number of rows bound to the query by the caller.
338 */
340
341
342/*! Retrieve the number of request rows (current subset) bound to the query
343 *
344 * Only available on:
345 * - #M_SQL_TRACE_EXECUTE_START
346 * - #M_SQL_TRACE_EXECUTE_FINISH
347 * - #M_SQL_TRACE_FETCH_START
348 * - #M_SQL_TRACE_FETCH_FINISH
349 * - #M_SQL_TRACE_TRANFAIL
350 * - #M_SQL_TRACE_STALL_QUERY
351 * - #M_SQL_TRACE_STALL_TRANS_LONG (possibly)
352 *
353 * \param[in] data Trace Data structure passed to trace callback
354 * \return Subset of rows currently being executed.
355 */
357
358
359/*! Retrieve the number of processed rows bound to the query (including current subset)
360 *
361 * Only available on:
362 * - #M_SQL_TRACE_EXECUTE_START
363 * - #M_SQL_TRACE_EXECUTE_FINISH
364 * - #M_SQL_TRACE_FETCH_START
365 * - #M_SQL_TRACE_FETCH_FINISH
366 * - #M_SQL_TRACE_TRANFAIL
367 * - #M_SQL_TRACE_STALL_QUERY
368 * - #M_SQL_TRACE_STALL_TRANS_LONG (possibly)
369 *
370 * \param[in] data Trace Data structure passed to trace callback
371 * \return Subset of rows that have been executed or are being executed.
372 */
374
375
376/*! Retrieve whether or not the query potentially has result data.
377 *
378 * If the query has result data, and this is a #M_SQL_TRACE_EXECUTE_FINISH,
379 * then you know for sure #M_SQL_TRACE_FETCH_START/#M_SQL_TRACE_FETCH_FINISH
380 * will also be called later.
381 *
382 * Only available on:
383 * - #M_SQL_TRACE_EXECUTE_FINISH
384 * - #M_SQL_TRACE_FETCH_START
385 * - #M_SQL_TRACE_FETCH_FINISH
386 * - #M_SQL_TRACE_STALL_QUERY (possibly)
387 * - #M_SQL_TRACE_STALL_TRANS_LONG (possibly)
388 *
389 * \param[in] data Trace Data structure passed to trace callback
390 * \return M_TRUE if the query could have result data, M_FALSE otherwise.
391 */
393
394
395/*! Retrieve the number of rows affected by a query.
396 *
397 * This mostly applies to INSERT/UPDATE/DELETE type queries.
398 *
399 * Only available on:
400 * - #M_SQL_TRACE_EXECUTE_FINISH
401 *
402 * \param[in] data Trace Data structure passed to trace callback
403 * \return Count of affected rows.
404 */
406
407
408/*! Retrieve the total number of rows fetched from the server.
409 *
410 * Only available on:
411 * - #M_SQL_TRACE_FETCH_FINISH
412 *
413 * \param[in] data Trace Data structure passed to trace callback
414 * \return Count of retrieved rows.
415 */
417
418
419/*! @} */
420
421__END_DECLS
422
423#endif /* __M_SQL_TRACE_H__ */
struct M_sql_connpool M_sql_connpool_t
Definition: m_sql.h:335
M_sql_error_t
Definition: m_sql.h:190
struct M_sql_stmt M_sql_stmt_t
Definition: m_sql_stmt.h:46
struct M_sql_trace_data M_sql_trace_data_t
Definition: m_sql_trace.h:93
size_t M_sql_trace_get_result_row_count(const M_sql_trace_data_t *data)
void M_sql_trace_ignore_tranfail(M_sql_stmt_t *stmt)
M_bool M_sql_trace_get_has_result_rows(const M_sql_trace_data_t *data)
void M_sql_connpool_trace_stalls(M_sql_connpool_t *pool, M_uint64 max_query_s, M_uint64 max_trans_idle_s, M_uint64 max_trans_s)
const char * M_sql_trace_get_query_user(const M_sql_trace_data_t *data)
size_t M_sql_trace_get_conn_id(const M_sql_trace_data_t *data)
const char * M_sql_trace_get_error_string(const M_sql_trace_data_t *data)
void(* M_sql_trace_cb_t)(M_sql_trace_t event_type, const M_sql_trace_data_t *data, void *arg)
Definition: m_sql_trace.h:102
M_sql_conn_type_t
Definition: m_sql_trace.h:83
M_sql_trace_t
Definition: m_sql_trace.h:47
M_bool M_sql_connpool_add_trace(M_sql_connpool_t *pool, M_sql_trace_cb_t cb, void *cb_arg)
const char * M_sql_trace_get_query_prepared(const M_sql_trace_data_t *data)
M_uint64 M_sql_trace_get_total_duration_ms(const M_sql_trace_data_t *data)
size_t M_sql_trace_get_bind_cols(const M_sql_trace_data_t *data)
M_uint64 M_sql_trace_get_duration_ms(const M_sql_trace_data_t *data)
size_t M_sql_trace_get_bind_rows_processed(const M_sql_trace_data_t *data)
size_t M_sql_trace_get_affected_rows(const M_sql_trace_data_t *data)
M_sql_error_t M_sql_trace_get_error(const M_sql_trace_data_t *data)
M_sql_conn_type_t M_sql_trace_get_conntype(const M_sql_trace_data_t *data)
size_t M_sql_trace_get_bind_rows_current(const M_sql_trace_data_t *data)
size_t M_sql_trace_get_bind_rows(const M_sql_trace_data_t *data)
char * M_sql_trace_get_query_formatted(const M_sql_trace_data_t *data)
@ M_SQL_CONN_TYPE_READONLY
Definition: m_sql_trace.h:86
@ M_SQL_CONN_TYPE_UNKNOWN
Definition: m_sql_trace.h:84
@ M_SQL_CONN_TYPE_PRIMARY
Definition: m_sql_trace.h:85
@ M_SQL_TRACE_BEGIN_START
Definition: m_sql_trace.h:53
@ M_SQL_TRACE_EXECUTE_FINISH
Definition: m_sql_trace.h:68
@ M_SQL_TRACE_STALL_TRANS_IDLE
Definition: m_sql_trace.h:77
@ M_SQL_TRACE_STALL_QUERY
Definition: m_sql_trace.h:76
@ M_SQL_TRACE_CONNFAIL
Definition: m_sql_trace.h:71
@ M_SQL_TRACE_DISCONNECTING
Definition: m_sql_trace.h:51
@ M_SQL_TRACE_FETCH_START
Definition: m_sql_trace.h:69
@ M_SQL_TRACE_FETCH_FINISH
Definition: m_sql_trace.h:70
@ M_SQL_TRACE_STALL_TRANS_LONG
Definition: m_sql_trace.h:78
@ M_SQL_TRACE_CONNECTED
Definition: m_sql_trace.h:49
@ M_SQL_TRACE_DISCONNECTED
Definition: m_sql_trace.h:52
@ M_SQL_TRACE_ROLLBACK_FINISH
Definition: m_sql_trace.h:60
@ M_SQL_TRACE_BEGIN_FINISH
Definition: m_sql_trace.h:56
@ M_SQL_TRACE_DRIVER_ERROR
Definition: m_sql_trace.h:75
@ M_SQL_TRACE_TRANFAIL
Definition: m_sql_trace.h:72
@ M_SQL_TRACE_EXECUTE_START
Definition: m_sql_trace.h:67
@ M_SQL_TRACE_COMMIT_FINISH
Definition: m_sql_trace.h:66
@ M_SQL_TRACE_ROLLBACK_START
Definition: m_sql_trace.h:57
@ M_SQL_TRACE_CONNECTING
Definition: m_sql_trace.h:48
@ M_SQL_TRACE_CONNECT_FAILED
Definition: m_sql_trace.h:50
@ M_SQL_TRACE_COMMIT_START
Definition: m_sql_trace.h:61
@ M_SQL_TRACE_DRIVER_DEBUG
Definition: m_sql_trace.h:74