Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom middleware to mutate input does not work #6743

Open
3 of 4 tasks
alfaproject opened this issue Dec 17, 2024 · 0 comments
Open
3 of 4 tasks

Custom middleware to mutate input does not work #6743

alfaproject opened this issue Dec 17, 2024 · 0 comments
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.

Comments

@alfaproject
Copy link

Checkboxes for prior research

Describe the bug

I did a custom DynamoDB client middleware to log used capacity units when a specific environment variable is set by mutating the input to the operations to include the ReturnConsumedCapacity when said env variable is configured, but the input that is mutated is ignored completely and only the original command input is serialized in the request

Regression Issue

  • Select this option if this issue appears to be a regression.

SDK version number

@aws-sdk/[email protected]

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v20.11.0

Reproduction Steps

import { ConsumedCapacity, ServiceInputTypes, ServiceOutputTypes } from '@aws-sdk/client-dynamodb';
import { InitializeMiddleware, Pluggable } from '@aws-sdk/types';
import { RegionResolvedConfig } from '@smithy/config-resolver';
import { logInfo } from '@tma/core-log';
import { getEnvNumber } from '@tma/node-env';

function createLogConsumedCapacityMiddleware(
  minConsumedCapacityUnits: number,
): InitializeMiddleware<ServiceInputTypes, ServiceOutputTypes> {
  return next => async args => {
    args.input = {
      ...args.input,
      ReturnConsumedCapacity: 'TOTAL',
    };

    const result = await next(args);

    const output = result.output as { ConsumedCapacity?: ConsumedCapacity | ConsumedCapacity[] };

    const consumedCapacities = output.ConsumedCapacity
      ? Array.isArray(output.ConsumedCapacity)
        ? output.ConsumedCapacity
        : [output.ConsumedCapacity]
      : [];

    if (consumedCapacities.some(consumedCapacity => (consumedCapacity.CapacityUnits ?? 0) >= minConsumedCapacityUnits)) {
      logInfo('ConsumedCapacity', {
        consumedCapacity: output.ConsumedCapacity,
        input: args.input,
      });
    }

    return result;
  };
}

export function createLogConsumedCapacityPlugin(_config: RegionResolvedConfig): Pluggable<ServiceInputTypes, ServiceOutputTypes> {
  return {
    applyToStack: stack => {
      const LOG_CONSUMED_CAPACITY = getEnvNumber('DYNAMODB_LOG_CONSUMED_CAPACITY', 0);
      if (LOG_CONSUMED_CAPACITY) {
        stack.add(createLogConsumedCapacityMiddleware(LOG_CONSUMED_CAPACITY), {
          name: 'logConsumedCapacityMiddleware',
          step: 'initialize',
        });
      }
    },
  };
}

Observed Behavior

ReturnConsumedCapacity is ignored in the new input

Expected Behavior

ReturnConsumedCapacity should not be ignored and the DynamoDB operations should return the CapacityUnits used

Possible Solution

No response

Additional Information/Context

No response

@alfaproject alfaproject added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

1 participant