Skip to content

Commit

Permalink
WIP: update dynamic CLI examples (#907)
Browse files Browse the repository at this point in the history
* WIP: update dynamic CLI examples

* WIP: update dynamic CLI examples

* WIP: update dynamic CLI examples

* WIP: update dynamic CLI examples

* update dynamic CLI examples
  • Loading branch information
ragog authored Oct 11, 2023
1 parent c402b72 commit a3c6826
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 59 deletions.
2 changes: 1 addition & 1 deletion site/content/docs/cli/cli-vs-terraform-pulumi.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: CLI vs. Terraform & Pulumi
weight: 8
weight: 9
menu:
platform:
parent: "CLI"
Expand Down
137 changes: 137 additions & 0 deletions site/content/docs/cli/dynamic-check-creation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
---
title: Dynamic check creation
weight: 6
menu:
platform:
parent: "CLI"
---

The Checkly CLI enables you to code your entire monitoring setup taking full advantage of the flexibility of TypeScript/JavaScript. Reusing language constructs that you are already familiar with, you will be able to create a MaC setup that neatly fits your unique use cases and workflows.

This page shows a few examples.

## Similar checks from a list of targets

Iterating through lists of target URLs is an easy way to manage checks at scale while avoiding code duplication.

```ts
// __checks__/api.check.ts
import { ApiCheck } from 'checkly/constructs'

const publicResources = ['/public-stats', '/v1/runtimes']

for (const publicResource of publicResources) {
new ApiCheck(`public-resource_${publicResource}`, {
name: `Public Resource ${publicResource}`,
request: {
url: `https://api.checkly.com${publicResource}`,
method: 'GET',
followRedirects: true,
skipSsl: false,
assertions: [ AssertionBuilder.statusCode().equals(200) ]
}
})
}
```

Asynchronous operations are supported by exporting an async function from your check files, too.

```ts
// __checks__/api.check.ts
import { ApiCheck } from 'checkly/constructs'
import { getPublicResources } from './helpers'

// an exported async function to signal that
// this check file performs asynchronous operations
export default async function createApiChecks() {
const publicResources = await getPublicResources();

for (const publicResource of publicResources) {
new ApiCheck(`public-resource_${publicResource}`, {
name: `Public Resource ${publicResource}`,
request: {
url: `https://api.checkly.com${publicResource}`,
method: 'GET',
followRedirects: true,
skipSsl: false,
assertions: [ AssertionBuilder.statusCode().equals(200) ]
}
})
}
}
```

## Separate groups for prod and pre-prod

Iterating through target environments (like `preview` and `production`) linked to `Group` resources allows you to reuse existing `Check` definitions.

```ts
// __checks__/browser.check.ts
import fs from 'fs';
import { BrowserCheck } from 'checkly/constructs';
import { groupProd, groupPreview } from './groups.check';
// This reads a directory and extracts all file paths containing '.spec.ts'
const files = fs.readdirSync('__checks__/');
const specFiles = files.filter((filename) => {
return filename.includes('.spec.ts');
});
// This is the list of environments and their matching group; it can be extended easily
const environments = [
{ name: 'preview', group: groupPreview },
{ name: 'production', group: groupProd },
];
// Here we create a new browser check for each environment x testspec combination
// Checks are added to the right groups - the group will set the right env variable for the target URL
environments.forEach((environment) => {
for (const specFile of specFiles) {
new BrowserCheck(`${specFile}${environment.name}`, {
name: `${specFile} [${environment.name}]`,
tags: [`${environment.name}`],
group: environment.group,
code: {
entrypoint: specFile,
},
});
}
});
```

You can handle potential differences between target environments via group-level environment variables, which are made available to all checks within a group.

```ts
// __checks__/groups.check.ts
import { CheckGroup } from 'checkly/constructs'
import { smsChannel, emailChannel } from '../alert-channels'
const alertChannels = [smsChannel, emailChannel]
export const groupPreview = new CheckGroup('group-browser-preview', {
name: 'WebShop - Preview',
activated: true,
muted: false,
runtimeId: '2023.09',
locations: ['us-east-1', 'eu-west-1'],
tags: ['mac', 'preview'],
// You can use group-level environment vars to point each group's checks to the right target URL
environmentVariables: [ { key: 'TARGET_URL', value: 'https://preview.mywebsite.com' }],
apiCheckDefaults: {},
concurrency: 100,
alertChannels
})

export const groupProd = new CheckGroup('group-browser-prod', {
name: 'WebShop - Production',
activated: true,
muted: false,
runtimeId: '2023.09',
locations: ['us-east-1', 'eu-west-1'],
tags: ['mac', 'production'],
// You can use group-level environment vars to point each group's checks to the right target URL
environmentVariables: [ { key: 'TARGET_URL', value: 'https://www.mywebsite.com' }],
apiCheckDefaults: {},
concurrency: 100,
alertChannels
})
```
2 changes: 1 addition & 1 deletion site/content/docs/cli/npm-packages.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Using NPM packages and local dependencies
weight: 7
weight: 8
menu:
platform:
parent: "CLI"
Expand Down
54 changes: 0 additions & 54 deletions site/content/docs/cli/project-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,57 +126,3 @@ const api = new ApiCheck('hello-api', {
```

Find a full reference of all check properties in [the `ApiCheck` construct](/docs/cli/constructs-reference/#apicheck) or [`BrowserCheck` construct section](/docs/cli/constructs-reference/#browsercheck).

## Dynamic and programmable check creation

The Checkly CLI enables you not only to define but also code your entire monitoring setup.

Use standard TypeScript/JavaScript, the file system or remote data to configure your Checkly project.


```ts
// __checks__/api.check.ts
import { ApiCheck } from 'checkly/constructs'

const publicResources = ['/public-stats', '/v1/runtimes']

for (const publicResource of publicResources) {
new ApiCheck(`public-resource_${publicResource}`, {
name: `Public Resource ${publicResource}`,
request: {
url: `https://api.checkly.com${publicResource}`,
method: 'GET',
followRedirects: true,
skipSsl: false,
assertions: [ AssertionBuilder.statusCode().equals(200) ]
}
})
}
```

Asynchronous operations are supported by exporting an async function from your check files, too.

```ts
// __checks__/api.check.ts
import { ApiCheck } from 'checkly/constructs'
import { getPublicResources } from './helpers'

// an exported async function to signal that
// this check file performs asynchronous operations
export default async function createApiChecks() {
const publicResources = await getPublicResources();

for (const publicResource of publicResources) {
new ApiCheck(`public-resource_${publicResource}`, {
name: `Public Resource ${publicResource}`,
request: {
url: `https://api.checkly.com${publicResource}`,
method: 'GET',
followRedirects: true,
skipSsl: false,
assertions: [ AssertionBuilder.statusCode().equals(200) ]
}
})
}
}
```
2 changes: 1 addition & 1 deletion site/content/docs/cli/using-constructs.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Using Constructs
weight: 3
weight: 4
menu:
platform:
parent: "CLI"
Expand Down
2 changes: 1 addition & 1 deletion site/content/docs/cli/using-environment-variables.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Using environment variables
weight: 6
weight: 5
menu:
platform:
parent: "CLI"
Expand Down
2 changes: 1 addition & 1 deletion site/content/docs/cli/using-git.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Using git metadata
weight: 7
weight: 8
menu:
platform:
parent: "CLI"
Expand Down

1 comment on commit a3c6826

@vercel
Copy link

@vercel vercel bot commented on a3c6826 Oct 11, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.