From 9c7d81bca20c97270e214f9274a91876019cee0b Mon Sep 17 00:00:00 2001 From: David Haifley Date: Fri, 9 Sep 2022 15:53:51 -0400 Subject: [PATCH 1/3] CIRC-9142: Remove call to graphite_translate from alerting backend --- CHANGELOG.md | 10 ++++ package.json | 2 +- pkg/irondb-backend.go | 107 ++++++++++++++---------------------------- src/plugin.json | 4 +- 4 files changed, 48 insertions(+), 75 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf9302c..3c1567a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,14 @@ +## 0.9.23 + + * Eliminates the need for a call to the graphite_translate IRONdb extension + in the alerting backend for Graphite style queries. + * Ensures that all CAQL queries issued via the API /caql endpoint by the + alerting backend contain a #min_period prefix if a min_period value has been + defined for the Grafana query object. + * Adds handling for the possibility of null or non-numeric data values being + returned to the alerting backend by its calls to the API /caql endpoint. + ## 0.9.19 * Updates dependencies to latest versions. diff --git a/package.json b/package.json index cf8efdd..594cf4a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "circonus-irondb-datasource", - "version": "0.9.22", + "version": "0.9.23", "description": "IRONdb Data Source for Grafana.", "scripts": { "build": "grafana-toolkit plugin:build", diff --git a/pkg/irondb-backend.go b/pkg/irondb-backend.go index 44b49f8..09d15a9 100644 --- a/pkg/irondb-backend.go +++ b/pkg/irondb-backend.go @@ -210,43 +210,11 @@ func (td *SampleDatasource) caqlQuery(ctx context.Context, q backend.DataQuery) return nil, fmt.Errorf("key query missing from CAQL query: %w", err) } - // If needed include min_period setting in the CAQL query. - if !strings.HasPrefix(query, "#min_period=") { - // Does the panel have a min_period set? - minPeriod, err := jsonp.GetString(q.JSON, "min_period") - if err != nil { - if err != jsonp.KeyPathNotFoundError { - log.DefaultLogger.Error("unable to parse CAQL query min_period", - "error", err) - return nil, fmt.Errorf("unable to parse CAQL query min_period: %w", err) - } - // Not set - } else { - if minPeriod != "" { - query = "#min_period=" + minPeriod + " " + query - } - } - } - log.DefaultLogger.Info("caqlQuery", "caql", query) return td.caqlAPI(ctx, q, query) } -// TranslateResponse values represent a response from the IRONdb graphite -// translator service -type TranslateResponse struct { - Input string `json:"input"` - CAQL string `json:"caql"` - Error string `json:"error"` -} - -// TranslateRequest values represent a request to the IRONdb graphite -// translator service -type TranslateRequest struct { - Query string `json:"q"` -} - func (td *SampleDatasource) graphiteQuery(ctx context.Context, q backend.DataQuery) (*backend.DataResponse, error) { query, err := jsonp.GetString(q.JSON, "query") if err != nil { @@ -255,55 +223,32 @@ func (td *SampleDatasource) graphiteQuery(ctx context.Context, q backend.DataQue return nil, fmt.Errorf("key query missing from graphite query: %w", err) } - // Convert the graphite query to CAQL using IRONdb graphite_translate. - graphiteURL := &url.URL{Path: "irondb/extension/lua/public/graphite_translate"} - query = strings.ReplaceAll(query, " ", "") - t := TranslateRequest{Query: query} - - reqBody, err := json.Marshal(t) - if err != nil { - log.DefaultLogger.Error("unable to marshal graphite translate request", - "error", err, "request", graphiteURL.String(), "body", string(reqBody)) - return nil, fmt.Errorf("unable to marshal graphite translate request: %w", err) - } - - respData, err := td.circ.Post(graphiteURL.String(), reqBody) - if err != nil { - log.DefaultLogger.Error("error returned from graphite translate", - "error", err, "request", graphiteURL.String(), "body", string(reqBody)) - return nil, fmt.Errorf("error returned from graphite translate: %w", err) + tagFilter, err := jsonp.GetString(q.JSON, "tagFilter") + if err != nil && err != jsonp.KeyPathNotFoundError { + log.DefaultLogger.Error("unable to get tagFilter for graphite query", + "error", err, "query", string(q.JSON)) + return nil, fmt.Errorf("unable to get tagFilter for graphite query: %w", err) } - var resp TranslateResponse - if err := json.Unmarshal(respData, &resp); err != nil { - log.DefaultLogger.Error("unable to unmarshal graphite translate response", - "error", err, "response", string(respData)) - return nil, fmt.Errorf("unable to unmarshal graphite translate response: %w", err) - } + // Convert the graphite query to an equivalent CAQL query. + caqlQ := "graphite:find('" + strings.ReplaceAll(query, " ", "") + "'" - if resp.CAQL == "" { - err := fmt.Errorf("unable to translate graphite query: null CAQL response") - log.DefaultLogger.Error(err.Error(), "error", err, "response", string(respData)) - return nil, err + if tagFilter != "" { + caqlQ += ",'" + tagFilter + "'" } - if resp.Error != "" { - err := fmt.Errorf(resp.Error) - log.DefaultLogger.Error("error translating graphite query", - "error", err, "response", string(respData)) - return nil, fmt.Errorf("error translating graphite query: %w", err) - } + caqlQ += ")" - log.DefaultLogger.Info("graphiteQuery", "graphite", query, "caql", resp.CAQL) + log.DefaultLogger.Info("graphiteQuery", "graphite", query, "caql", caqlQ) - return td.caqlAPI(ctx, q, resp.CAQL) + return td.caqlAPI(ctx, q, caqlQ) } type DF4Response struct { - Version string `json:"version"` - Data [][]float64 `json:"data"` - Meta []DF4Meta `json:"meta"` - Head DF4Head `json:"head"` + Version string `json:"version"` + Data [][]interface{} `json:"data"` + Meta []DF4Meta `json:"meta"` + Head DF4Head `json:"head"` } type DF4Head struct { @@ -319,6 +264,19 @@ type DF4Meta struct { } func (td *SampleDatasource) caqlAPI(_ context.Context, q backend.DataQuery, query string) (*backend.DataResponse, error) { + // If needed include min_period setting in the CAQL query. + if !strings.HasPrefix(query, "#min_period=") { + minPeriod, err := jsonp.GetString(q.JSON, "min_period") + if err != nil && err != jsonp.KeyPathNotFoundError { + log.DefaultLogger.Error("unable to parse CAQL query min_period", "error", err) + return nil, fmt.Errorf("unable to parse CAQL query min_period: %w", err) + } + + if minPeriod != "" { + query = "#min_period=" + minPeriod + " " + query + } + } + // https://docs.circonus.com/circonus/api/#/CAQL qp := url.Values{} qp.Set("query", query) @@ -371,8 +329,13 @@ func (td *SampleDatasource) caqlAPI(_ context.Context, q backend.DataQuery, quer ts := resp.Head.Start for _, sample := range resp.Data[id] { times = append(times, time.Unix(int64(ts), 0)) - values = append(values, sample) ts += resp.Head.Period + + if val, ok := sample.(float64); ok { + values = append(values, val) + } else { + values = append(values, 0) + } } tags := make(map[string]string) for _, tag := range meta.Tags { diff --git a/src/plugin.json b/src/plugin.json index 028ca1a..c56bc8f 100644 --- a/src/plugin.json +++ b/src/plugin.json @@ -21,8 +21,8 @@ { "name": "Project site", "url": "https://www.circonus.com/irondb/" }, { "name": "License", "url": "https://github.com/circonus-labs/circonusllhist/blob/master/LICENSE" } ], - "version": "0.9.22", - "updated": "2022-08-10" + "version": "0.9.23", + "updated": "2022-09-09" }, "dependencies": { From 411597a6895bc73af08a5ecb0221ecc643fd0b11 Mon Sep 17 00:00:00 2001 From: David Haifley Date: Tue, 20 Sep 2022 16:44:48 -0400 Subject: [PATCH 2/3] CIRC-9142: Update release version --- CHANGELOG.md | 2 +- package.json | 2 +- src/plugin.json | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c1567a..8ebc316 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ -## 0.9.23 +## 0.9.24 * Eliminates the need for a call to the graphite_translate IRONdb extension in the alerting backend for Graphite style queries. diff --git a/package.json b/package.json index 594cf4a..d282afc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "circonus-irondb-datasource", - "version": "0.9.23", + "version": "0.9.24", "description": "IRONdb Data Source for Grafana.", "scripts": { "build": "grafana-toolkit plugin:build", diff --git a/src/plugin.json b/src/plugin.json index c56bc8f..0740598 100644 --- a/src/plugin.json +++ b/src/plugin.json @@ -21,8 +21,8 @@ { "name": "Project site", "url": "https://www.circonus.com/irondb/" }, { "name": "License", "url": "https://github.com/circonus-labs/circonusllhist/blob/master/LICENSE" } ], - "version": "0.9.23", - "updated": "2022-09-09" + "version": "0.9.24", + "updated": "2022-09-20" }, "dependencies": { From 70b69cec63836bb9961432d369a671aa16babd8c Mon Sep 17 00:00:00 2001 From: David Haifley Date: Tue, 20 Sep 2022 16:48:18 -0400 Subject: [PATCH 3/3] CIRC-9142: Update plugin version --- dist/plugin.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/plugin.json b/dist/plugin.json index 74d2476..0740598 100644 --- a/dist/plugin.json +++ b/dist/plugin.json @@ -21,8 +21,8 @@ { "name": "Project site", "url": "https://www.circonus.com/irondb/" }, { "name": "License", "url": "https://github.com/circonus-labs/circonusllhist/blob/master/LICENSE" } ], - "version": "0.9.23", - "updated": "2022-09-15" + "version": "0.9.24", + "updated": "2022-09-20" }, "dependencies": {