Skip to content
Merged
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
55 changes: 31 additions & 24 deletions x-pack/plugins/apm/e2e/ingest-data/replay.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,40 @@ function incrementSpinnerCount({ success }) {
}
let iterIndex = 0;

function setItemMetaAndHeaders(item) {
const headers = {
'content-type': 'application/x-ndjson',
};

if (SECRET_TOKEN) {
headers.Authorization = `Bearer ${SECRET_TOKEN}`;
}

if (item.url === '/intake/v2/rum/events') {
if (iterIndex === userAgents.length) {
// set some event agent to opbean
setRumAgent(item);
iterIndex = 0;
}
headers['User-Agent'] = userAgents[iterIndex];
headers['X-Forwarded-For'] = userIps[iterIndex];
iterIndex++;
}
return headers;
}

Comment on lines +73 to +94
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we ran into this situation because of side-effects. If we can make this (more) functional, i.e. don't mutate item and iterIndex, I feel like this code would be more robust and easier to understand. I think changing insertItem() to something like createRequestFromEvent() would help.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, i will do it as a follow up PR, want to get this in to make sure it's working on other PRs.

function setRumAgent(item) {
item.body = item.body.replace(
'"name":"client"',
'"name":"opbean-client-rum"'
);
if (item.body) {
item.body = item.body.replace(
'"name":"client"',
'"name":"opbean-client-rum"'
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How risky is it to do a string replace? We might want to manipulate the body as an object rather than a string.

}
}

async function insertItem(item) {
async function insertItem(item, headers) {
try {
const url = `${APM_SERVER_URL}${item.url}`;
const headers = {
'content-type': 'application/x-ndjson',
};

if (item.url === '/intake/v2/rum/events') {
if (iterIndex === userAgents.length) {
// set some event agent to opbean
setRumAgent(item);
iterIndex = 0;
}
headers['User-Agent'] = userAgents[iterIndex];
headers['X-Forwarded-For'] = userIps[iterIndex];
iterIndex++;
}

if (SECRET_TOKEN) {
headers.Authorization = `Bearer ${SECRET_TOKEN}`;
}

await axios({
method: item.method,
Expand Down Expand Up @@ -133,8 +139,9 @@ async function init() {
await Promise.all(
items.map(async (item) => {
try {
const headers = setItemMetaAndHeaders(item);
// retry 5 times with exponential backoff
await pRetry(() => limit(() => insertItem(item)), {
await pRetry(() => limit(() => insertItem(item, headers)), {
retries: 5,
});
incrementSpinnerCount({ success: true });
Expand Down