Skip to content

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

Merged
itowlson merged 1 commit into
spinframework:mainfrom
ChihweiLHBird:zhiwei/otel-report-content-length-header
Apr 19, 2026
Merged

Add content-length header attributes to outbound HTTP spans#3459
itowlson merged 1 commit into
spinframework: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 thread crates/factor-outbound-http/src/wasi.rs
@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 thread crates/factor-outbound-http/src/wasi.rs
@itowlson itowlson merged commit 31df154 into spinframework:main Apr 19, 2026
17 checks passed
@ChihweiLHBird ChihweiLHBird deleted the zhiwei/otel-report-content-length-header branch April 20, 2026 01:03
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.

3 participants