Skip to content

GRPC TraceLayer example with custom methods #366

Open
@starsoccer

Description

@starsoccer

Following the example here, I am able to create a very bare bones example of logging with grpc, but Id like to be able to define custom methods for on_request, and on_response but cant seem to figure out the correct way to do so.

Ive tried tweaking the ServiceBuilder in the above example to look as follows:

    let channel = ServiceBuilder::new()
        // Decompress response bodies
        .layer(DecompressionLayer::new())
        // Set a `User-Agent` header
        .layer(SetRequestHeaderLayer::overriding(
            header::USER_AGENT,
            HeaderValue::from_static("tonic-key-value-store"),
        ))
        // Log all requests and responses
        .layer(
           /// SECTION I CHANGED BELOW
            TraceLayer::new_for_grpc().make_span_with(DefaultMakeSpan::new().include_headers(true))
            .on_request(|request: &Request<Body>, _span: &Span| {
                tracing::debug!("started")
            }),
        )
        // Build our final `Service`
        .service(channel);

This provides an error saying:

error[E0631]: type mismatch in closure arguments
   --> src\main.rs:311:8
    |
303 |             .on_request(|request: &Request<Body>, _span: &Span| {
    |                         --------------------------------------- found signature defined here
...
311 |     Ok(KeyValueStoreClient::new(channel))
    |        ^^^^^^^^^^^^^^^^^^^^^^^^ expected due to this
    |
    = note: expected closure signature `for<'r, 's> fn(&'r hyper::Request<http_body::combinators::box_body::BoxBody<bytes::Bytes, Status>>, &'s Span) -> _`
               found closure signature `for<'r, 's> fn(&'r tonic::Request<hyper::Body>, &'s Span) -> _`
    = note: required for `[closure@src\main.rs:303:25: 303:64]` to implement `OnRequest<http_body::combinators::box_body::BoxBody<bytes::Bytes, Status>>`
    = note: required for `tower_http::trace::Trace<Channel, SharedClassifier<GrpcErrorsAsFailures>, DefaultMakeSpan, [closure@src\main.rs:303:25: 303:64]>` to implement `Service<hyper::Request<http_body::combinators::box_body::BoxBody<bytes::Bytes, Status>>>`
    = note: required for `Decompression<SetRequestHeader<tower_http::trace::Trace<Channel, SharedClassifier<GrpcErrorsAsFailures>, DefaultMakeSpan, [closure@src\main.rs:303:25: 303:64]>, HeaderValue>>` to implement `GrpcService<http_body::combinators::box_body::BoxBody<bytes::Bytes, Status>>`

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