appoptics / appoptics-api-go

The official Go client for the AppOptics metrics API
https://docs.appoptics.com/api/
Apache License 2.0
4 stars 7 forks source link

Error creating alerts with a condition threshold of zero #69

Open solarchad opened 4 years ago

solarchad commented 4 years ago

When trying to create an alert with a condition that has a threshold value of "0" (zero), I'm seeing this response:

response status: 400 Bad Request
response body: {"errors":{"params":{"conditions":{"threshold":["is not present"]}}}}

Error creating AppOptics alert test-goalert.processingEvent.incorrect_format:

Here is the sample code I was testing with:

package main

import (
    "fmt"
    "os"

    "github.com/appoptics/appoptics-api-go"
)

func main() {
    appopticsURL := "https://api.appoptics.com/v1/"
    appopticsToken := os.Getenv("APPOPTICS_TOKEN")
    client := appoptics.NewClient(appopticsToken, appoptics.BaseURLClientOption(appopticsURL), appoptics.SetDebugMode())

    attributes := make(map[string]interface{})
    attributes["runbook_url"] = "https://google.com"

    alert := appoptics.AlertRequest{
        Name:        "test-goalert.processingEvent.incorrect_format",
        Description: "Managed by Terraform",
        Active:      true,
        Conditions: []*appoptics.AlertCondition{
            {
                Type:            "above",
                Threshold:       0,
                Duration:        60,
                MetricName:      "system.cpu.utilization",
                SummaryFunction: "sum",
                Tags: []*appoptics.Tag{
                    {
                        Name:    "environment",
                        Grouped: true,
                        Values: []string{
                            "test",
                        },
                    },
                    {
                        Name:    "event_status",
                        Grouped: true,
                        Values: []string{
                            "sqs message has incorrect format",
                        },
                    },
                },
            },
        },
        RearmSeconds: 10800,
        Attributes:   attributes,
    }

    alertResult, err := client.AlertsService().Create(&alert)

    if err != nil {
        fmt.Printf("Error creating AppOptics alert %s: %s", alert.Name, err)
    } else {
        fmt.Printf("[INFO] Created AppOptics alert: %s", alertResult.Name)
    }
}

I would expect for this alert to create successfully. Also, if there is an error, I would expect that error to be returned with the message:

Error creating AppOptics alert test-goalert.processingEvent.incorrect_format:

But there's nothing after the colon. I see the error in the response body because I have the debug mode on.

solarchad commented 4 years ago

Also, it seems like I can create an alert with a zero threshold using curl:

curl \                                                                                                                                         
                                                                 -u $APPOPTICS_TOKEN: \
                                                                 -X POST \
                                                                 -H "Content-Type: application/json" \
                                                                 -d '{
                                                                  "name":"production.web.frontend.response_time",
                                                                  "description":"Web Response Time",
                                                                  "conditions":[
                                                                     {
                                                                        "type":"above",
                                                                        "metric_name":"system.cpu.utilization",
                                                                        "threshold":0,
                                                                        "summary_function":"max",
                                                                        "tags":[
                                                                           {
                                                                              "name":"tag_name",
                                                                              "grouped":false,
                                                                              "values":[
                                                                                 "tag_value"
                                                                              ]
                                                                           }
                                                                        ]
                                                                     }
                                                                  ],
                                                                  "attributes": {
                                                                       "runbook_url": "http://myco.com/runbooks/response_time"
                                                                  },
                                                                  "active":true,
                                                                  "md":true
                                                               }' \
                                                               "https://api.appoptics.com/v1/alerts"