Revert "TUN-6576: Consume cf-trace-id from incoming TCP requests to create root span"
This reverts commit f48a7cd3dd
.
This commit is contained in:
parent
f48a7cd3dd
commit
7f1c890a82
|
@ -123,24 +123,23 @@ func (t Type) String() string {
|
||||||
|
|
||||||
// OriginProxy is how data flows from cloudflared to the origin services running behind it.
|
// OriginProxy is how data flows from cloudflared to the origin services running behind it.
|
||||||
type OriginProxy interface {
|
type OriginProxy interface {
|
||||||
ProxyHTTP(w ResponseWriter, tr *tracing.TracedHTTPRequest, isWebsocket bool) error
|
ProxyHTTP(w ResponseWriter, tr *tracing.TracedRequest, isWebsocket bool) error
|
||||||
ProxyTCP(ctx context.Context, rwa ReadWriteAcker, req *TCPRequest) error
|
ProxyTCP(ctx context.Context, rwa ReadWriteAcker, req *TCPRequest) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// TCPRequest defines the input format needed to perform a TCP proxy.
|
// TCPRequest defines the input format needed to perform a TCP proxy.
|
||||||
type TCPRequest struct {
|
type TCPRequest struct {
|
||||||
Dest string
|
Dest string
|
||||||
CFRay string
|
CFRay string
|
||||||
LBProbe bool
|
LBProbe bool
|
||||||
FlowID string
|
FlowID string
|
||||||
CfTraceID string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadWriteAcker is a readwriter with the ability to Acknowledge to the downstream (edge) that the origin has
|
// ReadWriteAcker is a readwriter with the ability to Acknowledge to the downstream (edge) that the origin has
|
||||||
// accepted the connection.
|
// accepted the connection.
|
||||||
type ReadWriteAcker interface {
|
type ReadWriteAcker interface {
|
||||||
io.ReadWriter
|
io.ReadWriter
|
||||||
AckConnection(tracePropagation string) error
|
AckConnection() error
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTPResponseReadWriteAcker is an HTTP implementation of ReadWriteAcker.
|
// HTTPResponseReadWriteAcker is an HTTP implementation of ReadWriteAcker.
|
||||||
|
@ -169,7 +168,7 @@ func (h *HTTPResponseReadWriteAcker) Write(p []byte) (int, error) {
|
||||||
|
|
||||||
// AckConnection acks an HTTP connection by sending a switch protocols status code that enables the caller to
|
// AckConnection acks an HTTP connection by sending a switch protocols status code that enables the caller to
|
||||||
// upgrade to streams.
|
// upgrade to streams.
|
||||||
func (h *HTTPResponseReadWriteAcker) AckConnection(tracePropagation string) error {
|
func (h *HTTPResponseReadWriteAcker) AckConnection() error {
|
||||||
resp := &http.Response{
|
resp := &http.Response{
|
||||||
Status: switchingProtocolText,
|
Status: switchingProtocolText,
|
||||||
StatusCode: http.StatusSwitchingProtocols,
|
StatusCode: http.StatusSwitchingProtocols,
|
||||||
|
@ -180,10 +179,6 @@ func (h *HTTPResponseReadWriteAcker) AckConnection(tracePropagation string) erro
|
||||||
resp.Header = websocket.NewResponseHeader(h.req)
|
resp.Header = websocket.NewResponseHeader(h.req)
|
||||||
}
|
}
|
||||||
|
|
||||||
if tracePropagation != "" {
|
|
||||||
resp.Header.Add(tracing.CanonicalCloudflaredTracingHeader, tracePropagation)
|
|
||||||
}
|
|
||||||
|
|
||||||
return h.w.WriteRespHeaders(resp.StatusCode, resp.Header)
|
return h.w.WriteRespHeaders(resp.StatusCode, resp.Header)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,8 +30,6 @@ var (
|
||||||
testLargeResp = make([]byte, largeFileSize)
|
testLargeResp = make([]byte, largeFileSize)
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ ReadWriteAcker = (*HTTPResponseReadWriteAcker)(nil)
|
|
||||||
|
|
||||||
type testRequest struct {
|
type testRequest struct {
|
||||||
name string
|
name string
|
||||||
endpoint string
|
endpoint string
|
||||||
|
@ -62,7 +60,7 @@ type mockOriginProxy struct{}
|
||||||
|
|
||||||
func (moc *mockOriginProxy) ProxyHTTP(
|
func (moc *mockOriginProxy) ProxyHTTP(
|
||||||
w ResponseWriter,
|
w ResponseWriter,
|
||||||
tr *tracing.TracedHTTPRequest,
|
tr *tracing.TracedRequest,
|
||||||
isWebsocket bool,
|
isWebsocket bool,
|
||||||
) error {
|
) error {
|
||||||
req := tr.Request
|
req := tr.Request
|
||||||
|
|
|
@ -69,7 +69,6 @@ func NewH2muxConnection(
|
||||||
connIndex uint8,
|
connIndex uint8,
|
||||||
observer *Observer,
|
observer *Observer,
|
||||||
gracefulShutdownC <-chan struct{},
|
gracefulShutdownC <-chan struct{},
|
||||||
log *zerolog.Logger,
|
|
||||||
) (*h2muxConnection, error, bool) {
|
) (*h2muxConnection, error, bool) {
|
||||||
h := &h2muxConnection{
|
h := &h2muxConnection{
|
||||||
orchestrator: orchestrator,
|
orchestrator: orchestrator,
|
||||||
|
@ -80,7 +79,6 @@ func NewH2muxConnection(
|
||||||
observer: observer,
|
observer: observer,
|
||||||
gracefulShutdownC: gracefulShutdownC,
|
gracefulShutdownC: gracefulShutdownC,
|
||||||
newRPCClientFunc: newRegistrationRPCClient,
|
newRPCClientFunc: newRegistrationRPCClient,
|
||||||
log: log,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establish a muxed connection with the edge
|
// Establish a muxed connection with the edge
|
||||||
|
@ -236,7 +234,7 @@ func (h *h2muxConnection) ServeStream(stream *h2mux.MuxedStream) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = originProxy.ProxyHTTP(respWriter, tracing.NewTracedHTTPRequest(req, h.log), sourceConnectionType == TypeWebsocket)
|
err = originProxy.ProxyHTTP(respWriter, tracing.NewTracedRequest(req), sourceConnectionType == TypeWebsocket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
respWriter.WriteErrorResponse()
|
respWriter.WriteErrorResponse()
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ func newH2MuxConnection(t require.TestingT) (*h2muxConnection, *h2mux.Muxer) {
|
||||||
}()
|
}()
|
||||||
var connIndex = uint8(0)
|
var connIndex = uint8(0)
|
||||||
testObserver := NewObserver(&log, &log)
|
testObserver := NewObserver(&log, &log)
|
||||||
h2muxConn, err, _ := NewH2muxConnection(testOrchestrator, testGracePeriod, testMuxerConfig, originConn, connIndex, testObserver, nil, &log)
|
h2muxConn, err, _ := NewH2muxConnection(testOrchestrator, testGracePeriod, testMuxerConfig, originConn, connIndex, testObserver, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
return h2muxConn, <-edgeMuxChan
|
return h2muxConn, <-edgeMuxChan
|
||||||
}
|
}
|
||||||
|
|
|
@ -132,7 +132,7 @@ func (c *HTTP2Connection) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
case TypeWebsocket, TypeHTTP:
|
case TypeWebsocket, TypeHTTP:
|
||||||
stripWebsocketUpgradeHeader(r)
|
stripWebsocketUpgradeHeader(r)
|
||||||
// Check for tracing on request
|
// Check for tracing on request
|
||||||
tr := tracing.NewTracedHTTPRequest(r, c.log)
|
tr := tracing.NewTracedRequest(r)
|
||||||
if err := originProxy.ProxyHTTP(respWriter, tr, connType == TypeWebsocket); err != nil {
|
if err := originProxy.ProxyHTTP(respWriter, tr, connType == TypeWebsocket); err != nil {
|
||||||
err := fmt.Errorf("Failed to proxy HTTP: %w", err)
|
err := fmt.Errorf("Failed to proxy HTTP: %w", err)
|
||||||
c.log.Error().Err(err)
|
c.log.Error().Err(err)
|
||||||
|
|
|
@ -197,7 +197,7 @@ func (q *QUICConnection) dispatchRequest(ctx context.Context, stream *quicpogs.R
|
||||||
|
|
||||||
switch request.Type {
|
switch request.Type {
|
||||||
case quicpogs.ConnectionTypeHTTP, quicpogs.ConnectionTypeWebsocket:
|
case quicpogs.ConnectionTypeHTTP, quicpogs.ConnectionTypeWebsocket:
|
||||||
tracedReq, err := buildHTTPRequest(ctx, request, stream, q.logger)
|
tracedReq, err := buildHTTPRequest(ctx, request, stream)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -208,9 +208,8 @@ func (q *QUICConnection) dispatchRequest(ctx context.Context, stream *quicpogs.R
|
||||||
rwa := &streamReadWriteAcker{stream}
|
rwa := &streamReadWriteAcker{stream}
|
||||||
metadata := request.MetadataMap()
|
metadata := request.MetadataMap()
|
||||||
return originProxy.ProxyTCP(ctx, rwa, &TCPRequest{
|
return originProxy.ProxyTCP(ctx, rwa, &TCPRequest{
|
||||||
Dest: request.Dest,
|
Dest: request.Dest,
|
||||||
FlowID: metadata[QUICMetadataFlowID],
|
FlowID: metadata[QUICMetadataFlowID],
|
||||||
CfTraceID: metadata[tracing.TracerContextName],
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -297,12 +296,8 @@ type streamReadWriteAcker struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AckConnection acks response back to the proxy.
|
// AckConnection acks response back to the proxy.
|
||||||
func (s *streamReadWriteAcker) AckConnection(tracePropagation string) error {
|
func (s *streamReadWriteAcker) AckConnection() error {
|
||||||
metadata := quicpogs.Metadata{
|
return s.WriteConnectResponseData(nil)
|
||||||
Key: tracing.CanonicalCloudflaredTracingHeader,
|
|
||||||
Val: tracePropagation,
|
|
||||||
}
|
|
||||||
return s.WriteConnectResponseData(nil, metadata)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// httpResponseAdapter translates responses written by the HTTP Proxy into ones that can be used in QUIC.
|
// httpResponseAdapter translates responses written by the HTTP Proxy into ones that can be used in QUIC.
|
||||||
|
@ -330,12 +325,7 @@ func (hrw httpResponseAdapter) WriteErrorResponse(err error) {
|
||||||
hrw.WriteConnectResponseData(err, quicpogs.Metadata{Key: "HttpStatus", Val: strconv.Itoa(http.StatusBadGateway)})
|
hrw.WriteConnectResponseData(err, quicpogs.Metadata{Key: "HttpStatus", Val: strconv.Itoa(http.StatusBadGateway)})
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildHTTPRequest(
|
func buildHTTPRequest(ctx context.Context, connectRequest *quicpogs.ConnectRequest, body io.ReadCloser) (*tracing.TracedRequest, error) {
|
||||||
ctx context.Context,
|
|
||||||
connectRequest *quicpogs.ConnectRequest,
|
|
||||||
body io.ReadCloser,
|
|
||||||
log *zerolog.Logger,
|
|
||||||
) (*tracing.TracedHTTPRequest, error) {
|
|
||||||
metadata := connectRequest.MetadataMap()
|
metadata := connectRequest.MetadataMap()
|
||||||
dest := connectRequest.Dest
|
dest := connectRequest.Dest
|
||||||
method := metadata[HTTPMethodKey]
|
method := metadata[HTTPMethodKey]
|
||||||
|
@ -377,7 +367,7 @@ func buildHTTPRequest(
|
||||||
stripWebsocketUpgradeHeader(req)
|
stripWebsocketUpgradeHeader(req)
|
||||||
|
|
||||||
// Check for tracing on request
|
// Check for tracing on request
|
||||||
tracedReq := tracing.NewTracedHTTPRequest(req, log)
|
tracedReq := tracing.NewTracedRequest(req)
|
||||||
return tracedReq, err
|
return tracedReq, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,8 +36,6 @@ var (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ ReadWriteAcker = (*streamReadWriteAcker)(nil)
|
|
||||||
|
|
||||||
// TestQUICServer tests if a quic server accepts and responds to a quic client with the acceptance protocol.
|
// TestQUICServer tests if a quic server accepts and responds to a quic client with the acceptance protocol.
|
||||||
// It also serves as a demonstration for communication with the QUIC connection started by a cloudflared.
|
// It also serves as a demonstration for communication with the QUIC connection started by a cloudflared.
|
||||||
func TestQUICServer(t *testing.T) {
|
func TestQUICServer(t *testing.T) {
|
||||||
|
@ -222,7 +220,7 @@ func quicServer(
|
||||||
|
|
||||||
type mockOriginProxyWithRequest struct{}
|
type mockOriginProxyWithRequest struct{}
|
||||||
|
|
||||||
func (moc *mockOriginProxyWithRequest) ProxyHTTP(w ResponseWriter, tr *tracing.TracedHTTPRequest, isWebsocket bool) error {
|
func (moc *mockOriginProxyWithRequest) ProxyHTTP(w ResponseWriter, tr *tracing.TracedRequest, isWebsocket bool) error {
|
||||||
// These are a series of crude tests to ensure the headers and http related data is transferred from
|
// These are a series of crude tests to ensure the headers and http related data is transferred from
|
||||||
// metadata.
|
// metadata.
|
||||||
r := tr.Request
|
r := tr.Request
|
||||||
|
@ -477,10 +475,9 @@ func TestBuildHTTPRequest(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
log := zerolog.Nop()
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
req, err := buildHTTPRequest(context.Background(), test.connectRequest, test.body, &log)
|
req, err := buildHTTPRequest(context.Background(), test.connectRequest, test.body)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
test.req = test.req.WithContext(req.Context())
|
test.req = test.req.WithContext(req.Context())
|
||||||
assert.Equal(t, test.req, req.Request)
|
assert.Equal(t, test.req, req.Request)
|
||||||
|
@ -489,7 +486,7 @@ func TestBuildHTTPRequest(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (moc *mockOriginProxyWithRequest) ProxyTCP(ctx context.Context, rwa ReadWriteAcker, tcpRequest *TCPRequest) error {
|
func (moc *mockOriginProxyWithRequest) ProxyTCP(ctx context.Context, rwa ReadWriteAcker, tcpRequest *TCPRequest) error {
|
||||||
rwa.AckConnection("")
|
rwa.AckConnection()
|
||||||
io.Copy(rwa, rwa)
|
io.Copy(rwa, rwa)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -355,7 +355,7 @@ func proxyHTTP(originProxy connection.OriginProxy, hostname string) (*http.Respo
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = originProxy.ProxyHTTP(respWriter, tracing.NewTracedHTTPRequest(req, &log), false)
|
err = originProxy.ProxyHTTP(respWriter, tracing.NewTracedRequest(req), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -604,7 +604,7 @@ func TestPersistentConnection(t *testing.T) {
|
||||||
respWriter, err := connection.NewHTTP2RespWriter(req, wsRespReadWriter, connection.TypeWebsocket, &log)
|
respWriter, err := connection.NewHTTP2RespWriter(req, wsRespReadWriter, connection.TypeWebsocket, &log)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
err = originProxy.ProxyHTTP(respWriter, tracing.NewTracedHTTPRequest(req, &log), true)
|
err = originProxy.ProxyHTTP(respWriter, tracing.NewTracedRequest(req), true)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
@ -63,7 +63,7 @@ func NewOriginProxy(
|
||||||
// a simple roundtrip or a tcp/websocket dial depending on ingres rule setup.
|
// a simple roundtrip or a tcp/websocket dial depending on ingres rule setup.
|
||||||
func (p *Proxy) ProxyHTTP(
|
func (p *Proxy) ProxyHTTP(
|
||||||
w connection.ResponseWriter,
|
w connection.ResponseWriter,
|
||||||
tr *tracing.TracedHTTPRequest,
|
tr *tracing.TracedRequest,
|
||||||
isWebsocket bool,
|
isWebsocket bool,
|
||||||
) error {
|
) error {
|
||||||
incrementRequests()
|
incrementRequests()
|
||||||
|
@ -108,7 +108,7 @@ func (p *Proxy) ProxyHTTP(
|
||||||
}
|
}
|
||||||
|
|
||||||
rws := connection.NewHTTPResponseReadWriterAcker(w, req)
|
rws := connection.NewHTTPResponseReadWriterAcker(w, req)
|
||||||
if err := p.proxyStream(tr.ToTracedContext(), rws, dest, originProxy); err != nil {
|
if err := p.proxyStream(req.Context(), rws, dest, originProxy); err != nil {
|
||||||
rule, srv := ruleField(p.ingressRules, ruleNum)
|
rule, srv := ruleField(p.ingressRules, ruleNum)
|
||||||
p.logRequestError(err, cfRay, "", rule, srv)
|
p.logRequestError(err, cfRay, "", rule, srv)
|
||||||
return err
|
return err
|
||||||
|
@ -137,11 +137,9 @@ func (p *Proxy) ProxyTCP(
|
||||||
serveCtx, cancel := context.WithCancel(ctx)
|
serveCtx, cancel := context.WithCancel(ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
tracedCtx := tracing.NewTracedContext(serveCtx, req.CfTraceID, p.log)
|
|
||||||
|
|
||||||
p.log.Debug().Str(LogFieldFlowID, req.FlowID).Msg("tcp proxy stream started")
|
p.log.Debug().Str(LogFieldFlowID, req.FlowID).Msg("tcp proxy stream started")
|
||||||
|
|
||||||
if err := p.proxyStream(tracedCtx, rwa, req.Dest, p.warpRouting.Proxy); err != nil {
|
if err := p.proxyStream(serveCtx, rwa, req.Dest, p.warpRouting.Proxy); err != nil {
|
||||||
p.logRequestError(err, req.CFRay, req.FlowID, "", ingress.ServiceWarpRouting)
|
p.logRequestError(err, req.CFRay, req.FlowID, "", ingress.ServiceWarpRouting)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -162,7 +160,7 @@ func ruleField(ing ingress.Ingress, ruleNum int) (ruleID string, srv string) {
|
||||||
// ProxyHTTPRequest proxies requests of underlying type http and websocket to the origin service.
|
// ProxyHTTPRequest proxies requests of underlying type http and websocket to the origin service.
|
||||||
func (p *Proxy) proxyHTTPRequest(
|
func (p *Proxy) proxyHTTPRequest(
|
||||||
w connection.ResponseWriter,
|
w connection.ResponseWriter,
|
||||||
tr *tracing.TracedHTTPRequest,
|
tr *tracing.TracedRequest,
|
||||||
httpService ingress.HTTPOriginProxy,
|
httpService ingress.HTTPOriginProxy,
|
||||||
isWebsocket bool,
|
isWebsocket bool,
|
||||||
disableChunkedEncoding bool,
|
disableChunkedEncoding bool,
|
||||||
|
@ -213,7 +211,7 @@ func (p *Proxy) proxyHTTPRequest(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add spans to response header (if available)
|
// Add spans to response header (if available)
|
||||||
tr.AddSpans(resp.Header)
|
tr.AddSpans(resp.Header, p.log)
|
||||||
|
|
||||||
err = w.WriteRespHeaders(resp.StatusCode, resp.Header)
|
err = w.WriteRespHeaders(resp.StatusCode, resp.Header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -250,23 +248,17 @@ func (p *Proxy) proxyHTTPRequest(
|
||||||
// proxyStream proxies type TCP and other underlying types if the connection is defined as a stream oriented
|
// proxyStream proxies type TCP and other underlying types if the connection is defined as a stream oriented
|
||||||
// ingress rule.
|
// ingress rule.
|
||||||
func (p *Proxy) proxyStream(
|
func (p *Proxy) proxyStream(
|
||||||
tr *tracing.TracedContext,
|
ctx context.Context,
|
||||||
rwa connection.ReadWriteAcker,
|
rwa connection.ReadWriteAcker,
|
||||||
dest string,
|
dest string,
|
||||||
connectionProxy ingress.StreamBasedOriginProxy,
|
connectionProxy ingress.StreamBasedOriginProxy,
|
||||||
) error {
|
) error {
|
||||||
ctx := tr.Context
|
|
||||||
_, connectSpan := tr.Tracer().Start(ctx, "stream_connect")
|
|
||||||
originConn, err := connectionProxy.EstablishConnection(ctx, dest)
|
originConn, err := connectionProxy.EstablishConnection(ctx, dest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tracing.EndWithErrorStatus(connectSpan, err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
connectSpan.End()
|
|
||||||
|
|
||||||
encodedSpans := tr.GetSpans()
|
if err := rwa.AckConnection(); err != nil {
|
||||||
|
|
||||||
if err := rwa.AckConnection(encodedSpans); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -157,8 +157,7 @@ func testProxyHTTP(proxy connection.OriginProxy) func(t *testing.T) {
|
||||||
req, err := http.NewRequest(http.MethodGet, "http://localhost:8080", nil)
|
req, err := http.NewRequest(http.MethodGet, "http://localhost:8080", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
log := zerolog.Nop()
|
err = proxy.ProxyHTTP(responseWriter, tracing.NewTracedRequest(req), false)
|
||||||
err = proxy.ProxyHTTP(responseWriter, tracing.NewTracedHTTPRequest(req, &log), false)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
for _, tag := range testTags {
|
for _, tag := range testTags {
|
||||||
assert.Equal(t, tag.Value, req.Header.Get(TagHeaderNamePrefix+tag.Name))
|
assert.Equal(t, tag.Value, req.Header.Get(TagHeaderNamePrefix+tag.Name))
|
||||||
|
@ -185,8 +184,7 @@ func testProxyWebsocket(proxy connection.OriginProxy) func(t *testing.T) {
|
||||||
|
|
||||||
errGroup, ctx := errgroup.WithContext(ctx)
|
errGroup, ctx := errgroup.WithContext(ctx)
|
||||||
errGroup.Go(func() error {
|
errGroup.Go(func() error {
|
||||||
log := zerolog.Nop()
|
err = proxy.ProxyHTTP(responseWriter, tracing.NewTracedRequest(req), true)
|
||||||
err = proxy.ProxyHTTP(responseWriter, tracing.NewTracedHTTPRequest(req, &log), true)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
require.Equal(t, http.StatusSwitchingProtocols, responseWriter.Code)
|
require.Equal(t, http.StatusSwitchingProtocols, responseWriter.Code)
|
||||||
|
@ -247,8 +245,7 @@ func testProxySSE(proxy connection.OriginProxy) func(t *testing.T) {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
log := zerolog.Nop()
|
err = proxy.ProxyHTTP(responseWriter, tracing.NewTracedRequest(req), false)
|
||||||
err = proxy.ProxyHTTP(responseWriter, tracing.NewTracedHTTPRequest(req, &log), false)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
require.Equal(t, http.StatusOK, responseWriter.Code)
|
require.Equal(t, http.StatusOK, responseWriter.Code)
|
||||||
|
@ -360,7 +357,7 @@ func runIngressTestScenarios(t *testing.T, unvalidatedIngress []config.Unvalidat
|
||||||
req, err := http.NewRequest(http.MethodGet, test.url, nil)
|
req, err := http.NewRequest(http.MethodGet, test.url, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
err = proxy.ProxyHTTP(responseWriter, tracing.NewTracedHTTPRequest(req, &log), false)
|
err = proxy.ProxyHTTP(responseWriter, tracing.NewTracedRequest(req), false)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, test.expectedStatus, responseWriter.Code)
|
assert.Equal(t, test.expectedStatus, responseWriter.Code)
|
||||||
|
@ -407,7 +404,7 @@ func TestProxyError(t *testing.T) {
|
||||||
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1", nil)
|
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1", nil)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
assert.Error(t, proxy.ProxyHTTP(responseWriter, tracing.NewTracedHTTPRequest(req, &log), false))
|
assert.Error(t, proxy.ProxyHTTP(responseWriter, tracing.NewTracedRequest(req), false))
|
||||||
}
|
}
|
||||||
|
|
||||||
type replayer struct {
|
type replayer struct {
|
||||||
|
@ -685,8 +682,7 @@ func TestConnections(t *testing.T) {
|
||||||
rwa := connection.NewHTTPResponseReadWriterAcker(respWriter, req)
|
rwa := connection.NewHTTPResponseReadWriterAcker(respWriter, req)
|
||||||
err = proxy.ProxyTCP(ctx, rwa, &connection.TCPRequest{Dest: dest})
|
err = proxy.ProxyTCP(ctx, rwa, &connection.TCPRequest{Dest: dest})
|
||||||
} else {
|
} else {
|
||||||
log := zerolog.Nop()
|
err = proxy.ProxyHTTP(respWriter, tracing.NewTracedRequest(req), test.args.connectionType == connection.TypeWebsocket)
|
||||||
err = proxy.ProxyHTTP(respWriter, tracing.NewTracedHTTPRequest(req, &log), test.args.connectionType == connection.TypeWebsocket)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cancel()
|
cancel()
|
||||||
|
|
|
@ -557,7 +557,6 @@ func ServeH2mux(
|
||||||
connIndex,
|
connIndex,
|
||||||
config.Observer,
|
config.Observer,
|
||||||
gracefulShutdownC,
|
gracefulShutdownC,
|
||||||
config.Log,
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !recoverable {
|
if !recoverable {
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
otelContrib "go.opentelemetry.io/contrib/propagators/jaeger"
|
otelContrib "go.opentelemetry.io/contrib/propagators/jaeger"
|
||||||
|
@ -34,9 +33,6 @@ const (
|
||||||
|
|
||||||
MaxErrorDescriptionLen = 100
|
MaxErrorDescriptionLen = 100
|
||||||
traceHttpStatusCodeKey = "upstreamStatusCode"
|
traceHttpStatusCodeKey = "upstreamStatusCode"
|
||||||
|
|
||||||
traceID128bitsWidth = 128 / 4
|
|
||||||
separator = ":"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -70,50 +66,22 @@ func Init(version string) {
|
||||||
cloudflaredVersionAttribute = semconv.ProcessRuntimeVersionKey.String(version)
|
cloudflaredVersionAttribute = semconv.ProcessRuntimeVersionKey.String(version)
|
||||||
}
|
}
|
||||||
|
|
||||||
type TracedHTTPRequest struct {
|
type TracedRequest struct {
|
||||||
*http.Request
|
*http.Request
|
||||||
*cfdTracer
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewTracedHTTPRequest creates a new tracer for the current HTTP request context.
|
|
||||||
func NewTracedHTTPRequest(req *http.Request, log *zerolog.Logger) *TracedHTTPRequest {
|
|
||||||
ctx, exists := extractTrace(req)
|
|
||||||
if !exists {
|
|
||||||
return &TracedHTTPRequest{req, &cfdTracer{trace.NewNoopTracerProvider(), &NoopOtlpClient{}, log}}
|
|
||||||
}
|
|
||||||
return &TracedHTTPRequest{req.WithContext(ctx), newCfdTracer(ctx, log)}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tr *TracedHTTPRequest) ToTracedContext() *TracedContext {
|
|
||||||
return &TracedContext{tr.Context(), tr.cfdTracer}
|
|
||||||
}
|
|
||||||
|
|
||||||
type TracedContext struct {
|
|
||||||
context.Context
|
|
||||||
*cfdTracer
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewTracedHTTPRequest creates a new tracer for the current HTTP request context.
|
|
||||||
func NewTracedContext(ctx context.Context, traceContext string, log *zerolog.Logger) *TracedContext {
|
|
||||||
ctx, exists := extractTraceFromString(ctx, traceContext)
|
|
||||||
if !exists {
|
|
||||||
return &TracedContext{ctx, &cfdTracer{trace.NewNoopTracerProvider(), &NoopOtlpClient{}, log}}
|
|
||||||
}
|
|
||||||
return &TracedContext{ctx, newCfdTracer(ctx, log)}
|
|
||||||
}
|
|
||||||
|
|
||||||
type cfdTracer struct {
|
|
||||||
trace.TracerProvider
|
trace.TracerProvider
|
||||||
exporter InMemoryClient
|
exporter InMemoryClient
|
||||||
log *zerolog.Logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCfdTracer creates a new tracer for the current request context.
|
// NewTracedRequest creates a new tracer for the current request context.
|
||||||
func newCfdTracer(ctx context.Context, log *zerolog.Logger) *cfdTracer {
|
func NewTracedRequest(req *http.Request) *TracedRequest {
|
||||||
|
ctx, exists := extractTrace(req)
|
||||||
|
if !exists {
|
||||||
|
return &TracedRequest{req, trace.NewNoopTracerProvider(), &NoopOtlpClient{}}
|
||||||
|
}
|
||||||
mc := new(InMemoryOtlpClient)
|
mc := new(InMemoryOtlpClient)
|
||||||
exp, err := otlptrace.New(ctx, mc)
|
exp, err := otlptrace.New(req.Context(), mc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &cfdTracer{trace.NewNoopTracerProvider(), &NoopOtlpClient{}, log}
|
return &TracedRequest{req, trace.NewNoopTracerProvider(), &NoopOtlpClient{}}
|
||||||
}
|
}
|
||||||
tp := tracesdk.NewTracerProvider(
|
tp := tracesdk.NewTracerProvider(
|
||||||
// We want to dump to in-memory exporter immediately
|
// We want to dump to in-memory exporter immediately
|
||||||
|
@ -130,43 +98,36 @@ func newCfdTracer(ctx context.Context, log *zerolog.Logger) *cfdTracer {
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
|
|
||||||
return &cfdTracer{tp, mc, log}
|
return &TracedRequest{req.WithContext(ctx), tp, mc}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cft *cfdTracer) Tracer() trace.Tracer {
|
func (cft *TracedRequest) Tracer() trace.Tracer {
|
||||||
return cft.TracerProvider.Tracer(tracerInstrumentName)
|
return cft.TracerProvider.Tracer(tracerInstrumentName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSpans returns the spans as base64 encoded string of protobuf otlp traces.
|
// Spans returns the spans as base64 encoded protobuf otlp traces.
|
||||||
func (cft *cfdTracer) GetSpans() (enc string) {
|
func (cft *TracedRequest) AddSpans(headers http.Header, log *zerolog.Logger) {
|
||||||
|
if headers == nil {
|
||||||
|
log.Error().Msgf("provided headers map is nil")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
enc, err := cft.exporter.Spans()
|
enc, err := cft.exporter.Spans()
|
||||||
switch err {
|
switch err {
|
||||||
case nil:
|
case nil:
|
||||||
break
|
break
|
||||||
case errNoTraces:
|
case errNoTraces:
|
||||||
cft.log.Error().Err(err).Msgf("expected traces to be available")
|
log.Error().Err(err).Msgf("expected traces to be available")
|
||||||
return
|
return
|
||||||
case errNoopTracer:
|
case errNoopTracer:
|
||||||
return // noop tracer has no traces
|
return // noop tracer has no traces
|
||||||
default:
|
default:
|
||||||
cft.log.Error().Err(err)
|
log.Error().Err(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddSpans assigns spans as base64 encoded protobuf otlp traces to provided
|
|
||||||
// HTTP headers.
|
|
||||||
func (cft *cfdTracer) AddSpans(headers http.Header) {
|
|
||||||
if headers == nil {
|
|
||||||
cft.log.Error().Msgf("provided headers map is nil")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
enc := cft.GetSpans()
|
|
||||||
// No need to add header if no traces
|
// No need to add header if no traces
|
||||||
if enc == "" {
|
if enc == "" {
|
||||||
cft.log.Error().Msgf("no traces provided and no error from exporter")
|
log.Error().Msgf("no traces provided and no error from exporter")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,33 +166,6 @@ func endSpan(span trace.Span, upstreamStatusCode int, spanStatusCode codes.Code,
|
||||||
span.End()
|
span.End()
|
||||||
}
|
}
|
||||||
|
|
||||||
// extractTraceFromString will extract the trace information from the provided
|
|
||||||
// propagated trace string context.
|
|
||||||
func extractTraceFromString(ctx context.Context, trace string) (context.Context, bool) {
|
|
||||||
if trace == "" {
|
|
||||||
return ctx, false
|
|
||||||
}
|
|
||||||
// Jaeger specific separator
|
|
||||||
parts := strings.Split(trace, separator)
|
|
||||||
if len(parts) != 4 {
|
|
||||||
return ctx, false
|
|
||||||
}
|
|
||||||
if parts[0] == "" {
|
|
||||||
return ctx, false
|
|
||||||
}
|
|
||||||
// Correctly left pad the trace to a length of 32
|
|
||||||
if len(parts[0]) < traceID128bitsWidth {
|
|
||||||
left := traceID128bitsWidth - len(parts[0])
|
|
||||||
parts[0] = strings.Repeat("0", left) + parts[0]
|
|
||||||
trace = strings.Join(parts, separator)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Override the 'cf-trace-id' as 'uber-trace-id' so the jaeger propagator can extract it.
|
|
||||||
traceHeader := map[string]string{TracerContextNameOverride: trace}
|
|
||||||
remoteCtx := otel.GetTextMapPropagator().Extract(ctx, propagation.MapCarrier(traceHeader))
|
|
||||||
return remoteCtx, true
|
|
||||||
}
|
|
||||||
|
|
||||||
// extractTrace attempts to check for a cf-trace-id from a request and return the
|
// extractTrace attempts to check for a cf-trace-id from a request and return the
|
||||||
// trace context with the provided http.Request.
|
// trace context with the provided http.Request.
|
||||||
func extractTrace(req *http.Request) (context.Context, bool) {
|
func extractTrace(req *http.Request) (context.Context, bool) {
|
||||||
|
|
|
@ -14,42 +14,38 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewCfTracer(t *testing.T) {
|
func TestNewCfTracer(t *testing.T) {
|
||||||
log := zerolog.Nop()
|
|
||||||
req := httptest.NewRequest("GET", "http://localhost", nil)
|
req := httptest.NewRequest("GET", "http://localhost", nil)
|
||||||
req.Header.Add(TracerContextName, "14cb070dde8e51fc5ae8514e69ba42ca:b38f1bf5eae406f3:0:1")
|
req.Header.Add(TracerContextName, "14cb070dde8e51fc5ae8514e69ba42ca:b38f1bf5eae406f3:0:1")
|
||||||
tr := NewTracedHTTPRequest(req, &log)
|
tr := NewTracedRequest(req)
|
||||||
assert.NotNil(t, tr)
|
assert.NotNil(t, tr)
|
||||||
assert.IsType(t, tracesdk.NewTracerProvider(), tr.TracerProvider)
|
assert.IsType(t, tracesdk.NewTracerProvider(), tr.TracerProvider)
|
||||||
assert.IsType(t, &InMemoryOtlpClient{}, tr.exporter)
|
assert.IsType(t, &InMemoryOtlpClient{}, tr.exporter)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewCfTracerMultiple(t *testing.T) {
|
func TestNewCfTracerMultiple(t *testing.T) {
|
||||||
log := zerolog.Nop()
|
|
||||||
req := httptest.NewRequest("GET", "http://localhost", nil)
|
req := httptest.NewRequest("GET", "http://localhost", nil)
|
||||||
req.Header.Add(TracerContextName, "1241ce3ecdefc68854e8514e69ba42ca:b38f1bf5eae406f3:0:1")
|
req.Header.Add(TracerContextName, "1241ce3ecdefc68854e8514e69ba42ca:b38f1bf5eae406f3:0:1")
|
||||||
req.Header.Add(TracerContextName, "14cb070dde8e51fc5ae8514e69ba42ca:b38f1bf5eae406f3:0:1")
|
req.Header.Add(TracerContextName, "14cb070dde8e51fc5ae8514e69ba42ca:b38f1bf5eae406f3:0:1")
|
||||||
tr := NewTracedHTTPRequest(req, &log)
|
tr := NewTracedRequest(req)
|
||||||
assert.NotNil(t, tr)
|
assert.NotNil(t, tr)
|
||||||
assert.IsType(t, tracesdk.NewTracerProvider(), tr.TracerProvider)
|
assert.IsType(t, tracesdk.NewTracerProvider(), tr.TracerProvider)
|
||||||
assert.IsType(t, &InMemoryOtlpClient{}, tr.exporter)
|
assert.IsType(t, &InMemoryOtlpClient{}, tr.exporter)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewCfTracerNilHeader(t *testing.T) {
|
func TestNewCfTracerNilHeader(t *testing.T) {
|
||||||
log := zerolog.Nop()
|
|
||||||
req := httptest.NewRequest("GET", "http://localhost", nil)
|
req := httptest.NewRequest("GET", "http://localhost", nil)
|
||||||
req.Header[http.CanonicalHeaderKey(TracerContextName)] = nil
|
req.Header[http.CanonicalHeaderKey(TracerContextName)] = nil
|
||||||
tr := NewTracedHTTPRequest(req, &log)
|
tr := NewTracedRequest(req)
|
||||||
assert.NotNil(t, tr)
|
assert.NotNil(t, tr)
|
||||||
assert.IsType(t, trace.NewNoopTracerProvider(), tr.TracerProvider)
|
assert.IsType(t, trace.NewNoopTracerProvider(), tr.TracerProvider)
|
||||||
assert.IsType(t, &NoopOtlpClient{}, tr.exporter)
|
assert.IsType(t, &NoopOtlpClient{}, tr.exporter)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewCfTracerInvalidHeaders(t *testing.T) {
|
func TestNewCfTracerInvalidHeaders(t *testing.T) {
|
||||||
log := zerolog.Nop()
|
|
||||||
req := httptest.NewRequest("GET", "http://localhost", nil)
|
req := httptest.NewRequest("GET", "http://localhost", nil)
|
||||||
for _, test := range [][]string{nil, {""}} {
|
for _, test := range [][]string{nil, {""}} {
|
||||||
req.Header[http.CanonicalHeaderKey(TracerContextName)] = test
|
req.Header[http.CanonicalHeaderKey(TracerContextName)] = test
|
||||||
tr := NewTracedHTTPRequest(req, &log)
|
tr := NewTracedRequest(req)
|
||||||
assert.NotNil(t, tr)
|
assert.NotNil(t, tr)
|
||||||
assert.IsType(t, trace.NewNoopTracerProvider(), tr.TracerProvider)
|
assert.IsType(t, trace.NewNoopTracerProvider(), tr.TracerProvider)
|
||||||
assert.IsType(t, &NoopOtlpClient{}, tr.exporter)
|
assert.IsType(t, &NoopOtlpClient{}, tr.exporter)
|
||||||
|
@ -57,10 +53,9 @@ func TestNewCfTracerInvalidHeaders(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddingSpansWithNilMap(t *testing.T) {
|
func TestAddingSpansWithNilMap(t *testing.T) {
|
||||||
log := zerolog.Nop()
|
|
||||||
req := httptest.NewRequest("GET", "http://localhost", nil)
|
req := httptest.NewRequest("GET", "http://localhost", nil)
|
||||||
req.Header.Add(TracerContextName, "14cb070dde8e51fc5ae8514e69ba42ca:b38f1bf5eae406f3:0:1")
|
req.Header.Add(TracerContextName, "14cb070dde8e51fc5ae8514e69ba42ca:b38f1bf5eae406f3:0:1")
|
||||||
tr := NewTracedHTTPRequest(req, &log)
|
tr := NewTracedRequest(req)
|
||||||
|
|
||||||
exporter := tr.exporter.(*InMemoryOtlpClient)
|
exporter := tr.exporter.(*InMemoryOtlpClient)
|
||||||
|
|
||||||
|
@ -70,5 +65,5 @@ func TestAddingSpansWithNilMap(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// a panic shouldn't occur
|
// a panic shouldn't occur
|
||||||
tr.AddSpans(nil)
|
tr.AddSpans(nil, &zerolog.Logger{})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue