Documentation forAppOptics

Go Agent/Serverless

The AppOptics Go agent offers a convenience wrapper to trace AWS Lambda functions, which can be used in combination with the instrumentation SDK to get additional insight into your function code. Ensure that prerequisites described in tracing are set up before proceeding.

Instrumentation

To use the agent, include github.com/appoptics/appoptics-apm-go/v1/contrib/lambda (or github.com/appoptics/appoptics-apm-go if the SDK is used instead) as dependencies in your Lambda function, and import the following path in the function handler code:

Copy
aolambda "github.com/appoptics/appoptics-apm-go/v1/contrib/lambda"

There are two ways to instrument Lambda functions. The easiest is to use the wrapper provided by the Go agent, which automatically traces the given handler:

Copy
lambda.Start(aolambda.Wrap(yourHandler))

An example of instrumenting your Lambda function with the Go agent wrapper:

Copy
package main
import (
   "context"
   "github.com/aws/aws-lambda-go/events"
   "github.com/aws/aws-lambda-go/lambda"
   aolambda "github.com/appoptics/appoptics-apm-go/v1/contrib/lambda"
)
func handler(ctx context.Context, request events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
      // do some work
       return events.APIGatewayV2HTTPResponse{
          Headers: request.QueryStringParameters,
          StatusCode: 200,
       }, nil
}
func main() {
   lambda.Start(aolambda.Wrap(handler))
}

Instead of the wrapper, you can also use the standard Go agent Instrumentation SDK to manually create traces and custom metrics.

The agent automatically detects when it is run in the AWS Lambda environment and switches to lambda mode; you may also explicitly switch the agent into lambda mode by defining the environment variable APPOPTICS_REPORTER=serverless.

Configuration

With a few exceptions, the standard agent configuration settings are supported in the AWS Lambda environment and can be set as Lambda function environment variables. See Using AWS Lambda environment variables in the Amazon Web Services documentation for more information about environment variables.

The following environment variables can be particularly useful:

  • APPOPTICS_DISABLED - can be set to “true” to disable the agent
  • APPOPTICS_DEBUG_LEVEL - can be used to turn on more verbose logging for diagnostics

The exceptions are described below.

  • APPOPTICS_SERVICE_KEY - not required since the AppOptics API Token is set during forwarder install, and the Service Name is set during the subscription filter configuration.
  • APPOPTICS_HOSTNAME_ALIAS - ignored in the Lambda environment; it should be set during the subscription filter configuration.
  • APPOPTICS_EC2_METADATA_TIMEOUT - not available in the Lambda environment.
  • APPOPTICS_PROXY and APPOPTICS_PROXY_CERT_PATH - proxy is not available in the Lambda environment.
  • APPOPTICS_RUNTIME_METRICS - runtime metrics are not supported in the Lambda environment.
  • APPOPTICS_TRIGGER_TRACE - trigger trace is not supported in the Lambda environment.
  • All settings for transaction-based filtering are not supported in the Lambda environment.

The agent supports the following configuration setting only in the AWS Lambda environment:

  • APPOPTICS_TRANSACTION_NAME -- A custom transaction name for the trace, which will be used in preference over the default or SDK-specified transaction name.

Troubleshooting

Make sure prerequisites described in tracing are set up, and check for trace data in the CloudWatch Logs log group for your Lambda function.

You can also look for the following message in CloudWatch Logs, which indicates that the agent is enabled and working:

2020/09/28 20:22:35.094163 WARN  [AO] The reporter (v1.13.1, go1.13) for Lambda is ready.

Conversely, if the message above is not present, there may be error logs that indicate the reason. All the AppOptics logs are prefixed with the string [AO], and you can search on this String to narrow down to agent logs.

You can also enable debug logging for the agent by defining the environment variable APPOPTICS_DEBUG_LEVEL=debug.

You may not want to enable debug logging in the production environment, as it may produce an excessive amount of logs.

If you still cannot identify the problem, email customer support at support@appoptics.com or submit a ticket for help troubleshooting tracing Lambda functions with the Go Agent.

Uninstall

Refer to the uninstall page for more information about uninstalling the agent. Define the environment variable APPOPTIC_DISABLED=true to disable the agent temporarily instead.