-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathtransport.ts
More file actions
105 lines (96 loc) · 3.73 KB
/
Copy pathtransport.ts
File metadata and controls
105 lines (96 loc) · 3.73 KB
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
/*
* 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-2026 Datadog, Inc.
*/
import log from "loglevel";
import { runtimeLookup, RuntimeType } from "./index";
import { LambdaFunction } from "./interfaces";
export const API_KEY_ENV_VAR = "DD_API_KEY";
export const API_KEY_SECRET_ARN_ENV_VAR = "DD_API_KEY_SECRET_ARN";
export const API_KEY_SSM_ARN_ENV_VAR = "DD_API_KEY_SSM_ARN";
export const KMS_API_KEY_ENV_VAR = "DD_KMS_API_KEY";
export const SITE_URL_ENV_VAR = "DD_SITE";
export const FLUSH_METRICS_TO_LOGS_ENV_VAR = "DD_FLUSH_TO_LOG";
export const transportDefaults = {
site: "datadoghq.com",
flushMetricsToLogs: true,
enableDatadogTracing: true,
};
export class Transport {
flushMetricsToLogs: boolean;
site: string;
apiKey?: string;
apiKeySecretArn?: string;
apiKeySsmArn?: string;
apiKmsKey?: string;
extensionLayerVersion?: number;
extensionLayerArn?: string;
constructor(
flushMetricsToLogs?: boolean,
site?: string,
apiKey?: string,
apiKeySecretArn?: string,
apiKeySsmArn?: string,
apiKmsKey?: string,
extensionLayerVersion?: number,
extensionLayerArn?: string,
) {
if (flushMetricsToLogs === undefined) {
log.debug(`No value provided for flushMetricsToLogs, defaulting to ${transportDefaults.flushMetricsToLogs}`);
this.flushMetricsToLogs = transportDefaults.flushMetricsToLogs;
} else {
this.flushMetricsToLogs = flushMetricsToLogs;
}
this.extensionLayerVersion = extensionLayerVersion;
this.extensionLayerArn = extensionLayerArn;
// If the extension is used, metrics will be submitted via the extension.
if (this.extensionLayerVersion !== undefined || this.extensionLayerArn !== undefined) {
const extensionInfo =
this.extensionLayerArn !== undefined
? `ARN ${this.extensionLayerArn}`
: `version ${this.extensionLayerVersion}`;
log.debug(`Using extension ${extensionInfo}, metrics will be submitted via the extension`);
this.flushMetricsToLogs = false;
}
if (site === undefined) {
log.debug(`No value provided for site, defaulting to ${transportDefaults.site}`);
this.site = transportDefaults.site;
} else {
this.site = site;
}
this.apiKey = apiKey;
this.apiKeySecretArn = apiKeySecretArn;
this.apiKeySsmArn = apiKeySsmArn;
this.apiKmsKey = apiKmsKey;
}
applyEnvVars(lam: LambdaFunction) {
log.debug(`Setting Datadog transport environment variables...`);
lam.addEnvironment(FLUSH_METRICS_TO_LOGS_ENV_VAR, this.flushMetricsToLogs.toString());
if (this.site !== undefined && this.flushMetricsToLogs === false) {
lam.addEnvironment(SITE_URL_ENV_VAR, this.site);
}
if (this.apiKey !== undefined) {
lam.addEnvironment(API_KEY_ENV_VAR, this.apiKey);
}
if (this.apiKeySecretArn !== undefined) {
const isNode = runtimeLookup[lam.runtime.name] === RuntimeType.NODE;
const isSendingSynchronousMetrics =
this.extensionLayerVersion === undefined && this.extensionLayerArn === undefined && !this.flushMetricsToLogs;
if (isSendingSynchronousMetrics && isNode) {
throw new Error(
`\`apiKeySecretArn\` is not supported for Node runtimes when using Synchronous Metrics. Use either \`apiKey\` or \`apiKmsKey\`.`,
);
}
lam.addEnvironment(API_KEY_SECRET_ARN_ENV_VAR, this.apiKeySecretArn);
}
if (this.apiKeySsmArn !== undefined) {
lam.addEnvironment(API_KEY_SSM_ARN_ENV_VAR, this.apiKeySsmArn);
}
if (this.apiKmsKey !== undefined) {
lam.addEnvironment(KMS_API_KEY_ENV_VAR, this.apiKmsKey);
}
}
}