Skip to content

Commit

Permalink
feat: report number of journeys in json (#247)
Browse files Browse the repository at this point in the history
* feat: report number of journeys in json

* mock global process for metadata
  • Loading branch information
vigneshshanmugam authored Mar 30, 2021
1 parent 6122228 commit 1cb180c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 21 deletions.
5 changes: 5 additions & 0 deletions __tests__/reporters/__snapshots__/json.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
29 changes: 21 additions & 8 deletions __tests__/reporters/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
});
Expand Down Expand Up @@ -129,7 +135,6 @@ describe('json reporter', () => {
],
});
runner.emit('end', 'done');
global.process = originalProcess;
expect((await readAndCloseStream()).toString()).toMatchSnapshot();
});

Expand Down Expand Up @@ -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();
});
});
60 changes: 47 additions & 13 deletions src/reporters/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { Metrics } from '../plugins';
const { version, name } = require('../../package.json');

type OutputType =
| 'synthetics/metadata'
| 'journey/register'
| 'journey/start'
| 'step/screenshot'
Expand All @@ -59,7 +60,7 @@ type Payload = {

type OutputFields = {
type: OutputType;
journey: Journey;
journey?: Journey;
timestamp?: number;
url?: string;
step?: Partial<Step>;
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 1cb180c

Please sign in to comment.