@elastic/synthetics 1.0.0-beta.25 → 1.0.0-beta.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/README.md +1 -1
  2. package/dist/cli.js +42 -5
  3. package/dist/cli.js.map +1 -1
  4. package/dist/common_types.d.ts +59 -5
  5. package/dist/common_types.d.ts.map +1 -1
  6. package/dist/core/gatherer.d.ts +2 -2
  7. package/dist/core/gatherer.d.ts.map +1 -1
  8. package/dist/core/gatherer.js.map +1 -1
  9. package/dist/core/index.d.ts +4 -0
  10. package/dist/core/index.d.ts.map +1 -1
  11. package/dist/core/index.js +15 -1
  12. package/dist/core/index.js.map +1 -1
  13. package/dist/core/runner.d.ts +10 -58
  14. package/dist/core/runner.d.ts.map +1 -1
  15. package/dist/core/runner.js +88 -49
  16. package/dist/core/runner.js.map +1 -1
  17. package/dist/dsl/journey.d.ts +3 -0
  18. package/dist/dsl/journey.d.ts.map +1 -1
  19. package/dist/dsl/journey.js +15 -0
  20. package/dist/dsl/journey.js.map +1 -1
  21. package/dist/dsl/monitor.d.ts +64 -0
  22. package/dist/dsl/monitor.d.ts.map +1 -0
  23. package/dist/dsl/monitor.js +62 -0
  24. package/dist/dsl/monitor.js.map +1 -0
  25. package/dist/generator/index.d.ts +13 -0
  26. package/dist/generator/index.d.ts.map +1 -0
  27. package/dist/generator/index.js +123 -0
  28. package/dist/generator/index.js.map +1 -0
  29. package/dist/{reporters/reporter.js → generator/utils.d.ts} +3 -3
  30. package/dist/generator/utils.d.ts.map +1 -0
  31. package/dist/generator/utils.js +44 -0
  32. package/dist/generator/utils.js.map +1 -0
  33. package/dist/helpers.d.ts +12 -6
  34. package/dist/helpers.d.ts.map +1 -1
  35. package/dist/helpers.js +33 -43
  36. package/dist/helpers.js.map +1 -1
  37. package/dist/index.d.ts +2 -7
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +4 -3
  40. package/dist/index.js.map +1 -1
  41. package/dist/loader.js +4 -4
  42. package/dist/loader.js.map +1 -1
  43. package/dist/options.d.ts +12 -0
  44. package/dist/options.d.ts.map +1 -1
  45. package/dist/options.js +66 -4
  46. package/dist/options.js.map +1 -1
  47. package/dist/plugins/performance.d.ts +1 -16
  48. package/dist/plugins/performance.d.ts.map +1 -1
  49. package/dist/plugins/performance.js.map +1 -1
  50. package/dist/plugins/plugin-manager.d.ts +1 -1
  51. package/dist/push/bundler.d.ts +34 -0
  52. package/dist/push/bundler.d.ts.map +1 -0
  53. package/dist/push/bundler.js +119 -0
  54. package/dist/push/bundler.js.map +1 -0
  55. package/{src/reporters/reporter.ts → dist/push/index.d.ts} +4 -11
  56. package/dist/push/index.d.ts.map +1 -0
  57. package/dist/push/index.js +58 -0
  58. package/dist/push/index.js.map +1 -0
  59. package/dist/{reporters/reporter.d.ts → push/monitor.d.ts} +7 -8
  60. package/dist/push/monitor.d.ts.map +1 -0
  61. package/dist/push/monitor.js +67 -0
  62. package/dist/push/monitor.js.map +1 -0
  63. package/dist/push/plugin.d.ts +33 -0
  64. package/dist/push/plugin.d.ts.map +1 -0
  65. package/dist/push/plugin.js +123 -0
  66. package/dist/push/plugin.js.map +1 -0
  67. package/dist/push/request.d.ts +43 -0
  68. package/dist/push/request.d.ts.map +1 -0
  69. package/dist/push/request.js +95 -0
  70. package/dist/push/request.js.map +1 -0
  71. package/dist/reporters/base.d.ts +14 -6
  72. package/dist/reporters/base.d.ts.map +1 -1
  73. package/dist/reporters/base.js +45 -47
  74. package/dist/reporters/base.js.map +1 -1
  75. package/dist/reporters/index.d.ts +18 -8
  76. package/dist/reporters/index.d.ts.map +1 -1
  77. package/dist/reporters/index.js.map +1 -1
  78. package/dist/reporters/json.d.ts +7 -3
  79. package/dist/reporters/json.d.ts.map +1 -1
  80. package/dist/reporters/json.js +118 -118
  81. package/dist/reporters/json.js.map +1 -1
  82. package/dist/reporters/junit.d.ts +7 -1
  83. package/dist/reporters/junit.d.ts.map +1 -1
  84. package/dist/reporters/junit.js +91 -86
  85. package/dist/reporters/junit.js.map +1 -1
  86. package/dist/tsconfig.tsbuildinfo +1 -1
  87. package/package.json +12 -8
  88. package/src/cli.ts +65 -9
  89. package/src/common_types.ts +69 -5
  90. package/src/core/gatherer.ts +2 -2
  91. package/src/core/index.ts +15 -0
  92. package/src/core/runner.ts +86 -113
  93. package/src/dsl/journey.ts +18 -0
  94. package/src/dsl/monitor.ts +83 -0
  95. package/src/generator/index.ts +159 -0
  96. package/src/generator/utils.ts +41 -0
  97. package/src/helpers.ts +36 -52
  98. package/src/index.ts +9 -1
  99. package/src/loader.ts +4 -4
  100. package/src/options.ts +71 -8
  101. package/src/plugins/performance.ts +1 -17
  102. package/src/push/bundler.ts +99 -0
  103. package/src/push/index.ts +63 -0
  104. package/src/push/monitor.ts +77 -0
  105. package/src/push/plugin.ts +113 -0
  106. package/src/push/request.ts +123 -0
  107. package/src/reporters/base.ts +57 -56
  108. package/src/reporters/index.ts +26 -3
  109. package/src/reporters/json.ts +153 -145
  110. package/src/reporters/junit.ts +98 -93
  111. package/templates/journeys/example.journey.ts +18 -0
  112. package/templates/synthetics.config.ts +19 -0
  113. package/CHANGELOG.md +0 -348
  114. package/NOTICE.txt +0 -221
  115. package/dist/reporters/reporter.d.ts.map +0 -1
  116. package/dist/reporters/reporter.js.map +0 -1
