Skip to content

Proposed new performance API (runTransaction) #1154

@bruno-garcia

Description

@bruno-garcia

Discuss new API to take callback and wrap the whole transaction (to be discussed on the 11th of Jan)

The proposal for this API:

Sentry.runTransaction("name", tx -> {
  // some code
});

Where runTransaction method would look like:

public static void runTransaction(String transactionName, Consumer<ITransaction> runnable) {
  ITransaction transaction = startTransaction(transactionName);
  try {
    runnable.accept(transaction);
    transaction.setStatus(SpanStatus.OK);
	return callback();
  } catch (Throwable e) {
    transaction.setThrowable(e);
    transaction.setStatus(SpanStatus.INTERNAL_ERROR);
	throw e;
  } finally {
    transaction.finish();
  }
}

With such simple API we don't give a an option to set different status on error, but it can be a good enough tradeoff.

From: #1151 (comment)

First usage of it: #1058 (comment)

Downside is that we have to account for async and callbacks with return type.

Alternative

Alternative APIs to make it less boilerplate but without going full on wrapping a callback:

public static void myAppStuff() {
  ITransaction transaction = startTransaction("test");
  try {
	// whatever I do 
    transaction.finish(); // sets OK status
  } catch (Throwable e) {
    transaction.finish(e); // maps exception to status (options.ExceptionToStatusCallback)
	throw e;
  }
}

This way we can extend the mapping between Throwable and SpanStatus. Other overloads that take a HttpStatus for example could also fit well.
finish parameterless would assume OK.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions