mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Fix attachment of external labels to Loki state history log streams (#65140)
Fix attachment of external labels, add tests
This commit is contained in:
parent
702ec59cc4
commit
07368dec74
@ -74,6 +74,7 @@ func NewLokiConfig(cfg setting.UnifiedAlertingStateHistorySettings) (LokiConfig,
|
|||||||
BasicAuthUser: cfg.LokiBasicAuthUsername,
|
BasicAuthUser: cfg.LokiBasicAuthUsername,
|
||||||
BasicAuthPassword: cfg.LokiBasicAuthPassword,
|
BasicAuthPassword: cfg.LokiBasicAuthPassword,
|
||||||
TenantID: cfg.LokiTenantID,
|
TenantID: cfg.LokiTenantID,
|
||||||
|
ExternalLabels: cfg.ExternalLabels,
|
||||||
// Snappy-compressed protobuf is the default, same goes for Promtail.
|
// Snappy-compressed protobuf is the default, same goes for Promtail.
|
||||||
Encoder: SnappyProtoEncoder{},
|
Encoder: SnappyProtoEncoder{},
|
||||||
}, nil
|
}, nil
|
||||||
|
@ -77,6 +77,18 @@ func TestLokiConfig(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("captures external labels", func(t *testing.T) {
|
||||||
|
set := setting.UnifiedAlertingStateHistorySettings{
|
||||||
|
LokiRemoteURL: "http://url.com",
|
||||||
|
ExternalLabels: map[string]string{"a": "b"},
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := NewLokiConfig(set)
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Contains(t, res.ExternalLabels, "a")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLokiHTTPClient(t *testing.T) {
|
func TestLokiHTTPClient(t *testing.T) {
|
||||||
|
@ -346,14 +346,32 @@ grafana_alerting_state_history_writes_total{org="1"} 2
|
|||||||
require.Contains(t, sent, "contains=equals")
|
require.Contains(t, sent, "contains=equals")
|
||||||
require.Contains(t, sent, "contains🤔emoji")
|
require.Contains(t, sent, "contains🤔emoji")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("adds external labels to log lines", func(t *testing.T) {
|
||||||
|
req := NewFakeRequester()
|
||||||
|
loki := createTestLokiBackend(req, metrics.NewHistorianMetrics(prometheus.NewRegistry()))
|
||||||
|
rule := createTestRule()
|
||||||
|
states := singleFromNormal(&state.State{
|
||||||
|
State: eval.Alerting,
|
||||||
|
})
|
||||||
|
|
||||||
|
err := <-loki.Record(context.Background(), rule, states)
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Contains(t, "/loki/api/v1/push", req.lastRequest.URL.Path)
|
||||||
|
sent := string(readBody(t, req.lastRequest))
|
||||||
|
require.Contains(t, sent, "externalLabelKey")
|
||||||
|
require.Contains(t, sent, "externalLabelValue")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTestLokiBackend(req client.Requester, met *metrics.Historian) *RemoteLokiBackend {
|
func createTestLokiBackend(req client.Requester, met *metrics.Historian) *RemoteLokiBackend {
|
||||||
url, _ := url.Parse("http://some.url")
|
url, _ := url.Parse("http://some.url")
|
||||||
cfg := LokiConfig{
|
cfg := LokiConfig{
|
||||||
WritePathURL: url,
|
WritePathURL: url,
|
||||||
ReadPathURL: url,
|
ReadPathURL: url,
|
||||||
Encoder: JsonEncoder{},
|
Encoder: JsonEncoder{},
|
||||||
|
ExternalLabels: map[string]string{"externalLabelKey": "externalLabelValue"},
|
||||||
}
|
}
|
||||||
return NewRemoteLokiBackend(cfg, req, met)
|
return NewRemoteLokiBackend(cfg, req, met)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user