diff --git a/pkg/infinity/httptrace.go b/pkg/infinity/httptrace.go index 2c284972..8cd0852b 100644 --- a/pkg/infinity/httptrace.go +++ b/pkg/infinity/httptrace.go @@ -5,11 +5,16 @@ import ( "net/http" "net/http/httptrace" - "github.com/grafana/grafana-plugin-sdk-go/backend/log" + "github.com/grafana/grafana-plugin-sdk-go/backend" ) -func reqWithHTTPTraceContext(req *http.Request, logger log.Logger) *http.Request { +func reqWithHTTPTraceContext(req *http.Request) *http.Request { + ctx := req.Context() + logger := backend.Logger.FromContext(ctx) trace := &httptrace.ClientTrace{ + GetConn: func(hostPort string) { + logger.Debug("HTTPTrace event", "event_name", "GetConn") + }, DNSStart: func(di httptrace.DNSStartInfo) { logger.Debug("HTTPTrace event", "event_name", "DNSStart") }, @@ -20,23 +25,28 @@ func reqWithHTTPTraceContext(req *http.Request, logger log.Logger) *http.Request logger.Debug("HTTPTrace event", "event_name", "ConnectStart") }, ConnectDone: func(network, addr string, err error) { - logger.Debug("HTTPTrace event", "event_name", "ConnectDone") - }, - GetConn: func(hostPort string) { - logger.Debug("HTTPTrace event", "event_name", "GetConn") - }, - GotConn: func(connInfo httptrace.GotConnInfo) { - logger.Debug("HTTPTrace event", "event_name", "GotConn") + if err != nil { + logger.Error("HTTPTrace event", "event_name", "ConnectDone", "error", err.Error()) + return + } + logger.Debug("HTTPTrace event", "event_name", "ConnectDone", "address", addr) }, TLSHandshakeStart: func() { logger.Debug("HTTPTrace event", "event_name", "TLSHandshakeStart") }, TLSHandshakeDone: func(cs tls.ConnectionState, err error) { + if err != nil { + logger.Error("HTTPTrace event", "event_name", "TLSHandshakeDone", "error", err.Error()) + return + } logger.Debug("HTTPTrace event", "event_name", "TLSHandshakeDone") }, + GotConn: func(connInfo httptrace.GotConnInfo) { + logger.Debug("HTTPTrace event", "event_name", "GotConn", "remote_address", connInfo.Conn.RemoteAddr().String()) + }, GotFirstResponseByte: func() { logger.Debug("HTTPTrace event", "event_name", "GotFirstResponseByte") }, } - return req.WithContext(httptrace.WithClientTrace(req.Context(), trace)) + return req.WithContext(httptrace.WithClientTrace(ctx, trace)) } diff --git a/pkg/infinity/request.go b/pkg/infinity/request.go index 60ceecbe..bdbe2ecc 100644 --- a/pkg/infinity/request.go +++ b/pkg/infinity/request.go @@ -9,14 +9,12 @@ import ( "strings" "github.com/grafana/grafana-infinity-datasource/pkg/models" - "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/backend/tracing" "moul.io/http2curl" ) func GetRequest(ctx context.Context, settings models.InfinitySettings, body io.Reader, query models.Query, requestHeaders map[string]string, includeSect bool) (req *http.Request, err error) { ctx, span := tracing.DefaultTracer().Start(ctx, "GetRequest") - logger := backend.Logger.FromContext(ctx) defer span.End() url, err := GetQueryURL(ctx, settings, query, includeSect) if err != nil { @@ -36,7 +34,7 @@ func GetRequest(ctx context.Context, settings models.InfinitySettings, body io.R req = ApplyBearerToken(settings, req, includeSect) req = ApplyApiKeyAuth(settings, req, includeSect) req = ApplyForwardedOAuthIdentity(requestHeaders, settings, req, includeSect) - req = reqWithHTTPTraceContext(req, logger) + req = reqWithHTTPTraceContext(req) return req, err }