Skip to content

Diagnostic tracing

Mechanism

Actian's Diagnostics.LogValue converts a value to text for its message and returns the original value through Diagnostics.Trace. If the conversion fails, the message says <error getting value> instead, and the value is still returned (line 2).1

LogValue2 takes a separate result expression and an optional delayed flag. The logged text comes from value, while the returned computation is result, so the two do not have to be the same object (line 3).1

LogFailure evaluates function() under try. On failure it logs the error through LogValue2 with delayed set to true and the expression () => error result[Error], so the error is raised again when the value is used. On success it returns result[Value] (lines 4–8). WrapFunctionResult and WrapHandlers apply LogFailure to each field of a handler record (lines 10–18).1

Registration and gating

Actian.m sets EnableTraceOutput = false (line 9), and its comment says the value should be false before release (lines 5–8). Its Diagnostics.LogValue is if (EnableTraceOutput) then Diagnostics[LogValue] else (prefix, value) => value (line 379), so disabled calls return the value unchanged.3

Actian.m uses only LogValue from the module. LogFailure and WrapHandlers are defined in Diagnostics.pqm but no code in Actian.m wires them in. Do not describe Actian as wrapping its handlers.3

Anaplan differs in two ways. It registers Diagnostics.LogValue = Diagnostics[LogValue] unconditionally (Anaplan.pq line 811), and its module's LogValue takes an optional delayed argument. Its serializer uses Expression.Constant for dates, times, numbers and text, where Actian writes date and time text by hand.4

Choosing a helper

Use LogValue for an inline trace point that must return its value unchanged. Use LogFailure or WrapHandlers when a handler should log its failure and still propagate it. Anaplan's unconditional registration shows the form for tracing that is not gated by a switch, but the live Anaplan code does not call it; its only call is in a commented block (line 707). Treat it as an unused pattern unless you add a call site.5

Adaptation and failure paths

Check whether a wrapper keeps the original evaluation timing as well as the return value. The delayed error expression () => error result[Error] (line 8) is part of the propagation behavior. Replacing it with an eager error or a null value changes the operation being observed.1

SQLColumns places Diagnostics.LogValue calls inside an if condition so the traces run before the table is used. The source comment says so (line 180). The comparisons against "***" are how that evaluation is forced, not a data check.2

The serializer visits every record field and writes its value (lines 101–108). It does not mask anything, so a credential, a token, or a whole HTTP payload passed to tracing will be written to the log. Pass deliberate fields instead. The top-level try Serialize(value) otherwise "<serialization failed>" catches a serializer failure (line 266), and LogValue still returns value, so the original error still propagates.1

Check the module's switch before shipping. With tracing on, SQLGetTypeInfo and SQLColumns log every row (lines 158–190), which can be large. Verify the output size on a real query before enabling it.2

See credential access for what must stay out of traces, structured errors for the error shapes that traces will reproduce, and buffering for evaluation timing.

Examples

Actian/Diagnostics.pqm · lines 1–18 Open original source ↗

let
    Diagnostics.LogValue = (prefix, value) => Diagnostics.Trace(TraceLevel.Information, prefix & ": " & (try Diagnostics.ValueToText(value) otherwise "<error getting value>"), value),
    Diagnostics.LogValue2 = (prefix, value, result, optional delayed) => Diagnostics.Trace(TraceLevel.Information, prefix & ": " & Diagnostics.ValueToText(value), result, delayed),    
    Diagnostics.LogFailure = (text, function) =>
        let
            result = try function()
        in
            if result[HasError] then Diagnostics.LogValue2(text, result[Error], () => error result[Error], true) else result[Value],

    Diagnostics.WrapFunctionResult = (innerFunction as function, outerFunction as function) as function =>
        Function.From(Value.Type(innerFunction), (list) => outerFunction(() => Function.Invoke(innerFunction, list))),

    Diagnostics.WrapHandlers = (handlers as record) as record =>
        Record.FromList(
            List.Transform(
                Record.FieldNames(handlers),
                (h) => Diagnostics.WrapFunctionResult(Record.Field(handlers, h), (fn) => Diagnostics.LogFailure(h, fn))),
            Record.FieldNames(handlers)),

The SQLColumns override uses the logging calls in its condition:

Actian/Actian.m · lines 178–190 Open original source ↗

        SQLColumns = (catalogName, schemaName, tableName, columnName, source) =>
            if (EnableTraceOutput <> true) then source else
            // the if statement conditions will force the values to evaluated/written to diagnostics
            if (Diagnostics.LogValue("SQLColumns.TableName", tableName) <> "***" and Diagnostics.LogValue("SQLColumns.ColumnName", columnName) <> "***") then
                let
                    // Outputting the entire table might be too large, and result in the value being truncated.
                    // We can output a row at a time instead with Table.TransformRows()
                    rows = Table.TransformRows(source, each Diagnostics.LogValue("SQLColumns", _)),
                    toTable = Table.FromRecords(rows)
                in
                    Value.ReplaceType(toTable, Value.Type(source))
            else
                source,

The module load and the gated registration:

Actian/Actian.m · lines 371–383 Open original source ↗

Extension.LoadFunction = (name as text) =>
    let
        binary = Extension.Contents(name),
        asText = Text.FromBinary(binary)
    in
        Expression.Evaluate(asText, #shared);
// Diagnostics module contains multiple functions. We can take the ones we need.
Diagnostics = Extension.LoadFunction("Diagnostics.pqm");
Diagnostics.LogValue = if (EnableTraceOutput) then Diagnostics[LogValue] else (prefix, value) => value;
// OdbcConstants contains numeric constants from the ODBC header files, and a 
// helper function to create bitfield values.
ODBC = Extension.LoadFunction("OdbcConstants.pqm");
Odbc.Flags = ODBC[Flags];

  1. Actian/Diagnostics.pqm lines 1–18: LogValue (line 2), LogValue2 (line 3), LogFailure with its delayed error (lines 4–8), WrapFunctionResult and WrapHandlers (lines 10–18); the serializer's record branch (lines 101–108) and its top-level try fallback (line 266). ↩↩↩↩↩

  2. Actian/Actian.m lines 178–190: the gated SQLColumns override, the forced-evaluation comment (line 180), and the per-row LogValue call (line 185); SQLGetTypeInfo (lines 158–166) logs rows the same way. ↩↩

  3. Actian/Actian.m lines 371–383: module load (line 378) and the gated Diagnostics.LogValue (line 379); the switch itself is EnableTraceOutput = false at line 9. ↩↩

  4. Anaplan/Diagnostics.pqm line 2 defines LogValue with an optional delayed argument; lines 138–154 serialize dates, times, numbers and text with Expression.Constant. ↩

  5. Anaplan/Anaplan.pq lines 810–811 load the module and register Diagnostics.LogValue = Diagnostics[LogValue] without a switch. ↩