-
Notifications
You must be signed in to change notification settings - Fork 874
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propagates the logger context into further locations to help provide more context for certain errors. For instance, upstream and downstream copying errors will properly have the assigned flow id attached and destination address.
- Loading branch information
Showing
2 changed files
with
97 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package proxy | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/rs/zerolog" | ||
|
||
"github.com/cloudflare/cloudflared/connection" | ||
"github.com/cloudflare/cloudflared/ingress" | ||
"github.com/cloudflare/cloudflared/management" | ||
) | ||
|
||
const ( | ||
logFieldCFRay = "cfRay" | ||
logFieldLBProbe = "lbProbe" | ||
logFieldRule = "ingressRule" | ||
logFieldOriginService = "originService" | ||
logFieldFlowID = "flowID" | ||
logFieldConnIndex = "connIndex" | ||
logFieldDestAddr = "destAddr" | ||
) | ||
|
||
// newHTTPLogger creates a child zerolog.Logger from the provided with added context from the HTTP request, ingress | ||
// services, and connection index. | ||
func newHTTPLogger(logger *zerolog.Logger, connIndex uint8, req *http.Request, rule int, serviceName string) zerolog.Logger { | ||
ctx := logger.With(). | ||
Int(management.EventTypeKey, int(management.HTTP)). | ||
Uint8(logFieldConnIndex, connIndex) | ||
cfRay := connection.FindCfRayHeader(req) | ||
lbProbe := connection.IsLBProbeRequest(req) | ||
if cfRay != "" { | ||
ctx.Str(logFieldCFRay, cfRay) | ||
} | ||
if lbProbe { | ||
ctx.Bool(logFieldLBProbe, lbProbe) | ||
} | ||
return ctx. | ||
Str(logFieldOriginService, serviceName). | ||
Interface(logFieldRule, rule). | ||
Logger() | ||
} | ||
|
||
// newTCPLogger creates a child zerolog.Logger from the provided with added context from the TCPRequest. | ||
func newTCPLogger(logger *zerolog.Logger, req *connection.TCPRequest) zerolog.Logger { | ||
return logger.With(). | ||
Int(management.EventTypeKey, int(management.TCP)). | ||
Uint8(logFieldConnIndex, req.ConnIndex). | ||
Str(logFieldOriginService, ingress.ServiceWarpRouting). | ||
Str(logFieldFlowID, req.FlowID). | ||
Str(logFieldDestAddr, req.Dest). | ||
Uint8(logFieldConnIndex, req.ConnIndex). | ||
Logger() | ||
} | ||
|
||
// logHTTPRequest logs a Debug message with the corresponding HTTP request details from the eyeball. | ||
func logHTTPRequest(logger *zerolog.Logger, r *http.Request) { | ||
logger.Debug(). | ||
Str("host", r.Host). | ||
Str("path", r.URL.Path). | ||
Interface("headers", r.Header). | ||
Int64("content-length", r.ContentLength). | ||
Msgf("%s %s %s", r.Method, r.URL, r.Proto) | ||
} | ||
|
||
// logOriginHTTPResponse logs a Debug message of the origin response. | ||
func logOriginHTTPResponse(logger *zerolog.Logger, resp *http.Response) { | ||
responseByCode.WithLabelValues(strconv.Itoa(resp.StatusCode)).Inc() | ||
logger.Debug(). | ||
Int64("content-length", resp.ContentLength). | ||
Msgf("%s", resp.Status) | ||
} | ||
|
||
// logRequestError logs an error for the proxied request. | ||
func logRequestError(logger *zerolog.Logger, err error) { | ||
requestErrors.Inc() | ||
logger.Error().Err(err).Send() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters