Skip to content

Conversation

@Samze
Copy link
Contributor

@Samze Samze commented Nov 18, 2024

Fixes #3309

Description of the Change

The CLI now sets the B3_TRACE headers as per https://docs.cloudfoundry.org/adminguide/distributed-tracing.html#unique-trace-id.

Notes:

  • Automatically sets the same X-B3-Traceid header for all API requests initiated by that CF command. (e.g. use the same header for all api calls for the command cf apps).
  • Automatically generates a new X-B3-Spanid per API request.
  • Adds the new environment variable CF_B3_TRACE_ID that allows the X-B3-Traceid to be overridden.
  • Sets this header for all clients used in the CLI (Cloud controller/Router/UAA).
  • As per the go-router behaviour described in the linked article above , this will set the X-Vcap-Request-Id to the same header as the X-B3-Traceid (in uuid v4 format) and will work with W3C headers if `router.tracing.enable_w3c is set.
  • Headers can be discovered via CF_TRACE or -v

Why Is This PR Valuable?

See #3309

Examples

With auto-generated headers:

 ./out/cf apps -v
Getting apps in org org-1 / space space-1 as admin...

REQUEST: [2025-01-21T14:04:19-07:00]
GET /v3/apps?order_by=name&per_page=5000&space_guids=e643fc8e-a90b-4a40-b51e-5bf1a143868e HTTP/1.1
Host: api.sys.tas.z00d47cae.shepherd.lease
Accept: application/json
Authorization: [PRIVATE DATA HIDDEN]
Content-Type: application/json
User-Agent: cf/9.0.0+b7e079787.2025-01-21 (go1.23.1; arm64 darwin)
X-B3-Spanid: 83c5b12313c7dd54
X-B3-Traceid: f6fc6d7e08c443fd81f34d643890d05d
[application/json Content Hidden]

RESPONSE: [2025-01-21T14:04:20-07:00]
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Tue, 21 Jan 2025 21:04:20 GMT
Referrer-Policy: strict-origin-when-cross-origin
Vary: Accept
X-B3-Spanid: 83c5b12313c7dd54
X-B3-Traceid: f6fc6d7e08c443fd81f34d643890d05d
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Frame-Options: SAMEORIGIN
X-Permitted-Cross-Domain-Policies: none
X-Runtime: 0.009293
X-Vcap-Request-Id: f6fc6d7e-08c4-43fd-81f3-4d643890d05d::ab389f8c-a57e-4a53-a82b-8395087b7cb0
X-Xss-Protection: 1; mode=block

With environment variable override:

CF_B3_TRACE_ID=e7edced4f7014a7080bb7bb5ce1e358d ./out/cf apps -v
Getting apps in org org-1 / space space-1 as admin...

REQUEST: [2025-01-21T14:04:49-07:00]
GET /v3/apps?order_by=name&per_page=5000&space_guids=e643fc8e-a90b-4a40-b51e-5bf1a143868e HTTP/1.1
Host: api.sys.tas.z00d47cae.shepherd.lease
Accept: application/json
Authorization: [PRIVATE DATA HIDDEN]
Content-Type: application/json
User-Agent: cf/9.0.0+b7e079787.2025-01-21 (go1.23.1; arm64 darwin)
X-B3-Spanid: e392eb41959dd48a
X-B3-Traceid: e7edced4f7014a7080bb7bb5ce1e358d
[application/json Content Hidden]

RESPONSE: [2025-01-21T14:04:50-07:00]
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Tue, 21 Jan 2025 21:04:50 GMT
Referrer-Policy: strict-origin-when-cross-origin
Vary: Accept
X-B3-Spanid: e392eb41959dd48a
X-B3-Traceid: e7edced4f7014a7080bb7bb5ce1e358d
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Frame-Options: SAMEORIGIN
X-Permitted-Cross-Domain-Policies: none
X-Runtime: 0.007993
X-Vcap-Request-Id: e7edced4-f701-4a70-80bb-7bb5ce1e358d::ae3e797f-104d-4fa7-a983-6ee2ed9f6e8a
X-Xss-Protection: 1; mode=block

@Samze Samze force-pushed the 3309_trace_ids branch 7 times, most recently from 1683294 to 8057cbf Compare November 19, 2024 22:14
@Samze Samze force-pushed the 3309_trace_ids branch 4 times, most recently from 37b425f to 2203c4e Compare November 25, 2024 22:37
Copy link
Member

@a-b a-b left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@joaopapereira
Copy link
Contributor

@Samze any update on this PR?

@Samze
Copy link
Contributor Author

Samze commented Jan 21, 2025

@Samze any update on this PR?

This dropped off my radar over the holidays, hope to pick this back up and finish it off in the next week or two.

@Samze Samze changed the title WIP: Add B3 trace IDs to cf cli commands Add B3 trace IDs to cf cli commands Mar 27, 2025
@Samze Samze marked this pull request as ready for review March 27, 2025 01:42
Copy link
Member

@gururajsh gururajsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@a-b a-b merged commit 1f3fb56 into main Mar 27, 2025
20 checks passed
@a-b a-b deleted the 3309_trace_ids branch March 27, 2025 14:54
@a-b a-b changed the title Add B3 trace IDs to cf cli commands [main] Add B3 trace IDs to cf cli commands Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate and send b3 tracing headers per CLI command

4 participants