57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package alert_manager
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/gofiber/fiber/v2"
|
|
"time"
|
|
)
|
|
|
|
type Alert struct {
|
|
StartsAT time.Time `json:"startsAt"`
|
|
EndsAT time.Time `json:"endsAt"`
|
|
Annotations map[string]string `json:"annotations"`
|
|
Labels map[string]string `json:"labels"`
|
|
GeneratorURL string `json:"generatorURL"`
|
|
}
|
|
|
|
type AlertManagerCfg struct {
|
|
AlertManagerHttpURL string `env:"ALERT_MANAGER_HTTP_URL"`
|
|
}
|
|
|
|
type Client struct {
|
|
baseURL string
|
|
httpClient *fiber.Client
|
|
}
|
|
|
|
func NewClient(cfg AlertManagerCfg) *Client {
|
|
httpClient := fiber.AcquireClient()
|
|
return &Client{
|
|
baseURL: cfg.AlertManagerHttpURL,
|
|
httpClient: httpClient,
|
|
}
|
|
}
|
|
|
|
func (c *Client) SendAlerts(alerts []Alert) error {
|
|
url := c.baseURL + "/api/v2/alerts"
|
|
|
|
requestBody, err := json.Marshal(alerts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
agent := c.httpClient.Post(url)
|
|
agent.Set("Content-Type", "application/json").Body(requestBody)
|
|
|
|
statusCode, respBody, errs := agent.Bytes()
|
|
if len(errs) > 0 {
|
|
return fmt.Errorf("%v", errs)
|
|
}
|
|
|
|
if statusCode != fiber.StatusOK {
|
|
return fmt.Errorf("invalid status code from alert manager: %d, response body:%s", statusCode, string(respBody))
|
|
}
|
|
|
|
return nil
|
|
}
|