DataDog / datadog-cdk-constructs

CDK construct library to automagically instrument your Lambda functions with Datadog
Apache License 2.0
65 stars 28 forks source link

feat: [Step Function] Subscribe forwarder to log group #319

Closed lym953 closed 2 weeks ago

lym953 commented 2 weeks ago

What does this PR do?

Enables adding a subscription filter to subscribe the forwarder to the state machine's log group.

Motivation

This is the second step of instrumenting a step function.

Testing Guidelines

Automated testing

Passed the added test for forwarder setup

Manual testing

Step:

  1. Deploy the example stacks in step-functions-typescript-stack.

Result:

  1. The subscription filter appeared in AWS UI image

@DylanLovesCoffee sorry I borrowed your forwarder.

Additional Notes

It takes a lot of code to extract log group from the log config of a state machine, if the user sets the log group themselves.

      // Extract log group from logging config
      const destinations = cfnStateMachine.loggingConfiguration.destinations;
      if (!this.isLogDestinationPropertyArray(destinations)) {
        throw new Error("destinations is not an array");
      }

      const destination = destinations[0];
      if (!("cloudWatchLogsLogGroup" in destination)) {
        throw new Error("cloudWatchLogsLogGroup is not in destination");
      }

      const logGroupConfig = destination.cloudWatchLogsLogGroup;
      if (logGroupConfig === undefined) {
        throw new Error("cloudWatchLogsLogGroup is undefined");
      }

      if (!("logGroupArn" in logGroupConfig)) {
        throw new Error("logGroupArn is not in cloudWatchLogsLogGroup");
      }

      const logGroupArn = logGroupConfig.logGroupArn;
      if (logGroupArn === undefined) {
        throw new Error("logGroupArn is undefined");
      }

      logGroup = logs.LogGroup.fromLogGroupArn(this, "LogGroup", logGroupArn);

There are so many checks because at each level, the variable can often have multiple possible types, e.g. logGroupConfig can be

IResolvable | sfn.CfnStateMachine.CloudWatchLogsLogGroupProperty | undefined

To be able to parse the log group, we assume that nothing is an unresolved token (i.e. IResolvable), and throw if anything is an unresolved token. I'm not sure in what case and if it's common that anything is an unresolved token. I think we can start from handling the simple case, and come back later if anyone asks for supporting the unresolved token cases.

Types of Changes

Check all that apply

lym953 commented 2 weeks ago

/merge

dd-devflow[bot] commented 2 weeks ago

Devflow running: /merge

View all feedbacks in Devflow UI.


2024-11-07 15:44:07 UTC :information_source: MergeQueue: pull request added to the queue

The median merge time in main is 8m.