logger.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package proxy
  2. import (
  3. "net/http"
  4. "strconv"
  5. "github.com/rs/zerolog"
  6. "github.com/cloudflare/cloudflared/connection"
  7. "github.com/cloudflare/cloudflared/ingress"
  8. "github.com/cloudflare/cloudflared/management"
  9. )
  10. const (
  11. logFieldCFRay = "cfRay"
  12. logFieldLBProbe = "lbProbe"
  13. logFieldRule = "ingressRule"
  14. logFieldOriginService = "originService"
  15. logFieldConnIndex = "connIndex"
  16. logFieldDestAddr = "destAddr"
  17. )
  18. var (
  19. LogFieldFlowID = "flowID"
  20. )
  21. // newHTTPLogger creates a child zerolog.Logger from the provided with added context from the HTTP request, ingress
  22. // services, and connection index.
  23. func newHTTPLogger(logger *zerolog.Logger, connIndex uint8, req *http.Request, rule int, serviceName string) zerolog.Logger {
  24. ctx := logger.With().
  25. Int(management.EventTypeKey, int(management.HTTP)).
  26. Uint8(logFieldConnIndex, connIndex)
  27. cfRay := connection.FindCfRayHeader(req)
  28. lbProbe := connection.IsLBProbeRequest(req)
  29. if cfRay != "" {
  30. ctx.Str(logFieldCFRay, cfRay)
  31. }
  32. if lbProbe {
  33. ctx.Bool(logFieldLBProbe, lbProbe)
  34. }
  35. return ctx.
  36. Str(logFieldOriginService, serviceName).
  37. Interface(logFieldRule, rule).
  38. Logger()
  39. }
  40. // newTCPLogger creates a child zerolog.Logger from the provided with added context from the TCPRequest.
  41. func newTCPLogger(logger *zerolog.Logger, req *connection.TCPRequest) zerolog.Logger {
  42. return logger.With().
  43. Int(management.EventTypeKey, int(management.TCP)).
  44. Uint8(logFieldConnIndex, req.ConnIndex).
  45. Str(logFieldOriginService, ingress.ServiceWarpRouting).
  46. Str(LogFieldFlowID, req.FlowID).
  47. Str(logFieldDestAddr, req.Dest).
  48. Uint8(logFieldConnIndex, req.ConnIndex).
  49. Logger()
  50. }
  51. // logHTTPRequest logs a Debug message with the corresponding HTTP request details from the eyeball.
  52. func logHTTPRequest(logger *zerolog.Logger, r *http.Request) {
  53. logger.Debug().
  54. Str("host", r.Host).
  55. Str("path", r.URL.Path).
  56. Interface("headers", r.Header).
  57. Int64("content-length", r.ContentLength).
  58. Msgf("%s %s %s", r.Method, r.URL, r.Proto)
  59. }
  60. // logOriginHTTPResponse logs a Debug message of the origin response.
  61. func logOriginHTTPResponse(logger *zerolog.Logger, resp *http.Response) {
  62. responseByCode.WithLabelValues(strconv.Itoa(resp.StatusCode)).Inc()
  63. logger.Debug().
  64. Int64("content-length", resp.ContentLength).
  65. Msgf("%s", resp.Status)
  66. }
  67. // logRequestError logs an error for the proxied request.
  68. func logRequestError(logger *zerolog.Logger, err error) {
  69. requestErrors.Inc()
  70. logger.Error().Err(err).Send()
  71. }