Emitting Trace Events in JavaScriptΒΆ

You can use the snowflake class in the Snowflake JavaScript API to emit trace events from a function or procedure handler written in JavaScript. The JavaScript API is already available to your JavaScript handler code.

Before emitting trace events, be sure you have the trace level set so that the data you want are stored in the event table. For more information, refer to Setting trace level.

Note

Before you can begin emitting trace events, you must set up an event table. For more information, refer to Setting up an Event Table.

You can access stored trace event data by executing a SELECT command on the event table. For more information, refer to Accessing Trace Data.

For general information about setting up logging and retrieving messages in Snowflake, refer to Trace Events for Functions and Procedures.

Note

For guidelines to keep in mind when adding trace events, refer to General Guidelines for Adding Trace Events.

Adding Trace EventsΒΆ

You can add trace events by calling the snowflake.addEvent function, passing a name for the event. You can also optionally associate attributes – key-value pairs – with an event.

The addEvent method is available in the following form:

snowflake.addEvent(name [, { key:value [, key:value] } ] );
Copy

Handler code in the following example adds two events, name_a and name_b. With name_b, the code also adds two attributes, score and pass.

create procedure PI_JS()
  returns double
  language javascript
  as
  $$
    snowflake.addEvent('name_a');  // add an event without attributes
    snowflake.addEvent('name_b', {'score': 89, 'pass': true});
    return 3.14;
  $$
  ;
Copy

Setting these attributes results in two rows in the event table, each with a different value in the RECORD column:

{
  "name": "name_a"
}
Copy
{
  "name": "name_b"
}
Copy

The name_b event row includes the following attributes in the row’s RECORD_ATTRIBUTES column:

{
  "score": 89,
  "pass": true
}
Copy

Adding Span AttributesΒΆ

You can set attributes – key-value pairs – associated with spans by calling the snowflake.setSpanAttribute function.

The setSpanAttribute function is available in the following form:

snowflake.setSpanAttribute(key, value);
Copy

For details on spans, see How Snowflake represents trace events.

Code in the following example creates four attributes and sets their values:

// Setting span attributes.
snowflake.setSpanAttribute("example.boolean", true);
snowflake.setSpanAttribute("example.long", 2L);
snowflake.setSpanAttribute("example.double", 2.5);
snowflake.setSpanAttribute("example.string", "testAttribute");
Copy

Setting these attributes results in the following in the event table’s RECORD_ATTRIBUTES column:

{
  "example.boolean": true,
  "example.long": 2,
  "example.double": 2.5,
  "example.string": "testAttribute"
}
Copy