-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Step Function] 3.2 Extract class ConfigLoader and LambdaConfigLoader #150
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,168 @@ export interface Configuration { | |
apmFlushDeadline?: string; | ||
} | ||
|
||
abstract class ConfigLoader<TConfig> { | ||
abstract readonly defaultConfiguration: TConfig; | ||
/** | ||
* Returns the default configuration with any values overwritten by environment variables. | ||
*/ | ||
abstract getConfigFromEnvVars(): TConfig; | ||
|
||
/** | ||
* Returns the configuration. | ||
* If DatadogServerless transform params are set, then the priority order is: | ||
* 1. CloudFormation Macro params | ||
* 2. Environment variables | ||
* 3. Default configuration | ||
* Otherwise, if CloudFormation Mappings for Datadog are set, then the priority order is: | ||
* 1. CloudFormation Mappings params | ||
* 2. Environment variables | ||
* 3. Default configuration | ||
* Otherwise, the priority order is: | ||
* 1. Environment variables | ||
* 2. Default configuration | ||
*/ | ||
public getConfig(event: InputEvent): TConfig { | ||
let config: TConfig; | ||
// Use the parameters given for this specific transform/macro if it exists | ||
const transformParams = event.params ?? {}; | ||
if (Object.keys(transformParams).length > 0) { | ||
log.debug("Parsing config from CloudFormation transform/macro parameters"); | ||
config = this.getConfigFromCfnParams(transformParams); | ||
} else { | ||
// If not, check the Mappings section for Datadog config parameters as well | ||
log.debug("Parsing config from CloudFormation template mappings"); | ||
config = this.getConfigFromCfnMappings(event.fragment.Mappings); | ||
} | ||
return config; | ||
} | ||
|
||
/** | ||
* Parses the Mappings section for Datadog config parameters. | ||
* Assumes that the parameters live under the Mappings section in this format: | ||
* | ||
* Mappings: | ||
* Datadog: | ||
* Parameters: | ||
* addLayers: true | ||
* ... | ||
*/ | ||
public getConfigFromCfnMappings(mappings: any): TConfig { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (mappings === undefined || mappings[DATADOG] === undefined) { | ||
log.debug("No Datadog mappings found in the CloudFormation template, using the default config"); | ||
return this.getConfigFromEnvVars(); | ||
} | ||
return this.getConfigFromCfnParams(mappings[DATADOG][PARAMETERS]); | ||
} | ||
|
||
/** | ||
* Takes a set of parameters from the CloudFormation template. This could come from either | ||
* the Mappings section of the template, or directly from the Parameters under the transform/macro | ||
* as the 'params' property under the original InputEvent to the handler in src/index.ts | ||
* | ||
* Uses these parameters as the Datadog configuration, and for values that are required in the | ||
* configuration but not provided in the parameters, uses the default values from | ||
* the defaultConfiguration above. | ||
*/ | ||
public getConfigFromCfnParams(params: CfnParams) { | ||
let datadogConfig = params as Partial<TConfig> | undefined; | ||
if (datadogConfig === undefined) { | ||
log.debug("No Datadog config found, using the default config"); | ||
datadogConfig = {}; | ||
} | ||
return { | ||
...this.getConfigFromEnvVars(), | ||
...datadogConfig, | ||
}; | ||
} | ||
} | ||
|
||
export class LambdaConfigLoader extends ConfigLoader<Configuration> { | ||
readonly defaultConfiguration: Configuration = { | ||
addLayers: true, | ||
addExtension: false, | ||
exclude: [], | ||
flushMetricsToLogs: true, | ||
logLevel: undefined, | ||
site: "datadoghq.com", | ||
enableXrayTracing: false, | ||
enableDDTracing: true, | ||
enableDDLogs: true, | ||
enableEnhancedMetrics: true, | ||
captureLambdaPayload: false, | ||
}; | ||
|
||
public getConfigFromEnvVars(): Configuration { | ||
const config: Configuration = { | ||
...this.defaultConfiguration, | ||
}; | ||
|
||
if (apiKeyEnvVar in process.env) { | ||
config.apiKey = process.env[apiKeyEnvVar]; | ||
} | ||
if (apiKeySecretArnEnvVar in process.env) { | ||
config.apiKeySecretArn = process.env[apiKeySecretArnEnvVar]; | ||
} | ||
if (apiKeyKMSEnvVar in process.env) { | ||
config.apiKMSKey = process.env[apiKeyKMSEnvVar]; | ||
} | ||
if (siteURLEnvVar in process.env && process.env[siteURLEnvVar] !== undefined) { | ||
// Fall back to default site for type safety | ||
config.site = process.env[siteURLEnvVar] ?? this.defaultConfiguration.site; | ||
} | ||
if (logLevelEnvVar in process.env) { | ||
config.logLevel = process.env[logLevelEnvVar]; | ||
} | ||
if (logForwardingEnvVar in process.env) { | ||
config.flushMetricsToLogs = process.env[logForwardingEnvVar] === "true"; | ||
} | ||
if (enhancedMetricsEnvVar in process.env) { | ||
config.enableEnhancedMetrics = process.env[enhancedMetricsEnvVar] === "true"; | ||
} | ||
if (enableDDLogsEnvVar in process.env) { | ||
config.enableDDLogs = process.env[enableDDLogsEnvVar] === "true"; | ||
} | ||
if (captureLambdaPayloadEnvVar in process.env) { | ||
config.captureLambdaPayload = process.env[captureLambdaPayloadEnvVar] === "true"; | ||
} | ||
if (serviceEnvVar in process.env) { | ||
config.service = process.env[serviceEnvVar]; | ||
} | ||
if (envEnvVar in process.env) { | ||
config.env = process.env[envEnvVar]; | ||
} | ||
if (versionEnvVar in process.env) { | ||
config.version = process.env[versionEnvVar]; | ||
} | ||
if (tagsEnvVar in process.env) { | ||
config.tags = process.env[tagsEnvVar]; | ||
} | ||
if (ddColdStartTracingEnabledEnvVar in process.env) { | ||
config.enableColdStartTracing = process.env[ddColdStartTracingEnabledEnvVar] === "true"; | ||
} | ||
if (ddMinColdStartDurationEnvVar in process.env) { | ||
config.minColdStartTraceDuration = process.env[ddMinColdStartDurationEnvVar]; | ||
} | ||
if (ddColdStartTracingSkipLibsEnvVar in process.env) { | ||
config.coldStartTraceSkipLibs = process.env[ddColdStartTracingSkipLibsEnvVar]; | ||
} | ||
if (ddProfilingEnabledEnvVar in process.env) { | ||
config.enableProfiling = process.env[ddProfilingEnabledEnvVar] === "true"; | ||
} | ||
if (ddEncodeAuthorizerContextEnvVar in process.env) { | ||
config.encodeAuthorizerContext = process.env[ddEncodeAuthorizerContextEnvVar] === "true"; | ||
} | ||
if (ddDecodeAuthorizerContextEnvVar in process.env) { | ||
config.decodeAuthorizerContext = process.env[ddDecodeAuthorizerContextEnvVar] === "true"; | ||
} | ||
if (ddApmFlushDeadlineMillisecondsEnvVar in process.env) { | ||
config.apmFlushDeadline = process.env[ddApmFlushDeadlineMillisecondsEnvVar]; | ||
} | ||
|
||
return config; | ||
} | ||
} | ||
|
||
// Same interface as Configuration above, except all parameters are optional, since user does | ||
// not have to provide the values (in which case we will use the default configuration below). | ||
interface CfnParams extends Partial<Configuration> {} | ||
|
@@ -113,150 +275,6 @@ const ddEncodeAuthorizerContextEnvVar = "DD_ENCODE_AUTHORIZER_CONTEXT"; | |
const ddDecodeAuthorizerContextEnvVar = "DD_DECODE_AUTHORIZER_CONTEXT"; | ||
const ddApmFlushDeadlineMillisecondsEnvVar = "DD_APM_FLUSH_DEADLINE_MILLISECONDS"; | ||
|
||
export const defaultConfiguration: Configuration = { | ||
addLayers: true, | ||
addExtension: false, | ||
exclude: [], | ||
flushMetricsToLogs: true, | ||
logLevel: undefined, | ||
site: "datadoghq.com", | ||
enableXrayTracing: false, | ||
enableDDTracing: true, | ||
enableDDLogs: true, | ||
enableEnhancedMetrics: true, | ||
captureLambdaPayload: false, | ||
}; | ||
|
||
/** | ||
* Returns the default configuration with any values overwritten by environment variables. | ||
*/ | ||
export function getConfig(event: InputEvent): Configuration { | ||
let config: Configuration; | ||
// Use the parameters given for this specific transform/macro if it exists | ||
const transformParams = event.params ?? {}; | ||
if (Object.keys(transformParams).length > 0) { | ||
log.debug("Parsing config from CloudFormation transform/macro parameters"); | ||
config = getConfigFromCfnParams(transformParams); | ||
} else { | ||
// If not, check the Mappings section for Datadog config parameters as well | ||
log.debug("Parsing config from CloudFormation template mappings"); | ||
config = getConfigFromCfnMappings(event.fragment.Mappings); | ||
} | ||
return config; | ||
} | ||
|
||
/** | ||
* Returns the default configuration with any values overwritten by environment variables. | ||
*/ | ||
export function getConfigFromEnvVars(): Configuration { | ||
const config: Configuration = { | ||
...defaultConfiguration, | ||
}; | ||
|
||
if (apiKeyEnvVar in process.env) { | ||
config.apiKey = process.env[apiKeyEnvVar]; | ||
} | ||
if (apiKeySecretArnEnvVar in process.env) { | ||
config.apiKeySecretArn = process.env[apiKeySecretArnEnvVar]; | ||
} | ||
if (apiKeyKMSEnvVar in process.env) { | ||
config.apiKMSKey = process.env[apiKeyKMSEnvVar]; | ||
} | ||
if (siteURLEnvVar in process.env && process.env[siteURLEnvVar] !== undefined) { | ||
// Fall back to default site for type safety | ||
config.site = process.env[siteURLEnvVar] ?? defaultConfiguration.site; | ||
} | ||
if (logLevelEnvVar in process.env) { | ||
config.logLevel = process.env[logLevelEnvVar]; | ||
} | ||
if (logForwardingEnvVar in process.env) { | ||
config.flushMetricsToLogs = process.env[logForwardingEnvVar] === "true"; | ||
} | ||
if (enhancedMetricsEnvVar in process.env) { | ||
config.enableEnhancedMetrics = process.env[enhancedMetricsEnvVar] === "true"; | ||
} | ||
if (enableDDLogsEnvVar in process.env) { | ||
config.enableDDLogs = process.env[enableDDLogsEnvVar] === "true"; | ||
} | ||
if (captureLambdaPayloadEnvVar in process.env) { | ||
config.captureLambdaPayload = process.env[captureLambdaPayloadEnvVar] === "true"; | ||
} | ||
if (serviceEnvVar in process.env) { | ||
config.service = process.env[serviceEnvVar]; | ||
} | ||
if (envEnvVar in process.env) { | ||
config.env = process.env[envEnvVar]; | ||
} | ||
if (versionEnvVar in process.env) { | ||
config.version = process.env[versionEnvVar]; | ||
} | ||
if (tagsEnvVar in process.env) { | ||
config.tags = process.env[tagsEnvVar]; | ||
} | ||
if (ddColdStartTracingEnabledEnvVar in process.env) { | ||
config.enableColdStartTracing = process.env[ddColdStartTracingEnabledEnvVar] === "true"; | ||
} | ||
if (ddMinColdStartDurationEnvVar in process.env) { | ||
config.minColdStartTraceDuration = process.env[ddMinColdStartDurationEnvVar]; | ||
} | ||
if (ddColdStartTracingSkipLibsEnvVar in process.env) { | ||
config.coldStartTraceSkipLibs = process.env[ddColdStartTracingSkipLibsEnvVar]; | ||
} | ||
if (ddProfilingEnabledEnvVar in process.env) { | ||
config.enableProfiling = process.env[ddProfilingEnabledEnvVar] === "true"; | ||
} | ||
if (ddEncodeAuthorizerContextEnvVar in process.env) { | ||
config.encodeAuthorizerContext = process.env[ddEncodeAuthorizerContextEnvVar] === "true"; | ||
} | ||
if (ddDecodeAuthorizerContextEnvVar in process.env) { | ||
config.decodeAuthorizerContext = process.env[ddDecodeAuthorizerContextEnvVar] === "true"; | ||
} | ||
if (ddApmFlushDeadlineMillisecondsEnvVar in process.env) { | ||
config.apmFlushDeadline = process.env[ddApmFlushDeadlineMillisecondsEnvVar]; | ||
} | ||
|
||
return config; | ||
} | ||
|
||
/** | ||
* Parses the Mappings section for Datadog config parameters. | ||
* Assumes that the parameters live under the Mappings section in this format: | ||
* | ||
* Mappings: | ||
* Datadog: | ||
* Parameters: | ||
* addLayers: true | ||
* ... | ||
*/ | ||
export function getConfigFromCfnMappings(mappings: any): Configuration { | ||
if (mappings === undefined || mappings[DATADOG] === undefined) { | ||
log.debug("No Datadog mappings found in the CloudFormation template, using the default config"); | ||
return getConfigFromEnvVars(); | ||
} | ||
return getConfigFromCfnParams(mappings[DATADOG][PARAMETERS]); | ||
} | ||
|
||
/** | ||
* Takes a set of parameters from the CloudFormation template. This could come from either | ||
* the Mappings section of the template, or directly from the Parameters under the transform/macro | ||
* as the 'params' property under the original InputEvent to the handler in src/index.ts | ||
* | ||
* Uses these parameters as the Datadog configuration, and for values that are required in the | ||
* configuration but not provided in the parameters, uses the default values from | ||
* the defaultConfiguration above. | ||
*/ | ||
export function getConfigFromCfnParams(params: CfnParams) { | ||
let datadogConfig = params as Partial<Configuration> | undefined; | ||
if (datadogConfig === undefined) { | ||
log.debug("No Datadog config found, using the default config"); | ||
datadogConfig = {}; | ||
} | ||
return { | ||
...getConfigFromEnvVars(), | ||
...datadogConfig, | ||
}; | ||
} | ||
|
||
export function validateParameters(config: Configuration): string[] { | ||
log.debug("Validating parameters..."); | ||
const errors: string[] = []; | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm understanding correctly, the stepfunction config loader will (in a future PR) will also extend this class is that right? Do we want it in the lambda folder or is there a shared code section that makes sense to keep the overlapping components in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
I'm okay with either, but since you raised it, let me move it to the src folder.