Create a Transaction
Next, you need to create transactions. A transaction is a monitored segment in a larger business transaction. Creating a transaction involves locating the correlation header, which the tracer uses to pass contextual information downstream.
Transactions either:
- Start a new business transaction, or
- Continue an existing business transaction
Start a New Business Transaction
A transaction that originates in the current function does not have an inbound correlation
header. To create a business transaction, use the createTransaction()
method and provide an empty correlation header.
Continue an Existing Business Transaction
To continue an existing business transaction, you need to locate an inbound correlation header. You can locate a correlation header automatically or manually.
If the tracer cannot find the correlation header, the tracer creates a new business transaction.
Continue a Business Transaction by Locating the Correlation Header Automatically
Use the createTransaction(inputStream, context)
method to create a
transaction that locates the correlation header automatically. This method searches your
code's object schema for a key called singularityheader
in the
inputStream
object. The correlation header finds the
singularityheader
key, then continues the business transaction.
inputStream
object, which is configured to be read
once. If your application needs to read the inputStream
object, you must use a converter
method to allow both the tracer and your application to read the stream.The code snippet shows an example of how to create a transaction and automatically locate the correlation header:
public class <LambdaChops> implements RequestStreamHandler {
@Override
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
Tracer tracer = AppDynamics.getTracer(context);
//Use a converter method if you need to read your inputStream more than once. The tracer reads your inputStream once.
inputStream = InputStreamConverter.convertToMarkSupportedInputStream(input);
//Create a transaction.
Transaction transaction = tracer.createTransaction(input, context);
//Your AWS Lambda function code begins here.
int letter = 0;
while((letter = input.read()) >= 0) {
output.write(Character.toUpperCase(letter));
}
}
}
Create a Transaction by Locating the Correlation Header Manually
To create a transaction that locates the correlation header using custom logic, you need to
manually parse a correlationHeader
string. Next, call the
createTransaction()
method and provide the
correlationHeader
object. This process gives you full control over the
parsing of your function's input payload.
The code snippet shows an example of how to obtain the correlation header string and create a transaction:
public class <LambdaChops> implements RequestHandler {
@Override
public O handleRequest(InputStream input, OutputStream output, Context context) {
Tracer tracer = AppDynamics.getTracer(context);
//Manually parse the correlation string.
String inputAsString = IOUtils.toString(inputStream, Charset.forName("UTF-8"));
JsonParser parser = new JsonParser();
JsonObject inputObject = parser.parse(inputAsString).getAsJsonObject();
String correlationHeader = "";
if (inputObject.has(Tracer.APPDYNAMICS_TRANSACTION_CORRELATION_HEADER_KEY)) {
correlationHeader = inputObject.get(Tracer.APPDYNAMICS_TRANSACTION_CORRELATION_HEADER_KEY).getAsString();
} else {
// Try reading from HTTP headers
if (inputObject.has("headers")) {
JsonObject httpHeaders = inputObject.getAsJsonObject("headers");
if (httpHeaders.has(Tracer.APPDYNAMICS_TRANSACTION_CORRELATION_HEADER_KEY)) {
correlationHeader = httpHeaders.get(Tracer.APPDYNAMICS_TRANSACTION_CORRELATION_HEADER_KEY).getAsString();
}
}
}
// Create transaction object using the correlation header. If the correlationHeader string is empty, the transaction you create uses the default transaction name.
Transaction transaction = tracer.createTransaction(correlationHeader);
//Your AWS Lambda function code starts here
...
}