@elastic/synthetics 1.0.0-beta.8 → 1.0.0

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 (220) hide show
  1. package/README.md +5 -4
  2. package/dist/cli.js +139 -165
  3. package/dist/cli.js.map +1 -1
  4. package/dist/common_types.d.ts +158 -34
  5. package/dist/common_types.d.ts.map +1 -1
  6. package/dist/config.d.ts +1 -0
  7. package/dist/config.d.ts.map +1 -1
  8. package/dist/config.js +7 -6
  9. package/dist/config.js.map +1 -1
  10. package/dist/core/browser-service.d.ts +1 -1
  11. package/dist/core/browser-service.d.ts.map +1 -1
  12. package/dist/core/browser-service.js +7 -6
  13. package/dist/core/browser-service.js.map +1 -1
  14. package/dist/core/expect.d.ts +25 -25
  15. package/dist/core/expect.d.ts.map +1 -1
  16. package/dist/core/expect.js.map +1 -1
  17. package/dist/core/gatherer.d.ts +5 -8
  18. package/dist/core/gatherer.d.ts.map +1 -1
  19. package/dist/core/gatherer.js +75 -17
  20. package/dist/core/gatherer.js.map +1 -1
  21. package/dist/core/index.d.ts +5 -1
  22. package/dist/core/index.d.ts.map +1 -1
  23. package/dist/core/index.js +23 -11
  24. package/dist/core/index.js.map +1 -1
  25. package/dist/core/logger.d.ts +0 -1
  26. package/dist/core/logger.d.ts.map +1 -1
  27. package/dist/core/logger.js +10 -17
  28. package/dist/core/logger.js.map +1 -1
  29. package/dist/core/runner.d.ts +12 -63
  30. package/dist/core/runner.d.ts.map +1 -1
  31. package/dist/core/runner.js +159 -92
  32. package/dist/core/runner.js.map +1 -1
  33. package/dist/core/transform.d.ts +37 -0
  34. package/dist/core/transform.d.ts.map +1 -0
  35. package/dist/core/transform.js +148 -0
  36. package/dist/core/transform.js.map +1 -0
  37. package/dist/dsl/index.js +5 -1
  38. package/dist/dsl/index.js.map +1 -1
  39. package/dist/dsl/journey.d.ts +9 -7
  40. package/dist/dsl/journey.d.ts.map +1 -1
  41. package/dist/dsl/journey.js +32 -39
  42. package/dist/dsl/journey.js.map +1 -1
  43. package/dist/dsl/monitor.d.ts +93 -0
  44. package/dist/dsl/monitor.d.ts.map +1 -0
  45. package/dist/dsl/monitor.js +110 -0
  46. package/dist/dsl/monitor.js.map +1 -0
  47. package/dist/dsl/step.d.ts +3 -2
  48. package/dist/dsl/step.d.ts.map +1 -1
  49. package/dist/dsl/step.js +6 -1
  50. package/dist/dsl/step.js.map +1 -1
  51. package/dist/formatter/javascript.d.ts +115 -0
  52. package/dist/formatter/javascript.d.ts.map +1 -0
  53. package/dist/formatter/javascript.js +331 -0
  54. package/dist/formatter/javascript.js.map +1 -0
  55. package/dist/generator/index.d.ts +24 -0
  56. package/dist/generator/index.d.ts.map +1 -0
  57. package/dist/generator/index.js +241 -0
  58. package/dist/generator/index.js.map +1 -0
  59. package/dist/{reporters/reporter.js → generator/utils.d.ts} +5 -3
  60. package/dist/generator/utils.d.ts.map +1 -0
  61. package/dist/generator/utils.js +84 -0
  62. package/dist/generator/utils.js.map +1 -0
  63. package/dist/helpers.d.ts +36 -9
  64. package/dist/helpers.d.ts.map +1 -1
  65. package/dist/helpers.js +170 -24
  66. package/dist/helpers.js.map +1 -1
  67. package/dist/index.d.ts +5 -7
  68. package/dist/index.d.ts.map +1 -1
  69. package/dist/index.js +5 -26
  70. package/dist/index.js.map +1 -1
  71. package/dist/loader.d.ts +34 -0
  72. package/dist/loader.d.ts.map +1 -0
  73. package/dist/loader.js +153 -0
  74. package/dist/loader.js.map +1 -0
  75. package/dist/locations/index.d.ts +45 -0
  76. package/dist/locations/index.d.ts.map +1 -0
  77. package/dist/locations/index.js +83 -0
  78. package/dist/locations/index.js.map +1 -0
  79. package/dist/{reporters/reporter.d.ts → locations/public-locations.d.ts} +12 -8
  80. package/dist/locations/public-locations.d.ts.map +1 -0
  81. package/dist/locations/public-locations.js +41 -0
  82. package/dist/locations/public-locations.js.map +1 -0
  83. package/dist/options.d.ts +42 -0
  84. package/dist/options.d.ts.map +1 -0
  85. package/dist/options.js +146 -0
  86. package/dist/options.js.map +1 -0
  87. package/dist/plugins/browser-console.d.ts +5 -4
  88. package/dist/plugins/browser-console.d.ts.map +1 -1
  89. package/dist/plugins/browser-console.js +47 -24
  90. package/dist/plugins/browser-console.js.map +1 -1
  91. package/dist/plugins/index.js +5 -1
  92. package/dist/plugins/index.js.map +1 -1
  93. package/dist/plugins/network.d.ts +24 -31
  94. package/dist/plugins/network.d.ts.map +1 -1
  95. package/dist/plugins/network.js +226 -229
  96. package/dist/plugins/network.js.map +1 -1
  97. package/dist/plugins/performance.d.ts +4 -19
  98. package/dist/plugins/performance.d.ts.map +1 -1
  99. package/dist/plugins/performance.js +6 -5
  100. package/dist/plugins/performance.js.map +1 -1
  101. package/dist/plugins/plugin-manager.d.ts +9 -5
  102. package/dist/plugins/plugin-manager.d.ts.map +1 -1
  103. package/dist/plugins/plugin-manager.js +46 -30
  104. package/dist/plugins/plugin-manager.js.map +1 -1
  105. package/dist/plugins/tracing.d.ts +6 -6
  106. package/dist/plugins/tracing.d.ts.map +1 -1
  107. package/dist/plugins/tracing.js +13 -7
  108. package/dist/plugins/tracing.js.map +1 -1
  109. package/dist/push/bundler.d.ts +35 -0
  110. package/dist/push/bundler.d.ts.map +1 -0
  111. package/dist/push/bundler.js +131 -0
  112. package/dist/push/bundler.js.map +1 -0
  113. package/dist/push/index.d.ts +10 -0
  114. package/dist/push/index.d.ts.map +1 -0
  115. package/dist/push/index.js +235 -0
  116. package/dist/push/index.js.map +1 -0
  117. package/dist/push/kibana_api.d.ts +56 -0
  118. package/dist/push/kibana_api.d.ts.map +1 -0
  119. package/dist/push/kibana_api.js +135 -0
  120. package/dist/push/kibana_api.js.map +1 -0
  121. package/dist/push/monitor.d.ts +55 -0
  122. package/dist/push/monitor.d.ts.map +1 -0
  123. package/dist/push/monitor.js +268 -0
  124. package/dist/push/monitor.js.map +1 -0
  125. package/dist/{parse_args.d.ts → push/plugin.d.ts} +9 -6
  126. package/dist/push/plugin.d.ts.map +1 -0
  127. package/dist/push/plugin.js +66 -0
  128. package/dist/push/plugin.js.map +1 -0
  129. package/dist/push/request.d.ts +45 -0
  130. package/dist/push/request.d.ts.map +1 -0
  131. package/dist/push/request.js +109 -0
  132. package/dist/push/request.js.map +1 -0
  133. package/dist/push/utils.d.ts +31 -0
  134. package/dist/push/utils.d.ts.map +1 -0
  135. package/dist/push/utils.js +64 -0
  136. package/dist/push/utils.js.map +1 -0
  137. package/dist/reporters/base.d.ts +14 -6
  138. package/dist/reporters/base.d.ts.map +1 -1
  139. package/dist/reporters/base.js +58 -50
  140. package/dist/reporters/base.js.map +1 -1
  141. package/dist/reporters/index.d.ts +18 -8
  142. package/dist/reporters/index.d.ts.map +1 -1
  143. package/dist/reporters/index.js.map +1 -1
  144. package/dist/reporters/json.d.ts +17 -62
  145. package/dist/reporters/json.d.ts.map +1 -1
  146. package/dist/reporters/json.js +147 -177
  147. package/dist/reporters/json.js.map +1 -1
  148. package/dist/reporters/junit.d.ts +7 -1
  149. package/dist/reporters/junit.d.ts.map +1 -1
  150. package/dist/reporters/junit.js +90 -95
  151. package/dist/reporters/junit.js.map +1 -1
  152. package/dist/sdk/lh-trace-processor.d.ts +35 -9
  153. package/dist/sdk/lh-trace-processor.d.ts.map +1 -1
  154. package/dist/sdk/lh-trace-processor.js +114 -53
  155. package/dist/sdk/lh-trace-processor.js.map +1 -1
  156. package/dist/sdk/trace-metrics.d.ts +4 -4
  157. package/dist/sdk/trace-metrics.d.ts.map +1 -1
  158. package/dist/sdk/trace-metrics.js +3 -4
  159. package/dist/sdk/trace-metrics.js.map +1 -1
  160. package/dist/sdk/trace-processor.d.ts +11 -3
  161. package/dist/sdk/trace-processor.d.ts.map +1 -1
  162. package/dist/sdk/trace-processor.js +17 -9
  163. package/dist/sdk/trace-processor.js.map +1 -1
  164. package/package.json +38 -30
  165. package/src/cli.ts +223 -158
  166. package/src/common_types.ts +191 -34
  167. package/src/config.ts +1 -1
  168. package/src/core/expect.ts +58 -24
  169. package/src/core/gatherer.ts +92 -25
  170. package/src/core/index.ts +36 -15
  171. package/src/core/logger.ts +8 -12
  172. package/src/core/runner.ts +158 -148
  173. package/src/core/transform.ts +160 -0
  174. package/src/dsl/journey.ts +41 -21
  175. package/src/dsl/monitor.ts +152 -0
  176. package/src/dsl/step.ts +9 -2
  177. package/src/formatter/javascript.ts +414 -0
  178. package/src/generator/index.ts +308 -0
  179. package/src/generator/utils.ts +84 -0
  180. package/src/helpers.ts +182 -16
  181. package/src/index.ts +21 -22
  182. package/src/loader.ts +171 -0
  183. package/src/locations/index.ts +95 -0
  184. package/src/{reporters/reporter.ts → locations/public-locations.ts} +12 -9
  185. package/src/options.ts +175 -0
  186. package/src/plugins/browser-console.ts +33 -8
  187. package/src/plugins/network.ts +266 -284
  188. package/src/plugins/performance.ts +6 -22
  189. package/src/plugins/plugin-manager.ts +49 -31
  190. package/src/plugins/tracing.ts +11 -9
  191. package/src/push/bundler.ts +112 -0
  192. package/src/push/index.ts +291 -0
  193. package/src/push/kibana_api.ts +191 -0
  194. package/src/push/monitor.ts +307 -0
  195. package/src/push/plugin.ts +66 -0
  196. package/src/push/request.ts +141 -0
  197. package/src/push/utils.ts +77 -0
  198. package/src/reporters/base.ts +63 -52
  199. package/src/reporters/index.ts +26 -3
  200. package/src/reporters/json.ts +181 -201
  201. package/src/reporters/junit.ts +98 -93
  202. package/src/sdk/lh-trace-processor.ts +132 -68
  203. package/src/sdk/trace-metrics.ts +8 -4
  204. package/src/sdk/trace-processor.ts +28 -13
  205. package/templates/.github/workflows/run-synthetics.yml +27 -0
  206. package/templates/README.md +21 -0
  207. package/templates/journeys/advanced-example-helpers.ts +54 -0
  208. package/templates/journeys/advanced-example.journey.ts +43 -0
  209. package/templates/journeys/example.journey.ts +18 -0
  210. package/templates/lightweight/heartbeat.yml +9 -0
  211. package/templates/synthetics.config.ts +35 -0
  212. package/CHANGELOG.md +0 -181
  213. package/NOTICE.txt +0 -221
  214. package/dist/parse_args.d.ts.map +0 -1
  215. package/dist/parse_args.js +0 -98
  216. package/dist/parse_args.js.map +0 -1
  217. package/dist/reporters/reporter.d.ts.map +0 -1
  218. package/dist/reporters/reporter.js.map +0 -1
  219. package/dist/tsconfig.tsbuildinfo +0 -1895
  220. package/src/parse_args.ts +0 -146
