Repository: elastic/synthetics
Pull request #247 (2021-03-27 00:59:59)

feat: report number of journeys in json
+ Reports the number of journeys as `synthetics/metadata` event which would be potentially used in the `--dry-run` command which reports this event as well registered journeys via `journey/register` command. 


Dry run in todos basic example 
```
{"type":"synthetics/metadata","@timestamp":1616806791018720,"root_fields":{"num_journeys":2,"os":{"platform":"darwin"},"package":{"name":"@elastic/synthetics","version":"0.0.1-alpha.11"}},"package_version":"0.0.1-alpha.11"}
{"type":"journey/register","@timestamp":1616806791019158,"journey":{"name":"check if title is present","id":"check if title is present"},"root_fields":{"os":{"platform":"darwin"},"package":{"name":"@elastic/synthetics","version":"0.0.1-alpha.11"}},"package_version":"0.0.1-alpha.11"}
{"type":"journey/register","@timestamp":1616806791019203.2,"journey":{"name":"check if input placeholder is correct","id":"check if input placeholder is correct"},"root_fields":{"os":{"platform":"darwin"},"package":{"name":"@elastic/synthetics","version":"0.0.1-alpha.11"}},"package_version":"0.0.1-alpha.11"}

```

apmmachine: 
## :broken_heart: Tests Failed


<!-- BUILD BADGES-->
> _the below badges are clickable and redirect to their specific view in the CI or DOCS_
[![Pipeline View](https://img.shields.io/badge/pipeline-pipeline%20-green)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-247/detail/PR-247/1//pipeline) [![Test View](https://img.shields.io/badge/test-test-green)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-247/detail/PR-247/1//tests) [![Changes](https://img.shields.io/badge/changes-changes-green)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-247/detail/PR-247/1//changes) [![Artifacts](https://img.shields.io/badge/artifacts-artifacts-yellow)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-247/detail/PR-247/1//artifacts) [![preview](https://img.shields.io/badge/docs-preview-yellowgreen)](http://synthetics_247.docs-preview.app.elstc.co/diff)

<!-- BUILD SUMMARY-->
<details><summary>Expand to view the summary</summary>
<p>

#### Build stats


* Build Cause: `Pull request #247 opened`



* Start Time: 2021-03-27T01:00:12.375+0000


* Duration: 7 min 36 sec


* Commit: b90fbb21ad7bb82e1bd70d71a438c78f590731dc



#### Test stats :test_tube:

| Test         | Results                         |
| ------------ | :-----------------------------: |
| Failed       | 1  |
| Passed       | 70  |
| Skipped      | 0 |
| Total        | 71   |


#### Trends :test_tube:


![Image of Build Times](https://apm-ci.elastic.co/job/apm-agent-rum/job/elastic-synthetics/job/PR-247/buildTimeGraph/png)


![Image of Tests](https://apm-ci.elastic.co/job/apm-agent-rum/job/elastic-synthetics/job/PR-247/1/testReport/history/countGraph/png?start=0&amp;end=25)


</p>
</details>

<!-- TEST RESULTS IF ANY-->

  ### Test errors [![1](https://img.shields.io/badge/1%20-red)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-247/detail/PR-247/1//tests)
<details><summary>Expand to view the tests failures</summary><p>
    
    
##### `Test / json reporter captures number of journeys as metadata event – json reporter captures number of journeys as metadata event`


<ul>
<li>no error details</li>
<details><summary>Expand to view the stacktrace</summary><p>

```
 Error: expect(received).toMatchSnapshot()

Snapshot name: `json reporter captures number of journeys as metadata event 1`

- Snapshot  - 1
+ Received  + 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"}
+ {"type":"synthetics/metadata","@timestamp":1600300800000000,"root_fields":{"num_journeys":10,"os":{"platform":"linux"},"package":{"name":"@elastic/synthetics","version":"0.0.1"}},"package_version":"0.0.1"}
  ↵
    at Object.<anonymous> (/var/lib/jenkins/workspace/nt-rum_elastic-synthetics_PR-247/src/github.com/elastic/synthetics/__tests__/reporters/json.test.ts:186:53) 
 ```
</p></details>
</ul>
    
</p></details>


<!-- STEPS ERRORS IF ANY -->









### Steps errors [![1](https://img.shields.io/badge/1%20-red)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-247/detail/PR-247/1//pipeline)
  <details><summary>Expand to view the steps failures</summary>
  <p>


  
  
  
  
##### `Runs the tests`
<ul>
<li>Took 0 min 27 sec . View more details on <a href="https://apm-ci.elastic.co//blue/rest/organizations/jenkins/pipelines/apm-agent-rum/pipelines/elastic-synthetics/pipelines/PR-247/runs/1/steps/300/log/?start=0">here</a></li>
<li>Description: <code>./node_modules/.bin/jest --ci --reporters=default --reporters=jest-junit</code></l1>
</ul>
  
  </p>
  </details>



<!-- LOG OUTPUT-->
### Log output

<details><summary>Expand to view the last 100 lines of log output</summary>
<p>

```
[2021-03-27T01:06:09.839Z] Running in /var/lib/jenkins/workspace/nt-rum_elastic-synthetics_PR-247/src/github.com/elastic/synthetics
[2021-03-27T01:06:10.171Z] + npm add --dev jest-junit
[2021-03-27T01:06:10.432Z] npm WARN install Usage of the `--dev` option is deprecated. Use `--also=dev` instead.
[2021-03-27T01:06:17.021Z] npm WARN @elastic/synthetics@0.0.1-alpha.11 No repository field.
[2021-03-27T01:06:17.021Z] npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.3 (node_modules/chokidar/node_modules/fsevents):
[2021-03-27T01:06:17.021Z] npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
[2021-03-27T01:06:17.021Z] npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.2.1 (node_modules/fsevents):
[2021-03-27T01:06:17.021Z] npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.2.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
[2021-03-27T01:06:17.021Z] 
[2021-03-27T01:06:17.021Z] + jest-junit@12.0.0
[2021-03-27T01:06:17.021Z] added 5 packages from 14 contributors and audited 754 packages in 5.452s
[2021-03-27T01:06:17.021Z] 
[2021-03-27T01:06:17.021Z] 48 packages are looking for funding
[2021-03-27T01:06:17.021Z]   run `npm fund` for details
[2021-03-27T01:06:17.021Z] 
[2021-03-27T01:06:17.021Z] found 0 vulnerabilities
[2021-03-27T01:06:17.021Z] 
[2021-03-27T01:06:17.345Z] + ./node_modules/.bin/jest --ci --reporters=default --reporters=jest-junit
[2021-03-27T01:06:20.722Z] 
[2021-03-27T01:06:20.722Z] Running without BrowserService
[2021-03-27T01:06:30.735Z] PASS __tests__/helper.test.ts (8.125 s)
[2021-03-27T01:06:31.308Z] FAIL __tests__/reporters/json.test.ts
[2021-03-27T01:06:31.308Z]   ● json reporter › captures number of journeys as metadata event
[2021-03-27T01:06:31.308Z] 
[2021-03-27T01:06:31.308Z]     expect(received).toMatchSnapshot()
[2021-03-27T01:06:31.308Z] 
[2021-03-27T01:06:31.308Z]     Snapshot name: `json reporter captures number of journeys as metadata event 1`
[2021-03-27T01:06:31.308Z] 
[2021-03-27T01:06:31.308Z]     - Snapshot  - 1
[2021-03-27T01:06:31.308Z]     + Received  + 1
[2021-03-27T01:06:31.308Z] 
[2021-03-27T01:06:31.308Z]     - {"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"}
[2021-03-27T01:06:31.308Z]     + {"type":"synthetics/metadata","@timestamp":1600300800000000,"root_fields":{"num_journeys":10,"os":{"platform":"linux"},"package":{"name":"@elastic/synthetics","version":"0.0.1"}},"package_version":"0.0.1"}
[2021-03-27T01:06:31.308Z]       ↵
[2021-03-27T01:06:31.308Z] 
[2021-03-27T01:06:31.308Z]       184 |     });
[2021-03-27T01:06:31.308Z]       185 | 
[2021-03-27T01:06:31.308Z]     > 186 |     expect((await readAndCloseStream()).toString()).toMatchSnapshot();
[2021-03-27T01:06:31.308Z]           |                                                     ^
[2021-03-27T01:06:31.308Z]       187 |   });
[2021-03-27T01:06:31.308Z]       188 | });
[2021-03-27T01:06:31.308Z]       189 | 
[2021-03-27T01:06:31.308Z] 
[2021-03-27T01:06:31.308Z]       at Object.<anonymous> (__tests__/reporters/json.test.ts:186:53)
[2021-03-27T01:06:31.308Z] 
[2021-03-27T01:06:31.308Z]  › 1 snapshot failed.
[2021-03-27T01:06:32.693Z] PASS __tests__/plugins/network.test.ts (11.392 s)
[2021-03-27T01:06:32.693Z] PASS __tests__/reporters/junit.test.ts
[2021-03-27T01:06:33.638Z] PASS __tests__/core/index.test.ts
[2021-03-27T01:06:35.044Z] PASS __tests__/reporters/base.test.ts
[2021-03-27T01:06:37.601Z] PASS __tests__/index.test.ts
[2021-03-27T01:06:37.865Z] PASS __tests__/cli.test.ts (5.479 s)
[2021-03-27T01:06:38.825Z] PASS __tests__/core/runner.test.ts (17.624 s)
[2021-03-27T01:06:40.224Z] PASS __tests__/plugins/browser-console.test.ts
[2021-03-27T01:06:40.224Z] PASS __tests__/plugins/performance.test.ts
[2021-03-27T01:06:40.797Z] PASS __tests__/plugins/tracing.test.ts
[2021-03-27T01:06:41.372Z] PASS __tests__/plugins/plugin-manager.test.ts
[2021-03-27T01:06:42.759Z] PASS __tests__/core/logger.test.ts
[2021-03-27T01:06:42.759Z] PASS __tests__/core/browser-service.test.ts
[2021-03-27T01:06:42.759Z] PASS __tests__/core/gatherer.test.ts
[2021-03-27T01:06:43.332Z] PASS __tests__/dsl/journey.test.ts
[2021-03-27T01:06:43.332Z] 
[2021-03-27T01:06:43.332Z] Snapshot Summary
[2021-03-27T01:06:43.332Z]  › 1 snapshot failed from 1 test suite. Inspect your code changes or re-run jest with `-u` to update them.
[2021-03-27T01:06:43.332Z] 
[2021-03-27T01:06:43.332Z] Test Suites: 1 failed, 16 passed, 17 total
[2021-03-27T01:06:43.332Z] Tests:       1 failed, 56 passed, 57 total
[2021-03-27T01:06:43.332Z] Snapshots:   1 failed, 7 passed, 8 total
[2021-03-27T01:06:43.332Z] Time:        22.736 s
[2021-03-27T01:06:43.332Z] Ran all test suites.
[2021-03-27T01:06:44.788Z] Post stage
[2021-03-27T01:06:44.822Z] Recording test results
[2021-03-27T01:06:44.895Z] [Checks API] No suitable checks publisher found.
[2021-03-27T01:06:45.097Z] Stage "E2e Test" skipped due to earlier failure(s)
[2021-03-27T01:06:45.209Z] Stage "Publish Docker image" skipped due to earlier failure(s)
[2021-03-27T01:06:45.410Z] Stage "Matrix - STACK_VERSION = '8.0.0-synthetics'" skipped due to earlier failure(s)
[2021-03-27T01:06:45.420Z] Stage "Matrix - STACK_VERSION = '7.10.0-synthetics'" skipped due to earlier failure(s)
[2021-03-27T01:06:45.430Z] Stage "Matrix - STACK_VERSION = '7.11.0-synthetics'" skipped due to earlier failure(s)
[2021-03-27T01:06:45.526Z] Stage "Matrix - STACK_VERSION = '8.0.0-synthetics'" skipped due to earlier failure(s)
[2021-03-27T01:06:45.541Z] Stage "Matrix - STACK_VERSION = '7.10.0-synthetics'" skipped due to earlier failure(s)
[2021-03-27T01:06:45.554Z] Stage "Matrix - STACK_VERSION = '7.11.0-synthetics'" skipped due to earlier failure(s)
[2021-03-27T01:06:45.785Z] Stage "Matrix - STACK_VERSION = '8.0.0-synthetics'" skipped due to earlier failure(s)
[2021-03-27T01:06:45.798Z] Stage "Matrix - STACK_VERSION = '7.10.0-synthetics'" skipped due to earlier failure(s)
[2021-03-27T01:06:45.808Z] Stage "Matrix - STACK_VERSION = '7.11.0-synthetics'" skipped due to earlier failure(s)
[2021-03-27T01:06:46.049Z] Stage "Matrix - STACK_VERSION = '8.0.0-synthetics'" skipped due to earlier failure(s)
[2021-03-27T01:06:46.063Z] Stage "Matrix - STACK_VERSION = '7.10.0-synthetics'" skipped due to earlier failure(s)
[2021-03-27T01:06:46.073Z] Stage "Matrix - STACK_VERSION = '7.11.0-synthetics'" skipped due to earlier failure(s)
[2021-03-27T01:06:46.357Z] Failed in branch Matrix - STACK_VERSION = '8.0.0-synthetics'
[2021-03-27T01:06:46.369Z] Failed in branch Matrix - STACK_VERSION = '7.10.0-synthetics'
[2021-03-27T01:06:46.381Z] Failed in branch Matrix - STACK_VERSION = '7.11.0-synthetics'
[2021-03-27T01:06:46.608Z] Stage "Release" skipped due to earlier failure(s)
[2021-03-27T01:06:46.665Z] Stage "Release" skipped due to earlier failure(s)
[2021-03-27T01:06:46.783Z] Stage "Release" skipped due to earlier failure(s)
[2021-03-27T01:06:47.274Z] Running on Jenkins in /var/lib/jenkins/workspace/nt-rum_elastic-synthetics_PR-247
[2021-03-27T01:06:47.392Z] [INFO] getVaultSecret: Getting secrets
[2021-03-27T01:06:47.640Z] Masking supported pattern matches of $VAULT_ADDR or $VAULT_ROLE_ID or $VAULT_SECRET_ID
[2021-03-27T01:06:48.712Z] + chmod 755 generate-build-data.sh
[2021-03-27T01:06:48.712Z] + ./generate-build-data.sh https://apm-ci.elastic.co/blue/rest/organizations/jenkins/pipelines/apm-agent-rum/elastic-synthetics/PR-247/ https://apm-ci.elastic.co/blue/rest/organizations/jenkins/pipelines/apm-agent-rum/elastic-synthetics/PR-247/runs/1 FAILURE 396054
[2021-03-27T01:06:48.963Z] INFO: curl https://apm-ci.elastic.co/blue/rest/organizations/jenkins/pipelines/apm-agent-rum/elastic-synthetics/PR-247/runs/1/steps/?limit=10000 -o steps-info.json
[2021-03-27T01:06:49.906Z] INFO: curl https://apm-ci.elastic.co/blue/rest/organizations/jenkins/pipelines/apm-agent-rum/elastic-synthetics/PR-247/runs/1/tests/?status=FAILED -o tests-errors.json

```

</p>
</details>



<!--COMMENT_GENERATED_WITH_ID_comment.id-->

Patch:
diff --git a/src/reporters/json.ts b/src/reporters/json.ts
index c071404..e117439 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<Step>;
@@ -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,


Test patch:
diff --git a/__tests__/reporters/__snapshots__/json.test.ts.snap b/__tests__/reporters/__snapshots__/json.test.ts.snap
index b925586..36ad015 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 a070bde..29c545c 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();
+  });
 });