bbab62ce39
The remote write path differs based on whether the data source is actually Prometheus, Mimir, Cortex, or an older version of Cortex. We do not want users to have to specify the path, so this change determines the path as best it can. It may be in the future we have to make this configurable per-datasource to cater for setups where it's impossible to determine the correct path.
85 lines
1.7 KiB
Go
85 lines
1.7 KiB
Go
package writer
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const RemoteWriteEndpoint = "/api/v1/write"
|
|
|
|
type TestRemoteWriteTarget struct {
|
|
srv *httptest.Server
|
|
|
|
mtx sync.Mutex
|
|
RequestsCount int
|
|
LastRequestBody string
|
|
|
|
ExpectedPath string
|
|
}
|
|
|
|
func NewTestRemoteWriteTarget(t *testing.T) *TestRemoteWriteTarget {
|
|
t.Helper()
|
|
|
|
target := &TestRemoteWriteTarget{
|
|
RequestsCount: 0,
|
|
LastRequestBody: "",
|
|
ExpectedPath: RemoteWriteEndpoint,
|
|
}
|
|
|
|
handler := func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != target.ExpectedPath {
|
|
require.Fail(t, "Received unexpected request for endpoint %s", r.URL.Path)
|
|
}
|
|
|
|
target.mtx.Lock()
|
|
defer target.mtx.Unlock()
|
|
target.RequestsCount += 1
|
|
bd, err := io.ReadAll(r.Body)
|
|
defer func() {
|
|
_ = r.Body.Close()
|
|
}()
|
|
require.NoError(t, err)
|
|
target.LastRequestBody = string(bd)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
_, err = w.Write([]byte(`{}`))
|
|
require.NoError(t, err)
|
|
}
|
|
server := httptest.NewServer(http.HandlerFunc(handler))
|
|
target.srv = server
|
|
|
|
return target
|
|
}
|
|
|
|
func (s *TestRemoteWriteTarget) Close() {
|
|
s.srv.Close()
|
|
}
|
|
|
|
func (s *TestRemoteWriteTarget) DatasourceURL() string {
|
|
return s.srv.URL
|
|
}
|
|
|
|
func (s *TestRemoteWriteTarget) ClientSettings() setting.RecordingRuleSettings {
|
|
return setting.RecordingRuleSettings{
|
|
URL: s.srv.URL + RemoteWriteEndpoint,
|
|
Timeout: 1 * time.Second,
|
|
BasicAuthUsername: "",
|
|
BasicAuthPassword: "",
|
|
}
|
|
}
|
|
|
|
// Reset resets all tracked requests and counters.
|
|
func (s *TestRemoteWriteTarget) Reset() {
|
|
s.mtx.Lock()
|
|
defer s.mtx.Unlock()
|
|
s.RequestsCount = 0
|
|
s.LastRequestBody = ""
|
|
}
|