|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// NetworkLoggingTransport wraps an http.RoundTripper to provide enhanced logging |
| 11 | +// for network operations, including timing, status codes, and error details. |
| 12 | +type NetworkLoggingTransport struct { |
| 13 | + Transport http.RoundTripper |
| 14 | + Enabled bool |
| 15 | +} |
| 16 | + |
| 17 | +// NewTransportWithNetworkLogging creates a new NetworkLoggingTransport that wraps |
| 18 | +// the provided transport with enhanced network logging capabilities. |
| 19 | +func NewTransportWithNetworkLogging(transport http.RoundTripper, enabled bool) *NetworkLoggingTransport { |
| 20 | + if transport == nil { |
| 21 | + transport = http.DefaultTransport |
| 22 | + } |
| 23 | + return &NetworkLoggingTransport{ |
| 24 | + Transport: transport, |
| 25 | + Enabled: enabled, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// RoundTrip implements the http.RoundTripper interface and adds enhanced logging |
| 30 | +// around the HTTP request/response cycle. |
| 31 | +func (t *NetworkLoggingTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 32 | + if !t.Enabled { |
| 33 | + return t.Transport.RoundTrip(req) |
| 34 | + } |
| 35 | + |
| 36 | + startTime := time.Now() |
| 37 | + log.Printf("[DEBUG] Network Request Start: %s %s (started at %s)", |
| 38 | + req.Method, req.URL.String(), startTime.Format(time.RFC3339Nano)) |
| 39 | + |
| 40 | + resp, err := t.Transport.RoundTrip(req) |
| 41 | + duration := time.Since(startTime) |
| 42 | + if err != nil { |
| 43 | + log.Printf("[ERROR] Network Request Failed: %s %s - Duration: %v - Error: %v", |
| 44 | + req.Method, req.URL.String(), duration, err) |
| 45 | + |
| 46 | + t.logNetworkErrorContext(err, req, duration) |
| 47 | + return resp, err |
| 48 | + } |
| 49 | + statusCode := resp.StatusCode |
| 50 | + statusClass := GetStatusClass(statusCode) |
| 51 | + |
| 52 | + log.Printf("[DEBUG] Network Request Complete: %s %s - Status: %d (%s) - Duration: %v", |
| 53 | + req.Method, req.URL.String(), statusCode, statusClass, duration) |
| 54 | + |
| 55 | + if statusCode == http.StatusUnauthorized { |
| 56 | + log.Printf("[DEBUG] Digest Authentication Challenge: %s %s - Status: 401 - Expected first request in digest authentication flow", |
| 57 | + req.Method, req.URL.String()) |
| 58 | + } else if statusCode >= 300 { |
| 59 | + log.Printf("[WARN] HTTP Error Response: %s %s - Status: %d %s - Duration: %v - Content-Type: %s", |
| 60 | + req.Method, req.URL.String(), statusCode, http.StatusText(statusCode), |
| 61 | + duration, resp.Header.Get("Content-Type")) |
| 62 | + } |
| 63 | + return resp, nil |
| 64 | +} |
| 65 | + |
| 66 | +// logNetworkErrorContext provides additional context for common network errors |
| 67 | +func (t *NetworkLoggingTransport) logNetworkErrorContext(err error, req *http.Request, duration time.Duration) { |
| 68 | + errStr := err.Error() |
| 69 | + switch { |
| 70 | + case strings.Contains(errStr, "timeout"): |
| 71 | + log.Printf("[ERROR] Network Timeout: %s %s - Duration: %v - This may indicate API server overload or network connectivity issues", |
| 72 | + req.Method, req.URL.String(), duration) |
| 73 | + case strings.Contains(errStr, "connection refused"): |
| 74 | + log.Printf("[ERROR] Connection Refused: %s %s - Duration: %v - API server may be down or unreachable", |
| 75 | + req.Method, req.URL.String(), duration) |
| 76 | + case strings.Contains(errStr, "no such host"): |
| 77 | + log.Printf("[ERROR] DNS Resolution Failed: %s %s - Duration: %v - Check DNS configuration and network connectivity", |
| 78 | + req.Method, req.URL.String(), duration) |
| 79 | + case strings.Contains(errStr, "certificate"): |
| 80 | + log.Printf("[ERROR] TLS Certificate Error: %s %s - Duration: %v - Check certificate validity and trust chain", |
| 81 | + req.Method, req.URL.String(), duration) |
| 82 | + case strings.Contains(errStr, "context deadline exceeded"): |
| 83 | + log.Printf("[ERROR] Request Deadline Exceeded: %s %s - Duration: %v - Request took longer than configured timeout", |
| 84 | + req.Method, req.URL.String(), duration) |
| 85 | + case strings.Contains(errStr, "connection reset"): |
| 86 | + log.Printf("[ERROR] Connection Reset: %s %s - Duration: %v - Server closed connection unexpectedly", |
| 87 | + req.Method, req.URL.String(), duration) |
| 88 | + default: |
| 89 | + log.Printf("[ERROR] Network Error: %s %s - Duration: %v - Error details: %v", |
| 90 | + req.Method, req.URL.String(), duration, err) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// GetStatusClass returns a human-readable status class for the HTTP status code |
| 95 | +func GetStatusClass(statusCode int) string { |
| 96 | + switch { |
| 97 | + case statusCode >= 200 && statusCode < 300: |
| 98 | + return "Success" |
| 99 | + case statusCode >= 300 && statusCode < 400: |
| 100 | + return "Redirection" |
| 101 | + case statusCode >= 400 && statusCode < 500: |
| 102 | + return "Client Error" |
| 103 | + case statusCode >= 500 && statusCode < 600: |
| 104 | + return "Server Error" |
| 105 | + default: |
| 106 | + return "Unknown" |
| 107 | + } |
| 108 | +} |
0 commit comments