From c61c9d0b4ae54d8c7a9dd8f90e37b978eb02a874 Mon Sep 17 00:00:00 2001 From: Bruno Date: Thu, 1 Feb 2024 10:07:15 -0300 Subject: [PATCH] Add datasource label to secure_socks_requests_duration (#866) * Add datasource label to secure_socks_requests_duration * Update backend/proxy/secure_socks_proxy.go Co-authored-by: Giuseppe Guerra * Update backend/proxy/secure_socks_proxy.go Co-authored-by: Giuseppe Guerra * Update backend/proxy/options.go Co-authored-by: Giuseppe Guerra --------- Co-authored-by: Giuseppe Guerra --- backend/proxy/options.go | 13 +++++++++---- backend/proxy/secure_socks_proxy.go | 19 +++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/backend/proxy/options.go b/backend/proxy/options.go index dc98da41a..2c9abfe20 100644 --- a/backend/proxy/options.go +++ b/backend/proxy/options.go @@ -4,10 +4,15 @@ import "time" // Options defines per datasource options for creating the proxy dialer. type Options struct { - Enabled bool - Auth *AuthOptions - Timeouts *TimeoutOptions - ClientCfg *ClientCfg + Enabled bool + // DatasourceName is the name of the datasource the proxy will be used to communicate with. + DatasourceName string + // DatasourceType is the type of the datasource the proxy will be used to communicate with. + // It should be the value assigned to the type property in a datasource provisioning file (e.g mysql, prometheus) + DatasourceType string + Auth *AuthOptions + Timeouts *TimeoutOptions + ClientCfg *ClientCfg } // AuthOptions socks5 username and password options. diff --git a/backend/proxy/secure_socks_proxy.go b/backend/proxy/secure_socks_proxy.go index a096cba13..68c2420bf 100644 --- a/backend/proxy/secure_socks_proxy.go +++ b/backend/proxy/secure_socks_proxy.go @@ -51,7 +51,7 @@ var ( Namespace: "grafana", Name: "secure_socks_requests_duration", Help: "Duration of requests to the secure socks proxy", - }, []string{"code"}) + }, []string{"code", "datasource", "datasource_type"}) ) // Client is the main Proxy Client interface. @@ -152,7 +152,7 @@ func (p *cfgProxyWrapper) NewSecureSocksProxyContextDialer() (proxy.Dialer, erro return nil, err } - return newInstrumentedSocksDialer(dialSocksProxy), nil + return newInstrumentedSocksDialer(dialSocksProxy, p.opts.DatasourceName, p.opts.DatasourceType), nil } func (p *cfgProxyWrapper) getTLSDialer() (*tls.Dialer, error) { @@ -280,13 +280,20 @@ func SecureSocksProxyEnabledOnDS(jsonData map[string]interface{}) bool { // instrumentedSocksDialer is a wrapper around the proxy.Dialer and proxy.DialContext // that records relevant socks secure socks proxy. type instrumentedSocksDialer struct { - dialer proxy.Dialer + // datasourceName is the name of the datasource the proxy will be used to communicate with. + datasourceName string + // datasourceType is the type of the datasourceType the proxy will be used to communicate with. + // It should be the value assigned to the type property in a datasourceType provisioning file (e.g mysql, prometheus) + datasourceType string + dialer proxy.Dialer } // newInstrumentedSocksDialer creates a new instrumented dialer -func newInstrumentedSocksDialer(dialer proxy.Dialer) proxy.Dialer { +func newInstrumentedSocksDialer(dialer proxy.Dialer, datasourceName, datasourceType string) proxy.Dialer { return &instrumentedSocksDialer{ - dialer: dialer, + dialer: dialer, + datasourceName: datasourceName, + datasourceType: datasourceType, } } @@ -348,6 +355,6 @@ func (d *instrumentedSocksDialer) DialContext(ctx context.Context, n, addr strin code = "dial_error" } - secureSocksRequestsDuration.WithLabelValues(code).Observe(time.Since(start).Seconds()) + secureSocksRequestsDuration.WithLabelValues(code, d.datasourceName, d.datasourceType).Observe(time.Since(start).Seconds()) return c, err }