Skip to content

Commit

Permalink
Add B3 trace IDs to cf cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Samze committed Nov 18, 2024
1 parent dfe2201 commit bef3fbf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
39 changes: 39 additions & 0 deletions api/cloudcontroller/wrapper/b3_trace_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package wrapper

import (
"code.cloudfoundry.org/cli/api/cloudcontroller"
)

// TODO
// 1. do not overwrite headers if explicitly set
// 2. headers should go on other clients (uaa/routing) as well (with same value)
// 3. tests
// 4. what to set span to?

// TraceHeaderRequest is a wrapper that adds b3 trace headers to requests.
type TraceHeaderRequest struct {
b3trace string
b3span string
connection cloudcontroller.Connection
}

// NewTraceHeaderRequest returns a pointer to a TraceHeaderRequest wrapper.
func NewTraceHeaderRequest(trace, span string) *TraceHeaderRequest {
return &TraceHeaderRequest{
b3trace: trace,
b3span: span,
}
}

// Add tracing headers
func (t *TraceHeaderRequest) Make(request *cloudcontroller.Request, passedResponse *cloudcontroller.Response) error {
request.Header.Add("X-B3-Traceid", t.b3trace)
request.Header.Add("X-B3-Spanid", t.b3span)
return t.connection.Make(request, passedResponse)
}

// Wrap sets the connection in the TraceHeaderRequest and returns itself.
func (t *TraceHeaderRequest) Wrap(innerconnection cloudcontroller.Connection) cloudcontroller.Connection {
t.connection = innerconnection
return t
}
13 changes: 13 additions & 0 deletions command/v7/shared/new_clients.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package shared

import (
"crypto/rand"
"encoding/hex"

"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
ccWrapper "code.cloudfoundry.org/cli/api/cloudcontroller/wrapper"
Expand Down Expand Up @@ -46,6 +49,7 @@ func NewWrappedCloudControllerClient(config command.Config, ui command.UI, extra
ccWrappers = append(ccWrappers, ccWrapper.NewRequestLogger(ui.RequestLoggerFileWriter(location)))
}

ccWrappers = append(ccWrappers, ccWrapper.NewTraceHeaderRequest(generateB3TraceID(), generateB3TraceID()))
ccWrappers = append(ccWrappers, extraWrappers...)
ccWrappers = append(ccWrappers, ccWrapper.NewRetryRequest(config.RequestRetryCount()))

Expand Down Expand Up @@ -153,3 +157,12 @@ func connectToCF(config command.Config, ui command.UI, ccClient *ccv3.Client, mi

return ccClient, nil
}

func generateB3TraceID() string {
traceIDBytes := make([]byte, 16)
if _, err := rand.Read(traceIDBytes); err != nil {
panic(err)
}

return hex.EncodeToString(traceIDBytes)
}

0 comments on commit bef3fbf

Please sign in to comment.