package/src/cli.ts CHANGED
@@ -26,12 +26,17 @@
26
26
  */
27
27
 
28
28
  import { program, Option } from 'commander';
29
- import { CliArgs } from './common_types';
29
+ import { CliArgs, PushOptions } from './common_types';
30
30
  import { reporters } from './reporters';
31
- import { DEFAULT_NETWORK_CONDITIONS_ARG } from './helpers';
32
- import { normalizeOptions } from './options';
31
+ import { normalizeOptions, parseThrottling } from './options';
33
32
  import { loadTestFiles } from './loader';
34
33
  import { run } from './';
34
+ import { runner } from './core';
35
+ import { SyntheticsLocations } from './dsl/monitor';
36
+ import { push } from './push';
37
+ import { resolve } from 'path';
38
+ import { Generator } from './generator';
39
+ import { error } from './helpers';
35
40
 
36
41
  /* eslint-disable-next-line @typescript-eslint/no-var-requires */
37
42
  const { name, version } = require('../package.json');
@@ -105,9 +110,10 @@ program
105
110
  'always return 0 as an exit code status, regardless of test pass / fail. Only return > 0 exit codes on internal errors where the suite could not be run'
106
111
  )
107
112
  .option(
108
- '--throttling <d/u/l>',
109
- 'List of options to throttle network conditions for download throughput (d) in megabits/second, upload throughput (u) in megabits/second and latency (l) in milliseconds.',
110
- DEFAULT_NETWORK_CONDITIONS_ARG
113
+ '--throttling <config>',
114
+ 'JSON object to throttle network conditions for download and upload throughput in megabits/second and latency in milliseconds. Ex: { "download": 10, "upload": 5, "latency": 200 }.',
115
+ parseThrottling,
116
+ {}
111
117
  )
112
118
  .option('--no-throttling', 'Turns off default network throttling.')
113
119
  .option(
@@ -138,13 +144,63 @@ program
138
144
  }
139
145
  });
140
146
 
147
+ // Push command
141
148
  program
142
- .command('push')
149
+ .command('push [files...]')
143
150
  .description(
144
151
  'Push monitors to create new montors with Kibana monitor management UI'
145
152
  )
146
- .action(() => {
147
- throw new Error('TODO: Implement me');
153
+ .option(
154
+ '--schedule <time-in-minutes>',
155
+ "schedule in minutes for the pushed monitors. Setting `10`, for example, configures monitors which don't have an interval defined to run every 10 minutes.",
156
+ parseInt
157
+ )
158
+ .addOption(
159
+ new Option(
160
+ '--locations <locations...>',
161
+ 'The default list of locations from which your monitors will run.'
162
+ ).choices(SyntheticsLocations)
163
+ )
164
+ .requiredOption(
165
+ '--project <id/name>',
166
+ 'project/repository that will be used for grouping the monitors.'
167
+ )
168
+ .requiredOption('--url <url>', 'kibana URL to upload the monitors')
169
+ .requiredOption(
170
+ '--auth <auth>',
171
+ 'user authentication (in user:password format) or API key that will be used to communicate with Kibana.'
172
+ )
173
+ .option(
174
+ '--space <space>',
175
+ 'the target Kibana spaces for the pushed monitors — spaces help you organise pushed monitors.',
176
+ 'default'
177
+ )
178
+ .option('--delete', 'automatically delete the stale monitors.')
179
+ .action(async (files, cmdOpts: PushOptions) => {
180
+ try {
181
+ const cliArgs = { inline: false };
182
+ await loadTestFiles(cliArgs, files);
183
+ const options = normalizeOptions({ ...program.opts(), ...cmdOpts });
184
+ const monitors = runner.buildMonitors(options);
185
+ await push(monitors, cmdOpts);
186
+ } catch (e) {
187
+ console.error(e);
188
+ process.exit(1);
189
+ }
190
+ });
191
+
192
+ // Init command
193
+ program
194
+ .command('init <project-dir>')
195
+ .description('Initalize Elastic synthetics project')
196
+ .action(async (dir: string) => {
197
+ try {
198
+ const generator = await new Generator(resolve(process.cwd(), dir));
199
+ await generator.setup();
200
+ } catch (e) {
201
+ error(e);
202
+ process.exit(1);
203
+ }
148
204
  });
149
205
 
150
206
  program.parse(process.argv);
@@ -32,7 +32,8 @@ import {
32
32
  Page,
33
33
  } from 'playwright-chromium';
34
34
  import { Step } from './dsl';
35
- import { Reporter, reporters } from './reporters';
35
+ import { BuiltInReporterName, ReporterInstance } from './reporters';
36
+ import { MonitorConfig } from './dsl/monitor';
36
37
 
37
38
  export type VoidCallback = () => void;
38
39
  export type Location = {
@@ -48,7 +49,6 @@ export type HooksArgs = {
48
49
  };
49
50
  export type HooksCallback = (args: HooksArgs) => void;
50
51
  export type StatusValue = 'succeeded' | 'failed' | 'skipped';
51
- export type Reporters = keyof typeof reporters;
52
52
 
53
53
  export type NetworkConditions = {
54
54
  offline: boolean;
@@ -171,6 +171,8 @@ export type NetworkInfo = {
171
171
  };
172
172
  } & DefaultPluginOutput;
173
173
 
174
+ export type PageMetrics = Record<string, number>;
175
+
174
176
  export type BrowserMessage = {
175
177
  text: string;
176
178
  type: string;
@@ -187,6 +189,12 @@ export type PluginOutput = {
187
189
 
188
190
  export type ScreenshotOptions = 'on' | 'off' | 'only-on-failure';
189
191
 
192
+ export type ThrottlingOptions = {
193
+ download?: number;
194
+ upload?: number;
195
+ latency?: number;
196
+ };
197
+
190
198
  type BaseArgs = {
191
199
  params?: Params;
192
200
  screenshots?: ScreenshotOptions;
@@ -199,11 +207,14 @@ type BaseArgs = {
199
207
  ignoreHttpsErrors?: boolean;
200
208
  playwrightOptions?: PlaywrightOptions;
201
209
  quietExitCode?: boolean;
210
+ throttling?: ThrottlingOptions;
211
+ schedule?: MonitorConfig['schedule'];
212
+ locations?: MonitorConfig['locations'];
202
213
  };
203
214
 
204
215
  export type CliArgs = BaseArgs & {
205
216
  config?: string;
206
- reporter?: Reporters;
217
+ reporter?: BuiltInReporterName;
207
218
  pattern?: string;
208
219
  inline?: boolean;
209
220
  require?: Array<string>;
@@ -212,7 +223,7 @@ export type CliArgs = BaseArgs & {
212
223
  richEvents?: boolean;
213
224
  capability?: Array<string>;
214
225
  ignoreHttpsErrors?: boolean;
215
- throttling?: boolean | string;
226
+ throttling?: boolean | ThrottlingOptions;
216
227
  };
217
228
 
218
229
  export type RunOptions = BaseArgs & {
@@ -224,11 +235,64 @@ export type RunOptions = BaseArgs & {
224
235
  environment?: string;
225
236
  playwrightOptions?: PlaywrightOptions;
226
237
  networkConditions?: NetworkConditions;
227
- reporter?: CliArgs['reporter'] | Reporter;
238
+ reporter?: BuiltInReporterName | ReporterInstance;
239
+ };
240
+
241
+ export type PushOptions = {
242
+ auth: string;
243
+ url: string;
244
+ project: string;
245
+ space: string;
246
+ delete: boolean;
247
+ schedule?: MonitorConfig['schedule'];
248
+ locations?: MonitorConfig['locations'];
228
249
  };
229
250
 
230
251
  export type PlaywrightOptions = LaunchOptions & BrowserContextOptions;
231
252
  export type SyntheticsConfig = {
232
253
  params?: Params;
233
254
  playwrightOptions?: PlaywrightOptions;
255
+ monitor?: MonitorConfig;
256
+ };
257
+
258
+ /** Runner Payload types */
259
+ export type JourneyResult = {
260
+ status: StatusValue;
261
+ error?: Error;
262
+ networkinfo?: PluginOutput['networkinfo'];
263
+ browserconsole?: PluginOutput['browserconsole'];
264
+ };
265
+
266
+ export type StepResult = {
267
+ status: StatusValue;
268
+ url?: string;
269
+ error?: Error;
270
+ pagemetrics?: PageMetrics;
271
+ filmstrips?: PluginOutput['filmstrips'];
272
+ metrics?: PluginOutput['metrics'];
273
+ traces?: PluginOutput['traces'];
274
+ };
275
+
276
+ /** Reporter and Runner contract */
277
+ export type StartEvent = {
278
+ numJourneys: number;
279
+ networkConditions?: NetworkConditions;
280
+ };
281
+
282
+ export type JourneyStartResult = {
283
+ timestamp: number;
284
+ params?: Params;
285
+ };
286
+
287
+ export type JourneyEndResult = JourneyStartResult &
288
+ JourneyResult & {
289
+ start: number;
290
+ end: number;
291
+ options: RunOptions;
292
+ timestamp: number;
293
+ };
294
+
295
+ export type StepEndResult = StepResult & {
296
+ start: number;
297
+ end: number;
234
298
  };
@@ -26,7 +26,7 @@
26
26
  import { chromium, ChromiumBrowser, BrowserContext } from 'playwright-chromium';
27
27
  import { PluginManager } from '../plugins';
28
28
  import { log } from './logger';
29
- import { Driver, RunOptions } from '../common_types';
29
+ import { Driver, NetworkConditions, RunOptions } from '../common_types';
30
30
 
31
31
  /**
32
32
  * Purpose of the Gatherer is to set up the necessary browser driver
@@ -75,7 +75,7 @@ export class Gatherer {
75
75
 
76
76
  static setNetworkConditions(
77
77
  context: BrowserContext,
78
- networkConditions: RunOptions['networkConditions']
78
+ networkConditions: NetworkConditions
79
79
  ) {
80
80
  if (networkConditions) {
81
81
  context.on('page', page => {
package/src/core/index.ts CHANGED
@@ -28,6 +28,7 @@ import Runner from './runner';
28
28
  import { VoidCallback, HooksCallback, Location } from '../common_types';
29
29
  import { wrapFnWithLocation } from '../helpers';
30
30
  import { log } from './logger';
31
+ import { MonitorConfig } from '../dsl/monitor';
31
32
 
32
33
  /**
33
34
  * Use a gloabl Runner which would be accessed by the runtime and
@@ -63,6 +64,20 @@ export const step = wrapFnWithLocation(
63
64
  }
64
65
  );
65
66
 
67
+ export const monitor = {
68
+ use: wrapFnWithLocation((location: Location, config: MonitorConfig) => {
69
+ /**
70
+ * If the context is inside journey, then set it to journey context
71
+ * otherwise set to the global monitor which will be used for all journeys
72
+ */
73
+ if (runner.currentJourney) {
74
+ runner.currentJourney.updateMonitor(config);
75
+ } else {
76
+ runner.updateMonitor(config);
77
+ }
78
+ }),
79
+ };
80
+
66
81
  export const beforeAll = (callback: HooksCallback) => {
67
82
  runner.addHook('beforeAll', callback);
68
83
  };
@@ -23,12 +23,11 @@
23
23
  *
24
24
  */
25
25
 
26
- import { once, EventEmitter } from 'events';
27
26
  import { join } from 'path';
28
27
  import { rm } from 'fs/promises';
29
28
  import { Journey } from '../dsl/journey';
30
29
  import { Step } from '../dsl/step';
31
- import { reporters } from '../reporters';
30
+ import { reporters, Reporter } from '../reporters';
32
31
  import {
33
32
  CACHE_PATH,
34
33
  monotonicTimeInSeconds,
@@ -39,20 +38,20 @@ import {
39
38
  writeFileAsync,
40
39
  } from '../helpers';
41
40
  import {
42
- StatusValue,
43
41
  HooksCallback,
44
42
  Params,
45
- NetworkConditions,
46
- PluginOutput,
47
43
  HooksArgs,
48
44
  Driver,
49
45
  Screenshot,
50
46
  RunOptions,
47
+ JourneyResult,
48
+ StepResult,
51
49
  } from '../common_types';
52
- import { PageMetrics, PluginManager } from '../plugins';
50
+ import { PluginManager } from '../plugins';
53
51
  import { PerformanceManager } from '../plugins';
54
52
  import { Gatherer } from './gatherer';
55
53
  import { log } from './logger';
54
+ import { Monitor, MonitorConfig } from '../dsl/monitor';
56
55
 
57
56
  type HookType = 'beforeAll' | 'afterAll';
58
57
  export type SuiteHooks = Record<HookType, Array<HooksCallback>>;
@@ -64,73 +63,16 @@ type JourneyContext = {
64
63
  pluginManager: PluginManager;
65
64
  };
66
65
 
67
- type StepResult = {
68
- status: StatusValue;
69
- url?: string;
70
- error?: Error;
71
- pagemetrics?: PageMetrics;
72
- filmstrips?: PluginOutput['filmstrips'];
73
- metrics?: PluginOutput['metrics'];
74
- traces?: PluginOutput['traces'];
75
- };
76
-
77
- type JourneyResult = {
78
- status: StatusValue;
79
- error?: Error;
80
- networkinfo?: PluginOutput['networkinfo'];
81
- browserconsole?: PluginOutput['browserconsole'];
82
- };
83
-
84
66
  type RunResult = Record<string, JourneyResult>;
85
67
 
86
- interface StepEvent {
87
- step: Step;
88
- journey: Journey;
89
- }
90
-
91
- interface JourneyEvent {
92
- journey: Journey;
93
- timestamp: number;
94
- params?: Params;
95
- }
96
-
97
- interface Events {
98
- start: {
99
- numJourneys: number;
100
- networkConditions?: NetworkConditions;
101
- };
102
- 'journey:register': {
103
- journey: Journey;
104
- };
105
- 'journey:start': JourneyEvent;
106
- 'journey:end': JourneyEvent &
107
- JourneyResult & {
108
- start: number;
109
- end: number;
110
- options: RunOptions;
111
- timestamp: number;
112
- };
113
- 'journey:end:reported': unknown;
114
- 'step:start': StepEvent;
115
- 'step:end': StepEvent &
116
- StepResult & {
117
- start: number;
118
- end: number;
119
- };
120
- end: unknown;
121
- }
122
-
123
- export default interface Runner {
124
- on<K extends keyof Events>(name: K, listener: (v: Events[K]) => void): this;
125
- emit<K extends keyof Events>(event: K, args: Events[K]): boolean;
126
- }
127
-
128
- export default class Runner extends EventEmitter {
129
- active = false;
130
- currentJourney?: Journey = null;
68
+ export default class Runner {
69
+ #active = false;
70
+ #reporter: Reporter;
71
+ #currentJourney?: Journey = null;
131
72
  journeys: Journey[] = [];
132
73
  hooks: SuiteHooks = { beforeAll: [], afterAll: [] };
133
74
  hookError: Error | undefined;
75
+ monitor?: Monitor;
134
76
  static screenshotPath = join(CACHE_PATH, 'screenshots');
135
77
 
136
78
  static async createContext(options: RunOptions): Promise<JourneyContext> {
@@ -181,13 +123,38 @@ export default class Runner extends EventEmitter {
181
123
  }
182
124
  }
183
125
 
126
+ get currentJourney() {
127
+ return this.#currentJourney;
128
+ }
129
+
184
130
  addHook(type: HookType, callback: HooksCallback) {
185
131
  this.hooks[type].push(callback);
186
132
  }
187
133
 
134
+ updateMonitor(config: MonitorConfig) {
135
+ if (!this.monitor) {
136
+ this.monitor = new Monitor(config);
137
+ return;
138
+ }
139
+ this.monitor.update(config);
140
+ }
141
+
188
142
  addJourney(journey: Journey) {
189
143
  this.journeys.push(journey);
190
- this.currentJourney = journey;
144
+ this.#currentJourney = journey;
145
+ }
146
+
147
+ setReporter(options: RunOptions) {
148
+ /**
149
+ * Set up the corresponding reporter and fallback
150
+ * to default reporter if not provided
151
+ */
152
+ const { reporter, outfd } = options;
153
+ const Reporter =
154
+ typeof reporter === 'function'
155
+ ? reporter
156
+ : reporters[reporter] || reporters['default'];
157
+ this.#reporter = new Reporter({ fd: outfd });
191
158
  }
192
159
 
193
160
  async runBeforeAllHook(args: HooksArgs) {
@@ -288,7 +255,7 @@ export default class Runner extends EventEmitter {
288
255
  let skipStep = false;
289
256
  for (const step of journey.steps) {
290
257
  const start = monotonicTimeInSeconds();
291
- this.emit('step:start', { journey, step });
258
+ this.#reporter?.onStepStart?.(journey, step);
292
259
  let data: StepResult = { status: 'succeeded' };
293
260
  if (skipStep) {
294
261
  data.status = 'skipped';
@@ -299,9 +266,7 @@ export default class Runner extends EventEmitter {
299
266
  */
300
267
  if (data.error) skipStep = true;
301
268
  }
302
- this.emit('step:end', {
303
- journey,
304
- step,
269
+ this.#reporter?.onStepEnd?.(journey, step, {
305
270
  start,
306
271
  end: monotonicTimeInSeconds(),
307
272
  ...data,
@@ -315,12 +280,16 @@ export default class Runner extends EventEmitter {
315
280
  }
316
281
 
317
282
  registerJourney(journey: Journey, context: JourneyContext) {
318
- this.currentJourney = journey;
283
+ this.#currentJourney = journey;
319
284
  const timestamp = getTimestamp();
320
285
  const { params } = context;
321
- this.emit('journey:start', { journey, timestamp, params });
286
+ this.#reporter?.onJourneyStart?.(journey, {
287
+ timestamp,
288
+ params,
289
+ });
322
290
  /**
323
- * Load and register the steps for the current journey
291
+ * Exeucute the journey callback which would
292
+ * register the steps for the current journey
324
293
  */
325
294
  journey.callback({ ...context.driver, params });
326
295
  }
@@ -333,8 +302,7 @@ export default class Runner extends EventEmitter {
333
302
  const end = monotonicTimeInSeconds();
334
303
  const { pluginManager, start, status, error } = result;
335
304
  const pluginOutput = await pluginManager.output();
336
- this.emit('journey:end', {
337
- journey,
305
+ await this.#reporter?.onJourneyEnd?.(journey, {
338
306
  status,
339
307
  error,
340
308
  start,
@@ -344,13 +312,6 @@ export default class Runner extends EventEmitter {
344
312
  ...pluginOutput,
345
313
  browserconsole: status == 'failed' ? pluginOutput.browserconsole : [],
346
314
  });
347
- /**
348
- * Wait for the all the reported events to be consumed aschronously
349
- * by reporter.
350
- */
351
- if (options.reporter === 'json') {
352
- await once(this, 'journey:end:reported');
353
- }
354
315
  // clear screenshots cache after each journey
355
316
  await rm(Runner.screenshotPath, { recursive: true, force: true });
356
317
  }
@@ -360,8 +321,7 @@ export default class Runner extends EventEmitter {
360
321
  */
361
322
  async runFakeJourney(journey: Journey, options: RunOptions) {
362
323
  const start = monotonicTimeInSeconds();
363
- this.emit('journey:start', {
364
- journey,
324
+ this.#reporter.onJourneyStart?.(journey, {
365
325
  timestamp: getTimestamp(),
366
326
  params: options.params,
367
327
  });
@@ -369,24 +329,18 @@ export default class Runner extends EventEmitter {
369
329
  status: 'failed',
370
330
  error: this.hookError,
371
331
  };
372
- this.emit('journey:end', {
373
- journey,
332
+ await this.#reporter.onJourneyEnd?.(journey, {
374
333
  timestamp: getTimestamp(),
375
334
  start,
376
335
  options,
377
336
  end: monotonicTimeInSeconds(),
378
337
  ...result,
379
338
  });
380
- if (options.reporter === 'json') {
381
- await once(this, 'journey:end:reported');
382
- }
383
339
  return result;
384
340
  }
385
341
 
386
342
  async runJourney(journey: Journey, options: RunOptions) {
387
- const result: JourneyResult = {
388
- status: 'succeeded',
389
- };
343
+ const result: JourneyResult = { status: 'succeeded' };
390
344
  const context = await Runner.createContext(options);
391
345
  log(`Runner: start journey (${journey.name})`);
392
346
  try {
@@ -419,18 +373,10 @@ export default class Runner extends EventEmitter {
419
373
  }
420
374
 
421
375
  async init(options: RunOptions) {
422
- const { reporter, outfd, networkConditions } = options;
423
- /**
424
- * Set up the corresponding reporter and fallback
425
- */
426
- const Reporter =
427
- typeof reporter === 'function'
428
- ? reporter
429
- : reporters[reporter] || reporters['default'];
430
- new Reporter(this, { fd: outfd });
431
- this.emit('start', {
376
+ this.setReporter(options);
377
+ this.#reporter.onStart?.({
432
378
  numJourneys: this.journeys.length,
433
- networkConditions,
379
+ networkConditions: options.networkConditions,
434
380
  });
435
381
  /**
436
382
  * Set up the directory for caching screenshots
@@ -438,12 +384,39 @@ export default class Runner extends EventEmitter {
438
384
  await mkdirAsync(CACHE_PATH, { recursive: true });
439
385
  }
440
386
 
387
+ buildMonitors(options: RunOptions) {
388
+ /**
389
+ * Update the global monitor configuration required for
390
+ * setting defaults
391
+ */
392
+ this.updateMonitor({
393
+ throttling: options.throttling,
394
+ schedule: options.schedule,
395
+ locations: options.locations,
396
+ params: options.params,
397
+ playwrightOptions: options.playwrightOptions,
398
+ });
399
+
400
+ const monitors: Monitor[] = [];
401
+ for (const journey of this.journeys) {
402
+ this.#currentJourney = journey;
403
+ /**
404
+ * Execute dummy callback to get all monitor specific
405
+ * configurations for the current journey
406
+ */
407
+ journey.callback({ params: options.params } as any);
408
+ journey.monitor.update(this.monitor?.config);
409
+ monitors.push(journey.monitor);
410
+ }
411
+ return monitors;
412
+ }
413
+
441
414
  async run(options: RunOptions) {
442
415
  const result: RunResult = {};
443
- if (this.active) {
416
+ if (this.#active) {
444
417
  return result;
445
418
  }
446
- this.active = true;
419
+ this.#active = true;
447
420
  log(`Runner: run ${this.journeys.length} journeys`);
448
421
  this.init(options);
449
422
  await this.runBeforeAllHook({
@@ -457,7 +430,7 @@ export default class Runner extends EventEmitter {
457
430
  * Used by heartbeat to gather all registered journeys
458
431
  */
459
432
  if (dryRun) {
460
- this.emit('journey:register', { journey });
433
+ this.#reporter.onJourneyRegister?.(journey);
461
434
  continue;
462
435
  }
463
436
  if (!journey.isMatch(match, tags)) {
@@ -478,14 +451,14 @@ export default class Runner extends EventEmitter {
478
451
  }
479
452
 
480
453
  async reset() {
454
+ this.#currentJourney = null;
455
+ this.journeys = [];
456
+ this.#active = false;
481
457
  /**
482
458
  * Clear all cache data stored for post processing by
483
459
  * the current synthetic agent run
484
460
  */
485
461
  await rm(CACHE_PATH, { recursive: true, force: true });
486
- this.currentJourney = null;
487
- this.journeys = [];
488
- this.active = false;
489
- this.emit('end', {});
462
+ await this.#reporter?.onEnd?.();
490
463
  }
491
464
  }
@@ -27,6 +27,7 @@ import { Browser, Page, BrowserContext, CDPSession } from 'playwright-chromium';
27
27
  import micromatch, { isMatch } from 'micromatch';
28
28
  import { Step } from './step';
29
29
  import { VoidCallback, HooksCallback, Params, Location } from '../common_types';
30
+ import { Monitor, MonitorConfig } from './monitor';
30
31
 
31
32
  export type JourneyOptions = {
32
33
  name: string;
@@ -52,6 +53,7 @@ export class Journey {
52
53
  location?: Location;
53
54
  steps: Step[] = [];
54
55
  hooks: Hooks = { before: [], after: [] };
56
+ monitor: Monitor;
55
57
 
56
58
  constructor(
57
59
  options: JourneyOptions,
@@ -63,6 +65,7 @@ export class Journey {
63
65
  this.tags = options.tags;
64
66
  this.callback = callback;
65
67
  this.location = location;
68
+ this.updateMonitor({});
66
69
  }
67
70
 
68
71
  addStep(name: string, callback: VoidCallback, location?: Location) {
@@ -74,6 +77,21 @@ export class Journey {
74
77
  addHook(type: HookType, callback: HooksCallback) {
75
78
  this.hooks[type].push(callback);
76
79
  }
80
+
81
+ updateMonitor(config: MonitorConfig) {
82
+ /**
83
+ * Use defaults values from journey for monitor object (id, name and tags)
84
+ */
85
+ this.monitor = new Monitor({
86
+ name: this.name,
87
+ id: this.id,
88
+ tags: this.tags ?? [],
89
+ ...config,
90
+ });
91
+ this.monitor.setSource(this.location);
92
+ this.monitor.setFilter({ match: this.name });
93
+ }
94
+
77
95
  /**
78
96
  * Matches journeys based on the provided args. Proitize tags over match
79
97
  * - tags pattern that matches only tags