You can use the com.snowflake.telemetry.Telemetry class in the Snowflake Telemetry API library to emit trace events from a function or
procedure handler written in Java. The Telemetry class is included with Snowflake.
Note
Using the Snowflake Telemetry Library adds other libraries to your function or procedure’s execution environment. For more information,
see Snowflake telemetry package dependencies.
To use Telemetry methods, you must make the open source Snowflake telemetry package available to your handler code.
In the PACKAGES clause in your CREATE PROCEDURE or CREATE FUNCTION statement, include the com.snowflake:telemetry package. The
PACKAGES clause makes the included Snowflake Telemetry API available to your code.
Code in the following example uses the PACKAGES clause to reference the Telemetry library as well as the Snowpark library (which is
required for stored procedures written in Java – for more information, see Writing Java handlers for stored procedures created with SQL).
You can add trace events by calling the Telemetry.addEvent method, passing a name for the event. You can also optionally associate
attributes – key-value pairs – with an event.
Code in the following example adds an event called testEvent, associating with the event two attributes: key and result.
// Adding an event without attributes.
Telemetry.addEvent("testEvent");
// Adding an event with attributes.AttributeseventAttributes= Attributes.of(
AttributeKey.stringKey("key"), "run",
AttributeKey.longKey("result"), Long.valueOf(123));
Telemetry.addEvent("testEventWithAttributes", eventAttributes);
Adding these events results in two rows in the event table, each with a different value in the RECORD column:
{"name":"testEvent"}
{"name":"testEventWithAttributes"}
The testEventWithAttributes event row includes the following attributes in the row’s RECORD_ATTRIBUTES column:
You can add custom spans that are separate from the default span created by Snowflake. For details on custom spans, see
Adding custom spans to a trace.
Code in the following example uses the OpenTelemetry API and OpenTelemetry context propagation API to create a new my.span
span. It then adds an event with attributes to the new span. Finally, the code ends the span to have the span’s event data
captured in the event table. If the code doesn’t call the Span.end method, data is not captured in the event table.
CREATE OR REPLACE FUNCTION customSpansJavaExample() RETURNS STRING
LANGUAGE JAVAPACKAGES= ('com.snowflake:telemetry:latest')
HANDLER = 'MyJavaClass.run'
as
$$
import com.snowflake.telemetry.Telemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Scope;
classMyJavaClass {
publicstatic String run() {
Tracertracer= GlobalOpenTelemetry.getTracerProvider().get("my.tracer");
Spanspan= tracer.spanBuilder("my.span").startSpan();
try (Scopescope= span.makeCurrent()) {
// Do processing, adding events that will be captured in a my.span.// Add an event with attributes.AttributeseventAttributes= Attributes.of(
AttributeKey.stringKey("key"), "run",
AttributeKey.longKey("result"), Long.valueOf(123));
span.addEvent("testEventWithAttributes", eventAttributes);
span.setAttribute("testAttribute", "value");
} finally {
span.end();
}
return"success";
}
}
$$;