Skip to content

chore(maintenance): set app sdk ua env variable when none is set #3651

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 6 commits into from
Feb 25, 2025
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
18 changes: 16 additions & 2 deletions packages/commons/src/awsSdkUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,23 @@ const isSdkClient = (client: unknown): client is SdkClient =>
const customUserAgentMiddleware = (feature: string) => {
return <T extends MiddlewareArgsLike>(next: (arg0: T) => Promise<T>) =>
async (args: T) => {
const powertoolsUserAgent = `PT/${feature}/${PT_VERSION} PTEnv/${EXEC_ENV}`;
const existingUserAgent = args.request.headers['user-agent'] || '';
if (existingUserAgent.includes('PT/NO-OP')) {
const featureSpecificUserAgent = existingUserAgent.replace(
'PT/NO-OP',
`PT/${feature}/${PT_VERSION} PTEnv/${EXEC_ENV}`
);

args.request.headers['user-agent'] = featureSpecificUserAgent;
return await next(args);
}
if (existingUserAgent.includes('PT/')) {
return await next(args);
}
args.request.headers['user-agent'] =
`${args.request.headers['user-agent']} ${powertoolsUserAgent}`;
existingUserAgent === ''
? `PT/${feature}/${PT_VERSION} PTEnv/${EXEC_ENV}`
: `${existingUserAgent} PT/${feature}/${PT_VERSION} PTEnv/${EXEC_ENV}`;

return await next(args);
};
Expand Down
6 changes: 5 additions & 1 deletion packages/commons/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { PT_VERSION } from './version.js';
if (!process.env.AWS_SDK_UA_APP_ID) {
process.env.AWS_SDK_UA_APP_ID = `PT/NO-OP/${PT_VERSION}`;
}
export { PT_VERSION } from './version.js';
export {
isRecord,
isString,
Expand All @@ -19,4 +24,3 @@ export {
METRICS_KEY,
IDEMPOTENCY_KEY,
} from './middleware/constants.js';
export { PT_VERSION } from './version.js';
45 changes: 34 additions & 11 deletions packages/commons/tests/unit/awsSdkUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,49 @@ describe('Helpers: awsSdk', () => {
expect(middleware).toBeInstanceOf(Function);
});

it('adds the Powertools UA to the request headers', async () => {
const feature = 'my-feature';
const middleware = customUserAgentMiddleware(feature);

it.each([
{
case: 'adds the feature-specific UA when none is present',
headers: {},
expected: `PT/my-feature/${version} PTEnv/NA`,
},
{
case: 'concatenates the ua to existing ones',
headers: {
'user-agent': 'foo',
},
expected: `foo PT/my-feature/${version} PTEnv/NA`,
},
{
case: 'replaces no-op UA with the feature-specific one',
headers: {
'user-agent': 'PT/NO-OP',
},
expected: `PT/my-feature/${version} PTEnv/NA`,
},
{
case: 'leaves a feature-specific UA intact',
headers: {
'user-agent': 'PT/other-feature/1.0 PTEnv/NA',
},
expected: 'PT/other-feature/1.0 PTEnv/NA',
},
])('it $case', async ({ headers, expected }) => {
// Prepare
const feature = 'my-feature';
const middleware = customUserAgentMiddleware(feature);
const next = vi.fn();
const args = {
request: {
headers: {
'user-agent': 'foo',
},
headers,
},
};

// Act
await middleware(next)(args);
await middleware(vi.fn())(args);

// Assess
expect(args.request.headers['user-agent']).toEqual(
`foo PT/my-feature/${version} PTEnv/NA`
);
expect(args.request.headers['user-agent']).toEqual(expected);
});
});

Expand Down