Like Prometheus, but for logs.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
loki/pkg/tool/client/alerts.go

76 lines
1.8 KiB

package client
import (
"context"
"io"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
const alertmanagerAPIPath = "/api/v1/alerts"
type configCompat struct {
TemplateFiles map[string]string `yaml:"template_files"`
AlertmanagerConfig string `yaml:"alertmanager_config"`
}
// CreateAlertmanagerConfig creates a new alertmanager config
func (r *LokiClient) CreateAlertmanagerConfig(ctx context.Context, cfg string, templates map[string]string) error {
payload, err := yaml.Marshal(&configCompat{
TemplateFiles: templates,
AlertmanagerConfig: cfg,
})
if err != nil {
return err
}
res, err := r.doRequest(ctx, alertmanagerAPIPath, "POST", payload)
if err != nil {
return err
}
res.Body.Close()
return nil
}
// DeleteAlermanagerConfig deletes the users alertmanagerconfig
func (r *LokiClient) DeleteAlermanagerConfig(ctx context.Context) error {
res, err := r.doRequest(ctx, alertmanagerAPIPath, "DELETE", nil)
if err != nil {
return err
}
res.Body.Close()
return nil
}
// GetAlertmanagerConfig retrieves a rule group
func (r *LokiClient) GetAlertmanagerConfig(ctx context.Context) (string, map[string]string, error) {
res, err := r.doRequest(ctx, alertmanagerAPIPath, "GET", nil)
if err != nil {
log.Debugln("no alert config present in response")
return "", nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return "", nil, err
}
compat := configCompat{}
err = yaml.Unmarshal(body, &compat)
if err != nil {
log.WithFields(log.Fields{
"body": string(body),
}).Debugln("failed to unmarshal rule group from response")
return "", nil, errors.Wrap(err, "unable to unmarshal response")
}
return compat.AlertmanagerConfig, compat.TemplateFiles, nil
}