Instrumenting Go Applications
Rapport de recherche Datadog : Bilan sur l'adoption de l'informatique sans serveur Rapport : Bilan sur l'adoption de l'informatique sans serveur

Instrumenting Go Applications

Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

After you have installed the AWS integration and the Datadog Forwarder, 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 Homepage.

If you would like to submit a custom metric, see 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)
}