Prometheus: Add resource for suggestions that include scopes/adhoc filters (#94001)

Co-authored-by: Dominik Prokop <dominik.prokop@grafana.com>
Co-authored-by: Bogdan Matei <bogdan.matei@grafana.com>
This commit is contained in:
Kyle Brandt
2024-10-01 07:17:59 -04:00
committed by GitHub
co-authored by Dominik Prokop Bogdan Matei
parent a20ebbc8f8
commit 2a73b89374
7 changed files with 277 additions and 6 deletions
+52
View File
@@ -117,6 +117,19 @@ func TestService(t *testing.T) {
err := service.CallResource(context.Background(), req, sender)
require.NoError(t, err)
})
t.Run("suggest resource", func(t *testing.T) {
f := &fakeHTTPClientProvider{}
httpProvider := getMockPromTestSDKProvider(f)
l := backend.NewLoggerWith("logger", "test")
service := NewService(httpProvider, l, mockExtendTransportOptions)
req := mockSuggestResource()
sender := &fakeSender{}
err := service.CallResource(context.Background(), req, sender)
require.NoError(t, err)
require.Equal(t, `http://localhost:9090/api/v1/labels?end=2022-06-01T12%3A00%3A00Z&limit=10&match%5B%5D=go_cgo_go_to_c_calls_calls_total%7Bjob%3D~%22.%2B%22%7D&match%5B%5D=up%7Bjob%3D~%22.%2B%22%7D&start=2022-06-01T00%3A00%3A00Z`, f.Roundtripper.Req.URL.String())
})
}
func mockRequest() *backend.CallResourceRequest {
@@ -146,3 +159,42 @@ func mockRequest() *backend.CallResourceRequest {
Body: []byte("match%5B%5D: ALERTS\nstart: 1655271408\nend: 1655293008"),
}
}
func mockSuggestResource() *backend.CallResourceRequest {
return &backend.CallResourceRequest{
PluginContext: backend.PluginContext{
OrgID: 0,
PluginID: "prometheus",
User: nil,
AppInstanceSettings: nil,
DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{
ID: 0,
UID: "",
Type: "prometheus",
Name: "test-prom",
URL: "http://localhost:9090",
User: "",
Database: "",
BasicAuthEnabled: true,
BasicAuthUser: "admin",
Updated: time.Time{},
JSONData: []byte("{}"),
},
},
Path: "suggestions",
URL: "suggestions",
Method: http.MethodPost,
Body: []byte(`
{
"queries": ["up + 1", "go_cgo_go_to_c_calls_calls_total + 2"],
"scopes": [{
"key": "job",
"value": ".+",
"operator": "regex-match"
}],
"start": "2022-06-01T00:00:00Z",
"end": "2022-06-01T12:00:00Z",
"limit": 10
}`),
}
}