Skip to content

Commit 0978274

Browse files
committed
feat: add logLevel and logRetryAfterSeconds options when initializing the lib
These are passed down to the underlying HttpExponentialBackoff instance from the @adobe/aio-lib-core-networking dependency
1 parent fbcf3c2 commit 0978274

File tree

5 files changed

+81
-5
lines changed

5 files changed

+81
-5
lines changed

doc/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ OpenWhisk credentials can also be read from environment variables `__OW_NAMESPAC
197197
| [config] | <code>object</code> | used to init the sdk |
198198
| [config.ow] | [<code>OpenWhiskCredentials</code>](#OpenWhiskCredentials) | [OpenWhiskCredentials](#OpenWhiskCredentials). Set those if you want to use ootb credentials to access the state management service. OpenWhisk namespace and auth can also be passed through environment variables: `__OW_NAMESPACE` and `__OW_API_KEY` |
199199
| [config.region] | <code>string</code> | optional region to use, accepted values: `amer` (default), `emea`, `apac` |
200+
| [config.logLevel] | <code>string</code> | optional log level for the HttpExponentialBackoff instance |
201+
| [config.logRetryAfterSeconds] | <code>number</code> | Defaults to 10. if the request has to retry because of a 429, it will log the retry attempt as a warning if the Retry-After value is greater than this number. Set to 0 to disable. |
200202

201203
<a name="AdobeStateCredentials"></a>
202204

lib/AdobeState.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,10 @@ class AdobeState {
149149
* @param {string} apikey the apikey for the Adobe State Store
150150
* @param {string} env the Adobe environment (AIO_CLI_ENV)
151151
* @param {('amer'|'apac'|'emea')} [region] the region for the Adobe State Store. defaults to 'amer'
152+
* @param {string} [logLevel] the log level for the HttpExponentialBackoff instance
153+
* @param {number} [logRetryAfterSeconds] if the request has to retry because of a 429, it will log the retry attempt as a warning if the Retry-After value is greater than this number. Set to 0 to disable.
152154
*/
153-
constructor (namespace, apikey, env, region) {
155+
constructor (namespace, apikey, env, region, logLevel, logRetryAfterSeconds) {
154156
/** @private */
155157
this.namespace = namespace
156158
/** @private */
@@ -162,7 +164,7 @@ class AdobeState {
162164
/** @private */
163165
this.endpoint = this.getRegionalEndpoint(ENDPOINTS[env], region)
164166
/** @private */
165-
this.fetchRetry = new HttpExponentialBackoff()
167+
this.fetchRetry = new HttpExponentialBackoff({ logLevel, logRetryAfterSeconds })
166168
}
167169

168170
/**
@@ -265,7 +267,14 @@ class AdobeState {
265267
}))
266268
}
267269

268-
return new AdobeState(credentials.namespace, credentials.apikey, env, credentials.region)
270+
return new AdobeState(
271+
credentials.namespace,
272+
credentials.apikey,
273+
env,
274+
credentials.region,
275+
credentials.logLevel,
276+
credentials.logRetryAfterSeconds
277+
)
269278
}
270279

271280
/* **************************** ADOBE STATE STORE OPERATORS ***************************** */

lib/init.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const logger = require('@adobe/aio-lib-core-logging')('@adobe/aio-lib-state', {
1414

1515
const utils = require('./utils')
1616
const { AdobeState } = require('./AdobeState')
17+
const DEFAULT_LOG_RETRY_AFTER_SECONDS = 10
1718

1819
/* *********************************** typedefs *********************************** */
1920
/**
@@ -43,15 +44,24 @@ const { AdobeState } = require('./AdobeState')
4344
* namespace and auth can also be passed through environment variables:
4445
* `__OW_NAMESPACE` and `__OW_API_KEY`
4546
* @param {string} [config.region] optional region to use, accepted values: `amer` (default), `emea`, `apac`
47+
* @param {string} [config.logLevel] optional log level for the HttpExponentialBackoff instance
48+
* @param {number} [config.logRetryAfterSeconds] Defaults to 10. if the request has to retry because of a 429, it will log the retry attempt as a warning if the Retry-After value is greater than this number. Set to 0 to disable.
4649
* @returns {Promise<AdobeState>} An AdobeState instance
4750
*/
4851
async function init (config = {}) {
4952
const logConfig = utils.withHiddenFields(config, ['ow.auth'])
50-
5153
logger.debug(`init with config: ${JSON.stringify(logConfig, null, 2)}`)
5254

5355
const { auth: apikey, namespace } = (config.ow ?? {})
54-
return AdobeState.init({ apikey, namespace, region: config.region })
56+
const { region, logLevel, logRetryAfterSeconds = DEFAULT_LOG_RETRY_AFTER_SECONDS } = config
57+
58+
return AdobeState.init({
59+
apikey,
60+
namespace,
61+
region,
62+
logLevel,
63+
logRetryAfterSeconds
64+
})
5565
}
5666

5767
module.exports = { init }

test/init.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,24 @@ OF ANY KIND, either express or implied. See the License for the specific languag
1010
governing permissions and limitations under the License.
1111
*/
1212
const stateLib = require('../index')
13+
const { HttpExponentialBackoff } = require('@adobe/aio-lib-core-networking')
14+
15+
jest.mock('@adobe/aio-lib-core-networking')
1316

1417
describe('init', () => {
1518
const env = process.env
19+
const mockExponentialBackoff = jest.fn()
1620

1721
beforeEach(() => {
1822
jest.resetModules()
1923
process.env = { ...env }
24+
jest.clearAllMocks()
25+
26+
HttpExponentialBackoff.mockImplementation((options) => {
27+
return {
28+
exponentialBackoff: mockExponentialBackoff
29+
}
30+
})
2031
})
2132

2233
afterEach(() => {
@@ -46,4 +57,44 @@ describe('init', () => {
4657
expect(store.namespace).toEqual(process.env.__OW_NAMESPACE)
4758
expect(store.apikey).toEqual(process.env.__OW_API_KEY)
4859
})
60+
61+
test('pass logLevel in config', async () => {
62+
expect.hasAssertions()
63+
const logLevel = 'debug'
64+
const store = await stateLib.init({ ow: fakeOWCreds, logLevel })
65+
66+
expect(store.namespace).toEqual(fakeOWCreds.namespace)
67+
expect(store.apikey).toEqual(fakeOWCreds.auth)
68+
expect(HttpExponentialBackoff).toHaveBeenCalledWith({ logLevel, logRetryAfterSeconds: 10 })
69+
})
70+
71+
test('when logLevel is not provided, HttpExponentialBackoff receives undefined', async () => {
72+
expect.hasAssertions()
73+
const store = await stateLib.init({ ow: fakeOWCreds })
74+
75+
expect(store.namespace).toEqual(fakeOWCreds.namespace)
76+
expect(store.apikey).toEqual(fakeOWCreds.auth)
77+
expect(HttpExponentialBackoff).toHaveBeenCalledWith({ logLevel: undefined, logRetryAfterSeconds: 10 })
78+
})
79+
80+
test('pass logRetryAfterSeconds in config', async () => {
81+
expect.hasAssertions()
82+
const logRetryAfterSeconds = 20
83+
const store = await stateLib.init({ ow: fakeOWCreds, logRetryAfterSeconds })
84+
85+
expect(store.namespace).toEqual(fakeOWCreds.namespace)
86+
expect(store.apikey).toEqual(fakeOWCreds.auth)
87+
expect(HttpExponentialBackoff).toHaveBeenCalledWith({ logLevel: undefined, logRetryAfterSeconds })
88+
})
89+
90+
test('pass both logLevel and logRetryAfterSeconds in config', async () => {
91+
expect.hasAssertions()
92+
const logLevel = 'debug'
93+
const logRetryAfterSeconds = 30
94+
const store = await stateLib.init({ ow: fakeOWCreds, logLevel, logRetryAfterSeconds })
95+
96+
expect(store.namespace).toEqual(fakeOWCreds.namespace)
97+
expect(store.apikey).toEqual(fakeOWCreds.auth)
98+
expect(HttpExponentialBackoff).toHaveBeenCalledWith({ logLevel, logRetryAfterSeconds })
99+
})
49100
})

types.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,14 @@ export type OpenWhiskCredentials = {
166166
* namespace and auth can also be passed through environment variables:
167167
* `__OW_NAMESPACE` and `__OW_API_KEY`
168168
* @param [config.region] - optional region to use, accepted values: `amer` (default), `emea`, `apac`
169+
* @param [config.logLevel] - optional log level for the HttpExponentialBackoff instance
170+
* @param [config.logRetryAfterSeconds] - Defaults to 10. if the request has to retry because of a 429, it will log the retry attempt as a warning if the Retry-After value is greater than this number. Set to 0 to disable.
169171
* @returns An AdobeState instance
170172
*/
171173
export function init(config?: {
172174
ow?: OpenWhiskCredentials;
173175
region?: string;
176+
logLevel?: string;
177+
logRetryAfterSeconds?: number;
174178
}): Promise<AdobeState>;
175179

0 commit comments

Comments
 (0)