Override the Tracer's Default Behavior
If you prefer to customize the function and business transaction names, or keep the
tracer settings in your code, you can override the environment variables using the
AppDynamics.Config.Builder
object. This option allows you to retrieve
values at runtime from any desired source.
AppDynamics.Config.Builder configBuilder = new AppDynamics.Config.Builder();
configBuilder.accountName(accountName)
.applicationName(appName)
.tierName(tierName)
.controllerHost(controllerHost)
.controllerPort(controllerPort)
.defaultBtName(context.getFunctionName() + "_bt")
.controllerAccessKey(controllerAccessKey)
.lambdaContext(context);
Implementation of the configBuilder
object differs slightly depending on
which instrumentation method you choose, automatic or manual.
Automatic Instrumentation
Automatic instrumentation uses the MonitoredRequestStreamHandler
class,
which implements a factory method, getConfigBuilder()
, to instrument the
tracer.
You can override this factory method and use any builder method to configure environment variables.
The code snippet shows how to enter variables in your code for automatic instrumentation:
@Override
public AppDynamics.Config.Builder getConfigBuilder(Context context) {
String controllerHost = System.getenv("APPDYNAMICS_CONTROLLER_HOST");
int controllerPort = Integer.parseInt(System.getenv("APPDYNAMICS_CONTROLLER_PORT"));
String accountName = System.getenv("APPDYNAMICS_ACCOUNT_NAME");
String appName = System.getenv("APPDYNAMICS_APPLICATION_NAME");
String tierName = System.getenv("APPDYNAMICS_TIER_NAME");
String controllerAccessKey = System.getenv("APPDYNAMICS_AGENT_ACCOUNT_ACCESS_KEY");
AppDynamics.Config.Builder configBuilder = new AppDynamics.Config.Builder();
configBuilder.accountName(accountName)
.applicationName(appName)
.tierName(tierName)
.controllerHost(controllerHost)
.controllerPort(controllerPort)
.defaultBtName(context.getFunctionName() + "_bt")
.controllerAccessKey(controllerAccessKey)
.lambdaContext(context);
return configBuilder;
}
Manual Instrumentation
In manual instrumentation, the overloaded version of the
AppDynamics.getTracer()
method configures the tracer's settings. The
overloaded method takes a configBuilder
object as a parameter, instead of
the context
object, to instrument the tracer.
When you use this variant of AppDynamics.getTracer()
, you must pass the
context
object via the
AppDynamics.Config.Builder.lambdaContext()
method.
The code snippet shows how to enter variables in your code for manual instrumentation.
@Override
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException) {
String controllerHost = System.getenv("APPDYNAMICS_CONTROLLER_HOST");
int controllerPort = Integer.parseInt(System.getenv("APPDYNAMICS_CONTROLLER_PORT"));
String accountName = System.getenv("APPDYNAMICS_ACCOUNT_NAME");
String appName = System.getenv("APPDYNAMICS_APPLICATION_NAME");
String tierName = System.getenv("APPDYNAMICS_TIER_NAME");
String controllerAccessKey = System.getenv("APPDYNAMICS_AGENT_ACCOUNT_ACCESS_KEY");
AppDynamics.Config.Builder configBuilder = new AppDynamics.Config.Builder();
configBuilder.accountName(accountName)
.applicationName(appName)
.tierName(tierName)
.controllerHost(controllerHost)
.controllerPort(controllerPort)
.defaultBtName(context.getFunctionName() + "_bt")
.controllerAccessKey(controllerAccessKey)
.lambdaContext(context);
//Replace the context object with config.Builder.build()
Tracer tracer = AppDynamics.getTracer(configBuilder.build());