-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[query] add list tags endpoint (#1565)
- Loading branch information
Showing
10 changed files
with
289 additions
and
0 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
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,89 @@ | ||
// Copyright (c) 2019 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package native | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/m3db/m3/src/query/api/v1/handler" | ||
"github.com/m3db/m3/src/query/api/v1/handler/prometheus" | ||
"github.com/m3db/m3/src/query/models" | ||
"github.com/m3db/m3/src/query/storage" | ||
"github.com/m3db/m3/src/query/util/logging" | ||
"github.com/m3db/m3/src/x/net/http" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
const ( | ||
// ListTagsURL is the url for listing tags. | ||
ListTagsURL = handler.RoutePrefixV1 + "/labels" | ||
) | ||
|
||
var ( | ||
// ListTagsHTTPMethods are the HTTP methods used with this resource. | ||
ListTagsHTTPMethods = []string{http.MethodGet, http.MethodPost} | ||
) | ||
|
||
// ListTagsHandler represents a handler for list tags endpoint. | ||
type ListTagsHandler struct { | ||
storage storage.Storage | ||
} | ||
|
||
// NewListTagsHandler returns a new instance of handler. | ||
func NewListTagsHandler( | ||
storage storage.Storage, | ||
) http.Handler { | ||
return &ListTagsHandler{ | ||
storage: storage, | ||
} | ||
} | ||
|
||
func (h *ListTagsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctx := context.WithValue(r.Context(), handler.HeaderKey, r.Header) | ||
logger := logging.WithContext(ctx) | ||
w.Header().Set("Content-Type", "application/json") | ||
|
||
query := &storage.CompleteTagsQuery{ | ||
CompleteNameOnly: true, | ||
TagMatchers: models.Matchers{{Type: models.MatchAll}}, | ||
|
||
// NB: necessarily spans entire possible query range. | ||
Start: time.Time{}, | ||
End: time.Now(), | ||
} | ||
|
||
opts := storage.NewFetchOptions() | ||
result, err := h.storage.CompleteTags(ctx, query, opts) | ||
if err != nil { | ||
logger.Error("unable to complete tags", zap.Error(err)) | ||
xhttp.Error(w, err, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if err = prometheus.RenderListTagResultsJSON(w, result); err != nil { | ||
logger.Error("unable to render results", zap.Error(err)) | ||
xhttp.Error(w, err, http.StatusBadRequest) | ||
return | ||
} | ||
} |
145 changes: 145 additions & 0 deletions
145
src/query/api/v1/handler/prometheus/native/list_tags_test.go
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,145 @@ | ||
// Copyright (c) 2019 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package native | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"math" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/m3db/m3/src/query/models" | ||
"github.com/m3db/m3/src/query/storage" | ||
"github.com/m3db/m3/src/query/util/logging" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type listTagsMatcher struct{} | ||
|
||
func (m *listTagsMatcher) String() string { return "list tags query" } | ||
func (m *listTagsMatcher) Matches(x interface{}) bool { | ||
q, ok := x.(*storage.CompleteTagsQuery) | ||
if !ok { | ||
return false | ||
} | ||
|
||
if !q.Start.Equal(time.Time{}) { | ||
return false | ||
} | ||
|
||
// NB: end time for the query should be roughly `Now` | ||
diff := q.End.Sub(time.Now()) | ||
absDiff := time.Duration(math.Abs(float64(diff))) | ||
if absDiff > time.Second { | ||
return false | ||
} | ||
|
||
if !q.CompleteNameOnly { | ||
return false | ||
} | ||
|
||
if len(q.FilterNameTags) != 0 { | ||
return false | ||
} | ||
|
||
if len(q.TagMatchers) != 1 { | ||
return false | ||
} | ||
|
||
return models.MatchAll == q.TagMatchers[0].Type | ||
} | ||
|
||
var _ gomock.Matcher = &listTagsMatcher{} | ||
|
||
func b(s string) []byte { return []byte(s) } | ||
|
||
func TestListTags(t *testing.T) { | ||
logging.InitWithCores(nil) | ||
|
||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
// setup storage and handler | ||
store := storage.NewMockStorage(ctrl) | ||
storeResult := &storage.CompleteTagsResult{ | ||
CompleteNameOnly: true, | ||
CompletedTags: []storage.CompletedTag{ | ||
{Name: b("bar")}, | ||
{Name: b("baz")}, | ||
{Name: b("foo")}, | ||
}, | ||
} | ||
|
||
handler := NewListTagsHandler(store) | ||
for _, method := range []string{"GET", "POST"} { | ||
store.EXPECT().CompleteTags(gomock.Any(), &listTagsMatcher{}, gomock.Any()). | ||
Return(storeResult, nil) | ||
|
||
req := httptest.NewRequest(method, "/labels", nil) | ||
w := httptest.NewRecorder() | ||
|
||
handler.ServeHTTP(w, req) | ||
body := w.Result().Body | ||
defer body.Close() | ||
|
||
r, err := ioutil.ReadAll(body) | ||
require.NoError(t, err) | ||
|
||
ex := `{"status":"success","data":["bar","baz","foo"]}` | ||
require.Equal(t, ex, string(r)) | ||
} | ||
} | ||
|
||
func TestListErrorTags(t *testing.T) { | ||
logging.InitWithCores(nil) | ||
|
||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
// setup storage and handler | ||
store := storage.NewMockStorage(ctrl) | ||
handler := NewListTagsHandler(store) | ||
|
||
for _, method := range []string{"GET", "POST"} { | ||
store.EXPECT().CompleteTags(gomock.Any(), &listTagsMatcher{}, gomock.Any()). | ||
Return(nil, errors.New("err")) | ||
|
||
req := httptest.NewRequest(method, "/labels", nil) | ||
w := httptest.NewRecorder() | ||
|
||
handler.ServeHTTP(w, req) | ||
body := w.Result().Body | ||
defer body.Close() | ||
|
||
r, err := ioutil.ReadAll(body) | ||
require.NoError(t, err) | ||
|
||
ex := `{"error":"err"}` | ||
// NB: error handler adds a newline to the output. | ||
ex = fmt.Sprintf("%s\n", ex) | ||
require.Equal(t, ex, string(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
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
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
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
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
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
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