Custom Instrumentation

To instrument certain regions of your code, you can create transactions to capture them.

Copied
import Sentry;

// Transaction can be started by providing the name and the operation
let transaction = SentrySDK.startTransaction(
  name: "transaction-name",
  operation: "transaction-operation"
)

// Transactions can have child spans, and those spans can have child spans as well.
let span = transaction.startChild(operation: "child-operation")

// ...
// Perform your operations
// ...

span.finish(); // Remember that only finished spans will be sent with the transaction
transaction.finish(); // Finishing the transaction will send it to Sentry

For example, if you want to create a transaction for a user interaction in your application:

Copied
// Let's say this method is called in a background thread when a user clicks on the checkout button of your shop
func performCheckout()
{
  // This will create a new Transaction for you
  let transaction = SentrySDK.startTransaction(
      name: "checkout", 
      operation: "perform-checkout" 
  )

  // Validate the cart
  let validationSpan = transaction.startChild(
      operation: "validation", 
      description: "validating shopping cart"
  )

  validateShoppingCart() //Some long process, maybe a sync http request.

  validationSpan.finish()

  // Process the order
  let processSpan = transaction.startChild(
      operation: "process", 
      description: "processing shopping cart"
  )

  processShoppingCart() //Another time consuming process.

  processSpan.finish();

  transaction.finish();
}

This example will send a transaction named checkout to Sentry. The transaction will contain a validation span that measures how long validateShoppingCart() took and a process span that measures processShoppingCart(). Finally, the call to transaction.finish() will finish the transaction and send it to Sentry.

Retrieve a Transaction

In cases where you want to attach Spans to an already ongoing Transaction you can use Sentry.span. This method will return a SpanProtocol in case there is a running Transaction or a Span in case there is already a running Span, otherwise it returns nil.

Copied
import Sentry

var span = SentrySDK.span

if span == nil {
    span = SentrySDK.startTransaction(name: "task", operation: "op")
} else {
    span = span.startChild(operation: "subtask")
}