Skip to content

Commit

Permalink
fix(rum-api-client): extra RUM Bundler requests logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ekremney committed Jan 24, 2025
1 parent 04a97ba commit 5549897
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,17 @@ async function fetchBundles(opts, log) {

const chunks = getUrlChunks(urls, CHUNK_SIZE);

let totalTransferSize = 0;

const result = [];
for (const chunk of chunks) {
const responses = await Promise.all(chunk.map((url) => {
log.info(`Retrieving RUM bundles. Granularity: ${granularity}. Domain: ${domain}`);
return fetch(url);
// eslint-disable-next-line no-loop-func
const responses = await Promise.all(chunk.map(async (url) => {
const response = await fetch(url);
const xCache = response.headers.get('x-cache')?.toLowerCase().includes('hit');
log.info(`Retrieving RUM bundles. Source: ${xCache ? 'CDN' : 'ORIGIN'}. Granularity: ${granularity}. Domain: ${domain}`);
totalTransferSize += parseInt(response.headers.get('content-length'), 10);
return response;
}));
const bundles = await Promise.all(responses.map((response) => response.json()));

Expand All @@ -201,6 +207,7 @@ async function fetchBundles(opts, log) {
.forEach((bundle) => result.push(bundle));
});
}
log.info(`Retrieved RUM bundles. Total transfer size (in KB): ${(totalTransferSize / 1024).toFixed(2)}`);
return mergeBundlesWithSameId(result);
}

Expand Down
13 changes: 11 additions & 2 deletions packages/spacecat-shared-rum-api-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import { hasText } from '@adobe/spacecat-shared-utils';
import { fetchBundles } from './common/rum-bundler-client.js';
import notfound from './functions/404.js';
import notfoundInternalLinks from './functions/404-internal-links.js';
Expand Down Expand Up @@ -36,6 +37,14 @@ const HANDLERS = {
'high-organic-low-ctr': highOrganicLowCtr,
};

function sanitize(opts) {
return {
...opts,
/* c8 ignore next 1 */
...(hasText(opts.domainkey) && { domainkey: `${opts.domainkey.slice(0, 3)}***` }),
};
}

export default class RUMAPIClient {
static createFrom(context) {
const { log = console } = context;
Expand Down Expand Up @@ -66,7 +75,7 @@ export default class RUMAPIClient {

return handler(bundles, opts);
} catch (e) {
throw new Error(`Query '${query}' failed. Opts: ${JSON.stringify(opts)}. Reason: ${e.message}`);
throw new Error(`Query '${query}' failed. Opts: ${JSON.stringify(sanitize(opts))}. Reason: ${e.message}`);
}
}

Expand Down Expand Up @@ -105,7 +114,7 @@ export default class RUMAPIClient {

return results;
} catch (e) {
throw new Error(`Multi query failed. Queries: ${JSON.stringify(queries)}, Opts: ${JSON.stringify(opts)}. Reason: ${e.message}`);
throw new Error(`Multi query failed. Queries: ${JSON.stringify(queries)}, Opts: ${JSON.stringify(sanitize(opts))}. Reason: ${e.message}`);
}
}
}

0 comments on commit 5549897

Please sign in to comment.