Start and Stop a Transaction

transaction.start() and transaction.stop() methods to place boundaries around any section of code that you want the tracer to monitor.

As a best practice, you can report all events, even if your function crashes, by running your entire function within a try block, and then stopping the transaction within a finally block.

This code sample demonstrates how to start and stop a transaction that monitors an entire function:

@Override
 public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
  Tracer tracer = AppDynamics.getTracer(context);
  Transaction transaction = tracer.createTransaction(input, context);
 
	//Start the transaction monitoring.
  	transaction.start();
  	try { 
   		int letter = 0;
        while((letter = input.read()) >= 0) {
            output.write(Character.toUpperCase(letter));
		}
 
	//Stop the transaction monitoring. Place in a finally block to monitor all events. 
	} finally {
   		transaction.stop();
		AppDynamics.cleanup();
  	}  
 }