mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Datasources: Add user_agent header customization for outgoing HTTP requests (#63769)
This commit is contained in:
parent
177aa254c5
commit
3c218d742f
@ -213,6 +213,9 @@ response_limit = 0
|
|||||||
# Limits the number of rows that Grafana will process from SQL data sources.
|
# Limits the number of rows that Grafana will process from SQL data sources.
|
||||||
row_limit = 1000000
|
row_limit = 1000000
|
||||||
|
|
||||||
|
# Sets a custom value for the `User-Agent` header for outgoing data proxy requests. If empty, the default value is `Grafana/<BuildVersion>` (for example `Grafana/9.0.0`).
|
||||||
|
user_agent =
|
||||||
|
|
||||||
#################################### Analytics ###########################
|
#################################### Analytics ###########################
|
||||||
[analytics]
|
[analytics]
|
||||||
# Server reporting, sends usage counters to stats.grafana.org every 24 hours.
|
# Server reporting, sends usage counters to stats.grafana.org every 24 hours.
|
||||||
|
@ -220,6 +220,9 @@
|
|||||||
# Limits the number of rows that Grafana will process from SQL data sources.
|
# Limits the number of rows that Grafana will process from SQL data sources.
|
||||||
;row_limit = 1000000
|
;row_limit = 1000000
|
||||||
|
|
||||||
|
# Sets a custom value for the `User-Agent` header for outgoing data proxy requests. If empty, the default value is `Grafana/<BuildVersion>` (for example `Grafana/9.0.0`).
|
||||||
|
;user_agent =
|
||||||
|
|
||||||
#################################### Analytics ####################################
|
#################################### Analytics ####################################
|
||||||
[analytics]
|
[analytics]
|
||||||
# Server reporting, sends usage counters to stats.grafana.org every 24 hours.
|
# Server reporting, sends usage counters to stats.grafana.org every 24 hours.
|
||||||
|
@ -484,6 +484,10 @@ Limits the amount of bytes that will be read/accepted from responses of outgoing
|
|||||||
|
|
||||||
Limits the number of rows that Grafana will process from SQL (relational) data sources. Default is `1000000`.
|
Limits the number of rows that Grafana will process from SQL (relational) data sources. Default is `1000000`.
|
||||||
|
|
||||||
|
### user_agent
|
||||||
|
|
||||||
|
Sets a custom value for the `User-Agent` header for outgoing data proxy requests. If empty, the default value is `Grafana/<BuildVersion>` (for example `Grafana/9.0.0`).
|
||||||
|
|
||||||
<hr />
|
<hr />
|
||||||
|
|
||||||
## [analytics]
|
## [analytics]
|
||||||
|
@ -225,7 +225,7 @@ func (proxy *DataSourceProxy) director(req *http.Request) {
|
|||||||
proxyutil.ApplyUserHeader(proxy.cfg.SendUserHeader, req, proxy.ctx.SignedInUser)
|
proxyutil.ApplyUserHeader(proxy.cfg.SendUserHeader, req, proxy.ctx.SignedInUser)
|
||||||
|
|
||||||
proxyutil.ClearCookieHeader(req, proxy.ds.AllowedCookies(), []string{proxy.cfg.LoginCookieName})
|
proxyutil.ClearCookieHeader(req, proxy.ds.AllowedCookies(), []string{proxy.cfg.LoginCookieName})
|
||||||
req.Header.Set("User-Agent", fmt.Sprintf("Grafana/%s", setting.BuildVersion))
|
req.Header.Set("User-Agent", proxy.cfg.DataProxyUserAgent)
|
||||||
|
|
||||||
jsonData := make(map[string]interface{})
|
jsonData := make(map[string]interface{})
|
||||||
if proxy.ds.JsonData != nil {
|
if proxy.ds.JsonData != nil {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package httpclientprovider
|
package httpclientprovider
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -21,13 +20,12 @@ var newProviderFunc = sdkhttpclient.NewProvider
|
|||||||
// New creates a new HTTP client provider with pre-configured middlewares.
|
// New creates a new HTTP client provider with pre-configured middlewares.
|
||||||
func New(cfg *setting.Cfg, validator validations.PluginRequestValidator, tracer tracing.Tracer) *sdkhttpclient.Provider {
|
func New(cfg *setting.Cfg, validator validations.PluginRequestValidator, tracer tracing.Tracer) *sdkhttpclient.Provider {
|
||||||
logger := log.New("httpclient")
|
logger := log.New("httpclient")
|
||||||
userAgent := fmt.Sprintf("Grafana/%s", cfg.BuildVersion)
|
|
||||||
|
|
||||||
middlewares := []sdkhttpclient.Middleware{
|
middlewares := []sdkhttpclient.Middleware{
|
||||||
TracingMiddleware(logger, tracer),
|
TracingMiddleware(logger, tracer),
|
||||||
DataSourceMetricsMiddleware(),
|
DataSourceMetricsMiddleware(),
|
||||||
sdkhttpclient.ContextualMiddleware(),
|
sdkhttpclient.ContextualMiddleware(),
|
||||||
SetUserAgentMiddleware(userAgent),
|
SetUserAgentMiddleware(cfg.DataProxyUserAgent),
|
||||||
sdkhttpclient.BasicAuthenticationMiddleware(),
|
sdkhttpclient.BasicAuthenticationMiddleware(),
|
||||||
sdkhttpclient.CustomHeadersMiddleware(),
|
sdkhttpclient.CustomHeadersMiddleware(),
|
||||||
ResponseLimitMiddleware(cfg.ResponseLimit),
|
ResponseLimitMiddleware(cfg.ResponseLimit),
|
||||||
|
@ -324,6 +324,7 @@ type Cfg struct {
|
|||||||
DataProxyIdleConnTimeout int
|
DataProxyIdleConnTimeout int
|
||||||
ResponseLimit int64
|
ResponseLimit int64
|
||||||
DataProxyRowLimit int64
|
DataProxyRowLimit int64
|
||||||
|
DataProxyUserAgent string
|
||||||
|
|
||||||
// DistributedCache
|
// DistributedCache
|
||||||
RemoteCacheOptions *RemoteCacheOptions
|
RemoteCacheOptions *RemoteCacheOptions
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package setting
|
package setting
|
||||||
|
|
||||||
import "gopkg.in/ini.v1"
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gopkg.in/ini.v1"
|
||||||
|
)
|
||||||
|
|
||||||
const defaultDataProxyRowLimit = int64(1000000)
|
const defaultDataProxyRowLimit = int64(1000000)
|
||||||
|
|
||||||
@ -18,6 +22,11 @@ func readDataProxySettings(iniFile *ini.File, cfg *Cfg) error {
|
|||||||
cfg.DataProxyIdleConnTimeout = dataproxy.Key("idle_conn_timeout_seconds").MustInt(90)
|
cfg.DataProxyIdleConnTimeout = dataproxy.Key("idle_conn_timeout_seconds").MustInt(90)
|
||||||
cfg.ResponseLimit = dataproxy.Key("response_limit").MustInt64(0)
|
cfg.ResponseLimit = dataproxy.Key("response_limit").MustInt64(0)
|
||||||
cfg.DataProxyRowLimit = dataproxy.Key("row_limit").MustInt64(defaultDataProxyRowLimit)
|
cfg.DataProxyRowLimit = dataproxy.Key("row_limit").MustInt64(defaultDataProxyRowLimit)
|
||||||
|
cfg.DataProxyUserAgent = dataproxy.Key("user_agent").String()
|
||||||
|
|
||||||
|
if cfg.DataProxyUserAgent == "" {
|
||||||
|
cfg.DataProxyUserAgent = fmt.Sprintf("Grafana/%s", BuildVersion)
|
||||||
|
}
|
||||||
|
|
||||||
if cfg.DataProxyRowLimit <= 0 {
|
if cfg.DataProxyRowLimit <= 0 {
|
||||||
cfg.DataProxyRowLimit = defaultDataProxyRowLimit
|
cfg.DataProxyRowLimit = defaultDataProxyRowLimit
|
||||||
|
@ -19,7 +19,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
|
||||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/loganalytics"
|
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/loganalytics"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/macros"
|
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/macros"
|
||||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
||||||
@ -243,7 +242,6 @@ func (e *AzureResourceGraphDatasource) createRequest(ctx context.Context, logger
|
|||||||
}
|
}
|
||||||
req.URL.Path = "/"
|
req.URL.Path = "/"
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req.Header.Set("User-Agent", fmt.Sprintf("Grafana/%s", setting.BuildVersion))
|
|
||||||
|
|
||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
@ -98,7 +98,6 @@ func TestAzureResourceGraphCreateRequest(t *testing.T) {
|
|||||||
expectedURL: "http://ds/",
|
expectedURL: "http://ds/",
|
||||||
expectedHeaders: http.Header{
|
expectedHeaders: http.Header{
|
||||||
"Content-Type": []string{"application/json"},
|
"Content-Type": []string{"application/json"},
|
||||||
"User-Agent": []string{"Grafana/"},
|
|
||||||
},
|
},
|
||||||
Err: require.NoError,
|
Err: require.NoError,
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user