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: report number of journeys in json #247

Merged
merged 2 commits into from
Mar 30, 2021
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
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK with this, but we'll need to confirm that heartbeat doesn't exist this event. It shouldn't because it should only index events inside a journey context, but it's something to be aware of.

We should follow-up by adding explicit support to synthetics/metadata events to heartbeat

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes thats correct, heartbeat doesn't also support journey/register events if i remember correctly. Would it still pipe these events to the stdout?

},
});
});

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