-
Notifications
You must be signed in to change notification settings - Fork 156
chore(maintenance): add powertools to user-agent in SDK clients #1567
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
dreamorosi
merged 17 commits into
aws-powertools:main
from
am29d:1533-user-agent-in-aws-sdk-clients
Jul 3, 2023
Merged
Changes from 6 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
fe431b7
add custom user agent middleware
d14811a
remove singleton for ddb client, add useragent in constructor
b4bd495
remove conditional, because SDK will always resolve user-agent header
26cee59
simplify test
8296eea
remove retry, no longer needed
bdda143
remove ua from idempotency, will be done in separate PR
1ad12b4
review changes
2a23d25
revert client lazy loading
54bd3ed
Revert "remove ua from idempotency, will be done in separate PR"
7a73b3b
revert the revert, misunderstanding
4a7d814
add explicit ts-ignore instead of the entire file
2eb80e2
merge main
8d3c839
parameterized tests for useragent
30f3cf6
in case of failure, warn and don't block user code
4cbc526
add test to check if middleware absorbs an error and continue
89cb7f7
add more client to useragent test
0af820c
fix imports
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './cleanupMiddlewares'; | ||
export * from './constants'; | ||
export * from './userAgentMiddleware'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-nocheck | ||
dreamorosi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* @internal | ||
*/ | ||
import { version as PT_VERSION } from '../version'; | ||
|
||
const EXEC_ENV = process.env.AWS_EXECUTION_ENV || 'NA'; | ||
const middlewareOptions = { | ||
step: 'finalizeRequest', | ||
name: 'addPowertoolsToUserAgent', | ||
tags: ['POWERTOOLS', 'USER_AGENT'], | ||
}; | ||
|
||
/** | ||
* @internal | ||
* returns a middleware function for the MiddlewareStack, that can be used for the SDK clients | ||
* @param feature | ||
*/ | ||
const customUserAgentMiddleware = (feature: string) => { | ||
return (next, _context) => async (args) => { | ||
const powertoolsUserAgent = `PT/${feature}/${PT_VERSION} PTEnv/${EXEC_ENV}`; | ||
args.request.headers[ | ||
'user-agent' | ||
] = `${args.request.headers['user-agent']} ${powertoolsUserAgent}`; | ||
|
||
return await next(args); | ||
}; | ||
}; | ||
|
||
const addUserAgentMiddleware = (client, feature): void => { | ||
client.middlewareStack.add( | ||
customUserAgentMiddleware(feature), | ||
middlewareOptions | ||
); | ||
}; | ||
|
||
export { addUserAgentMiddleware }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// this file is auto generated, do not modify | ||
module.exports = { version: '1.10.0' }; | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { addUserAgentMiddleware } from '../../src/middleware/userAgentMiddleware'; | ||
import { InvokeCommand, LambdaClient } from '@aws-sdk/client-lambda'; | ||
|
||
/** | ||
* Logs request headers | ||
* | ||
* This is a middleware we use to test | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
const logHeadersMiddleware = (next, _context) => async (args) => { | ||
console.log(args.request.headers); | ||
|
||
return await next(args); | ||
}; | ||
|
||
describe('Function: addUserAgentMiddleware', () => { | ||
it('adds powertools user agent to request header at the end', async () => { | ||
const lambdaClient = new LambdaClient({ | ||
logger: console, | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
region: 'us-east-1', | ||
endpoint: 'http://localhost:9001', | ||
}); | ||
|
||
// Set a spy on the console.log method, so we can check the headers | ||
const consoleSpy = jest.spyOn(console, 'log').mockImplementation(); | ||
|
||
addUserAgentMiddleware(lambdaClient, 'my-feature'); | ||
|
||
expect(lambdaClient.middlewareStack.identify()).toContain( | ||
'addPowertoolsToUserAgent: POWERTOOLS,USER_AGENT' | ||
); | ||
|
||
lambdaClient.middlewareStack.addRelativeTo(logHeadersMiddleware, { | ||
relation: 'after', | ||
toMiddleware: 'addPowertoolsToUserAgent', | ||
name: 'logHeadersMiddleware', | ||
tags: ['TEST'], | ||
}); | ||
|
||
await expect(() => | ||
lambdaClient.send( | ||
new InvokeCommand({ | ||
FunctionName: 'test', | ||
Payload: new TextEncoder().encode(JSON.stringify('foo')), | ||
}) | ||
) | ||
).rejects.toThrow(); | ||
|
||
expect(consoleSpy).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
'user-agent': expect.stringContaining('PT/my-feature/1.10.0 PTEnv/NA'), | ||
}) | ||
); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.