Skip to content

Commit

Permalink
[Step Function] 3.1 Refactor code for getting Lambda config (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
lym953 authored Jan 7, 2025
1 parent f0bcb46 commit 46d4883
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
19 changes: 4 additions & 15 deletions serverless/src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -11,19 +11,8 @@ export const handler = async (event: InputEvent, _: any): Promise<OutputEvent> =

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,
Expand All @@ -33,7 +22,7 @@ export const handler = async (event: InputEvent, _: any): Promise<OutputEvent> =
};
}

const lambdaOutput = await instrumentLambdas(event, config);
const lambdaOutput = await instrumentLambdas(event, lambdaConfig);
if (lambdaOutput.status === FAILURE) {
return lambdaOutput;
}
Expand Down
19 changes: 19 additions & 0 deletions serverless/src/lambda/env.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit 46d4883

Please sign in to comment.