Skip to content

Commit 69b28e3

Browse files
TUN-6347: Add TCP stream logs with FlowID
1 parent 4f468b8 commit 69b28e3

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

connection/connection.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ type TCPRequest struct {
132132
Dest string
133133
CFRay string
134134
LBProbe bool
135+
FlowID string
135136
}
136137

137138
// ReadWriteAcker is a readwriter with the ability to Acknowledge to the downstream (edge) that the origin has

connection/quic.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const (
3131
HTTPMethodKey = "HttpMethod"
3232
// HTTPHostKey is used to get or set http Method in QUIC ALPN if the underlying proxy connection type is HTTP.
3333
HTTPHostKey = "HttpHost"
34+
35+
QUICMetadataFlowID = "FlowID"
3436
)
3537

3638
// QUICConnection represents the type that facilitates Proxying via QUIC streams.
@@ -180,6 +182,7 @@ func (q *QUICConnection) handleDataStream(stream *quicpogs.RequestServerStream)
180182
if err != nil {
181183
return err
182184
}
185+
183186
switch connectRequest.Type {
184187
case quicpogs.ConnectionTypeHTTP, quicpogs.ConnectionTypeWebsocket:
185188
tracedReq, err := buildHTTPRequest(connectRequest, stream)
@@ -191,7 +194,9 @@ func (q *QUICConnection) handleDataStream(stream *quicpogs.RequestServerStream)
191194
return originProxy.ProxyHTTP(w, tracedReq, connectRequest.Type == quicpogs.ConnectionTypeWebsocket)
192195
case quicpogs.ConnectionTypeTCP:
193196
rwa := &streamReadWriteAcker{stream}
194-
return originProxy.ProxyTCP(context.Background(), rwa, &TCPRequest{Dest: connectRequest.Dest})
197+
metadata := connectRequest.MetadataMap()
198+
return originProxy.ProxyTCP(context.Background(), rwa, &TCPRequest{Dest: connectRequest.Dest,
199+
FlowID: metadata[QUICMetadataFlowID]})
195200
}
196201
return nil
197202
}

proxy/proxy.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
LogFieldCFRay = "cfRay"
2929
LogFieldRule = "ingressRule"
3030
LogFieldOriginService = "originService"
31+
LogFieldFlowID = "flowID"
3132
)
3233

3334
// Proxy represents a means to Proxy between cloudflared and the origin services.
@@ -96,7 +97,7 @@ func (p *Proxy) ProxyHTTP(
9697
logFields,
9798
); err != nil {
9899
rule, srv := ruleField(p.ingressRules, ruleNum)
99-
p.logRequestError(err, cfRay, rule, srv)
100+
p.logRequestError(err, cfRay, "", rule, srv)
100101
return err
101102
}
102103
return nil
@@ -109,7 +110,7 @@ func (p *Proxy) ProxyHTTP(
109110
rws := connection.NewHTTPResponseReadWriterAcker(w, req)
110111
if err := p.proxyStream(req.Context(), rws, dest, originProxy, logFields); err != nil {
111112
rule, srv := ruleField(p.ingressRules, ruleNum)
112-
p.logRequestError(err, cfRay, rule, srv)
113+
p.logRequestError(err, cfRay, "", rule, srv)
113114
return err
114115
}
115116
return nil
@@ -140,13 +141,17 @@ func (p *Proxy) ProxyTCP(
140141
cfRay: req.CFRay,
141142
lbProbe: req.LBProbe,
142143
rule: ingress.ServiceWarpRouting,
144+
flowID: req.FlowID,
143145
}
144146

147+
p.log.Debug().Str(LogFieldFlowID, req.FlowID).Msg("tcp proxy stream started")
145148
if err := p.proxyStream(serveCtx, rwa, req.Dest, p.warpRouting.Proxy, logFields); err != nil {
146-
p.logRequestError(err, req.CFRay, "", ingress.ServiceWarpRouting)
149+
p.logRequestError(err, req.CFRay, req.FlowID, "", ingress.ServiceWarpRouting)
147150
return err
148151
}
149152

153+
p.log.Debug().Str(LogFieldFlowID, req.FlowID).Msg("tcp proxy stream finished successfully")
154+
150155
return nil
151156
}
152157

@@ -317,6 +322,7 @@ type logFields struct {
317322
cfRay string
318323
lbProbe bool
319324
rule interface{}
325+
flowID string
320326
}
321327

322328
func (p *Proxy) logRequest(r *http.Request, fields logFields) {
@@ -360,12 +366,15 @@ func (p *Proxy) logOriginResponse(resp *http.Response, fields logFields) {
360366
}
361367
}
362368

363-
func (p *Proxy) logRequestError(err error, cfRay string, rule, service string) {
369+
func (p *Proxy) logRequestError(err error, cfRay string, flowID string, rule, service string) {
364370
requestErrors.Inc()
365371
log := p.log.Error().Err(err)
366372
if cfRay != "" {
367373
log = log.Str(LogFieldCFRay, cfRay)
368374
}
375+
if flowID != "" {
376+
log = log.Str(LogFieldFlowID, flowID)
377+
}
369378
if rule != "" {
370379
log = log.Str(LogFieldRule, rule)
371380
}

0 commit comments

Comments
 (0)