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
3 changed files with 18 additions and 5 deletions
+14 -5
View File
@@ -29,15 +29,17 @@ type remoteLokiClient interface {
}
type RemoteLokiBackend struct {
client remoteLokiClient
log log.Logger
client remoteLokiClient
externalLabels map[string]string
log log.Logger
}
func NewRemoteLokiBackend(cfg LokiConfig) *RemoteLokiBackend {
logger := log.New("ngalert.state.historian", "backend", "loki")
return &RemoteLokiBackend{
client: newLokiClient(cfg, logger),
log: logger,
client: newLokiClient(cfg, logger),
externalLabels: cfg.ExternalLabels,
log: logger,
}
}
@@ -62,7 +64,7 @@ func (h *RemoteLokiBackend) statesToStreams(rule history_model.RuleMeta, states
continue
}
labels := removePrivateLabels(state.State.Labels)
labels := h.addExternalLabels(removePrivateLabels(state.State.Labels))
labels[OrgIDLabel] = fmt.Sprint(rule.OrgID)
labels[RuleUIDLabel] = fmt.Sprint(rule.UID)
labels[GroupLabel] = fmt.Sprint(rule.Group)
@@ -124,6 +126,13 @@ func (h *RemoteLokiBackend) recordStreams(ctx context.Context, streams []stream,
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 {
SchemaVersion int `json:"schemaVersion"`
Previous string `json:"previous"`
@@ -20,6 +20,7 @@ type LokiConfig struct {
BasicAuthUser string
BasicAuthPassword string
TenantID string
ExternalLabels map[string]string
}
type httpLokiClient struct {