Repository: elastic/synthetics
Pull request #713 (2023-03-06 11:15:04)

add types for status alert
add types for status alert

status alert can be enabled/disable using 
```

    monitor.use({
      schedule: 10,
      alert: {
        status: {
          enabled: false,
        },
      },
    });
```

in lightweight:
```

heartbeat.monitors:
- type: http
  name: Todos Lightweight
  id: todos-lightweight
  enabled: true
  urls: "https://elastic.github.io/synthetics-demo/"
  schedule: '@every 3m'
  timeout: 16s
  alert.status.enabled: true
```

apmmachine: 
## :green_heart: Build Succeeded


<!-- 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-713/detail/PR-713/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-713/detail/PR-713/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-713/detail/PR-713/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-713/detail/PR-713/1//artifacts) [![preview](https://img.shields.io/badge/docs-preview-yellowgreen)](http://synthetics_713.docs-preview.app.elstc.co/diff)  [![preview](https://img.shields.io/badge/elastic-observability-blue)](https://ci-stats.elastic.co/app/apm/services/apm-ci/transactions/view?rangeFrom=2023-03-06T11:05:19.485Z&rangeTo=2023-03-06T11:25:19.485Z&transactionName=BUILD+apm-agent-rum%2Felastic-synthetics%2FPR-%7Bnumber%7D&transactionType=job&latencyAggregationType=avg&traceId=bb7eafb7e01433112fefd54f6e8fec5c&transactionId=107d8f9f7ba262b3)

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

#### Build stats




* Start Time: 2023-03-06T11:15:19.485+0000


* Duration: 14 min 28 sec




#### Test stats :test_tube:

| Test         | Results                         |
| ------------ | :-----------------------------: |
| Failed       | 0  |
| Passed       | 213  |
| Skipped      | 2 |
| Total        | 215   |


</p>
</details>

<!-- TEST RESULTS IF ANY-->


<!-- STEPS ERRORS IF ANY -->














## :robot: GitHub comments

<details><summary>Expand to view the GitHub comments</summary>
<p>

To re-run your PR in the CI, just comment with:



- `/test` : Re-trigger the build.




</p>
</details>

<!--COMMENT_GENERATED_WITH_ID_comment.id-->
dominiqueclarke: > Probably this is a bit of a different usage case, Do you foresee a future where we might want to disable individual alerts for monitor instead of just disabling them all which is what the PR is doing.

> So its of the type here it would be like alert: []Alerts for a given monitor. /cc @dominiqueclarke

I think we envision the type being an object representing the different alerts, rather than an array, which we can add to later. So let's say we added a tls alert, the type would be

```
alerts: {
   status: {
       enabled: boolean
   },
   tls: {
       enabled: boolean
   },
}
```



Patch:
diff --git a/.eslintignore b/.eslintignore
index d83b8ee..dd2d80f 100644
--- a/.eslintignore
+++ b/.eslintignore
@@ -3,3 +3,4 @@ examples
 templates
 __tests__/e2e/tmp/
 src/sdk/lh-trace-processor.ts
+test-projects/
diff --git a/package-lock.json b/package-lock.json
index 582d795..1bda120 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -52,7 +52,7 @@
         "unzipper": "^0.10.11"
       },
       "engines": {
-        "node": ">14.14.0"
+        "node": ">=18.12.0"
       }
     },
     "node_modules/@ampproject/remapping": {
diff --git a/src/dsl/monitor.ts b/src/dsl/monitor.ts
index 785c392..e60cfb5 100644
--- a/src/dsl/monitor.ts
+++ b/src/dsl/monitor.ts
@@ -55,6 +55,11 @@ export type MonitorConfig = {
   screenshot?: ScreenshotOptions;
   params?: Params;
   playwrightOptions?: PlaywrightOptions;
+  alert?: {
+    status: {
+      enabled: boolean;
+    };
+  };
 };
 
 type MonitorFilter = {
diff --git a/src/push/monitor.ts b/src/push/monitor.ts
index 750bd6f..dd71b6a 100644
--- a/src/push/monitor.ts
+++ b/src/push/monitor.ts
@@ -148,8 +148,9 @@ export async function createLightweightMonitors(
   options: PushOptions
 ) {
   const lwFiles = new Set<string>();
+  const ignore = /(node_modules|.github)/;
   await totalist(workDir, (rel, abs) => {
-    if (/.(yml|yaml)$/.test(rel)) {
+    if (!ignore.test(rel) && /.(yml|yaml)$/.test(rel)) {
       lwFiles.add(abs);
     }
   });
@@ -218,14 +219,30 @@ export function buildMonitorFromYaml(
     config['private_locations'] || options.privateLocations;
   delete config['private_locations'];
 
+  const alertConfig = parseAlertConfig(config);
+
   return new Monitor({
     locations: options.locations,
     ...config,
     privateLocations,
     schedule: schedule || options.schedule,
+    alert: alertConfig,
   });
 }
 
+export const parseAlertConfig = (config: MonitorConfig) => {
+  if (config['alert.status.enabled'] !== undefined) {
+    const value = config['alert.status.enabled'];
+    delete config['alert.status.enabled'];
+    return {
+      status: {
+        enabled: value,
+      },
+    };
+  }
+  return config.alert;
+};
+
 export function parseSchedule(schedule: string) {
   const EVERY_SYNTAX = '@every';
   if (!(schedule + '').startsWith(EVERY_SYNTAX)) {
diff --git a/templates/lightweight/heartbeat.yml b/templates/lightweight/heartbeat.yml
index 97b66a2..c1032c1 100644
--- a/templates/lightweight/heartbeat.yml
+++ b/templates/lightweight/heartbeat.yml
@@ -6,3 +6,4 @@ heartbeat.monitors:
   urls: "https://elastic.github.io/synthetics-demo/"
   schedule: '@every 3m'
   timeout: 16s
+  alert.status.enabled: true


Test patch:
diff --git a/__tests__/push/monitor.test.ts b/__tests__/push/monitor.test.ts
index 9fec189..5d2f374 100644
--- a/__tests__/push/monitor.test.ts
+++ b/__tests__/push/monitor.test.ts
@@ -30,6 +30,7 @@ import {
   buildMonitorSchema,
   createLightweightMonitors,
   diffMonitors,
+  parseAlertConfig,
   parseSchedule,
 } from '../../src/push/monitor';
 import { Server } from '../utils/server';
@@ -123,6 +124,16 @@ describe('Monitors', () => {
     expect(parseSchedule('@every 10h2m10s')).toBe(60);
   });
 
+  it('parse alert config option', async () => {
+    expect(parseAlertConfig({})).toBe(undefined);
+    expect(parseAlertConfig({ 'alert.status.enabled': true } as any)).toEqual({
+      status: { enabled: true },
+    });
+    expect(parseAlertConfig({ alert: { status: { enabled: true } } })).toEqual({
+      status: { enabled: true },
+    });
+  });
+
   describe('Lightweight monitors', () => {
     const PROJECT_DIR = generateTempPath();
     const HB_SOURCE = join(PROJECT_DIR, 'heartbeat.yml');