|
| 1 | +const path = require('path') |
| 2 | +const chalk = require('chalk') |
| 3 | +const Libhoney = require('libhoney') |
| 4 | + |
| 5 | +const pkg = require('@packages/root') |
| 6 | +const ciProvider = require('@packages/server/lib/util/ci_provider') |
| 7 | +const { commitInfo } = require('@cypress/commit-info') |
| 8 | + |
| 9 | +class StatsdReporter { |
| 10 | + constructor (runner) { |
| 11 | + if (!process.env.HONEYCOMB_API_KEY) { |
| 12 | + return |
| 13 | + } |
| 14 | + |
| 15 | + console.log(chalk.green('Reporting to honeycomb')) |
| 16 | + |
| 17 | + let branch; let commitSha |
| 18 | + |
| 19 | + this.honey = new Libhoney({ |
| 20 | + dataset: 'systemtest-performance', |
| 21 | + writeKey: process.env.HONEYCOMB_API_KEY, |
| 22 | + }) |
| 23 | + |
| 24 | + commitInfo().then((commitInformation) => { |
| 25 | + const ciInformation = ciProvider.commitParams() || {} |
| 26 | + |
| 27 | + this.branch = commitInformation.branch || ciInformation.branch |
| 28 | + this.commitSha = commitInformation.sha || ciInformation.sha |
| 29 | + }) |
| 30 | + |
| 31 | + runner.on('test', (test) => { |
| 32 | + test.wallclockStart = Date.now() |
| 33 | + }) |
| 34 | + |
| 35 | + runner.on('test end', (test) => { |
| 36 | + // Skipped tests never get a 'start' event, but they still get 'test end' somehow. |
| 37 | + if (!test.state || test.state === 'skipped') { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + const title = test.titlePath().join(' / ') |
| 42 | + // This regex pulls apart a string like `e2e async timeouts / failing1 [electron]` |
| 43 | + // into `e2e async timeouts / failing1` and `electron`, letting us use the same |
| 44 | + // test name for all browsers, with the browser as a separate field. |
| 45 | + // The browser capture group is optional because some tests aren't browser specific, |
| 46 | + // in which case it will be undefined and not passed as a field to honeycomb. |
| 47 | + const [, testTitle, browser] = title.match(/(.+?)(?: \[([a-z]+)\])?$/) |
| 48 | + |
| 49 | + const honeycombEvent = this.honey.newEvent() |
| 50 | + |
| 51 | + honeycombEvent.timestamp = test.wallclockStart |
| 52 | + honeycombEvent.add({ |
| 53 | + test: testTitle, |
| 54 | + specFile: path.basename(test.file), |
| 55 | + browser, |
| 56 | + state: test.state, |
| 57 | + err: test.err && test.err.message, |
| 58 | + errStack: test.err && test.err.stack, |
| 59 | + durationMs: Date.now() - test.wallclockStart, |
| 60 | + mochaDurationMs: test.duration, |
| 61 | + branch, |
| 62 | + commitSha, |
| 63 | + buildUrl: process.env.CIRCLE_BUILD_URL, |
| 64 | + platform: process.platform, |
| 65 | + arch: process.arch, |
| 66 | + version: pkg.version, |
| 67 | + }) |
| 68 | + |
| 69 | + honeycombEvent.send() |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + // If there is no done callback, then mocha-multi-reporter will kill the process without waiting for our honeycomb post to complete. |
| 74 | + done (failures, callback) { |
| 75 | + if (this.honey) { |
| 76 | + this.honey.flush().then(callback) |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +module.exports = StatsdReporter |
0 commit comments