Instrumenting Go Applications
Security Monitoring is now available Security Monitoring is now available

Instrumenting Go Applications

After you have installed the AWS integration, follow the steps below to instrument your application to send metrics, logs, and traces to Datadog.

Configuration

Install the Datadog Lambda Library

You can install the Datadog Lambda Library locally by running the following command:

go get github.com/DataDog/datadog-lambda-go

Configure the Function

  1. Set environment variable DD_FLUSH_TO_LOG to true.
  2. Enable AWS X-Ray active tracing for your Lambda function.

Subscribe the Datadog Forwarder to the Log Groups

You need to subscribe the Datadog Forwarder Lambda function to each of your function’s log groups, in order to send metrics, traces and logs to Datadog.

  1. Install the Datadog Forwarder if you haven’t.
  2. Ensure the option DdFetchLambdaTags is enabled.
  3. Subscribe the Datadog Forwarder to your function’s log groups.

Explore Datadog Serverless Monitoring

After you have configured your function following the steps above, you should be able to view metrics, logs and traces on the Serverless page. If you need to submit a custom metric, refer to the sample code below:

package main

import (
  "github.com/aws/aws-lambda-go/lambda"
  "github.com/DataDog/datadog-lambda-go"
)

func main() {
  // Wrap your handler function
  lambda.Start(ddlambda.WrapHandler(myHandler, nil))
}

func myHandler(ctx context.Context, event MyEvent) (string, error) {
  // Submit a custom metric
  ddlambda.Metric(
    "coffee_house.order_value", // Metric name
    12.45, // Metric value
    "product:latte", "order:online" // Associated tags
  )

  req, err := http.NewRequest("GET", "http://example.com/status")

  // Add the datadog distributed tracing headers
  ddlambda.AddTraceHeaders(ctx, req)

  client := http.Client{}
  client.Do(req)
}