Skip to content

Cannot apply addLambdaFunctions via aspect: "We detected an Aspect was added via another Aspect, and will not be applied" #114

Description

@mhenniges

Expected Behavior

I attempted to write an aspect that would automatically set up tracing for my nodeJS lambdas. I expected this would result in full instrumentation of these functions without multiple source file edits.

Actual Behavior

Aspect fails to apply with an error "We detected an Aspect was added via another Aspect, and will not be applied".

Steps to Reproduce the Problem

Example aspect for datadog:

abstract class TraceAgentBase implements IAspect {

    protected readonly props: DatadogProps;
    protected readonly ddtrace: Datadog;
    // protected readonly scope: IConstruct;

    protected constructor(ddtrace: Datadog, props: DatadogProps) {
        this.props = props;
        this.ddtrace = ddtrace
    }

    public visit(node: IConstruct): void {
        if (node instanceof NodejsFunction) {
            this.applyNodeTracingToLambda(node)
        }
    }
    protected abstract applyNodeTracingToLambda(resource: NodejsFunction): void;
}
`
export class TraceAgent extends TraceAgentBase  {
    constructor(ddtrace: Datadog, props: DatadogProps) {
        super(ddtrace, props);
    }
    protected applyNodeTracingToLambda(resource: NodejsFunction) {
        this.ddtrace.addLambdaFunctions([resource])
        resource.addEnvironment('DD_MERGE_XRAY_TRACES', 'true');
        const cfnFunc = resource.node.defaultChild as CfnFunction;
        cfnFunc.tracingConfig = {mode: Tracing.ACTIVE};
    }
}

apply to stack with something like this:

        Aspects.of(runner).add(new TraceAgent(ddtrace, {
            apiKey: DatadogAPISecret.secretValue.toString(),
            nodeLayerVersion: 78,
            extensionLayerVersion: 23,
            env: 'dev',
            service: serviceName,
            version: '0.1.2-datadog',
            captureLambdaPayload: true,
        }));

Specifications

  • Datadog Lambda Layer version: 23
  • Node version: 78

Stacktrace

None available

After much debugging, I've tracked this down to the use of Tags.of() in addCdkConstructVersionTag and setTags() calls starting here - Tags.of() is an aspect as well.

Has anybody else hit this, and did you work around it successfully? Has there been any thought or exploration into how to make the datadog construct aspect-compatible?

Although not ideal, I believe this can be solved by using something like construct.tags.setTag(key, value, 100, true) to set tags directly, rather than calling the aspect to do the same. There won't be much (any?) cascading of tags to child objects in this case, so really not much is lost.

Happy to make a PR if there is no objection.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions