Categorygithub.com/DataDog/datadog-lambda-go
modulepackage
0.10.0
Repository: https://github.com/datadog/datadog-lambda-go.git
Documentation: pkg.go.dev

# README

datadog-lambda-go

build Code Coverage Slack Godoc License

Datadog Lambda Library for Go enables enhanced Lambda metrics, distributed tracing, and custom metric submission from AWS Lambda functions.

Installation

Follow the installation instructions here.

Enhanced Metrics

Once installed, you should be able to view enhanced metrics for your Lambda function in Datadog.

Check out the official documentation on Datadog Lambda enhanced metrics.

Custom Metrics

Once installed, you should be able to submit custom metrics from your Lambda function.

Check out the instructions for submitting custom metrics from AWS Lambda functions.

Tracing

Set the DD_TRACE_ENABLED environment variable to true to enable Datadog tracing. When Datadog tracing is enabled, the library will inject a span representing the Lambda's execution into the context object. You can then use the included dd-trace-go package to create additional spans from the context or pass the context to other services. For more information, see the dd-trace-go documentation.

import (
  "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
  httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
)

func handleRequest(ctx context.Context, ev events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
  // Trace an HTTP request
  req, _ := http.NewRequestWithContext(ctx, "GET", "https://www.datadoghq.com", nil)
  client := http.Client{}
  client = *httptrace.WrapClient(&client)
  client.Do(req)

  // Create a custom span
  s, _ := tracer.StartSpanFromContext(ctx, "child.span")
  time.Sleep(100 * time.Millisecond)
  s.Finish()
}

You can also use the injected span to connect your logs and traces.

func handleRequest(ctx context.Context, ev events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
  currentSpan, _ := tracer.SpanFromContext(ctx)
  log.Printf("my log message %v", currentSpan)
}

If you are also using AWS X-Ray to trace your Lambda functions, you can set the DD_MERGE_XRAY_TRACES environment variable to true, and Datadog will merge your Datadog and X-Ray traces into a single, unified trace.

Environment Variables

DD_FLUSH_TO_LOG

Set to true (recommended) to send custom metrics asynchronously (with no added latency to your Lambda function executions) through CloudWatch Logs with the help of Datadog Forwarder. Defaults to false. If set to false, you also need to set DD_API_KEY and DD_SITE.

DD_API_KEY

If DD_FLUSH_TO_LOG is set to false (not recommended), the Datadog API Key must be defined.

DD_SITE

If DD_FLUSH_TO_LOG is set to false (not recommended), you must set DD_SITE. Possible values are datadoghq.com, datadoghq.eu, us3.datadoghq.com and ddog-gov.com. The default is datadoghq.com.

DD_LOG_LEVEL

Set to debug enable debug logs from the Datadog Lambda Library. Defaults to info.

DD_ENHANCED_METRICS

Generate enhanced Datadog Lambda integration metrics, such as, aws.lambda.enhanced.invocations and aws.lambda.enhanced.errors. Defaults to true.

DD_TRACE_ENABLED

Initialize the Datadog tracer when set to true. Defaults to false.

DD_MERGE_XRAY_TRACES

If you are using both X-Ray and Datadog tracing, set this to true to merge the X-Ray and Datadog traces. Defaults to false.

Opening Issues

If you encounter a bug with this package, we want to hear about it. Before opening a new issue, search the existing issues to avoid duplicates.

When opening an issue, include the datadog-lambda-go version, go version, and stack trace if available. In addition, include the steps to reproduce when appropriate.

You can also open an issue for a feature request.

Contributing

If you find an issue with this package and have a fix, please feel free to open a pull request following the procedures.

Community

For product feedback and questions, join the #serverless channel in the Datadog community on Slack.

License

Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.

This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2020 Datadog, Inc.

# Functions

AddTraceHeaders adds Datadog trace headers to a HTTP Request reflecting the current X-Ray subsegment.
Distribution sends a distribution metric to Datadog Deprecated: Use Metric method instead.
GetContext retrieves the last created lambda context.
GetTraceHeaders returns a map containing Datadog trace headers that reflect the current X-Ray subsegment.
InvokeDryRun is a utility to easily run your lambda for testing.
Metric sends a distribution metric to DataDog.
MetricWithTimestamp sends a distribution metric to DataDog with a custom timestamp.
WrapHandler is used to instrument your lambda functions.

# Constants

DatadogAPIKeyEnvVar is the environment variable that will be used to set the API key.
DatadogKMSAPIKeyEnvVar is the environment variable that will be sent to KMS for decryption, then used as an API key.
DatadogSiteEnvVar is the environment variable that will be used as the API host.
DatadogTraceEnabledEnvVar is the environment variable that enables Datadog tracing.
DefaultEnhancedMetrics enables enhanced metrics by default.
DefaultSite to send API messages to.
LogLevelEnvVar is the environment variable that will be used to set the log level.
MergeXrayTracesEnvVar is the environment variable that enables the merging of X-Ray and Datadog traces.
ShouldUseLogForwarderEnvVar is the environment variable that enables log forwarding of metrics.

# Structs

No description provided by the author