grafana/pkg/services/ngalert/remote/client/alertmanager_state.go
Santiago 73776f37eb
Alerting: Send state to the remote Alertmanager (#78538)
* Alerting: Introduce a Mimir client as part of the Remote Alertmanager

Mimir client that understands the new APIs developed for mimir. Very much a WIP still.

* more wip

* appease the linter

* more linting

* add more code

* get state from kvstore, encode, send

* send state to the remote Alertmanager, extract fullstate logic into its own function

* pass kvstore to remote.NewAlertmanager()

* refactor

* add fake kvstore to tests

* tests

* use FileStore to get state

* always log 'completed state upload'

* refactor compareRemoteConfig

* base64-encode the state in the file store

* export silences and nflog filenames, refactor

* log 'completed state/config upload...' regardless of outcome

* add values to the state store in tests

* address code review comments

* log error from filestore

---------

Co-authored-by: gotjosh <josue.abreu@gmail.com>
2023-11-29 12:49:39 +01:00

52 lines
1.1 KiB
Go

package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
const (
grafanaAlertmanagerStatePath = "/api/v1/grafana/state"
)
type UserGrafanaState struct {
State string `json:"state"`
}
func (mc *Mimir) GetGrafanaAlertmanagerState(ctx context.Context) (*UserGrafanaState, error) {
gs := &UserGrafanaState{}
response := successResponse{
Data: gs,
}
// nolint:bodyclose
// closed within `do`
_, err := mc.do(ctx, grafanaAlertmanagerStatePath, http.MethodGet, nil, &response)
if err != nil {
return nil, err
}
if response.Status != "success" {
return nil, fmt.Errorf("returned non-success `status` from the MimirAPI: %s", response.Status)
}
return gs, nil
}
func (mc *Mimir) CreateGrafanaAlertmanagerState(ctx context.Context, state string) error {
payload, err := json.Marshal(&UserGrafanaState{
State: state,
})
if err != nil {
return err
}
return mc.doOK(ctx, grafanaAlertmanagerStatePath, http.MethodPost, bytes.NewBuffer(payload))
}
func (mc *Mimir) DeleteGrafanaAlertmanagerState(ctx context.Context) error {
return mc.doOK(ctx, grafanaAlertmanagerStatePath, http.MethodDelete, nil)
}