@@ -0,0 +1,191 @@
1
+ /**
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2020-present, Elastic NV
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ *
24
+ */
25
+
26
+ import { PushOptions } from '../common_types';
27
+ import { safeNDJSONParse } from '../helpers';
28
+ import { MonitorHashID, MonitorSchema } from './monitor';
29
+ import {
30
+ formatFailedMonitors,
31
+ formatStaleMonitors,
32
+ handleError,
33
+ sendReqAndHandleError,
34
+ sendRequest,
35
+ APIMonitorError,
36
+ } from './request';
37
+ import { generateURL } from './utils';
38
+
39
+ // Default chunk size for bulk put / delete
40
+ export const CHUNK_SIZE = 100;
41
+
42
+ export type PutResponse = {
43
+ createdMonitors: string[];
44
+ updatedMonitors: string[];
45
+ failedMonitors: APIMonitorError[];
46
+ };
47
+
48
+ export async function bulkPutMonitors(
49
+ options: PushOptions,
50
+ schemas: MonitorSchema[]
51
+ ) {
52
+ const resp = await sendReqAndHandleError<PutResponse>({
53
+ url: generateURL(options, 'bulk_update') + '/_bulk_update',
54
+ method: 'PUT',
55
+ auth: options.auth,
56
+ body: JSON.stringify({ monitors: schemas }),
57
+ });
58
+ const { failedMonitors } = resp;
59
+ if (failedMonitors && failedMonitors.length > 0) {
60
+ throw formatFailedMonitors(failedMonitors);
61
+ }
62
+ return resp;
63
+ }
64
+
65
+ export type GetResponse = {
66
+ total: number;
67
+ monitors: MonitorHashID[];
68
+ after_key?: string;
69
+ };
70
+
71
+ export async function bulkGetMonitors(
72
+ options: PushOptions
73
+ ): Promise<GetResponse> {
74
+ const allMonitors: MonitorHashID[] = [];
75
+
76
+ const resp = await fetchMonitors(options);
77
+ allMonitors.push(...resp.monitors);
78
+ let afterKey = resp.afterKey;
79
+ const total = resp.total;
80
+
81
+ while (allMonitors.length < total) {
82
+ const resp = await fetchMonitors(options, afterKey);
83
+ allMonitors.push(...resp.monitors);
84
+ afterKey = resp.afterKey;
85
+ }
86
+
87
+ return {
88
+ total,
89
+ monitors: allMonitors,
90
+ };
91
+ }
92
+
93
+ const fetchMonitors = async (options: PushOptions, afterKey?: string) => {
94
+ let url = generateURL(options, 'bulk_get');
95
+ if (afterKey) {
96
+ url += `?search_after=${afterKey}`;
97
+ }
98
+ const resp = await sendReqAndHandleError<GetResponse>({
99
+ url,
100
+ method: 'GET',
101
+ auth: options.auth,
102
+ });
103
+ return {
104
+ afterKey: resp.after_key,
105
+ total: resp.total,
106
+ monitors: resp.monitors,
107
+ };
108
+ };
109
+
110
+ export type DeleteResponse = {
111
+ deleted_monitors: string[];
112
+ };
113
+
114
+ export async function bulkDeleteMonitors(
115
+ options: PushOptions,
116
+ monitorIDs: string[]
117
+ ) {
118
+ return await sendReqAndHandleError<DeleteResponse>({
119
+ url: generateURL(options, 'bulk_delete') + '/_bulk_delete',
120
+ method: 'DELETE',
121
+ auth: options.auth,
122
+ body: JSON.stringify({ monitors: monitorIDs }),
123
+ });
124
+ }
125
+
126
+ type StatusResponse = {
127
+ version: {
128
+ number: number;
129
+ };
130
+ };
131
+
132
+ export async function getVersion(options: PushOptions) {
133
+ const data = await sendReqAndHandleError<StatusResponse>({
134
+ url: generateURL(options, 'status'),
135
+ method: 'GET',
136
+ auth: options.auth,
137
+ });
138
+ return data.version.number;
139
+ }
140
+
141
+ export type LegacyAPISchema = {
142
+ project: string;
143
+ keep_stale: boolean;
144
+ monitors: MonitorSchema[];
145
+ };
146
+
147
+ export async function createMonitorsLegacy({
148
+ schemas,
149
+ keepStale,
150
+ options,
151
+ }: {
152
+ schemas: MonitorSchema[];
153
+ keepStale: boolean;
154
+ options: PushOptions;
155
+ }) {
156
+ const schema: LegacyAPISchema = {
157
+ project: options.id,
158
+ keep_stale: keepStale,
159
+ monitors: schemas,
160
+ };
161
+ const url = generateURL(options, 'legacy');
162
+ const { body, statusCode } = await sendRequest({
163
+ url,
164
+ method: 'PUT',
165
+ auth: options.auth,
166
+ body: JSON.stringify(schema),
167
+ });
168
+
169
+ const resBody = await handleError(statusCode, url, body);
170
+ const allchunks = [];
171
+ for await (const data of resBody) {
172
+ allchunks.push(Buffer.from(data));
173
+ }
174
+ const chunks = safeNDJSONParse(Buffer.concat(allchunks).toString('utf-8'));
175
+ // Its kind of hacky for now where Kibana streams the response by
176
+ // writing the data as NDJSON events (data can be interleaved), we
177
+ // distinguish the final data by checking if the event was a progress vs complete event
178
+ for (const chunk of chunks) {
179
+ if (typeof chunk === 'string') {
180
+ // Ignore the progress from Kibana as we chunk the requests
181
+ continue;
182
+ }
183
+ const { failedMonitors, failedStaleMonitors } = chunk;
184
+ if (failedMonitors && failedMonitors.length > 0) {
185
+ throw formatFailedMonitors(failedMonitors);
186
+ }
187
+ if (failedStaleMonitors.length > 0) {
188
+ throw formatStaleMonitors(failedStaleMonitors);
189
+ }
190
+ }
191
+ }
@@ -0,0 +1,307 @@
1
+ /**
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2020-present, Elastic NV
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ *
24
+ */
25
+
26
+ import { mkdir, rm, readFile } from 'fs/promises';
27
+ import { extname, join } from 'path';
28
+ import { LineCounter, parseDocument, YAMLSeq, YAMLMap } from 'yaml';
29
+ import { bold, red } from 'kleur/colors';
30
+ import { Bundler } from './bundler';
31
+ import { SYNTHETICS_PATH, totalist, indent, warn, isMatch } from '../helpers';
32
+ import { LocationsMap } from '../locations/public-locations';
33
+ import { ALLOWED_SCHEDULES, Monitor, MonitorConfig } from '../dsl/monitor';
34
+ import { PushOptions } from '../common_types';
35
+
36
+ // Allowed extensions for lightweight monitor files
37
+ const ALLOWED_LW_EXTENSIONS = ['.yml', '.yaml'];
38
+
39
+ export type MonitorSchema = Omit<MonitorConfig, 'locations'> & {
40
+ locations: string[];
41
+ content?: string;
42
+ filter?: Monitor['filter'];
43
+ hash?: string;
44
+ };
45
+
46
+ // Abbreviated monitor info, as often returned by the API,
47
+ // just the journey ID and hash
48
+ export type MonitorHashID = {
49
+ journey_id?: string;
50
+ hash?: string;
51
+ };
52
+
53
+ function translateLocation(locations?: MonitorConfig['locations']) {
54
+ if (!locations) return [];
55
+ return locations.map(loc => LocationsMap[loc] || loc).filter(Boolean);
56
+ }
57
+
58
+ class RemoteDiffResult {
59
+ // The set of monitor IDs that have been added
60
+ newIDs = new Set<string>();
61
+ // Monitor IDs that are different locally than remotely
62
+ changedIDs = new Set<string>();
63
+ // Monitor IDs that are no longer present locally
64
+ removedIDs = new Set<string>();
65
+ // Monitor IDs that are identical on the remote server
66
+ unchangedIDs = new Set<string>();
67
+ }
68
+
69
+ export function diffMonitors(
70
+ local: MonitorHashID[],
71
+ remote: MonitorHashID[]
72
+ ): RemoteDiffResult {
73
+ const result = new RemoteDiffResult();
74
+ const localMonitorsIDToHash = new Map<string, string>();
75
+ for (const hashID of local) {
76
+ localMonitorsIDToHash.set(hashID.journey_id, hashID.hash);
77
+ }
78
+ const remoteMonitorsIDToHash = new Map<string, string>();
79
+ for (const hashID of remote) {
80
+ remoteMonitorsIDToHash.set(hashID.journey_id, hashID.hash);
81
+ }
82
+
83
+ // Compare local to remote
84
+ for (const [localID, localHash] of localMonitorsIDToHash) {
85
+ // Hash is reset to '' when a monitor is edited on the UI
86
+ if (!remoteMonitorsIDToHash.has(localID)) {
87
+ result.newIDs.add(localID);
88
+ } else {
89
+ const remoteHash = remoteMonitorsIDToHash.get(localID);
90
+ if (remoteHash != localHash) {
91
+ result.changedIDs.add(localID);
92
+ } else if (remoteHash === localHash) {
93
+ result.unchangedIDs.add(localID);
94
+ }
95
+ }
96
+ // We no longer need to process this ID, removing it here
97
+ // reduces the numbers considered in the next phase
98
+ remoteMonitorsIDToHash.delete(localID);
99
+ }
100
+
101
+ for (const [id] of remoteMonitorsIDToHash) {
102
+ result.removedIDs.add(id);
103
+ }
104
+ return result;
105
+ }
106
+
107
+ export function getLocalMonitors(monitors: Monitor[]) {
108
+ const data: MonitorHashID[] = [];
109
+ for (const monitor of monitors) {
110
+ data.push({
111
+ journey_id: monitor.config.id,
112
+ hash: monitor.hash(),
113
+ });
114
+ }
115
+ return data;
116
+ }
117
+
118
+ export async function buildMonitorSchema(monitors: Monitor[], isV2: boolean) {
119
+ /**
120
+ * Set up the bundle artifacts path which can be used to
121
+ * create the bundles required for uploading journeys
122
+ */
123
+ const bundlePath = join(SYNTHETICS_PATH, 'bundles');
124
+ await mkdir(bundlePath, { recursive: true });
125
+ const bundler = new Bundler();
126
+ const schemas: MonitorSchema[] = [];
127
+
128
+ for (const monitor of monitors) {
129
+ const { source, config, filter, type } = monitor;
130
+ const schema: MonitorSchema = {
131
+ ...config,
132
+ locations: translateLocation(config.locations),
133
+ };
134
+ if (isV2) {
135
+ schema.hash = monitor.hash();
136
+ }
137
+ if (type === 'browser') {
138
+ const outPath = join(bundlePath, config.name + '.zip');
139
+ const content = await bundler.build(source.file, outPath);
140
+ Object.assign(schema, { content, filter });
141
+ }
142
+ schemas.push(schema);
143
+ }
144
+
145
+ await rm(bundlePath, { recursive: true });
146
+ return schemas;
147
+ }
148
+
149
+ export async function createLightweightMonitors(
150
+ workDir: string,
151
+ options: PushOptions
152
+ ) {
153
+ const lwFiles = new Set<string>();
154
+ // Filter monitor files based on the provided pattern
155
+ const pattern = options.pattern
156
+ ? new RegExp(options.pattern, 'i')
157
+ : /.(yml|yaml)$/;
158
+ const ignore = /(node_modules|.github)/;
159
+ await totalist(workDir, (rel, abs) => {
160
+ if (
161
+ !ignore.test(rel) &&
162
+ pattern.test(rel) &&
163
+ ALLOWED_LW_EXTENSIONS.includes(extname(abs))
164
+ ) {
165
+ lwFiles.add(abs);
166
+ }
167
+ });
168
+
169
+ let warnOnce = false;
170
+ const monitors: Monitor[] = [];
171
+ for (const file of lwFiles.values()) {
172
+ const content = await readFile(file, 'utf-8');
173
+ const lineCounter = new LineCounter();
174
+ const parsedDoc = parseDocument(content, {
175
+ lineCounter,
176
+ keepSourceTokens: true,
177
+ });
178
+ // Skip other yml files that are not relevant
179
+ const monitorSeq = parsedDoc.get('heartbeat.monitors') as YAMLSeq<YAMLMap>;
180
+ if (!monitorSeq) {
181
+ continue;
182
+ }
183
+ // Warn users about schedule that are less than 60 seconds
184
+ if (!warnOnce) {
185
+ warn(
186
+ 'Lightweight monitor schedules will be adjusted to their nearest frequency supported by our synthetics infrastructure.'
187
+ );
188
+ warnOnce = true;
189
+ }
190
+
191
+ for (const monNode of monitorSeq.items) {
192
+ // Skip browser monitors and disabled monitors from pushing
193
+ if (
194
+ monNode.get('type') === 'browser' ||
195
+ monNode.get('enabled') === false
196
+ ) {
197
+ continue;
198
+ }
199
+ const config = monNode.toJSON();
200
+ // Filter based on matched tags and name
201
+ if (!isMatch(config.tags, config.name, options.tags, options.match)) {
202
+ continue;
203
+ }
204
+ const { line, col } = lineCounter.linePos(monNode.srcToken.offset);
205
+ try {
206
+ const mon = buildMonitorFromYaml(config, options);
207
+ mon.setSource({ file, line, column: col });
208
+ monitors.push(mon);
209
+ } catch (e) {
210
+ let outer = bold(`Aborted: ${e}\n`);
211
+ outer += indent(
212
+ `* ${config.id || config.name} - ${file}:${line}:${col}\n`
213
+ );
214
+ throw red(outer);
215
+ }
216
+ }
217
+ }
218
+ return monitors;
219
+ }
220
+
221
+ const REQUIRED_MONITOR_FIELDS = ['id', 'name'];
222
+ export function buildMonitorFromYaml(
223
+ config: MonitorConfig,
224
+ options: PushOptions
225
+ ) {
226
+ // Validate required fields
227
+ for (const field of REQUIRED_MONITOR_FIELDS) {
228
+ if (!config[field]) {
229
+ throw `Monitor ${field} is required`;
230
+ }
231
+ }
232
+ const schedule = config.schedule && parseSchedule(String(config.schedule));
233
+ const privateLocations =
234
+ config['private_locations'] || options.privateLocations;
235
+ delete config['private_locations'];
236
+
237
+ const alertConfig = parseAlertConfig(config);
238
+
239
+ return new Monitor({
240
+ locations: options.locations,
241
+ ...config,
242
+ privateLocations,
243
+ schedule: schedule || options.schedule,
244
+ alert: alertConfig,
245
+ });
246
+ }
247
+
248
+ export const parseAlertConfig = (config: MonitorConfig) => {
249
+ if (config['alert.status.enabled'] !== undefined) {
250
+ const value = config['alert.status.enabled'];
251
+ delete config['alert.status.enabled'];
252
+ return {
253
+ status: {
254
+ enabled: value,
255
+ },
256
+ };
257
+ }
258
+ return config.alert;
259
+ };
260
+
261
+ export function parseSchedule(schedule: string) {
262
+ const EVERY_SYNTAX = '@every';
263
+ if (!(schedule + '').startsWith(EVERY_SYNTAX)) {
264
+ throw `Monitor schedule format(${schedule}) not supported: use '@every' syntax instead`;
265
+ }
266
+
267
+ const duration = schedule.substring(EVERY_SYNTAX.length + 1);
268
+ // split between non-digit (\D) and a digit (\d)
269
+ const durations = duration.split(/(?<=\D)(?=\d)/g);
270
+ let minutes = 0;
271
+ for (const dur of durations) {
272
+ // split between a digit and non-digit
273
+ const [value, format] = dur.split(/(?<=\d)(?=\D)/g);
274
+ // Calculate based on the duration symbol
275
+ const scheduleValue = parseInt(value, 10);
276
+ switch (format) {
277
+ case 's':
278
+ minutes += Math.round(scheduleValue / 60);
279
+ break;
280
+ case 'm':
281
+ minutes += scheduleValue;
282
+ break;
283
+ case 'h':
284
+ minutes += scheduleValue * 60;
285
+ break;
286
+ case 'd':
287
+ minutes += scheduleValue * 24 * 60;
288
+ break;
289
+ }
290
+ }
291
+ return nearestSchedule(minutes);
292
+ }
293
+
294
+ // Find the nearest schedule that is supported by the platform
295
+ // from the parsed schedule value
296
+ function nearestSchedule(minutes) {
297
+ let nearest: typeof ALLOWED_SCHEDULES[number] = ALLOWED_SCHEDULES[0];
298
+ let prev = Math.abs(nearest - minutes);
299
+ for (let i = 1; i < ALLOWED_SCHEDULES.length; i++) {
300
+ const curr = Math.abs(ALLOWED_SCHEDULES[i] - minutes);
301
+ if (curr <= prev) {
302
+ nearest = ALLOWED_SCHEDULES[i];
303
+ prev = curr;
304
+ }
305
+ }
306
+ return nearest;
307
+ }
@@ -0,0 +1,66 @@
1
+ /**
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2020-present, Elastic NV
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ *
24
+ */
25
+
26
+ import { join } from 'path';
27
+ import * as esbuild from 'esbuild';
28
+
29
+ // Source of the package - /src, /dist, etc.
30
+ const SOURCE_DIR = join(__dirname, '..');
31
+
32
+ /**
33
+ * Avoid importing @elastic/synthetics package from source, this avoids
34
+ * bundling the synthetics package in tests or when resolved using
35
+ * absolute paths.
36
+ * ex: import {journey} from "../../"
37
+ */
38
+ const isLocalSynthetics = (resolvedPath: string) => {
39
+ return resolvedPath.startsWith(SOURCE_DIR);
40
+ };
41
+
42
+ /**
43
+ * Esbuild Plugin to create separate bundles for all the journeys.
44
+ * Treats journeys as the entry point and bundles all the dependencies
45
+ * including the node_modules and ignores bundling the synthetics package
46
+ * when imported externally or from source.
47
+ */
48
+ export function SyntheticsBundlePlugin(): esbuild.Plugin {
49
+ return {
50
+ name: 'synthetics-bundle-plugin',
51
+ setup(build) {
52
+ build.onResolve({ filter: /.*?/ }, async args => {
53
+ // Ignore entry points as these refer to the journey files
54
+ if (args.kind === 'entry-point') {
55
+ return;
56
+ }
57
+ const resolvedPath = join(args.resolveDir, args.path);
58
+ if (isLocalSynthetics(resolvedPath)) {
59
+ return { external: true };
60
+ }
61
+ // Allow esbuild to resolve the module
62
+ return;
63
+ });
64
+ },
65
+ };
66
+ }
@@ -0,0 +1,141 @@
1
+ /**
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2020-present, Elastic NV
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ *
24
+ */
25
+
26
+ import { bold, red, yellow } from 'kleur/colors';
27
+ import type { Dispatcher } from 'undici';
28
+ import { request } from 'undici';
29
+ import { indent, symbols } from '../helpers';
30
+
31
+ /* eslint-disable @typescript-eslint/no-var-requires */
32
+ const { version } = require('../../package.json');
33
+
34
+ export type APIRequestOptions = {
35
+ url: string;
36
+ method: Dispatcher.HttpMethod;
37
+ auth: string;
38
+ body?: string;
39
+ };
40
+
41
+ export async function sendRequest(options: APIRequestOptions) {
42
+ return await request(options.url, {
43
+ method: options.method,
44
+ body: options.body,
45
+ headers: {
46
+ authorization: `ApiKey ${options.auth}`,
47
+ 'content-type': 'application/json',
48
+ 'user-agent': `Elastic/Synthetics ${version}`,
49
+ 'kbn-xsrf': 'true',
50
+ },
51
+ headersTimeout: 60 * 1000,
52
+ });
53
+ }
54
+
55
+ export async function sendReqAndHandleError<T>(
56
+ options: APIRequestOptions
57
+ ): Promise<T> {
58
+ const { statusCode, body } = await sendRequest(options);
59
+ return await (await handleError(statusCode, options.url, body)).json();
60
+ }
61
+
62
+ // Handle bad status code errors from Kibana API and format the
63
+ // error message to be displayed to the user.
64
+ // returns the response stream if no error is found
65
+ export async function handleError(
66
+ statusCode: number,
67
+ url: string,
68
+ body: Dispatcher.ResponseData['body']
69
+ ): Promise<Dispatcher.ResponseData['body']> {
70
+ if (statusCode === 404) {
71
+ throw formatNotFoundError(url, await body.text());
72
+ } else if (!ok(statusCode)) {
73
+ let parsed: { error: string; message: string };
74
+ try {
75
+ parsed = await body.json();
76
+ } catch (e) {
77
+ throw formatAPIError(
78
+ statusCode,
79
+ 'unexpected non-JSON error',
80
+ await body.text()
81
+ );
82
+ }
83
+ throw formatAPIError(statusCode, parsed.error, parsed.message);
84
+ }
85
+
86
+ return body;
87
+ }
88
+
89
+ export function ok(statusCode: number) {
90
+ return statusCode >= 200 && statusCode <= 299;
91
+ }
92
+
93
+ export type APIMonitorError = {
94
+ id?: string;
95
+ reason: string;
96
+ details: string;
97
+ };
98
+
99
+ export function formatNotFoundError(url: string, message: string) {
100
+ return red(
101
+ bold(
102
+ `${symbols['failed']} Please check your kibana url: ${url} and try again - 404:${message}`
103
+ )
104
+ );
105
+ }
106
+
107
+ export function formatAPIError(
108
+ statuCode: number,
109
+ error: string,
110
+ message: string
111
+ ) {
112
+ let outer = bold(`${symbols['failed']} Error\n`);
113
+ let inner = bold(
114
+ `${symbols['failed']} monitor creation failed - ${statuCode}:${error}\n`
115
+ );
116
+ inner += indent(message, ' ');
117
+ outer += indent(inner);
118
+ return red(outer);
119
+ }
120
+
121
+ function formatMonitorError(errors: APIMonitorError[]) {
122
+ let outer = '';
123
+ for (const error of errors) {
124
+ const monitorId = error.id ? `: monitor(${error.id})` : '';
125
+ let inner = bold(`> ${error.reason}${monitorId}\n`);
126
+ inner += indent(error.details, ' ');
127
+ outer += indent(inner) + '\n';
128
+ outer += '\n';
129
+ }
130
+ return outer;
131
+ }
132
+
133
+ export function formatFailedMonitors(errors: APIMonitorError[]) {
134
+ const heading = bold(`${symbols['failed']} Error\n`);
135
+ return red(heading + formatMonitorError(errors));
136
+ }
137
+
138
+ export function formatStaleMonitors(errors: APIMonitorError[]) {
139
+ const heading = bold(`${symbols['warning']} Warnings\n`);
140
+ return yellow(heading + formatMonitorError(errors));
141
+ }