Skip to content

Add content-length header attributes to outbound HTTP spans#3459

Open
ChihweiLHBird wants to merge 1 commit intospinframework:mainfrom
ChihweiLHBird:zhiwei/otel-report-content-length-header
Open

Add content-length header attributes to outbound HTTP spans#3459
ChihweiLHBird wants to merge 1 commit intospinframework:mainfrom
ChihweiLHBird:zhiwei/otel-report-content-length-header

Conversation

@ChihweiLHBird
Copy link
Copy Markdown
Contributor

@ChihweiLHBird ChihweiLHBird commented Apr 14, 2026

Description

Records http.request.header.content-length and http.response.header.content-length on outbound HTTP spans when the header is present.

Uses set_attribute() from tracing_opentelemetry::OpenTelemetrySpanExt because the OTel attribute names contain hyphens, which tracing's #[instrument] field system cannot represent.

I didn't implement a test because I thought it's not worth to add another expensive integration test case just for a new field sent to OTel collector.

I used a small HTTP component and otel-desktop-viewer to verify this works as expected:

#[http_component]
async fn handle_test_spin_rust(req: Request) -> anyhow::Result<impl IntoResponse> {
    println!("Handling request to {:?}", req.header("spin-full-url"));

    let outbound_request = Request::get("https://httpbin.org/get")
        .header("accept", "application/json")
        .build();
    let outbound_response: Response = spin_sdk::http::send(outbound_request)
        .await
        .context("failed to call https://httpbin.org/get")?;
    let outbound_status = outbound_response.status();
    let outbound_body_len = outbound_response.body().len();
    
    Ok(Response::builder()
        .status(200)
        .header("content-type", "text/plain")
        .body(format!(
            "Hello World!\nhttpbin.org/get status: {outbound_status}\nhttpbin.org/get body bytes: {outbound_body_len}"
        ))
        .build())
}

Addresses part of #3188 (Option 1).

Changes

  • crates/factor-outbound-http/Cargo.toml — added tracing-opentelemetry dependency
  • crates/factor-outbound-http/src/wasi.rs — added record_content_length_header helper and call sites for request (after interceptor) and response

Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
Comment on lines +462 to +467
record_content_length_header(
&span,
request.headers(),
"http.request.header.content-length",
);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I intentionally put this after interceptor, but I wasn't confident about this decision. I would like to hear more opinion on this.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Assuming you've manually tested and values look correct this seems fine to me. Before after the interceptor shouldn't matter too much from my understanding.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you!

@itowlson itowlson requested review from calebschoepp and lann April 14, 2026 19:32
Copy link
Copy Markdown
Collaborator

@calebschoepp calebschoepp left a comment

Choose a reason for hiding this comment

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

This change seems reasonable to me. LGTM

Comment on lines +462 to +467
record_content_length_header(
&span,
request.headers(),
"http.request.header.content-length",
);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Assuming you've manually tested and values look correct this seems fine to me. Before after the interceptor shouldn't matter too much from my understanding.

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.

2 participants