Skip to content

Commit

Permalink
Add datasource label to secure_socks_requests_duration (#866)
Browse files Browse the repository at this point in the history
* Add datasource label to secure_socks_requests_duration

* Update backend/proxy/secure_socks_proxy.go

Co-authored-by: Giuseppe Guerra <[email protected]>

* Update backend/proxy/secure_socks_proxy.go

Co-authored-by: Giuseppe Guerra <[email protected]>

* Update backend/proxy/options.go

Co-authored-by: Giuseppe Guerra <[email protected]>

---------

Co-authored-by: Giuseppe Guerra <[email protected]>
  • Loading branch information
PoorlyDefinedBehaviour and xnyo authored Feb 1, 2024
1 parent 94f94a8 commit c61c9d0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
13 changes: 9 additions & 4 deletions backend/proxy/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 13 additions & 6 deletions backend/proxy/secure_socks_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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
}

0 comments on commit c61c9d0

Please sign in to comment.