Skip to content

Commit

Permalink
added error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
yesoreyeram committed Dec 9, 2024
1 parent e8d8f88 commit 17c78ae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
30 changes: 20 additions & 10 deletions pkg/infinity/httptrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
},
Expand All @@ -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))
}
4 changes: 1 addition & 3 deletions pkg/infinity/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down

0 comments on commit 17c78ae

Please sign in to comment.