Setting Up Tracing

Trace messages from the DBMS such as query execution plans (QEPs) or I/O traces arrive at unpredictable times. They need to be handled using callback functions. See Using Callbacks for more information.

Note

A trace -handling callback is installed in the OpenAPI environment. It is not session-specific. If multiple sessions are started by the application it is good to be aware of which session has enabled tracing and to avoid tracing multiple sessions at the same time.

Note

This example makes use of the IIapi_getCallbackPtr() and IIapi_getClosure() helper functions provided by pyngres. They are not part of the Actian OpenAPI.

Tip

Refer to the ctypes documentation for additional explanation of callbacks.

Download

 1import pyngres as py
 2from loguru import logger
 3
 4# initialize the OpenAPI and note the envHandle for use later
 5inp = py.IIAPI_INITPARM()
 6inp.in_timeout = -1
 7inp.in_version = py.IIAPI_VERSION
 8
 9py.IIapi_initialize(inp)
10envHandle = inp.in_envHandle
11
12...
13
14# create iiapi_trace() as the callback for trace messages
15@py.IIapi_callback
16def iiapi_trace(trace_block,parmBlock=None):
17    '''trace-message call-back'''
18
19    # get the trace_block argument as a Python IIAPI_TRACEPARM instance
20    trp = py.IIapi_getClosure(trace_block,py.IIAPI_TRACEPARM)
21    # extract the tr_message member containing a line of trace info
22    tr_message = trp.tr_message.decode().rstrip()
23    # do something with the trace-message (in this example, send it to loguru)
24    logger.info(tr_message)
25
26...
27
28# install iiapi_trace() as the callback to handle trace-messages
29tracer = py.IIapi_getCallbackPtr(iiapi_trace)
30sep = py.IIAPI_SETENVPRMPARM()
31sep.se_envHandle = envHandle
32sep.se_paramID = py.IIAPI_EP_TRACE_FUNC
33sep.se_paramValue = tracer
34py.IIapi_setEnvParam(sep)

Download