tower-rs / tower-rs/tower-http

GRPC TraceLayer example with custom methods

Open
#366 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
913
Forks
231
Avg merge
1d 20h
Merged PRs (30d)
8

Description

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>>`

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with examples/tonic-key-value-store/src/main.rs at the linked ServiceBuilder and compare the callback types with the compiler’s expected signature. Determine how the example should demonstrate custom on_request and on_response callbacks, then verify the example builds and emits the intended logs.

Written by the indexing model from the issue text.

Assessment

Tech stack
grpc, rust
Domain
api, observability
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.