Skip to content

Commit

Permalink
Merge pull request #134 from flanksource/feat/canary-checker-url-temp…
Browse files Browse the repository at this point in the history
…lating

docs: templating URL and body in HTTP check
  • Loading branch information
moshloop authored Jan 15, 2024
2 parents 05f67ef + a39126e commit be92c4e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 46 deletions.
119 changes: 73 additions & 46 deletions canary-checker/docs/reference/http.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
title: HTTP
---


# <Icon name="http"/> HTTP

This check performs queries on HTTP endpoints, and HTTP Namespaces to monitor their activity.

<!-- ![](/snippets/http.svg.png) -->

```yaml title=http-check.yaml
apiVersion: canaries.flanksource.com/v1
kind: Canary
Expand All @@ -24,48 +21,48 @@ spec:
maxSSLExpiry: 7
```
| Field | Description | Scheme | Required |
| ----- | ----------- | ------ | -------- |
| **`url`** | | *string* | Yes |
| `endpoint` | Deprecated, Use `url` instread | *string* | |
| `method` | HTTP Request method, default to `GET` | *string* | |
| `headers` | Header fields to be used in the query | [[]*EnvVar*](../../concepts/authentication/#envvar) | |
| `body` | Request Body Contents | *string* | |
| `templateBody` | If true the `body` field will be templated using go templates these [variables](#templateVariables) will be available | boo; | |
| `responseCodes` | Expected response codes for the HTTP Request. | *[]int* | |
| `responseContent` | Exact response content expected to be returned by the endpoint. | *string* | |
| `thresholdMillis` | Maximum duration in milliseconds for the HTTP request. It will fail the check if it takes longer. | *int* | |
| `maxSSLExpiry` | Maximum number of days until the SSL Certificate expires. | *int* | |
| **`name`** | Name of the check, must be unique within the canary | `string` | Yes |
| `description` | Description for the check | `string` | |
| `icon` | Icon for overwriting default icon on the dashboard | `string` | |
| `labels` | Labels for check | `map[string]string` | |
| `test` | Evaluate whether a check is healthy | [`Expression`](../concepts/health-evaluation) | |
| `display` | Expression to change the formatting of the display | [`Expression`](../concepts/display-formatting) | |
| `transform` | Transform data from a check into multiple individual checks | [`Expression`](../concepts/transforms) | |
| `metrics` | Metrics to export from | [`[]Metrics`](../concepts/metrics-exporter) | |
| **Connection** | | | |
| `connection` | Path of existing connection e.g. `connection://http/instance` <br/> Mutually exclusive with `username`, `password` <br/> <Commercial/> | <CommonLink to="connection" >*Connection*</CommonLink> | |
| `username` | Mutually exclusive with `connection` | <CommonLink to="authentication" anchor="envvar">*EnvVar*</CommonLink> | |
| `password` | Mutually exclusive with `connection` | <CommonLink to="authentication" anchor="envvar">*EnvVar*</CommonLink> | |
| **`url`** | HTTP URL, if a URL is specified on both the connection and check, the url on the check takes precedence. | *string* | Yes |
| `ntlm` | When true will do authentication using NTLM v1 protocol | *bool* | |
| `ntlmv2` | When true will do authentication using NTLM v2 protocol | *bool* | |
| Field | Description | Scheme | Required | Templatable |
| ----------------- | -------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------- | -------- | ----------- |
| **`url`** | | _string_ | `true` | `true` |
| `endpoint` | _(Deprecated)_ Use `url` instread | _string_ | | |
| `method` | HTTP Request method, default to `GET` | _string_ | | |
| `headers` | Header fields to be used in the query | [[]_EnvVar_](../../concepts/authentication/#envvar) | | |
| `body` | Request Body Contents | _string_ | | `true` |
| `templateBody` | _(Deprecated)_ If true the `body` field will be templated using go templates these [variables](#templateVariables) will be available | boo; | | |
| `responseCodes` | Expected response codes for the HTTP Request. | _[]int_ | | |
| `responseContent` | Exact response content expected to be returned by the endpoint. | _string_ | | |
| `thresholdMillis` | Maximum duration in milliseconds for the HTTP request. It will fail the check if it takes longer. | _int_ | | |
| `maxSSLExpiry` | Maximum number of days until the SSL Certificate expires. | _int_ | | |
| **`name`** | Name of the check, must be unique within the canary | `string` | `true` | |
| `description` | Description for the check | `string` | | |
| `icon` | Icon for overwriting default icon on the dashboard | `string` | | |
| `labels` | Labels for check | `map[string]string` | | |
| `test` | Evaluate whether a check is healthy | [`Expression`](../concepts/health-evaluation) | | |
| `display` | Expression to change the formatting of the display | [`Expression`](../concepts/display-formatting) | | |
| `transform` | Transform data from a check into multiple individual checks | [`Expression`](../concepts/transforms) | | |
| `metrics` | Metrics to export from | [`[]Metrics`](../concepts/metrics-exporter) | | |
| `env` | Specify env vars that can be accessed during templating of the URL and the body | [[]_EnvVar_](../../concepts/authentication/#envvar) | | |
| **Connection** | | | | |
| `connection` | Path of existing connection e.g. `connection://http/instance` <br/> Mutually exclusive with `username`, `password` <br/> <Commercial/> | <CommonLink to="connection" >_Connection_</CommonLink> | | |
| `username` | Mutually exclusive with `connection` | <CommonLink to="authentication" anchor="envvar">_EnvVar_</CommonLink> | | |
| `password` | Mutually exclusive with `connection` | <CommonLink to="authentication" anchor="envvar">_EnvVar_</CommonLink> | | |
| **`url`** | HTTP URL, if a URL is specified on both the connection and check, the url on the check takes precedence. | _string_ | `true` | |
| `ntlm` | When true will do authentication using NTLM v1 protocol | _bool_ | | |
| `ntlmv2` | When true will do authentication using NTLM v2 protocol | _bool_ | | |

### Result Variables

Result variables can be used in `test`, `display` and `transform` [expressions](../concepts/expressions)

| Name | Description | Scheme |
| --------- | ------------------------------------------------------------ | ------------------- |
| `code` | HTTP response code | *int* |
| `headers` | HTTP response headers | *map[string]string* |
| `elapsed` | HTTP Request duration | *time.Duration* |
| `sslAge` | Time until SSL certificate expires | *time.Duration* |
| `content` | HTTP Response body | string |
| Name | Description | Scheme |
| --------- | --------------------------------------------------------------------------- | ------------------- |
| `code` | HTTP response code | _int_ |
| `headers` | HTTP response headers | _map[string]string_ |
| `elapsed` | HTTP Request duration | _time.Duration_ |
| `sslAge` | Time until SSL certificate expires | _time.Duration_ |
| `content` | HTTP Response body | string |
| `json` | If `Content-Type=application/json` response body converted into JSON object | `JSON` |


The above canary (`http-check.yaml`) is functionally equivalent to `http-check-expr.yaml` below

```yaml title=http-check-expr.yaml
Expand All @@ -84,17 +81,47 @@ spec:

### Template Body Variables

| Name | Scheme |
| ----- | ------ |
| `metadata.name` | string |
| `metadata.metadata.namespace` | *string* |
| `metadata.labels` | *map[string]string* |
| Name | Scheme |
| ----------------------------- | ------------------- |
| `metadata.name` | string |
| `metadata.metadata.namespace` | _string_ |
| `metadata.labels` | _map[string]string_ |
| `{fields from []env}` | any |

Variables defined in `env` will be available to template with the name that's configured on the spec.
Eg: In the following spec, the vars `my_secret_path` and `my_secret_var`, defined in `env`, are available during templating.

```yaml title=http-check.yaml
apiVersion: canaries.flanksource.com/v1
kind: Canary
metadata:
name: http-check
spec:
interval: 30
http:
- name: http pass response 200 status code
endpoint: https://example.com/{{.my_secret_path}}
body: |
{
"secret": {{.my_secret_var}}
}
env:
- name: my_secret_var
valueFrom:
secretKeyRef:
name: the-secret
key: key
- name: my_secret_path
value: secret_path
```

**Read**: [Escaping variables](../scripting/gotemplate#escaping)

## Metrics

| **HTTP Check Metrics** | | |
| ------------------------------------------------------------ | ------- | --------------------------------------- |
| **HTTP Check Metrics** | | |
| ------------------------------------------------------------- | ------- | --------------------------------------- |
| `canary_check_http_response_status{status, statusClass, url}` | Counter | Response code counter for each endpoint |
| `canary_check_http_ssl_expiry{url}` | Guage | |
| `canary_check_http_ssl_expiry{url}` | Guage | |

Status class is one of `1xx`, `2xx`, `3xx`, `4xx`, `5xx`
15 changes: 15 additions & 0 deletions canary-checker/docs/scripting/gotemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ spec:
```
## Escaping
In case you might need to pass in a template variable without templating it, then you can put the template inside a string.
Example:
```
{{ .secret }}
```

To send `{{ .secret }}` as it is do this

```
{{`{{ .secret }}`}}
```

## Encoding

### Encode
Expand Down

0 comments on commit be92c4e

Please sign in to comment.