diff --git a/__tests__/reporters/__snapshots__/json.test.ts.snap b/__tests__/reporters/__snapshots__/json.test.ts.snap index b925586d..36ad0155 100644 --- a/__tests__/reporters/__snapshots__/json.test.ts.snap +++ b/__tests__/reporters/__snapshots__/json.test.ts.snap @@ -1,5 +1,10 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`json reporter captures number of journeys as metadata event 1`] = ` +"{\\"type\\":\\"synthetics/metadata\\",\\"@timestamp\\":1600300800000000,\\"root_fields\\":{\\"num_journeys\\":10,\\"os\\":{\\"platform\\":\\"darwin\\"},\\"package\\":{\\"name\\":\\"@elastic/synthetics\\",\\"version\\":\\"0.0.1\\"}},\\"package_version\\":\\"0.0.1\\"} +" +`; + exports[`json reporter formats network fields in ECS format 1`] = ` Object { "http": Object { diff --git a/__tests__/reporters/json.test.ts b/__tests__/reporters/json.test.ts index a070bdee..29c545c5 100644 --- a/__tests__/reporters/json.test.ts +++ b/__tests__/reporters/json.test.ts @@ -46,6 +46,19 @@ describe('json reporter', () => { let stream; let runner: Runner; const timestamp = 1600300800000000; + const originalProcess = global.process; + + beforeAll(() => { + // Mocking the process in node environment + global.process = { + ...originalProcess, + platform: 'darwin', + }; + }); + + afterAll(() => { + global.process = originalProcess; + }); beforeEach(() => { runner = new Runner(); @@ -83,13 +96,6 @@ describe('json reporter', () => { }; it('writes each step as NDJSON to the FD', async () => { - // Mocking the process in node environment - const originalProcess = global.process; - global.process = { - ...originalProcess, - platform: 'darwin', - }; - runner.emit('journey:register', { journey: j1, }); @@ -129,7 +135,6 @@ describe('json reporter', () => { ], }); runner.emit('end', 'done'); - global.process = originalProcess; expect((await readAndCloseStream()).toString()).toMatchSnapshot(); }); @@ -177,4 +182,12 @@ describe('json reporter', () => { ); expect(journeyEnd.error).toEqual(helpers.formatError(myErr)); }); + + it('captures number of journeys as metadata event', async () => { + runner.emit('start', { + numJourneys: 10, + }); + + expect((await readAndCloseStream()).toString()).toMatchSnapshot(); + }); }); diff --git a/src/reporters/json.ts b/src/reporters/json.ts index c071404d..e1174394 100644 --- a/src/reporters/json.ts +++ b/src/reporters/json.ts @@ -35,6 +35,7 @@ import { Metrics } from '../plugins'; const { version, name } = require('../../package.json'); type OutputType = + | 'synthetics/metadata' | 'journey/register' | 'journey/start' | 'step/screenshot' @@ -59,7 +60,7 @@ type Payload = { type OutputFields = { type: OutputType; - journey: Journey; + journey?: Journey; timestamp?: number; url?: string; step?: Partial; @@ -167,8 +168,51 @@ export function formatNetworkFields(network: NetworkInfo) { }; } +function journeyInfo( + journey: OutputFields['journey'], + type: OutputFields['type'], + status: Payload['status'] +) { + if (!journey) { + return; + } + return { + name: journey.name, + id: journey.id, + status: type === 'journey/end' ? status : undefined, + }; +} + +function stepInfo( + step: OutputFields['step'], + type: OutputFields['type'], + status: Payload['status'] +) { + if (!step) { + return; + } + return { + name: step.name, + index: step.index, + status: type === 'step/end' ? status : undefined, + }; +} + export default class JSONReporter extends BaseReporter { _registerListeners() { + /** + * report the number of journeys that exists on a suite which + * could be used for better sharding + */ + this.runner.on('start', ({ numJourneys }) => { + this.writeJSON({ + type: 'synthetics/metadata', + root_fields: { + num_journeys: numJourneys, + }, + }); + }); + this.runner.on('journey:register', ({ journey }) => { this.writeJSON({ type: 'journey/register', @@ -309,18 +353,8 @@ export default class JSONReporter extends BaseReporter { this.write({ type, '@timestamp': timestamp || getTimestamp(), - journey: { - name: journey.name, - id: journey.id, - status: type === 'journey/end' ? payload.status : undefined, - }, - step: step - ? { - name: step.name, - index: step.index, - status: type === 'step/end' ? payload.status : undefined, - } - : undefined, + journey: journeyInfo(journey, type, payload?.status), + step: stepInfo(step, type, payload?.status), root_fields: { ...(root_fields || {}), ...getMetadata() }, payload, blob,