Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[browserstack-service] Add Funnel Data instrumentation [v8] (#12392)
* add TestObservability usage stats * eslint fixes * eslint fixes * send funnel data from cleanup * add test errors and refactor workers data * remove test errors code * review comments fixes * review comment fixes * removed events from log * add and fix tests * fix tests * refactor * refactor code and fix tests * refactor code and add comments * store env in constants.ts * fix sending funnel data from cleanup * Update packages/wdio-browserstack-service/src/config.ts Co-authored-by: Christian Bromann <git@bromann.dev> * Update packages/wdio-browserstack-service/src/request-handler.ts Co-authored-by: Christian Bromann <git@bromann.dev> * empty commit to trigger tests * fix empty request and send funnel data from onPrepare hook * Update packages/wdio-browserstack-service/tests/request-handler.test.ts --------- Co-authored-by: Christian Bromann <git@bromann.dev>
- Loading branch information
1 parent
ad93e91
commit 915f780
Showing
32 changed files
with
1,944 additions
and
479 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { AppConfig, BrowserstackConfig } from './types.js' | ||
import type { Options } from '@wdio/types' | ||
import TestOpsConfig from './testOps/testOpsConfig.js' | ||
import { isUndefined } from './util.js' | ||
|
||
class BrowserStackConfig { | ||
static getInstance(options?: BrowserstackConfig & Options.Testrunner, config?: Options.Testrunner): BrowserStackConfig { | ||
if (!this._instance && options && config) { | ||
this._instance = new BrowserStackConfig(options, config) | ||
} | ||
return this._instance | ||
} | ||
|
||
public userName?: string | ||
public accessKey?: string | ||
public framework?: string | ||
public buildName?: string | ||
public buildIdentifier?: string | ||
public testObservability: TestOpsConfig | ||
public percy: boolean | ||
public accessibility: boolean | ||
public app?: string|AppConfig | ||
private static _instance: BrowserStackConfig | ||
public appAutomate: boolean | ||
public automate: boolean | ||
public funnelDataSent: boolean = false | ||
|
||
private constructor(options: BrowserstackConfig & Options.Testrunner, config: Options.Testrunner) { | ||
this.framework = config.framework | ||
this.userName = config.user | ||
this.accessKey = config.key | ||
this.testObservability = new TestOpsConfig(options.testObservability !== false, !isUndefined(options.testObservability)) | ||
this.percy = options.percy || false | ||
this.accessibility = options.accessibility || false | ||
this.app = options.app | ||
this.appAutomate = !isUndefined(options.app) | ||
this.automate = !this.appAutomate | ||
this.buildIdentifier = options.buildIdentifier | ||
} | ||
|
||
sentFunnelData() { | ||
this.funnelDataSent = true | ||
} | ||
|
||
} | ||
|
||
export default BrowserStackConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import path from 'node:path' | ||
import fs from 'node:fs' | ||
|
||
import { BStackLogger } from './bstackLogger.js' | ||
|
||
const workersDataDirPath = path.join(process.cwd(), 'logs', 'worker_data') | ||
|
||
export function getDataFromWorkers(){ | ||
const workersData: Record<string, object>[] = [] | ||
|
||
if (!fs.existsSync(workersDataDirPath)) { | ||
return workersData | ||
} | ||
|
||
const files = fs.readdirSync(workersDataDirPath) | ||
files.forEach((file) => { | ||
BStackLogger.debug('Reading worker file ' + file) | ||
const filePath = path.join(workersDataDirPath, file) | ||
const fileContent = fs.readFileSync(filePath, 'utf8') | ||
const workerData = JSON.parse(fileContent) | ||
workersData.push(workerData) | ||
}) | ||
|
||
// Remove worker data after all reading | ||
removeWorkersDataDir() | ||
|
||
return workersData | ||
} | ||
|
||
export function saveWorkerData(data: Record<string, any>) { | ||
const filePath = path.join(workersDataDirPath, 'worker-data-' + process.pid + '.json') | ||
|
||
try { | ||
createWorkersDataDir() | ||
fs.writeFileSync(filePath, JSON.stringify(data)) | ||
} catch (e) { | ||
BStackLogger.debug('Exception in saving worker data: ' + e) | ||
} | ||
} | ||
|
||
export function removeWorkersDataDir(): boolean { | ||
fs.rmSync(workersDataDirPath, { recursive: true, force: true }) | ||
return true | ||
} | ||
|
||
function createWorkersDataDir() { | ||
if (!fs.existsSync(workersDataDirPath)) { | ||
fs.mkdirSync(workersDataDirPath, { recursive: true }) | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { spawn } from 'node:child_process' | ||
import path from 'node:path' | ||
import BrowserStackConfig from './config.js' | ||
import { saveFunnelData } from './instrumentation/funnelInstrumentation.js' | ||
import { fileURLToPath } from 'node:url' | ||
import { TESTOPS_JWT_ENV } from './constants.js' | ||
import { BStackLogger } from './bstackLogger.js' | ||
|
||
const __filename = fileURLToPath(import.meta.url) | ||
const __dirname = path.dirname(__filename) | ||
|
||
export function setupExitHandlers() { | ||
process.on('exit', (code) => { | ||
BStackLogger.debug('Exit hook called') | ||
const args = shouldCallCleanup(BrowserStackConfig.getInstance()) | ||
if (Array.isArray(args) && args.length) { | ||
BStackLogger.debug('Spawning cleanup with args ' + args.toString()) | ||
const childProcess = spawn('node', [`${path.join(__dirname, 'cleanup.js')}`, ...args], { detached: true, stdio: 'inherit', env: { ...process.env } }) | ||
childProcess.unref() | ||
process.exit(code) | ||
} | ||
}) | ||
} | ||
|
||
export function shouldCallCleanup(config: BrowserStackConfig): string[] { | ||
const args: string[] = [] | ||
if (!!process.env[TESTOPS_JWT_ENV] && !config.testObservability.buildStopped) { | ||
args.push('--observability') | ||
} | ||
|
||
if (config.userName && config.accessKey && !config.funnelDataSent) { | ||
const savedFilePath = saveFunnelData('SDKTestSuccessful', config) | ||
args.push('--funnelData', savedFilePath) | ||
} | ||
|
||
return args | ||
} |
Oops, something went wrong.