I’m running into trouble creating an emal alert using sendgrid using the example from documentation
import "http"
// Import the Secrets package if you store your API key as a secret.
// For detail on how to do this, see Step 4 above.
import "influxdata/influxdb/secrets"
// Retrieve the secret if applicable. Otherwise, skip this line
// and add the API key as the Bearer token in the Authorization header.
SENDGRID_APIKEY = secrets.get(key: "SENDGRID_APIKEY")
numberOfCrits = from(bucket: "_monitoring")
|> range(start: -task.every)
|> filter(fn: (r) => r.measurement == "statuses" and r.level == "crit")
|> count()
numberOfCrits
|> map(fn: (r) => (if r._value > 3 then {
r with _value: http.post(
url: "https://api.sendgrid.com/v3/mail/send",
headers: {"Content-Type": "application/json", Authorization: "Bearer ${SENDGRID_APIKEY}"},
data: bytes(v: "{
\"personalizations\": [{
\"to\": [{
\"email\": \”jane.doe@example.com\"}],
\"subject\": \”InfluxData critical alert\"
}],
\"from\": {\"email\": \"john.doe@example.com\"},
\"content\": [{
\"type\": \"text/plain\",
\"value\": \”Example alert text\"
}]
}\""))} else {r with _value: 0}))
I stored my sendcloud API key using the CLI for my organization.
Even if I change the email in the script above, when I’m trying to save the script I get the following error
Failed to create new task: invalid AST: loc 0:0-0:0: unknown operator "<INVALID_OP>
I just went through all the email examples in the docs, and most of them had syntax issues. I have a PR that fixes them. Try this:
import "http"
// Import the Secrets package if you store your API key as a secret.
// For detail on how to do this, see Step 4 above.
import "influxdata/influxdb/secrets"
// Retrieve the secret if applicable. Otherwise, skip this line
// and add the API key as the Bearer token in the Authorization header.
SENDGRID_APIKEY = secrets.get(key: "SENDGRID_APIKEY")
numberOfCrits = from(bucket: "_monitoring")
|> range(start: -task.every)
|> filter(fn: (r) => r.measurement == "statuses" and r.level == "crit")
|> count()
numberOfCrits
|> map(fn: (r) => (if r._value > 3 then {
r with _value: http.post(
url: "https://api.sendgrid.com/v3/mail/send",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer ${SENDGRID_APIKEY}"
},
data: bytes(v: "{
\"personalizations\": [
{
\"to\": [
{
\"email\": \"jane.doe@example.com\"
}
]
}
],
\"from\": {
\"email\": \"john.doe@example.com\"
},
\"subject\": \"InfluxDB critical alert\",
\"content\": [
{
\"type\": \"text/plain\",
\"value\": \"There have been ${string(v: r._value)} critical statuses.\"
}
]
}"))} else {r with _value: 0}))
Did anybody use sendgrid so far?
I dont get an error, when running the task but I don’t get an email also.
I changed the r._value > 3 to r._value >=1 so every crit alert should be reportet.
The API Key I’m using has full access and works when I test it via sendgrid API documentation website.
Thaks!
I don’t know if http.post() has any response error code handling. I’m wondering if there’s an error in the payload. Can you try a manual API call (outside of Flux) using the same request body to see if it returns an error?