-
Notifications
You must be signed in to change notification settings - Fork 42
/
ddlambda.go
368 lines (321 loc) · 14.3 KB
/
ddlambda.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* 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 2021 Datadog, Inc.
*/
package ddlambda
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/aws/aws-lambda-go/lambda"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"github.com/DataDog/datadog-lambda-go/internal/extension"
"github.com/DataDog/datadog-lambda-go/internal/logger"
"github.com/DataDog/datadog-lambda-go/internal/metrics"
"github.com/DataDog/datadog-lambda-go/internal/trace"
"github.com/DataDog/datadog-lambda-go/internal/wrapper"
)
type (
// Config gives options for how ddlambda should behave
Config struct {
// APIKey is your Datadog API key. This is used for sending metrics.
APIKey string
// KMSAPIKey is your Datadog API key, encrypted using the AWS KMS service. This is used for sending metrics.
KMSAPIKey string
// ShouldRetryOnFailure is used to turn on retry logic when sending metrics via the API. This can negatively effect the performance of your lambda,
// and should only be turned on if you can't afford to lose metrics data under poor network conditions.
ShouldRetryOnFailure bool
// ShouldUseLogForwarder enabled the log forwarding method for sending metrics to Datadog. This approach requires the user to set up a custom lambda
// function that forwards metrics from cloudwatch to the Datadog api. This approach doesn't have any impact on the performance of your lambda function.
ShouldUseLogForwarder bool
// BatchInterval is the period of time which metrics are grouped together for processing to be sent to the API or written to logs.
// Any pending metrics are flushed at the end of the lambda.
BatchInterval time.Duration
// Site is the host to send metrics to. If empty, this value is read from the 'DD_SITE' environment variable, or if that is empty
// will default to 'datadoghq.com'.
Site string
// DebugLogging will turn on extended debug logging.
DebugLogging bool
// EnhancedMetrics enables the reporting of enhanced metrics under `aws.lambda.enhanced*` and adds enhanced metric tags
EnhancedMetrics bool
// DDTraceEnabled enables the Datadog tracer.
DDTraceEnabled bool
// MergeXrayTraces will cause Datadog traces to be merged with traces from AWS X-Ray.
MergeXrayTraces bool
// HTTPClientTimeout specifies a time limit for requests to the API. It defaults to 5s.
HTTPClientTimeout time.Duration
// CircuitBreakerInterval is the cyclic period of the closed state
// for the CircuitBreaker to clear the internal Counts.
// default: 30s
CircuitBreakerInterval time.Duration
// CircuitBreakerTimeout is the period of the open state,
// after which the state of the CircuitBreaker becomes half-open.
// default: 60s
CircuitBreakerTimeout time.Duration
// CircuitBreakerTotalFailures after this amount of times
// of a request failing in the closed state, the state will become open.
// the counter will get totally reset after CircuitBreakerInterval
// default: 4
CircuitBreakerTotalFailures uint32
// TraceContextExtractor is the function that extracts a root/parent trace context from the Lambda event body.
// See trace.DefaultTraceExtractor for an example.
TraceContextExtractor trace.ContextExtractor
// TracerOptions are additional options passed to the tracer.
TracerOptions []tracer.StartOption
}
)
const (
// DatadogAPIKeyEnvVar is the environment variable that will be used to set the API key.
DatadogAPIKeyEnvVar = "DD_API_KEY"
// DatadogKMSAPIKeyEnvVar is the environment variable that will be sent to KMS for decryption, then used as an API key.
DatadogKMSAPIKeyEnvVar = "DD_KMS_API_KEY"
// DatadogSiteEnvVar is the environment variable that will be used as the API host.
DatadogSiteEnvVar = "DD_SITE"
// LogLevelEnvVar is the environment variable that will be used to set the log level.
LogLevelEnvVar = "DD_LOG_LEVEL"
// ShouldUseLogForwarderEnvVar is the environment variable that enables log forwarding of metrics.
ShouldUseLogForwarderEnvVar = "DD_FLUSH_TO_LOG"
// DatadogTraceEnabledEnvVar is the environment variable that enables Datadog tracing.
DatadogTraceEnabledEnvVar = "DD_TRACE_ENABLED"
// MergeXrayTracesEnvVar is the environment variable that enables the merging of X-Ray and Datadog traces.
MergeXrayTracesEnvVar = "DD_MERGE_XRAY_TRACES"
// UniversalInstrumentation is the environment variable that enables universal instrumentation with the DD Extension
UniversalInstrumentation = "DD_UNIVERSAL_INSTRUMENTATION"
// Initialize otel tracer provider if enabled
OtelTracerEnabled = "DD_TRACE_OTEL_ENABLED"
// DefaultSite to send API messages to.
DefaultSite = "datadoghq.com"
// DefaultEnhancedMetrics enables enhanced metrics by default.
DefaultEnhancedMetrics = true
// serverlessAppSecEnabledEnvVar is the environment variable used to activate Serverless ASM through the use of an
// AWS Lambda runtime API proxy.
serverlessAppSecEnabledEnvVar = "DD_SERVERLESS_APPSEC_ENABLED"
// awsLambdaRuntimeApiEnvVar is the environment variable used to redirect AWS Lambda runtime API calls to the proxy.
awsLambdaRuntimeApiEnvVar = "AWS_LAMBDA_RUNTIME_API"
// datadogAgentUrl is the URL of the agent and proxy started by the Datadog lambda extension.
datadogAgentUrl = "127.0.0.1:9000"
// ddExtensionFilePath is the path on disk of the datadog lambda extension.
ddExtensionFilePath = "/opt/extensions/datadog-agent"
// awsLambdaServerPortEnvVar is the environment variable set by the go1.x Lambda Runtime to indicate which port the
// RCP server should listen on. This is used as a sign that a warning should be printed if customers want to enable
// ASM support, but did not enable the lambda.norpc build taf.
awsLambdaServerPortEnvVar = "_LAMBDA_SERVER_PORT"
)
// WrapLambdaHandlerInterface is used to instrument your lambda functions.
// It returns a modified handler that can be passed directly to the lambda.StartHandler function from aws-lambda-go.
func WrapLambdaHandlerInterface(handler lambda.Handler, cfg *Config) lambda.Handler {
setupAppSec()
listeners := initializeListeners(cfg)
return wrapper.WrapHandlerInterfaceWithListeners(handler, listeners...)
}
// WrapFunction is used to instrument your lambda functions.
// It returns a modified handler that can be passed directly to the lambda.Start function from aws-lambda-go.
func WrapFunction(handler interface{}, cfg *Config) interface{} {
setupAppSec()
listeners := initializeListeners(cfg)
return wrapper.WrapHandlerWithListeners(handler, listeners...)
}
// WrapHandler is used to instrument your lambda functions.
// It returns a modified handler that can be passed directly to the lambda.Start function from aws-lambda-go.
// Deprecated: use WrapFunction instead
func WrapHandler(handler interface{}, cfg *Config) interface{} {
return WrapFunction(handler, cfg)
}
// GetTraceHeaders returns a map containing Datadog trace headers that reflect the
// current X-Ray subsegment.
// Deprecated: use native Datadog tracing instead.
func GetTraceHeaders(ctx context.Context) map[string]string {
result := trace.ConvertCurrentXrayTraceContext(ctx)
return result
}
// AddTraceHeaders adds Datadog trace headers to a HTTP Request reflecting the current X-Ray
// subsegment.
// Deprecated: use native Datadog tracing instead.
func AddTraceHeaders(ctx context.Context, req *http.Request) {
headers := trace.ConvertCurrentXrayTraceContext(ctx)
for key, value := range headers {
req.Header.Add(key, value)
}
}
// GetContext retrieves the last created lambda context.
// Only use this if you aren't manually passing context through your call hierarchy.
func GetContext() context.Context {
return wrapper.CurrentContext
}
// Distribution sends a distribution metric to Datadog
// Deprecated: Use Metric method instead
func Distribution(metric string, value float64, tags ...string) {
Metric(metric, value, tags...)
}
// Metric sends a distribution metric to DataDog
func Metric(metric string, value float64, tags ...string) {
MetricWithTimestamp(metric, value, time.Now(), tags...)
}
// MetricWithTimestamp sends a distribution metric to DataDog with a custom timestamp
func MetricWithTimestamp(metric string, value float64, timestamp time.Time, tags ...string) {
ctx := GetContext()
if ctx == nil {
logger.Debug("no context available, did you wrap your handler?")
return
}
listener := metrics.GetListener(ctx)
if listener == nil {
logger.Error(fmt.Errorf("couldn't get metrics listener from current context"))
return
}
listener.AddDistributionMetric(metric, value, timestamp, false, tags...)
}
// InvokeDryRun is a utility to easily run your lambda for testing
func InvokeDryRun(callback func(ctx context.Context), cfg *Config) (interface{}, error) {
wrapped := WrapHandler(callback, cfg)
// Convert the wrapped handler to it's underlying raw handler type
handler, ok := wrapped.(func(ctx context.Context, msg json.RawMessage) (interface{}, error))
if !ok {
logger.Debug("Could not unwrap lambda during dry run")
}
return handler(context.Background(), json.RawMessage("{}"))
}
func (cfg *Config) toTraceConfig() trace.Config {
traceConfig := trace.Config{
DDTraceEnabled: true,
MergeXrayTraces: false,
UniversalInstrumentation: true,
OtelTracerEnabled: false,
}
if cfg != nil {
traceConfig.DDTraceEnabled = cfg.DDTraceEnabled
traceConfig.MergeXrayTraces = cfg.MergeXrayTraces
traceConfig.TraceContextExtractor = cfg.TraceContextExtractor
traceConfig.TracerOptions = cfg.TracerOptions
}
if traceConfig.TraceContextExtractor == nil {
traceConfig.TraceContextExtractor = trace.DefaultTraceExtractor
}
if tracingEnabled, err := strconv.ParseBool(os.Getenv(DatadogTraceEnabledEnvVar)); err == nil {
traceConfig.DDTraceEnabled = tracingEnabled
// Only read the OTEL env var if DD tracing is disabled
if tracingEnabled {
if otelTracerEnabled, err := strconv.ParseBool(os.Getenv(OtelTracerEnabled)); err == nil {
traceConfig.OtelTracerEnabled = otelTracerEnabled
}
}
}
if !traceConfig.MergeXrayTraces {
traceConfig.MergeXrayTraces, _ = strconv.ParseBool(os.Getenv(MergeXrayTracesEnvVar))
}
if universalInstrumentation, err := strconv.ParseBool(os.Getenv(UniversalInstrumentation)); err == nil {
traceConfig.UniversalInstrumentation = universalInstrumentation
}
return traceConfig
}
func initializeListeners(cfg *Config) []wrapper.HandlerListener {
logLevel := os.Getenv(LogLevelEnvVar)
if strings.EqualFold(logLevel, "debug") || (cfg != nil && cfg.DebugLogging) {
logger.SetLogLevel(logger.LevelDebug)
}
traceConfig := cfg.toTraceConfig()
extensionManager := extension.BuildExtensionManager(traceConfig.UniversalInstrumentation)
isExtensionRunning := extensionManager.IsExtensionRunning()
metricsConfig := cfg.toMetricsConfig(isExtensionRunning)
// Wrap the handler with listeners that add instrumentation for traces and metrics.
tl := trace.MakeListener(traceConfig, extensionManager)
ml := metrics.MakeListener(metricsConfig, extensionManager)
return []wrapper.HandlerListener{
&tl, &ml,
}
}
func (cfg *Config) toMetricsConfig(isExtensionRunning bool) metrics.Config {
mc := metrics.Config{
ShouldRetryOnFailure: false,
}
if cfg != nil {
mc.BatchInterval = cfg.BatchInterval
mc.ShouldRetryOnFailure = cfg.ShouldRetryOnFailure
mc.APIKey = cfg.APIKey
mc.KMSAPIKey = cfg.KMSAPIKey
mc.Site = cfg.Site
mc.ShouldUseLogForwarder = cfg.ShouldUseLogForwarder
mc.HTTPClientTimeout = cfg.HTTPClientTimeout
}
if mc.Site == "" {
mc.Site = os.Getenv(DatadogSiteEnvVar)
}
if mc.Site == "" {
mc.Site = DefaultSite
}
if strings.HasPrefix(mc.Site, "https://") || strings.HasPrefix(mc.Site, "http://") {
mc.Site = fmt.Sprintf("%s/api/v1", mc.Site)
} else {
mc.Site = fmt.Sprintf("https://api.%s/api/v1", mc.Site)
}
if !mc.ShouldUseLogForwarder {
shouldUseLogForwarder := os.Getenv(ShouldUseLogForwarderEnvVar)
mc.ShouldUseLogForwarder = strings.EqualFold(shouldUseLogForwarder, "true")
}
if mc.APIKey == "" {
mc.APIKey = os.Getenv(DatadogAPIKeyEnvVar)
}
if mc.KMSAPIKey == "" {
mc.KMSAPIKey = os.Getenv(DatadogKMSAPIKeyEnvVar)
}
if !isExtensionRunning && mc.APIKey == "" && mc.KMSAPIKey == "" && !mc.ShouldUseLogForwarder {
logger.Error(fmt.Errorf(
"couldn't read %s or %s from environment", DatadogAPIKeyEnvVar, DatadogKMSAPIKeyEnvVar,
))
}
enhancedMetrics := os.Getenv("DD_ENHANCED_METRICS")
if enhancedMetrics == "" {
mc.EnhancedMetrics = DefaultEnhancedMetrics
}
if !mc.EnhancedMetrics {
mc.EnhancedMetrics = strings.EqualFold(enhancedMetrics, "true")
}
if localTest := os.Getenv("DD_LOCAL_TEST"); localTest == "1" || strings.ToLower(localTest) == "true" {
mc.LocalTest = true
}
return mc
}
// setupAppSec checks if DD_SERVERLESS_APPSEC_ENABLED is set (to true) and when that
// is the case, redirects `AWS_LAMBDA_RUNTIME_API` to the agent extension, and turns
// on universal instrumentation unless it was already configured by the customer, so
// that the HTTP context (invocation details span tags) is available on AppSec traces.
func setupAppSec() {
enabled := false
if env := os.Getenv(serverlessAppSecEnabledEnvVar); env != "" {
if on, err := strconv.ParseBool(env); err == nil {
enabled = on
}
}
if !enabled {
return
}
if _, err := os.Stat(ddExtensionFilePath); os.IsNotExist(err) {
logger.Debug(fmt.Sprintf("%s is enabled, but the Datadog extension was not found at %s", serverlessAppSecEnabledEnvVar, ddExtensionFilePath))
return
}
if awsLambdaRpcSupport {
if port := os.Getenv(awsLambdaServerPortEnvVar); port != "" {
logger.Warn(fmt.Sprintf("%s activation with the go1.x AWS Lambda runtime requires setting the `lambda.norpc` go build tag", serverlessAppSecEnabledEnvVar))
}
}
if err := os.Setenv(awsLambdaRuntimeApiEnvVar, datadogAgentUrl); err != nil {
logger.Debug(fmt.Sprintf("failed to set %s=%s: %v", awsLambdaRuntimeApiEnvVar, datadogAgentUrl, err))
} else {
logger.Debug(fmt.Sprintf("successfully set %s=%s", awsLambdaRuntimeApiEnvVar, datadogAgentUrl))
}
if val := os.Getenv(UniversalInstrumentation); val == "" {
if err := os.Setenv(UniversalInstrumentation, "1"); err != nil {
logger.Debug(fmt.Sprintf("failed to set %s=%d: %v", UniversalInstrumentation, 1, err))
} else {
logger.Debug(fmt.Sprintf("successfully set %s=%d", UniversalInstrumentation, 1))
}
}
}