Skip to content
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

feat: generate "X-Request-ID" header for each browser request (backport to v7) #823

Merged
merged 1 commit into from
Dec 28, 2023
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,20 @@ Allows to intercept [HTTP request options](https://github.com/sindresorhus/got#o
(RequestOptions) => RequestOptions
```

In runtime a unique `X-Request-ID` header is generated for each browser request which consists of `${FIRST_X_REQ_ID}__${LAST_X_REQ_ID}`, where:
- `FIRST_X_REQ_ID` - unique uuid for each test (different for each retry), allows to find all requests related to a single test run;
- `LAST_X_REQ_ID` - unique uuid for each browser request, allows to find one unique request in one test (together with `FIRST_X_REQ_ID`).

Header `X-Request-ID` can be useful if you manage the cloud with browsers yourself and collect logs with requests. Real-world example: `2f31ffb7-369d-41f4-bbb8-77744615d2eb__e8d011d8-bb76-42b9-b80e-02f03b8d6fe1`.

To override generated `X-Request-ID` to your own value you need specify it in `transformRequest` handler. Example:

```javascript
transformRequest: (req) => {
req.handler["X-Request-ID"] = "my_x_req_id";
}
```

#### transformResponse
Allows to intercept [HTTP response object](https://github.com/sindresorhus/got#response) after a WebDriver response has arrived. Default value is `null`. If function is passed then it takes `Response` (original response object) as the first and `RequestOptions` as the second argument. Should return modified `Response`. Example:

Expand Down
3 changes: 1 addition & 2 deletions src/browser-pool/basic-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ module.exports = class BasicPool extends Pool {
}

async getBrowser(id, opts = {}) {
const { version } = opts;
const browser = Browser.create(this._config, id, version);
const browser = Browser.create(this._config, { ...opts, id });

try {
await browser.init();
Expand Down
29 changes: 23 additions & 6 deletions src/browser/browser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"use strict";

const crypto = require("crypto");
const _ = require("lodash");
const { SAVE_HISTORY_MODE } = require("../constants/config");
const { X_REQUEST_ID_DELIMITER } = require("../constants/browser");
const history = require("./history");
const addRunStepCommand = require("./commands/runStep");

Expand All @@ -19,13 +21,14 @@ const CUSTOM_SESSION_OPTS = [
];

module.exports = class Browser {
static create(config, id, version) {
return new this(config, id, version);
static create(config, opts) {
return new this(config, opts);
}

constructor(config, id, version) {
this.id = id;
this.version = version;
constructor(config, opts) {
this.id = opts.id;
this.version = opts.version;
this.testXReqId = opts.testXReqId;

this._config = config.forBrowser(this.id);
this._debug = config.system.debug;
Expand Down Expand Up @@ -82,7 +85,21 @@ module.exports = class Browser {

_getSessionOptsFromConfig(optNames = CUSTOM_SESSION_OPTS) {
return optNames.reduce((options, optName) => {
if (!_.isNull(this._config[optName])) {
if (optName === "transformRequest") {
options[optName] = req => {
if (!_.isNull(this._config[optName])) {
req = this._config[optName](req);
}

if (!req.headers["X-Request-ID"]) {
req.headers["X-Request-ID"] = `${
this.testXReqId
}${X_REQUEST_ID_DELIMITER}${crypto.randomUUID()}`;
}

return req;
};
} else if (!_.isNull(this._config[optName])) {
options[optName] = this._config[optName];
}

Expand Down
11 changes: 6 additions & 5 deletions src/browser/existing-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ const { isSupportIsolation } = require("../utils/browser");
const OPTIONAL_SESSION_OPTS = ["transformRequest", "transformResponse"];

module.exports = class ExistingBrowser extends Browser {
static create(config, id, version, emitter) {
return new this(config, id, version, emitter);
static create(config, opts) {
return new this(config, opts);
}

constructor(config, id, version, emitter) {
super(config, id, version);
constructor(config, opts) {
super(config, opts);

this._emitter = emitter;
this._emitter = opts.emitter;
this._camera = Camera.create(this._config.screenshotMode, () => this._takeScreenshot());

this._meta = this._initMeta();
Expand Down Expand Up @@ -165,6 +165,7 @@ module.exports = class ExistingBrowser extends Browser {
return {
pid: process.pid,
browserVersion: this.version,
testXReqId: this.testXReqId,
...this._config.meta,
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/browser/new-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const headlessBrowserOptions = {
};

module.exports = class NewBrowser extends Browser {
constructor(config, id, version) {
super(config, id, version);
constructor(config, opts) {
super(config, opts);

signalHandler.on("exit", () => this.quit());
}
Expand Down
1 change: 1 addition & 0 deletions src/constants/browser.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const MIN_CHROME_VERSION_SUPPORT_ISOLATION = 93;
export const X_REQUEST_ID_DELIMITER = "__";
6 changes: 3 additions & 3 deletions src/runner/browser-agent.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use strict";

module.exports = class BrowserAgent {
static create(id, version, pool) {
return new this(id, version, pool);
static create(opts = {}) {
return new this(opts);
}

constructor(id, version, pool) {
constructor({ id, version, pool }) {
this.browserId = id;

this._version = version;
Expand Down
6 changes: 5 additions & 1 deletion src/runner/browser-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ module.exports = class BrowserRunner extends Runner {
}

async _runTest(test) {
const browserAgent = BrowserAgent.create(this._browserId, test.browserVersion, this._browserPool);
const browserAgent = BrowserAgent.create({
id: this._browserId,
version: test.browserVersion,
pool: this._browserPool,
});
const runner = TestRunner.create(test, this._config, browserAgent);

runner.on(Events.TEST_BEGIN, test => this._suiteMonitor.testBegin(test));
Expand Down
4 changes: 2 additions & 2 deletions src/runner/test-runner/high-priority-browser-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module.exports = class HighPriorityBrowserAgent {
this._browserAgent = browserAgent;
}

getBrowser() {
return this._browserAgent.getBrowser({ highPriority: true });
getBrowser(opts = {}) {
return this._browserAgent.getBrowser({ ...opts, highPriority: true });
}

freeBrowser(...args) {
Expand Down
4 changes: 3 additions & 1 deletion src/runner/test-runner/regular-test-runner.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";

const crypto = require("crypto");
const _ = require("lodash");
const Runner = require("../runner");
const logger = require("../../utils/logger");
Expand Down Expand Up @@ -64,6 +65,7 @@ module.exports = class RegularTestRunner extends Runner {
sessionCaps: this._browser.capabilities,
sessionOpts: this._browser.publicAPI.options,
file: this._test.file,
testXReqId: this._browser.testXReqId,
});
}

Expand All @@ -80,7 +82,7 @@ module.exports = class RegularTestRunner extends Runner {

async _getBrowser() {
try {
this._browser = await this._browserAgent.getBrowser();
this._browser = await this._browserAgent.getBrowser({ testXReqId: crypto.randomUUID() });
this._test.sessionId = this._browser.sessionId;

return this._browser;
Expand Down
13 changes: 7 additions & 6 deletions src/worker/runner/browser-agent.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
"use strict";

module.exports = class BrowserAgent {
static create(browserId, browserVersion, pool) {
return new BrowserAgent(browserId, browserVersion, pool);
static create(opts) {
return new this(opts);
}

constructor(browserId, browserVersion, pool) {
this.browserId = browserId;
this.browserVersion = browserVersion;
constructor({ id, version, pool }) {
this.browserId = id;
this.browserVersion = version;

this._pool = pool;
}

getBrowser({ sessionId, sessionCaps, sessionOpts }) {
getBrowser({ sessionId, sessionCaps, sessionOpts, testXReqId }) {
return this._pool.getBrowser({
browserId: this.browserId,
browserVersion: this.browserVersion,
sessionId,
sessionCaps,
sessionOpts,
testXReqId,
});
}

Expand Down
9 changes: 7 additions & 2 deletions src/worker/runner/browser-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ module.exports = class BrowserPool {
this._calibrator = new Calibrator();
}

async getBrowser({ browserId, browserVersion, sessionId, sessionCaps, sessionOpts }) {
const browser = Browser.create(this._config, browserId, browserVersion, this._emitter);
async getBrowser({ browserId, browserVersion, sessionId, sessionCaps, sessionOpts, testXReqId }) {
const browser = Browser.create(this._config, {
id: browserId,
version: browserVersion,
testXReqId,
emitter: this._emitter,
});

try {
await browser.init({ sessionId, sessionCaps, sessionOpts }, this._calibrator);
Expand Down
6 changes: 3 additions & 3 deletions src/worker/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ module.exports = class Runner extends AsyncEmitter {
]);
}

async runTest(fullTitle, { browserId, browserVersion, file, sessionId, sessionCaps, sessionOpts }) {
async runTest(fullTitle, { browserId, browserVersion, file, sessionId, sessionCaps, sessionOpts, testXReqId }) {
const tests = await this._testParser.parse({ file, browserId });
const test = tests.find(t => t.fullTitle() === fullTitle);
const browserAgent = BrowserAgent.create(browserId, browserVersion, this._browserPool);
const browserAgent = BrowserAgent.create({ id: browserId, version: browserVersion, pool: this._browserPool });
const runner = TestRunner.create(test, this._config.forBrowser(browserId), browserAgent);

return runner.run({ sessionId, sessionCaps, sessionOpts });
return runner.run({ sessionId, sessionCaps, sessionOpts, testXReqId });
}
};
4 changes: 2 additions & 2 deletions src/worker/runner/test-runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ module.exports = class TestRunner {
this._browserAgent = browserAgent;
}

async run({ sessionId, sessionCaps, sessionOpts }) {
async run({ sessionId, sessionCaps, sessionOpts, testXReqId }) {
const test = this._test;
const hermioneCtx = test.hermioneCtx || {};

let browser;

try {
browser = await this._browserAgent.getBrowser({ sessionId, sessionCaps, sessionOpts });
browser = await this._browserAgent.getBrowser({ sessionId, sessionCaps, sessionOpts, testXReqId });
} catch (e) {
throw Object.assign(e, { hermioneCtx });
}
Expand Down
4 changes: 2 additions & 2 deletions test/src/browser-pool/basic-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ describe("browser-pool/basic-pool", () => {

await mkPool_({ config }).getBrowser("broId");

assert.calledWith(Browser.create, config, "broId");
assert.calledWith(Browser.create, config, { id: "broId" });
});

it("should create new browser with specified version when requested", async () => {
await mkPool_().getBrowser("broId", { version: "1.0" });

assert.calledWith(Browser.create, sinon.match.any, "broId", "1.0");
assert.calledWith(Browser.create, sinon.match.any, { id: "broId", version: "1.0" });
});

it("should init browser", async () => {
Expand Down
Loading
Loading