Skip to content

Commit

Permalink
Update QueryResponse type to parse errMessage and errType
Browse files Browse the repository at this point in the history
Ref: https://code.vmware.com/apis/714/wavefront-rest#/Query/queryApi.
The model for QueryResult here says there can be two optional string
types which represent errors:

*  errType
*  errMessage

I have frequently seen wavefront return statuses like 'Query Execution
Terminated', in errType fields. Without these types defined, there is
no way for clients to know the query execution failed on the server.
  • Loading branch information
Ankur Huralikoppi committed Nov 22, 2019
1 parent 0e31d81 commit b40d576
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
5 changes: 5 additions & 0 deletions fixtures/failed-query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"query": "ts(servers.load.load.longterm, source=server1.example.net)",
"errorType": "QueryExecutionFailed",
"errorMessage": "Terminating Query to save resources"
}
6 changes: 6 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ type QueryResponse struct {
Granularity int `json:"granularity"`
Hosts []string `json:"hostsUsed"`
Warnings string `json:"warnings"`

// ErrType : ref https://code.vmware.com/apis/714/wavefront-rest#/Query/queryApi
ErrType string `json:"errorType"`

// ErrMessage : ref https://code.vmware.com/apis/714/wavefront-rest#/Query/queryApi
ErrMessage string `json:"errorMessage"`
}

// DataPoint represents a single timestamp/value data point as returned
Expand Down
56 changes: 34 additions & 22 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ func TestQuery(t *testing.T) {
}
}

func TestQuery_SingleSeries(t *testing.T) {
func getQueryOutputFromFixture(fixture string) (*QueryResponse, error) {
baseurl, _ := url.Parse("http:https://testing.wavefront.com")
response, err := ioutil.ReadFile("./fixtures/single-series.json")
response, err := ioutil.ReadFile(fixture)
if err != nil {
t.Fatal(err)
return nil, err
}
q := &Query{
Params: NewQueryParams("ts(some.query)"),
Expand All @@ -87,7 +87,11 @@ func TestQuery_SingleSeries(t *testing.T) {
},
},
}
resp, err := q.Execute()
return q.Execute()
}

func TestQuery_SingleSeries(t *testing.T) {
resp, err := getQueryOutputFromFixture("./fixtures/single-series.json")
if err != nil {
t.Fatal("error executing query:", err)
}
Expand All @@ -108,24 +112,7 @@ func TestQuery_SingleSeries(t *testing.T) {
}

func TestQuery_MultiSeries(t *testing.T) {
baseurl, _ := url.Parse("http:https://testing.wavefront.com")
response, err := ioutil.ReadFile("./fixtures/multi-series.json")
if err != nil {
t.Fatal(err)
}
q := &Query{
Params: NewQueryParams("ts(some.query)"),
client: &MockWavefrontClient{
Response: response,
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
debug: true,
},
},
}
resp, err := q.Execute()
resp, err := getQueryOutputFromFixture("./fixtures/multi-series.json")
if err != nil {
t.Fatal("error executing query:", err)
}
Expand Down Expand Up @@ -161,3 +148,28 @@ func TestQuery_MultiSeries(t *testing.T) {
}

}

func TestQuery_Error(t *testing.T) {
resp, err := getQueryOutputFromFixture("./fixtures/failed-query.json")
if err != nil {
t.Fatal("error executing query:", err)
}

// must have the ErrType field populated if there is one
if resp.ErrType == "" {
t.Errorf("Expected to have ErrType field populated.")
}

if resp.ErrType != "QueryExecutionFailed" {
t.Errorf("Unexpected Error Type found.")
}

// must have the ErrType field populated if there is one
if resp.ErrMessage == "" {
t.Errorf("Expected to have ErrMessage field populated.")
}

if resp.ErrMessage != "Terminating Query to save resources" {
t.Errorf("Unexpected Error Message found.")
}
}

0 comments on commit b40d576

Please sign in to comment.