-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abort request if data is missing (not all Thanos stores are available) (
#89) * Abort request if not all Thanos stores are available We got missing data and did not see it since the default is just returning data that can be queried https://thanos.io/v0.28/components/query.md/#partial-response-strategy * Make partial response behaviour configurable
- Loading branch information
Showing
3 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package thanos | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
// PartialResponseRoundTripper adds a new RoundTripper to the chain that sets the partial_response query parameter to the value of Allow. | ||
type PartialResponseRoundTripper struct { | ||
http.RoundTripper | ||
Allow bool | ||
} | ||
|
||
// RoundTrip implements the RoundTripper interface. | ||
func (t *PartialResponseRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
q := req.URL.Query() | ||
q.Set("partial_response", strconv.FormatBool(t.Allow)) | ||
req.URL.RawQuery = q.Encode() | ||
return t.RoundTripper.RoundTrip(req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package thanos | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPartialResponseRoundTripper_X(t *testing.T) { | ||
testCases := []struct { | ||
url string | ||
allow bool | ||
}{ | ||
{ | ||
url: "https://thanos.io", | ||
allow: false, | ||
}, | ||
{ | ||
url: "https://thanos.io?testly=blub", | ||
allow: false, | ||
}, | ||
{ | ||
url: "https://thanos.io", | ||
allow: true, | ||
}, | ||
{ | ||
url: "https://thanos.io?testly=blub", | ||
allow: true, | ||
}, | ||
} | ||
for _, tC := range testCases { | ||
t.Run(fmt.Sprintf("allow %v, url %s", tC.allow, tC.url), func(t *testing.T) { | ||
origUrl, err := url.Parse(tC.url) | ||
require.NoError(t, err) | ||
|
||
rt := PartialResponseRoundTripper{ | ||
RoundTripper: roundTripFunc(func(r *http.Request) (*http.Response, error) { | ||
require.Contains(t, r.URL.RawQuery, `partial_response=`+strconv.FormatBool(tC.allow)) | ||
require.Contains(t, r.URL.RawQuery, origUrl.RawQuery) | ||
return nil, errors.New("not implemented") | ||
}), | ||
Allow: tC.allow, | ||
} | ||
|
||
_, _ = rt.RoundTrip(httptest.NewRequest("GET", tC.url, nil)) | ||
}) | ||
} | ||
} | ||
|
||
type roundTripFunc func(r *http.Request) (*http.Response, error) | ||
|
||
func (s roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { | ||
return s(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters