Alerting: Configurable externalLabels for Loki state history (#62404)

* Add config option for external labels

* Remove redundant nilcheck
This commit is contained in:
Alexander Weaver 2023-01-30 14:24:45 -06:00 committed by GitHub
parent c8e7e7ccf1
commit b4682fe3cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 5 deletions

View File

@ -29,15 +29,17 @@ type remoteLokiClient interface {
} }
type RemoteLokiBackend struct { type RemoteLokiBackend struct {
client remoteLokiClient client remoteLokiClient
log log.Logger externalLabels map[string]string
log log.Logger
} }
func NewRemoteLokiBackend(cfg LokiConfig) *RemoteLokiBackend { func NewRemoteLokiBackend(cfg LokiConfig) *RemoteLokiBackend {
logger := log.New("ngalert.state.historian", "backend", "loki") logger := log.New("ngalert.state.historian", "backend", "loki")
return &RemoteLokiBackend{ return &RemoteLokiBackend{
client: newLokiClient(cfg, logger), client: newLokiClient(cfg, logger),
log: logger, externalLabels: cfg.ExternalLabels,
log: logger,
} }
} }
@ -62,7 +64,7 @@ func (h *RemoteLokiBackend) statesToStreams(rule history_model.RuleMeta, states
continue continue
} }
labels := removePrivateLabels(state.State.Labels) labels := h.addExternalLabels(removePrivateLabels(state.State.Labels))
labels[OrgIDLabel] = fmt.Sprint(rule.OrgID) labels[OrgIDLabel] = fmt.Sprint(rule.OrgID)
labels[RuleUIDLabel] = fmt.Sprint(rule.UID) labels[RuleUIDLabel] = fmt.Sprint(rule.UID)
labels[GroupLabel] = fmt.Sprint(rule.Group) labels[GroupLabel] = fmt.Sprint(rule.Group)
@ -124,6 +126,13 @@ func (h *RemoteLokiBackend) recordStreams(ctx context.Context, streams []stream,
return nil return nil
} }
func (h *RemoteLokiBackend) addExternalLabels(labels data.Labels) data.Labels {
for k, v := range h.externalLabels {
labels[k] = v
}
return labels
}
type lokiEntry struct { type lokiEntry struct {
SchemaVersion int `json:"schemaVersion"` SchemaVersion int `json:"schemaVersion"`
Previous string `json:"previous"` Previous string `json:"previous"`

View File

@ -20,6 +20,7 @@ type LokiConfig struct {
BasicAuthUser string BasicAuthUser string
BasicAuthPassword string BasicAuthPassword string
TenantID string TenantID string
ExternalLabels map[string]string
} }
type httpLokiClient struct { type httpLokiClient struct {

View File

@ -108,6 +108,7 @@ type UnifiedAlertingStateHistorySettings struct {
// if one of them is set. // if one of them is set.
LokiBasicAuthPassword string LokiBasicAuthPassword string
LokiBasicAuthUsername string LokiBasicAuthUsername string
ExternalLabels map[string]string
} }
// IsEnabled returns true if UnifiedAlertingSettings.Enabled is either nil or true. // IsEnabled returns true if UnifiedAlertingSettings.Enabled is either nil or true.
@ -317,6 +318,7 @@ func (cfg *Cfg) ReadUnifiedAlertingSettings(iniFile *ini.File) error {
uaCfg.ReservedLabels = uaCfgReservedLabels uaCfg.ReservedLabels = uaCfgReservedLabels
stateHistory := iniFile.Section("unified_alerting.state_history") stateHistory := iniFile.Section("unified_alerting.state_history")
stateHistoryLabels := iniFile.Section("unified_alerting.state_history.external_labels")
uaCfgStateHistory := UnifiedAlertingStateHistorySettings{ uaCfgStateHistory := UnifiedAlertingStateHistorySettings{
Enabled: stateHistory.Key("enabled").MustBool(stateHistoryDefaultEnabled), Enabled: stateHistory.Key("enabled").MustBool(stateHistoryDefaultEnabled),
Backend: stateHistory.Key("backend").MustString("annotations"), Backend: stateHistory.Key("backend").MustString("annotations"),
@ -324,6 +326,7 @@ func (cfg *Cfg) ReadUnifiedAlertingSettings(iniFile *ini.File) error {
LokiTenantID: stateHistory.Key("loki_tenant_id").MustString(""), LokiTenantID: stateHistory.Key("loki_tenant_id").MustString(""),
LokiBasicAuthUsername: stateHistory.Key("loki_basic_auth_username").MustString(""), LokiBasicAuthUsername: stateHistory.Key("loki_basic_auth_username").MustString(""),
LokiBasicAuthPassword: stateHistory.Key("loki_basic_auth_password").MustString(""), LokiBasicAuthPassword: stateHistory.Key("loki_basic_auth_password").MustString(""),
ExternalLabels: stateHistoryLabels.KeysHash(),
} }
uaCfg.StateHistory = uaCfgStateHistory uaCfg.StateHistory = uaCfgStateHistory