Skip to content

impl<T: Stream> Stream for Instrumented<T> #2540

Closed
@evgeniy-terekhin

Description

@evgeniy-terekhin

it would be nice to have impl<T: Stream> Stream for Instrumented<T> just as we have impl<T: Stream> Stream for Instruemnted<T> otherwise it is really unergonomic to propagate tracing spans into streams

I have this use case: actix-web streaming response

let span = tracing::Span::current();
Ok(
    HttpResponse::Ok().streaming(ReceiverStream::new(rx).map_ok(web::Bytes::from).map_err(
        move |err| {
            span.in_scope(|| {
                tracing::error!("some error happened: {err:#}");
            });

            service::errors::internal_server_error()
        },
    )),
)

what I would like to have is

use tracing::Instrument;

Ok(HttpResponse::Ok().streaming(
    ReceiverStream::new(rx)
        .in_current_span()
        .map_ok(web::Bytes::from)
        .map_err(|err| {
            tracing::error!("some error happened: {err:#}");

            service::errors::internal_server_error()
        }),
))

however this doesn't compile

error[E0599]: the method `map_ok` exists for struct `Instrumented<ReceiverStream<Result<Vec<u8>, anyhow::Error>>>`, but its trait bounds were not satisfied
    |
71  |               .map_ok(web::Bytes::from)
    |                ^^^^^^ method cannot be called on `Instrumented<ReceiverStream<Result<Vec<u8>, anyhow::Error>>>` due to unsatisfied trait bounds
    |
   ::: /home/evgeniy/.cargo/registry/src/github.com-1ecc6299db9ec823/tracing-0.1.37/src/instrument.rs:247:1
    |
247 | / pin_project! {
248 | |     /// A [`Future`] that has been instrumented with a `tracing` [`Span`].
249 | |     ///
250 | |     /// This type is returned by the [`Instrument`] extension trait. See that
...   |
261 | |     }
262 | | }
    | | -
    | | |
    | |_doesn't satisfy `_: TryStreamExt`
    |   doesn't satisfy `_: TryStream`
    |
    = note: the following trait bounds were not satisfied:
            `Instrumented<ReceiverStream<Result<Vec<u8>, anyhow::Error>>>: TryStream`
            which is required by `Instrumented<ReceiverStream<Result<Vec<u8>, anyhow::Error>>>: TryStreamExt`
            `&Instrumented<ReceiverStream<Result<Vec<u8>, anyhow::Error>>>: TryStream`
            which is required by `&Instrumented<ReceiverStream<Result<Vec<u8>, anyhow::Error>>>: TryStreamExt`
            `&mut Instrumented<ReceiverStream<Result<Vec<u8>, anyhow::Error>>>: TryStream`
            which is required by `&mut Instrumented<ReceiverStream<Result<Vec<u8>, anyhow::Error>>>: TryStreamExt`

warning: unused import: `futures_util::strea

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions