Flux Alerta package (#2494)

* added flux alerta pkg, closes #2492

* duplicated alerta packages in cloud
pull/2497/head
Scott Anderson 2021-05-06 16:30:54 -06:00 committed by GitHub
parent 08c1c29586
commit e8907fa54a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 372 additions and 0 deletions

View File

@ -0,0 +1,22 @@
---
title: Flux Alerta package
list_title: Alerta package
description: >
The Flux Alerta package provides functions that send alerts to
[Alerta](https://alerta.io/).
menu:
influxdb_cloud_ref:
name: Alerta
parent: Contributed
weight: 202
influxdb/cloud/tags: [functions, alerta, package]
cascade:
append:
block: note
content: |
#### Package author and maintainer
**Github:** [@alespour](https://github.com/alespour), [@bonitoo-io](https://github.com/bonitoo-io)
**InfluxDB Slack:** [@Ales Pour](https://influxdata.com/slack)
---
{{< duplicate-oss >}}

View File

@ -0,0 +1,12 @@
---
title: alerta.alert() function
description: >
The `alerta.alert()` function sends an alert to Alerta.
menu:
influxdb_cloud_ref:
name: alerta.alert
parent: Alerta
weight: 202
---
{{< duplicate-oss >}}

View File

@ -0,0 +1,12 @@
---
title: alerta.endpoint() function
description: >
The `alerta.endpoint()` function sends alerts to Alerta using data from input rows.
menu:
influxdb_cloud_ref:
name: alerta.endpoint
parent: Alerta
weight: 202
---
{{< duplicate-oss >}}

View File

@ -0,0 +1,31 @@
---
title: Flux Alerta package
list_title: Alerta package
description: >
The Flux Alerta package provides functions that send alerts to
[Alerta](https://alerta.io/).
menu:
influxdb_2_0_ref:
name: Alerta
parent: Contributed
weight: 202
influxdb/v2.0/tags: [functions, alerta, package]
cascade:
append:
block: note
content: |
#### Package author and maintainer
**Github:** [@alespour](https://github.com/alespour), [@bonitoo-io](https://github.com/bonitoo-io)
**InfluxDB Slack:** [@Ales Pour](https://influxdata.com/slack)
---
The Flux Alerta package provides functions that send alerts to
[Alerta](https://alerta.io/).
Import the `contrib/bonitoo-io/alerta` package:
```js
import "contrib/bonitoo-io/alerta"
```
## Functions
{{< children type="functions" show="pages" >}}

View File

@ -0,0 +1,173 @@
---
title: alerta.alert() function
description: >
The `alerta.alert()` function sends an alert to Alerta.
menu:
influxdb_2_0_ref:
name: alerta.alert
parent: Alerta
weight: 202
---
The `alerta.alert()` function sends an alert to [Alerta](https://www.alerta.io/).
```js
import "contrib/bonitoo-io/alerta"
alerta.alert(
url: "https://alerta.io:8080/alert",
apiKey: "0Xx00xxXx00Xxx0x0X",
resource: "example-resource",
event: "Example event",
environment: "",
severity: "critical",
service: [],
group: "",
value: "",
text: "",
tags: [],
attributes: {},
origin: "InfluxDB",
type: "",
timestamp: now(),
)
```
## Parameters
### url
({{< req >}})
Alerta URL.
_**Data type:** String_
### apiKey
({{< req >}})
Alerta API key.
_**Data type:** String_
### resource
({{< req >}})
Resource associated with the alert.
_**Data type:** String_
### event
({{< req >}})
Event name.
_**Data type:** String_
### environment
Alert environment.
Default is `""`.
_**Data type:** String_
**Valid values:**
- `""`
- `"Production"`
- `"Development"`
### severity
({{< req >}})
Event severity.
See [Alerta severities](https://docs.alerta.io/en/latest/api/alert.html#alert-severities).
_**Data type:** String_
### service
List of affected services.
Default is `[]`.
_**Data type:** Array of strings_
### group
Alerta event group.
Default is `""`.
_**Data type:** String_
### value
Event value.
Default is `""`.
_**Data type:** String_
### text
Alert text description.
Default is `""`.
_**Data type:** String_
### tags
List of event tags.
Default is `[]`.
_**Data type:** Array of strings_
### attributes
({{< req >}})
Alert attributes.
_**Data type:** Record_
### origin
Alert origin.
Default is `"InfluxDB"`.
_**Data type:** String_
### type
Event type.
Default is `""`.
_**Data type:** String_
### timestamp
time alert was generated.
Default is `now()`.
_**Data type:** Time_
## Examples
##### Send the last reported value and status to Alerta
```js
import "contrib/bonitoo-io/alerta"
import "influxdata/influxdb/secrets"
apiKey = secrets.get(key: "ALERTA_API_KEY")
lastReported =
from(bucket: "example-bucket")
|> range(start: -1m)
|> filter(fn: (r) =>
r._measurement == "example-measurement" and
r._field == "level"
)
|> last()
|> findRecord(fn: (key) => true, idx: 0)
severity = if lastReported._value > 50 then "warning" else "ok"
alerta.alert(
url: "https://alerta.io:8080/alert",
apiKey: apiKey,
resource: "example-resource",
event: "Example event",
environment: "Production",
severity: severity,
service: ["example-service"],
group: "example-group",
value: string(v: lastReported._value),
text: "Service is ${severity}. The last reported value was ${string(v: lastReported._value)}.",
tags: ["ex1", "ex2"],
attributes: {},
origin: "InfluxDB",
type: "exampleAlertType",
timestamp: now(),
)
```

View File

@ -0,0 +1,122 @@
---
title: alerta.endpoint() function
description: >
The `alerta.endpoint()` function sends alerts to Alerta using data from input rows.
menu:
influxdb_2_0_ref:
name: alerta.endpoint
parent: Alerta
weight: 202
---
The `alerta.endpoint()` function sends alerts to [Alerta](https://alerta.io/)
using data from input rows.
_**Function type:** Output_
```js
import "contrib/bonitoo-io/alerta"
alerta.endpoint(
url: "https://alerta.io:8080/alert,
apiKey: "0Xx00xxXx00Xxx0x0X",
environment: "",
origin: "InfluxDB"
)
```
## Parameters
### url
({{< req >}})
Alerta URL.
_**Data type:** String_
### apiKey
({{< req >}})
Alerta API key.
_**Data type:** String_
### environment
Alert environment.
Default is `""`.
_**Data type:** String_
**Valid values:**
- `""`
- `"Production"`
- `"Development"`
### origin
Alert origin.
Default is `"InfluxDB"`.
_**Data type:** String_
## Usage
`alerta.endpoint` is a factory function that outputs another function.
The output function requires a `mapFn` parameter.
### mapFn
A function that builds the object used to generate the POST request.
Requires an `r` parameter.
_**Data type:** Function_
`mapFn` accepts a table row (`r`) and returns an object that must include the
following fields:
- `resource`
- `event`
- `severity`
- `service`
- `group`
- `value`
- `text`
- `tags`
- `attributes`
- `type`
- `timestamp`
_For more information, see [`alerta.alert()` parameters](/influxdb/v2.0/reference/flux/stdlib/contrib/alerta/alert/#parameters)._
## Examples
##### Send critical alerts to Alerta
```js
import "contrib/bonitoo-io/alerta"
import "influxdata/influxdb/secrets"
apiKey = secrets.get(key: "ALERTA_API_KEY")
endpoint = alerta.endpoint(
url: "https://alerta.io:8080/alert",
apiKey: apiKey,
environment: "Production",
origin: "InfluxDB"
)
crit_events = from(bucket: "example-bucket")
|> range(start: -1m)
|> filter(fn: (r) => r._measurement == "statuses" and status == "crit")
crit_events
|> endpoint(mapFn: (r) => {
return { r with
resource: "example-resource",
event: "example-event",
severity: "critical",
service: r.service,
group: "example-group",
value: r.status,
text: "Status is critical.",
tags: ["ex1", "ex2"],
attributes: {},
type: "exampleAlertType",
timestamp: now(),
}
})()
```