How to reference an existing endpoint in Flux

I have an endpoint that I want to use in multiple tasks. I’ve defined this endpoint in the alerting section of UI in InfluxDB 2.0 but the examples of creating tasks in Flux only show how to define the endpoint within each task.

Ideally, if I create 30 tasks each going to the same endpoint, I would like to define that endpoint once. And, more importantly, if I ever have to update it, I would like to update it once and for all tasks to use that updated endpoint.

Anyone got an example of how I can do this (eg by using the endpoint id)?

Hello @matthew-jones-bjss,
You can definitely use the same endpoint. How that endpoint is used is defined by the notification rule.
Here’s an example of a notification rule:

package main
//CPU Notification Rule 
import "influxdata/influxdb/monitor"
import "slack"
import "influxdata/influxdb/secrets"
import "experimental"
option task = {name: "CPU Notification Rule ", 
               every: 10m, 
               offset: 0s}
slack_endpoint = slack["endpoint"](url: "https:hooks.slack.com/services/xxx/xxx/xxx")
notification = {_notification_rule_id: "0758afea09061000",
                _notification_rule_name: "CPU Notification Rule ",
                _notification_endpoint_id: "0754bad181a5b000",
                _notification_endpoint_name: "My slack",}
statuses = monitor["from"](start: -10s, fn: (r) = r["_check_name"] == "CPU Check")
crit = statuses 
       |> filter(fn: (r) => r["_level"] == "crit")
all_statuses = crit 
               |> filter(fn: (r) => (r["_time"] >= experimental["subDuration"](from: now(), d: 10m)))

all_statuses 
|> monitor["notify"](data: notification, 
                     endpoint: slack_endpoint(mapFn: (r) = (
{channel: "", 
 text: "Notification Rule: ${ r._notification_rule_name } triggered by     check: ${ r._check_name }: ${ r._message }", 
 color: if r["_level"] == "crit" then "danger" else if r["_level"] == "warn" then "warning" else "good"})))

This is what’s generated by the UI. You query the data from the _monitoring bucket that is written there after using the monitor.check() function in your check. Of course you don’t have to do this yourself, you can create this notification rule task in the UI.

If you update your notification endpoint, your notification rule task will be updated as well. If you use a notification endpoint task (i.e. you don’t specify any tags when you create it) then it will alert on an all of your checks.

So TLDR if you create 30 tasks and specify that you want to send them to one endpoint you don’t have to worry about updating them.

You might find this blog useful:

The checks and notifications system is a little bit of a black box if you’re just using the UI.

1 Like