From 506214957a2737962a0a5233d28c5a49fe5ee59f Mon Sep 17 00:00:00 2001 From: "Ryan J. Geyer" Date: Fri, 4 Feb 2022 11:31:16 -0800 Subject: [PATCH] Update grafana-tools/sdk dependency to suppor datasources as a map --- go.mod | 4 ++ go.sum | 2 + vendor/github.com/grafana-tools/sdk/board.go | 36 +++++------ vendor/github.com/grafana-tools/sdk/panel.go | 61 +++++++++++++++---- .../grafana-tools/sdk/rest-request.go | 17 +++--- vendor/modules.txt | 3 +- 6 files changed, 84 insertions(+), 39 deletions(-) diff --git a/go.mod b/go.mod index b23bb3d4b..35ee6b93f 100644 --- a/go.mod +++ b/go.mod @@ -78,3 +78,7 @@ replace github.com/bradfitz/gomemcache => github.com/themihai/gomemcache v0.0.0- // Required for Alertmanager replace github.com/hashicorp/consul => github.com/hashicorp/consul v1.8.1 + +// Grafana SDK override. This can be removed when the SDK natively supports a dashboard -> panel -> datasource which is +// a map, rather than a string. https://github.com/grafana-tools/sdk/pull/190 +replace github.com/grafana-tools/sdk => github.com/csmarchbanks/sdk v0.0.0-20220120205302-870d00a83f4e \ No newline at end of file diff --git a/go.sum b/go.sum index 8c57923a5..9ddb123e6 100644 --- a/go.sum +++ b/go.sum @@ -594,6 +594,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/csmarchbanks/sdk v0.0.0-20220120205302-870d00a83f4e h1:FjcnI9tmUibKj0UH9P761WwkEhAWUAinuuKVI2SyEB4= +github.com/csmarchbanks/sdk v0.0.0-20220120205302-870d00a83f4e/go.mod h1:AHHlOEv1+GGQ3ktHMlhuTUwo3zljV3QJbC0+8o2kn+4= github.com/cucumber/godog v0.8.1/go.mod h1:vSh3r/lM+psC1BPXvdkSEuNjmXfpVqrMGYAElF6hxnA= github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4= github.com/cyphar/filepath-securejoin v0.2.2/go.mod h1:FpkQEhXnPnOthhzymB7CGsFk2G9VLXONKD9G7QGMM+4= diff --git a/vendor/github.com/grafana-tools/sdk/board.go b/vendor/github.com/grafana-tools/sdk/board.go index da8edf1fe..9b39e97f1 100644 --- a/vendor/github.com/grafana-tools/sdk/board.go +++ b/vendor/github.com/grafana-tools/sdk/board.go @@ -83,7 +83,7 @@ type ( Type string `json:"type"` Auto bool `json:"auto,omitempty"` AutoCount *int `json:"auto_count,omitempty"` - Datasource *string `json:"datasource"` + Datasource interface{} `json:"datasource"` Refresh BoolInt `json:"refresh"` Options []Option `json:"options"` IncludeAll bool `json:"includeAll"` @@ -111,23 +111,23 @@ type ( Value interface{} `json:"value"` // TODO select more precise type } Annotation struct { - Name string `json:"name"` - Datasource *string `json:"datasource"` - ShowLine bool `json:"showLine"` - IconColor string `json:"iconColor"` - LineColor string `json:"lineColor"` - IconSize uint `json:"iconSize"` - Enable bool `json:"enable"` - Query string `json:"query"` - Expr string `json:"expr"` - Step string `json:"step"` - TextField string `json:"textField"` - TextFormat string `json:"textFormat"` - TitleFormat string `json:"titleFormat"` - TagsField string `json:"tagsField"` - Tags []string `json:"tags"` - TagKeys string `json:"tagKeys"` - Type string `json:"type"` + Name string `json:"name"` + Datasource interface{} `json:"datasource"` + ShowLine bool `json:"showLine"` + IconColor string `json:"iconColor"` + LineColor string `json:"lineColor"` + IconSize uint `json:"iconSize"` + Enable bool `json:"enable"` + Query string `json:"query"` + Expr string `json:"expr"` + Step string `json:"step"` + TextField string `json:"textField"` + TextFormat string `json:"textFormat"` + TitleFormat string `json:"titleFormat"` + TagsField string `json:"tagsField"` + Tags []string `json:"tags"` + TagKeys string `json:"tagKeys"` + Type string `json:"type"` } // Link represents link to another dashboard or external weblink Link struct { diff --git a/vendor/github.com/grafana-tools/sdk/panel.go b/vendor/github.com/grafana-tools/sdk/panel.go index 9a7d201fa..95fdb9a36 100644 --- a/vendor/github.com/grafana-tools/sdk/panel.go +++ b/vendor/github.com/grafana-tools/sdk/panel.go @@ -20,6 +20,7 @@ package sdk */ import ( + "bytes" "encoding/json" "errors" ) @@ -63,9 +64,9 @@ type ( } panelType int8 CommonPanel struct { - Datasource *string `json:"datasource,omitempty"` // metrics - Editable bool `json:"editable"` - Error bool `json:"error"` + Datasource interface{} `json:"datasource,omitempty"` // metrics + Editable bool `json:"editable"` + Error bool `json:"error"` GridPos struct { H *int `json:"h,omitempty"` W *int `json:"w,omitempty"` @@ -133,7 +134,7 @@ type ( Bars bool `json:"bars"` DashLength *uint `json:"dashLength,omitempty"` Dashes *bool `json:"dashes,omitempty"` - Decimals *int `json:"decimals,omitempty"` + Decimals *int `json:"decimals,omitempty"` Fill int `json:"fill"` // Grid grid `json:"grid"` obsoleted in 4.1 by xaxis and yaxis @@ -160,6 +161,7 @@ type ( YFormats []string `json:"y_formats,omitempty"` Xaxis Axis `json:"xaxis"` // was added in Grafana 4.x? Yaxes []Axis `json:"yaxes"` // was added in Grafana 4.x? + FieldConfig *FieldConfig `json:"fieldConfig,omitempty"` } FieldConfig struct { Defaults struct { @@ -171,6 +173,7 @@ type ( Value string `json:"value"` } `json:"steps"` } `json:"threshold"` + Links []Link `json:"links,omitempty"` } `json:"defaults"` } Options struct { @@ -442,7 +445,7 @@ type ( Type string `json:"type"` ColorMode *string `json:"colorMode,omitempty"` Colors *[]string `json:"colors,omitempty"` - Decimals *int `json:"decimals,omitempty"` + Decimals *int `json:"decimals,omitempty"` Thresholds *[]string `json:"thresholds,omitempty"` Unit *string `json:"unit,omitempty"` MappingType int `json:"mappingType,omitempty"` @@ -480,9 +483,9 @@ type ( // for an any panel type Target struct { - RefID string `json:"refId"` - Datasource string `json:"datasource,omitempty"` - Hide bool `json:"hide,omitempty"` + RefID string `json:"refId"` + Datasource interface{} `json:"datasource,omitempty"` + Hide bool `json:"hide,omitempty"` // For PostgreSQL Table string `json:"table,omitempty"` @@ -563,7 +566,12 @@ type Target struct { CrossSeriesReducer string `json:"crossSeriesReducer,omitempty"` PerSeriesAligner string `json:"perSeriesAligner,omitempty"` ValueType string `json:"valueType,omitempty"` - GroupBys []string `json:"groupBys,omitempty"` + GroupBy []string `json:"groupBy,omitempty"` + Tags []struct { + Key string `json:"key,omitempty"` + Operator string `json:"operator,omitempty"` + Value string `json:"value,omitempty"` + } `json:"tags,omitempty"` } // StackdriverAlignOptions defines the list of alignment options shown in @@ -1053,15 +1061,42 @@ func (p *Panel) MarshalJSON() ([]byte, error) { }{p.CommonPanel, *p.HeatmapPanel} return json.Marshal(outHeatmap) case CustomType: - var outCustom = struct { - CommonPanel - CustomPanel - }{p.CommonPanel, *p.CustomPanel} + var outCustom = customPanelOutput{ + p.CommonPanel, + *p.CustomPanel, + } return json.Marshal(outCustom) } return nil, errors.New("can't marshal unknown panel type") } +type customPanelOutput struct { + CommonPanel + CustomPanel +} + +func (c customPanelOutput) MarshalJSON() ([]byte, error) { + b, err := json.Marshal(c.CommonPanel) + if err != nil { + return b, err + } + // Append custom keys to marshalled CommonPanel. + buf := bytes.NewBuffer(b[:len(b)-1]) + + for k, v := range c.CustomPanel { + buf.WriteString(`,"`) + buf.WriteString(k) + buf.WriteString(`":`) + b, err := json.Marshal(v) + if err != nil { + return b, err + } + buf.Write(b) + } + buf.WriteString("}") + return buf.Bytes(), nil +} + func incRefID(refID string) string { firstLetter := refID[0] ordinal := int(firstLetter) diff --git a/vendor/github.com/grafana-tools/sdk/rest-request.go b/vendor/github.com/grafana-tools/sdk/rest-request.go index aaf6fd469..8601affbe 100644 --- a/vendor/github.com/grafana-tools/sdk/rest-request.go +++ b/vendor/github.com/grafana-tools/sdk/rest-request.go @@ -57,7 +57,7 @@ type StatusMessage struct { // NewClient initializes client for interacting with an instance of Grafana server; // apiKeyOrBasicAuth accepts either 'username:password' basic authentication credentials, -// or a Grafana API key +// or a Grafana API key. If it is an empty string then no authentication is used. func NewClient(apiURL, apiKeyOrBasicAuth string, client *http.Client) (*Client, error) { key := "" basicAuth := strings.Contains(apiKeyOrBasicAuth, ":") @@ -65,12 +65,15 @@ func NewClient(apiURL, apiKeyOrBasicAuth string, client *http.Client) (*Client, if err != nil { return nil, err } - if !basicAuth { - key = fmt.Sprintf("Bearer %s", apiKeyOrBasicAuth) - } else { - parts := strings.Split(apiKeyOrBasicAuth, ":") - baseURL.User = url.UserPassword(parts[0], parts[1]) + if len(apiKeyOrBasicAuth) > 0 { + if !basicAuth { + key = fmt.Sprintf("Bearer %s", apiKeyOrBasicAuth) + } else { + parts := strings.Split(apiKeyOrBasicAuth, ":") + baseURL.User = url.UserPassword(parts[0], parts[1]) + } } + return &Client{baseURL: baseURL.String(), basicAuth: basicAuth, key: key, client: client}, nil } @@ -105,7 +108,7 @@ func (r *Client) doRequest(ctx context.Context, method, query string, params url return nil, 0, err } req = req.WithContext(ctx) - if !r.basicAuth { + if !r.basicAuth && len(r.key) > 0 { req.Header.Set("Authorization", r.key) } req.Header.Set("Accept", "application/json") diff --git a/vendor/modules.txt b/vendor/modules.txt index 4778e3815..e55173203 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -455,7 +455,7 @@ github.com/googleapis/gax-go/v2/apierror/internal/proto github.com/gorilla/mux # github.com/gosimple/slug v1.1.1 github.com/gosimple/slug -# github.com/grafana-tools/sdk v0.0.0-20210808170449-9de4d14888c5 +# github.com/grafana-tools/sdk v0.0.0-20210808170449-9de4d14888c5 => github.com/csmarchbanks/sdk v0.0.0-20220120205302-870d00a83f4e ## explicit github.com/grafana-tools/sdk # github.com/grafana/dskit v0.0.0-20211103155626-4e784973d341 @@ -1184,3 +1184,4 @@ rsc.io/binaryregexp/syntax # github.com/gocql/gocql => github.com/grafana/gocql v0.0.0-20200605141915-ba5dc39ece85 # github.com/bradfitz/gomemcache => github.com/themihai/gomemcache v0.0.0-20180902122335-24332e2d58ab # github.com/hashicorp/consul => github.com/hashicorp/consul v1.8.1 +# github.com/grafana-tools/sdk => github.com/csmarchbanks/sdk v0.0.0-20220120205302-870d00a83f4e