Extensions

Let’s make some changes in the system to monitor multiple Lambdas.

We can now monitor one function. What kind of functionality are we likely to add in the future? One plausible extension would be to monitor multiple Lambdas. How would we go about doing this? Well, it would require multiple minor modifications, with our type system helping us discover what to change.

Changing configuration

First, because we now have multiple functions to monitor, we’d change our configuration to contain an array of strings (or newtypes), causing compile issues in Config. For now, we could solve these issues by changing addToConfig.

Press + to interact
export type Config = {
between: Between,
functionNames: string[], // also note that the property is now plural!
};
Press + to interact
const addToConfig = (between: Between, functionName: string) => ({
between,
functionNames: [functionName],
});

There’s currently only one function to track, but if we change our code to work with an array, adding more functions is easy. The easiest way to get multiple names at this stage might be to change our environment variable to a comma-separated list. We’ll ignore these issues for now because our compiler has started complaining about retrieveMetrics (in index.ts). So, change config.functionName to config.functionNames and make retrieveMetrics accept an array. Now, some further changes are required (we shortened the parameter names because the signatures are quite long).

Press + to interact
const retrieveMetricsForFunction = (b: Between, m: readonly Metric[]) =>
(fun: string): TE.TaskEither<string, StatsResult[]> => {
return A.array.sequence(TE.taskEither)
(m.map(getMetricInfo(b, fun)));
};
export const retrieveMetrics = (b: Between, m: readonly Metric[]) =>
(fun: string[]): TE.TaskEither<string, StatsResult[][]> => {
const genericRetrieveMetrics = retrieveMetricsForFunction(b, m);
return A.array.sequence(TE.taskEither)
(fun.map(genericRetrieveMetrics));
};

We add retrieveMetricsForFunction, which is identical to the original retrieveMetrics. The modified retrieveMetrics now first partially applies the arguments that are the same for each function (the dates and metrics to retrieve) to retrieveMetricsForFunction. Next, we map over our function names and pass them to the new helper. All of this is again performed in parallel using sequence. Next, we’ll get an error on the return type because we now have a nested array for StatsResult. So, we fixed that as well.

Let’s return to our ...