diff --git a/serverless/src/index.ts b/serverless/src/index.ts index fc1f3d5..ccb45a7 100644 --- a/serverless/src/index.ts +++ b/serverless/src/index.ts @@ -1,4 +1,4 @@ -import { getConfigFromCfnMappings, getConfigFromCfnParams, validateParameters, Configuration } from "./lambda/env"; +import { validateParameters as validateLambdaParameters, getConfig as getLambdaConfig } from "./lambda/env"; import { instrumentLambdas } from "./lambda/lambda"; import { InputEvent, OutputEvent, SUCCESS, FAILURE } from "./types"; import { instrumentStateMachines } from "./step_function/step_function"; @@ -11,19 +11,8 @@ export const handler = async (event: InputEvent, _: any): Promise = const fragment = event.fragment; - 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(fragment.Mappings); - } - - const errors = validateParameters(config); + const lambdaConfig = getLambdaConfig(event); + const errors = validateLambdaParameters(lambdaConfig); if (errors.length > 0) { return { requestId: event.requestId, @@ -33,7 +22,7 @@ export const handler = async (event: InputEvent, _: any): Promise = }; } - const lambdaOutput = await instrumentLambdas(event, config); + const lambdaOutput = await instrumentLambdas(event, lambdaConfig); if (lambdaOutput.status === FAILURE) { return lambdaOutput; } diff --git a/serverless/src/lambda/env.ts b/serverless/src/lambda/env.ts index 7400070..f44cc13 100644 --- a/serverless/src/lambda/env.ts +++ b/serverless/src/lambda/env.ts @@ -1,5 +1,6 @@ import { getGitTagsFromParam } from "./git"; import { LambdaFunction, runtimeLookup, RuntimeType } from "./layer"; +import { InputEvent } from "types"; import log from "loglevel"; export interface Configuration { @@ -126,6 +127,24 @@ export const defaultConfiguration: Configuration = { 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. */