Skip to content

Commit 1f776e6

Browse files
feat: implement configuration to pre-populate the cache with spending plans (#3058)
* chore: refactor configurations of `HbarLimitService` Signed-off-by: Victor Yanev <[email protected]> * chore: fix ts errors Signed-off-by: Victor Yanev <[email protected]> * chore: revert formatting of table Signed-off-by: Victor Yanev <[email protected]> * refactor: configurations of `HbarLimitService` Signed-off-by: Victor Yanev <[email protected]> * Merge branch 'main' into 2970-Note-what-configurations-are-used Signed-off-by: Victor Yanev <[email protected]> # Conflicts: # packages/relay/src/lib/db/repositories/hbarLimiter/hbarSpendingPlanRepository.ts # packages/relay/src/lib/services/hbarLimitService/index.ts # packages/relay/tests/lib/services/hbarLimitService/hbarLimitService.spec.ts * docs: add configuration subsections to table of contents Signed-off-by: Victor Yanev <[email protected]> * docs: make descriptions of configurations consistent Signed-off-by: Victor Yanev <[email protected]> * chore: revert changes to localLRUCache.ts and redisCache.ts Signed-off-by: Victor Yanev <[email protected]> * chore: address comments Signed-off-by: Victor Yanev <[email protected]> * chore: address comments Signed-off-by: Victor Yanev <[email protected]> * chore: address comments Signed-off-by: Victor Yanev <[email protected]> * chore: address comments Signed-off-by: Victor Yanev <[email protected]> * chore: moves TODO comment to constants.ts Signed-off-by: Victor Yanev <[email protected]> * chore: optimize resetDate calculation Signed-off-by: Victor Yanev <[email protected]> * chore: remove unused imports Signed-off-by: Victor Yanev <[email protected]> * Merge branch 'main' into 2970-Note-what-configurations-are-used Signed-off-by: Victor Yanev <[email protected]> # Conflicts: # packages/relay/src/lib/db/repositories/hbarLimiter/ethAddressHbarSpendingPlanRepository.ts # packages/relay/src/lib/db/repositories/hbarLimiter/hbarSpendingPlanRepository.ts # packages/relay/src/lib/db/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.ts # packages/relay/src/lib/services/hbarLimitService/index.ts # packages/relay/tests/lib/repositories/hbarLimiter/hbarSpendingPlanRepository.spec.ts # packages/relay/tests/lib/sdkClient.spec.ts # packages/relay/tests/lib/services/hbarLimitService/hbarLimitService.spec.ts * feat: Implement configuration to pre-populate the cache with spending plans Signed-off-by: Victor Yanev <[email protected]> * chore: address comments Signed-off-by: Victor Yanev <[email protected]> * chore: address comments Signed-off-by: Victor Yanev <[email protected]> * chore: address comments + added improvements to logic Signed-off-by: Victor Yanev <[email protected]> * chore: added tests + improvements to logic Signed-off-by: Victor Yanev <[email protected]> * docs: update docs Signed-off-by: Victor Yanev <[email protected]> * docs: reformat table Signed-off-by: Victor Yanev <[email protected]> * chore: improvements to hbarSpendingPlanConfigService.spec.ts Signed-off-by: Victor Yanev <[email protected]> * chore: fix typo Signed-off-by: Victor Yanev <[email protected]> * chore: revert unneeded changes Signed-off-by: Victor Yanev <[email protected]> * chore: censor IP addresses in logs of localLRUCache.ts and redisCache.ts Signed-off-by: Victor Yanev <[email protected]> * chore: censor IP addresses in logs of localLRUCache.ts and redisCache.ts Signed-off-by: Victor Yanev <[email protected]> * fix: hbarSpendingPlanRepository.spec.ts after resolving conflicts from main branch Signed-off-by: Victor Yanev <[email protected]> * fix: metricService.spec.ts Signed-off-by: Victor Yanev <[email protected]> * chore: fix wrong value for limit duration in `hbar-limiter.md` Co-authored-by: Logan Nguyen <[email protected]> Signed-off-by: Victor Yanev <[email protected]> * chore: fix jsdocs Co-authored-by: Logan Nguyen <[email protected]> Signed-off-by: Victor Yanev <[email protected]> * fix: hbarSpendingPlanRepository.ts Signed-off-by: Victor Yanev <[email protected]> * fix: ethAddressHbarSpendingPlanRepository.ts Signed-off-by: Victor Yanev <[email protected]> * fix: ipAddressHbarSpendingPlanRepository.ts Signed-off-by: Victor Yanev <[email protected]> * fix: jsdocs in hbarLimitService/index.ts Signed-off-by: Victor Yanev <[email protected]> * fix: do not reset metrics Signed-off-by: Victor Yanev <[email protected]> * Merge branch '2970-Note-what-configurations-are-used' into 3055-Implement-configuration-to-pre-populate-the-cache-with-spending-plans Signed-off-by: Victor Yanev <[email protected]> # Conflicts: # packages/relay/src/lib/db/repositories/hbarLimiter/hbarSpendingPlanRepository.ts # packages/relay/tests/lib/repositories/hbarLimiter/hbarSpendingPlanRepository.spec.ts * chore: remove mocks from hbarSpendingPlanConfigService.spec.ts Signed-off-by: Victor Yanev <[email protected]> * chore: remove mocks from hbarSpendingPlanConfigService.spec.ts Signed-off-by: Victor Yanev <[email protected]> * fix: delete obsolete IP and ETH address associations from the cache Signed-off-by: Victor Yanev <[email protected]> * chore: rename `SubscriptionType` to `SubscriptionTier` Signed-off-by: Victor Yanev <[email protected]> * chore: add jsdocs in hbarSpendingPlanConfigService.ts Signed-off-by: Victor Yanev <[email protected]> * chore: address sonar issue Signed-off-by: Victor Yanev <[email protected]> * chore: extend tests in relay.spec.ts Signed-off-by: Victor Yanev <[email protected]> * chore: extend tests in server.spec.ts Signed-off-by: Victor Yanev <[email protected]> * chore: extend tests in hbarSpendingPlanConfigService.spec.ts Signed-off-by: Victor Yanev <[email protected]> * chore: extend tests in hbarSpendingPlanConfigService.spec.ts Signed-off-by: Victor Yanev <[email protected]> * chore: reduce code duplication in hbarSpendingPlanConfigService.spec.ts Signed-off-by: Victor Yanev <[email protected]> * chore: remove unneeded mocks from hbarSpendingPlanConfigService.spec.ts Signed-off-by: Victor Yanev <[email protected]> * chore: extend tests in hbarSpendingPlanConfigService.spec.ts Signed-off-by: Victor Yanev <[email protected]> * chore: remove unused argument of helper method in hbarSpendingPlanConfigService.spec.ts Signed-off-by: Victor Yanev <[email protected]> * chore: move call of populatePreconfiguredSpendingPlans() back to constructor Signed-off-by: Victor Yanev <[email protected]> * chore: remove unused import Signed-off-by: Victor Yanev <[email protected]> * chore: address comments Signed-off-by: Victor Yanev <[email protected]> * chore: extend tests in localLRUCache.spec.ts and redisCache.spec.ts Signed-off-by: Victor Yanev <[email protected]> * fix: case sensitivity in ethAddressHbarSpendingPlanRepository.ts Signed-off-by: Victor Yanev <[email protected]> * chore: only trace log when configuration file is missing Signed-off-by: Victor Yanev <[email protected]> * chore: log `Pre-configured spending plans populated successfully` only if any spending plans have been deleted or added Signed-off-by: Victor Yanev <[email protected]> * test: extend repository tests with new methods Signed-off-by: Victor Yanev <[email protected]> * test: extend relay.spec.ts Signed-off-by: Victor Yanev <[email protected]> * fix: failing unit tests Signed-off-by: Victor Yanev <[email protected]> * chore: use override envs helper method in tests Signed-off-by: Victor Yanev <[email protected]> --------- Signed-off-by: Victor Yanev <[email protected]> Signed-off-by: Victor Yanev <[email protected]> Co-authored-by: Logan Nguyen <[email protected]>
1 parent 2e2fc07 commit 1f776e6

27 files changed

+1792
-291
lines changed

docs/configuration.md

Lines changed: 78 additions & 77 deletions
Large diffs are not rendered by default.

docs/design/hbar-limiter.md

Lines changed: 103 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ The purpose of the HBar Limiter is to track and control the spending of HBars in
8181

8282
The HBar limiter will be implemented as a separate service, used by other services/classes that need it. It will have two main purposes - to capture the gas fees for different operation and to check if an operation needs to be paused, due to an exceeded HBar limit.
8383

84+
### What is an HbarSpendingPlan?
85+
86+
An `HbarSpendingPlan` is a record that tracks the total amount of HBars spent by a user or group of users (linked by ETH and IP addresses) over a specific period. It includes the following information:
87+
- **id**: A unique identifier for the spending plan.
88+
- **subscriptionTier**: The tier of the user or group of users (BASIC, EXTENDED, or PRIVILEGED).
89+
- **createdAt**: The timestamp when the spending plan was created.
90+
- **active**: A flag indicating whether the spending plan is currently active.
91+
- **spendingHistory**: A list of spending records, each containing the amount spent and the timestamp.
92+
8493
### General Users (BASIC tier):
8594

8695
**NOTE:** Each general user will have a unique spending plan, linked both to their ETH and IP addresses. Each new user will be automatically assigned a BASIC spending plan when they send their first transaction and this plan will remain linked to them for any subsequent requests.
@@ -172,7 +181,7 @@ classDiagram
172181
classDiagram
173182
class HbarSpendingPlan {
174183
-id: string
175-
-subscriptionType: SubscriptionType
184+
-subscriptionTier: SubscriptionTier
176185
-createdAt: Date
177186
-active: boolean
178187
-spendingHistory: HbarSpendingRecord[]
@@ -211,7 +220,7 @@ classDiagram
211220
-cache: CacheService
212221
+findById(id: string): Promise<IHbarSpendingPlan>
213222
+findByIdWithDetails(id: string): Promise<IDetailedHbarSpendingPlan>
214-
+create(subscriptionType: SubscriptionType): Promise<IDetailedHbarSpendingPlan>
223+
+create(subscriptionTier: SubscriptionTier): Promise<IDetailedHbarSpendingPlan>
215224
+checkExistsAndActive(id: string): Promise<void>
216225
+getSpendingHistory(id: string): Promise<HbarSpendingRecord[]>
217226
+addAmountToSpendingHistory(id: string, amount: number): Promise<number>
@@ -233,13 +242,13 @@ classDiagram
233242
+delete(ip: string): Promise<void>
234243
}
235244
236-
class SubscriptionType
237-
<<Enumeration>> SubscriptionType
238-
SubscriptionType : BASIC
239-
SubscriptionType : EXTENDED
240-
SubscriptionType : PRIVILEGED
245+
class SubscriptionTier
246+
<<Enumeration>> SubscriptionTier
247+
SubscriptionTier : BASIC
248+
SubscriptionTier : EXTENDED
249+
SubscriptionTier : PRIVILEGED
241250
242-
HbarSpendingPlan --> SubscriptionType : could be one of the types
251+
HbarSpendingPlan --> SubscriptionTier : could be one of the types
243252
HbarSpendingPlan --> HbarSpendingRecord : stores history of
244253
EthAddressHbarSpendingPlan --> HbarSpendingPlan : links an ETH address to
245254
IpAddressHbarSpendingPlan --> HbarSpendingPlan : links an IP address to
@@ -306,61 +315,128 @@ The following configurations will be used to automatically populate the cache wi
306315

307316
All other users (ETH and IP addresses which are not specified in the configuration file) will be treated as "general users" and will be assigned a basic `HbarSpendingPlan` on their first request and their ETH address and IP address will be linked to that plan for all subsequent requests.
308317

318+
### JSON Configuration File
319+
320+
The relay will read the pre-configured spending plans from a JSON file. This file should be placed in the root directory of the relay.
321+
322+
The default filename for the configuration file is `spendingPlansConfig.json`, but it could also be specified by the environment variable `HBAR_SPENDING_PLANS_CONFIG_FILE`.
323+
- `HBAR_SPENDING_PLANS_CONFIG_FILE`: The name of the file containing the pre-configured spending plans for supported projects and partners.
324+
325+
#### The JSON file should have the following structure:
309326
```json
310327
[
311328
{
329+
"id": "c758c095-342c-4607-9db5-867d7e90ab9d",
312330
"name": "partner name",
313331
"ethAddresses": ["0x123", "0x124"],
314332
"ipAddresses": ["127.0.0.1", "128.0.0.1"],
315-
"subscriptionType": "PRIVILEGED"
333+
"subscriptionTier": "PRIVILEGED"
316334
},
317335
{
336+
"id": "a68488b0-6f7d-44a0-87c1-774ad64615f2",
318337
"name": "some other partner that has given us only eth addresses",
319338
"ethAddresses": ["0x125", "0x126"],
320-
"subscriptionType": "PRIVILEGED"
339+
"subscriptionTier": "PRIVILEGED"
321340
},
322341
{
342+
"id": "af13d6ed-d676-4d33-8b9d-cf05d1ad7134",
323343
"name": "supported project name",
324344
"ethAddresses": ["0x127", "0x128"],
325345
"ipAddresses": ["129.0.0.1", "130.0.0.1"],
326-
"subscriptionType": "EXTENDED"
346+
"subscriptionTier": "EXTENDED"
327347
},
328348
{
349+
"id": "7f665aa3-6b73-41d7-bf9b-92d04cdab96b",
329350
"name": "some other supported project that has given us only ip addresses",
330351
"ipAddresses": ["131.0.0.1", "132.0.0.1"],
331-
"subscriptionType": "EXTENDED"
352+
"subscriptionTier": "EXTENDED"
332353
}
333354
]
334355
```
335356

336-
On every start-up, the relay will check if these entries are already populated in the cache. If not, it will populate them accordingly.
357+
#### Important notes
358+
- The `id` field is **strictly required** for each supported project or partner project. It is used as a unique identifier and as key in the cache and also for reference in the logs. We recommend using a UUID for this field, but any unique string will work.
359+
- The `name` field is used just for reference and can be any string. It is not used in the cache or for any other purpose, only for better readability in the logs on start-up of the relay when the spending plans are being configured.
360+
- The `ethAddresses` and `ipAddresses` fields are arrays of strings containing the ETH addresses and IP addresses associated with the supported project or partner project. **At least one** of these two fields must be present and contain **at least one entry**.
361+
- The `subscriptionTier` field is also **required**. It is an enum with the following possible values: `BASIC`, `EXTENDED`, and `PRIVILEGED`.
337362

338-
The JSON file can also be updated over time to add new supported projects or partner projects, and it will populate only the new entries on the next start-up.
363+
On every start-up, the relay will check if these entries are already populated in the cache. If not, it will populate them accordingly.
339364

340-
```json
365+
If the cache already contains some of these entries, it will only populate the new entries and remove the obsolete ones.
366+
367+
### Incremental changes to the JSON file
368+
369+
#### Adding new partners or supported projects
370+
371+
The JSON file can also be updated over time to add new partners or supported projects, and it will populate only the new entries on the next start-up.
372+
373+
```javascript
341374
[
342-
...,
375+
// rest of JSON file remains the same
376+
...oldContent,
343377
{
378+
"id": "0b054498-5c48-4402-aad4-b9b455f33457",
344379
"name": "new partner name",
345380
"ethAddresses": ["0x129", "0x130"],
346381
"ipAddresses": ["133.0.0.1"],
347-
"subscriptionType": "PRIVILEGED"
382+
"subscriptionTier": "PRIVILEGED"
383+
}
384+
]
385+
```
386+
387+
#### Removing or updating existing partners or supported projects
388+
389+
If some of the pre-configured plans are removed them from the JSON file, they will be considered "obsolete" and removed from the cache on the next start-up of the relay.
390+
391+
You can also add new ETH addresses or IP addresses to existing plans by updating the JSON file.
392+
393+
```javascript
394+
[
395+
// rest of JSON file remains the same
396+
...oldContent,
397+
{
398+
"id": "c758c095-342c-4607-9db5-867d7e90ab9d",
399+
"name": "partner name",
400+
"ethAddresses": ["0x123", "0x124", "<new_eth_address>"],
401+
"ipAddresses": ["127.0.0.1", "128.0.0.1", "<new_ip_address>", "<another_new_ip_address>"],
402+
"subscriptionTier": "PRIVILEGED",
403+
}
404+
]
405+
```
406+
407+
Or if you remove any existing ETH addresses or IP addresses from the JSON file, only those will be removed from the cache on the next start-up.
408+
409+
```javascript
410+
[
411+
// rest of JSON file remains the same
412+
...oldContent,
413+
{
414+
"id": "c758c095-342c-4607-9db5-867d7e90ab9d",
415+
"name": "partner name",
416+
"ethAddresses": ["0x123"], // removed "0x124"
417+
"ipAddresses": ["127.0.0.1"], // removed "128.0.0.1"
418+
"subscriptionTier": "PRIVILEGED",
348419
}
349420
]
350421
```
351422

352423
### Spending Limits of Different Tiers
353424

425+
**Units:** All environment variables are specified in tinybars (tℏ):
426+
```math
427+
1 HBAR (ℏ) = 100,000,000 tinybars (tℏ)
428+
```
429+
354430
The spending limits for different tiers are defined as environment variables:
355-
- `HBAR_RATE_LIMIT_BASIC`: The spending limit (in tinybars) for general users (tier 3)
356-
- `HBAR_RATE_LIMIT_EXTENDED`: The spending limit (in tinybars) for supported projects (tier 2)
357-
- `HBAR_RATE_LIMIT_PRIVILEGED`: The spending limit (in tinybars) for trusted partners (tier 1)
431+
- `HBAR_RATE_LIMIT_BASIC`: The spending limit (tℏ) for general users (tier 3)
432+
- `HBAR_RATE_LIMIT_EXTENDED`: The spending limit (tℏ) for supported projects (tier 2)
433+
- `HBAR_RATE_LIMIT_PRIVILEGED`: The spending limit (tℏ) for trusted partners (tier 1)
358434

359435
Example configuration for tiered spending limits:
360436
```dotenv
361-
HBAR_RATE_LIMIT_BASIC=92592592
362-
HBAR_RATE_LIMIT_EXTENDED=925925925
363-
HBAR_RATE_LIMIT_PRIVILEGED=1851851850
437+
HBAR_RATE_LIMIT_BASIC=10000000# 0.1 ℏ
438+
HBAR_RATE_LIMIT_EXTENDED=100000000# 1 ℏ
439+
HBAR_RATE_LIMIT_PRIVILEGED=1000000000# 10 ℏ
364440
```
365441

366442
### Total Budget and Limit Duration
@@ -369,14 +445,14 @@ The total budget and the limit duration are defined as environment variables:
369445
- `HBAR_RATE_LIMIT_DURATION`: The time window (in milliseconds) for which both the total budget and the spending limits are applicable.
370446
- On initialization of `HbarLimitService`, a reset timestamp is calculated by adding the `HBAR_RATE_LIMIT_DURATION` to the current timestamp.
371447
- The total budget and spending limits are reset when the current timestamp exceeds the reset timestamp.
372-
- `HBAR_RATE_LIMIT_TINYBAR`: The ceiling (in tinybars) on the total amount of HBARs that can be spent in the limit duration.
448+
- `HBAR_RATE_LIMIT_TINYBAR`: The ceiling on the total amount (in tℏ) that can be spent in the limit duration.
373449
- This is the largest bucket from which others pull from.
374450
- If the total amount spent exceeds this limit, all spending is paused until the next reset.
375451

376-
Example configuration for a total budget of 110 HBARs (11_000_000_000 tinybars) per 80 seconds:
452+
Example configuration for a total budget and limit duration:
377453
```dotenv
378-
HBAR_RATE_LIMIT_TINYBAR=11000000000
379-
HBAR_RATE_LIMIT_DURATION=80000
454+
HBAR_RATE_LIMIT_TINYBAR=11000000000# 110 ℏ
455+
HBAR_RATE_LIMIT_DURATION=80000# 80 seconds
380456
```
381457

382458
## Additional Considerations

packages/relay/src/lib/clients/cache/localLRUCache.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { ICacheClient } from './ICacheClient';
2424
import constants from '../../constants';
2525
import LRUCache, { LimitedByCount, LimitedByTTL } from 'lru-cache';
2626
import { RequestDetails } from '../../types';
27+
import { Utils } from '../../../utils';
2728

2829
/**
2930
* Represents a LocalLRUCache instance that uses an LRU (Least Recently Used) caching strategy
@@ -105,11 +106,10 @@ export class LocalLRUCache implements ICacheClient {
105106
public async get(key: string, callingMethod: string, requestDetails: RequestDetails): Promise<any> {
106107
const value = this.cache.get(key);
107108
if (value !== undefined) {
108-
this.logger.trace(
109-
`${requestDetails.formattedRequestId} returning cached value ${key}:${JSON.stringify(
110-
value,
111-
)} on ${callingMethod} call`,
112-
);
109+
const censoredKey = key.replace(Utils.IP_ADDRESS_REGEX, '<REDACTED>');
110+
const censoredValue = JSON.stringify(value).replace(/"ipAddress":"[^"]+"/, '"ipAddress":"<REDACTED>"');
111+
const message = `Returning cached value ${censoredKey}:${censoredValue} on ${callingMethod} call`;
112+
this.logger.trace(`${requestDetails.formattedRequestId} ${message}`);
113113
return value;
114114
}
115115

@@ -148,10 +148,19 @@ export class LocalLRUCache implements ICacheClient {
148148
ttl?: number,
149149
): Promise<void> {
150150
const resolvedTtl = ttl ?? this.options.ttl;
151+
if (resolvedTtl > 0) {
152+
this.cache.set(key, value, { ttl: resolvedTtl });
153+
} else {
154+
this.cache.set(key, value, { ttl: 0 }); // 0 means indefinite time
155+
}
156+
const censoredKey = key.replace(Utils.IP_ADDRESS_REGEX, '<REDACTED>');
157+
const censoredValue = JSON.stringify(value).replace(/"ipAddress":"[^"]+"/, '"ipAddress":"<REDACTED>"');
158+
const message = `Caching ${censoredKey}:${censoredValue} on ${callingMethod} for ${
159+
resolvedTtl > 0 ? `${resolvedTtl} ms` : 'indefinite time'
160+
}`;
151161
this.logger.trace(
152-
`${requestDetails.formattedRequestId} caching ${key}:${JSON.stringify(value)} for ${resolvedTtl} ms`,
162+
`${requestDetails.formattedRequestId} ${message} (cache size: ${this.cache.size}, max: ${this.options.max})`,
153163
);
154-
this.cache.set(key, value, { ttl: resolvedTtl });
155164
}
156165

157166
/**

packages/relay/src/lib/clients/cache/redisCache.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { RedisCacheError } from '../../errors/RedisCacheError';
2626
import constants from '../../constants';
2727
import { IRedisCacheClient } from './IRedisCacheClient';
2828
import { RequestDetails } from '../../types';
29+
import { Utils } from '../../../utils';
2930

3031
/**
3132
* A class that provides caching functionality using Redis.
@@ -133,11 +134,10 @@ export class RedisCache implements IRedisCacheClient {
133134
const client = await this.getConnectedClient();
134135
const result = await client.get(key);
135136
if (result) {
136-
this.logger.trace(
137-
`${requestDetails.formattedRequestId} returning cached value ${key}:${JSON.stringify(
138-
result,
139-
)} on ${callingMethod} call`,
140-
);
137+
const censoredKey = key.replace(Utils.IP_ADDRESS_REGEX, '<REDACTED>');
138+
const censoredValue = result.replace(/"ipAddress":"[^"]+"/, '"ipAddress":"<REDACTED>"');
139+
const message = `Returning cached value ${censoredKey}:${censoredValue} on ${callingMethod} call`;
140+
this.logger.trace(`${requestDetails.formattedRequestId} ${message}`);
141141
// TODO: add metrics
142142
return JSON.parse(result);
143143
}
@@ -164,11 +164,18 @@ export class RedisCache implements IRedisCacheClient {
164164
const client = await this.getConnectedClient();
165165
const serializedValue = JSON.stringify(value);
166166
const resolvedTtl = ttl ?? this.options.ttl; // in milliseconds
167+
if (resolvedTtl > 0) {
168+
await client.set(key, serializedValue, { PX: resolvedTtl });
169+
} else {
170+
await client.set(key, serializedValue);
171+
}
167172

168-
await client.set(key, serializedValue, { PX: resolvedTtl });
169-
this.logger.trace(
170-
`${requestDetails.formattedRequestId} caching ${key}: ${serializedValue} on ${callingMethod} for ${resolvedTtl} s`,
171-
);
173+
const censoredKey = key.replace(Utils.IP_ADDRESS_REGEX, '<REDACTED>');
174+
const censoredValue = serializedValue.replace(/"ipAddress":"[^"]+"/, '"ipAddress":"<REDACTED>"');
175+
const message = `Caching ${censoredKey}:${censoredValue} on ${callingMethod} for ${
176+
resolvedTtl > 0 ? `${resolvedTtl} ms` : 'indefinite time'
177+
}`;
178+
this.logger.trace(`${requestDetails.formattedRequestId} ${message}`);
172179
// TODO: add metrics
173180
}
174181

@@ -192,12 +199,8 @@ export class RedisCache implements IRedisCacheClient {
192199
serializedKeyValuePairs[key] = JSON.stringify(value);
193200
}
194201

195-
try {
196-
// Perform mSet operation
197-
await client.mSet(serializedKeyValuePairs);
198-
} catch (e) {
199-
this.logger.error(e);
200-
}
202+
// Perform mSet operation
203+
await client.mSet(serializedKeyValuePairs);
201204

202205
// Log the operation
203206
const entriesLength = Object.keys(keyValuePairs).length;

0 commit comments

Comments
 (0)