Skip to content

feat(endpoint): log endpoint decisions at debug level #4106

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

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/util-endpoints/src/debug/debugId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const debugId = "endpoints";
2 changes: 2 additions & 0 deletions packages/util-endpoints/src/debug/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./debugId";
export * from "./toDebugString";
26 changes: 26 additions & 0 deletions packages/util-endpoints/src/debug/toDebugString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { EndpointParameters, EndpointV2 } from "@aws-sdk/types";

import { GetAttrValue } from "../lib";
import { EndpointObject, FunctionObject, FunctionReturn } from "../types";

export function toDebugString(input: EndpointParameters): string;
export function toDebugString(input: EndpointV2): string;
export function toDebugString(input: GetAttrValue): string;
export function toDebugString(input: FunctionObject): string;
export function toDebugString(input: FunctionReturn): string;
export function toDebugString(input: EndpointObject): string;
export function toDebugString(input: any): string {
if (typeof input !== 'object' || input == null) {
return input;
}

if ('ref' in input) {
return `$${toDebugString(input.ref)}`
}

if ('fn' in input) {
return `${input.fn}(${(input.argv || []).map(toDebugString)})`;
}

return JSON.stringify(input, null, 2);
}
5 changes: 5 additions & 0 deletions packages/util-endpoints/src/resolveEndpoint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EndpointV2 } from "@aws-sdk/types";

import { debugId, toDebugString } from "./debug";
import { EndpointError, EndpointResolverOptions, RuleSetObject } from "./types";
import { evaluateRules } from "./utils";

Expand All @@ -10,6 +11,8 @@ export const resolveEndpoint = (ruleSetObject: RuleSetObject, options: EndpointR
const { endpointParams, logger } = options;
const { parameters, rules } = ruleSetObject;

options.logger?.debug(debugId, `Initial EndpointParams: ${toDebugString(endpointParams)}`);

// @ts-ignore Type 'undefined' is not assignable to type 'string | boolean' (2322)
const paramsWithDefault: [string, string | boolean][] = Object.entries(parameters)
.filter(([, v]) => v.default != null)
Expand Down Expand Up @@ -45,5 +48,7 @@ export const resolveEndpoint = (ruleSetObject: RuleSetObject, options: EndpointR
}
}

options.logger?.debug(debugId, `Resolved endpoint: ${toDebugString(endpoint)}`);

return endpoint;
};
4 changes: 4 additions & 0 deletions packages/util-endpoints/src/utils/evaluateCondition.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { debugId, toDebugString } from "../debug";
import { ConditionObject, EndpointError, EvaluateOptions } from "../types";
import { callFunction } from "./callFunction";

Expand All @@ -6,6 +7,9 @@ export const evaluateCondition = ({ assign, ...fnArgs }: ConditionObject, option
throw new EndpointError(`'${assign}' is already defined in Reference Record.`);
}
const value = callFunction(fnArgs, options);

options.logger?.debug(debugId, `evaluateCondition: ${toDebugString(fnArgs)} = ${toDebugString(value)}`);

return {
result: value === "" ? true : !!value,
...(assign != null && { toAssign: { name: assign, value } }),
Expand Down
2 changes: 2 additions & 0 deletions packages/util-endpoints/src/utils/evaluateConditions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { debugId, toDebugString } from "../debug";
import { ConditionObject, EvaluateOptions, FunctionReturn } from "../types";
import { evaluateCondition } from "./evaluateCondition";

Expand All @@ -19,6 +20,7 @@ export const evaluateConditions = (conditions: ConditionObject[] = [], options:

if (toAssign) {
conditionsReferenceRecord[toAssign.name] = toAssign.value;
options.logger?.debug(debugId, `assign: ${toAssign.name} := ${toDebugString(toAssign.value)}`);
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/util-endpoints/src/utils/evaluateEndpointRule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EndpointV2 } from "@aws-sdk/types";

import { debugId, toDebugString } from "../debug";
import { EndpointRuleObject, EvaluateOptions } from "../types";
import { evaluateConditions } from "./evaluateConditions";
import { getEndpointHeaders } from "./getEndpointHeaders";
Expand All @@ -23,6 +24,9 @@ export const evaluateEndpointRule = (
};

const { url, properties, headers } = endpoint;

options.logger?.debug(debugId, `Resolving endpoint from template: ${toDebugString(endpoint)}`);

return {
...(headers != undefined && {
headers: getEndpointHeaders(headers, endpointRuleOptions),
Expand Down