nlopes / actix-web-prom

Actix-web middleware to expose Prometheus metrics
MIT License
89 stars 56 forks source link

Compiler error in 0.6.0.beta-3 #60

Closed Ploppz closed 2 years ago

Ploppz commented 2 years ago

To reproduce the issue I'm having, create a new crate with the following dependencies (possibly not all required)

[dependencies]
actix-web = "4.0.0-beta.8"
actix-cors = "0.6.0-beta.3"
actix-web-prom = "0.6.0-beta.3"

It gives the following compiler error:

error[E0432]: unresolved imports `actix_web::body::AnyBody`, `actix_web::dev::BodySize`, `actix_web::dev::MessageBody`, `actix_web::http::HeaderValue`
   --> /home/erlend/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-prom-0.6.0-beta.3/src/lib.rs:218:5
    |
218 |     body::AnyBody,
    |     ^^^^^^^^^^^^^ no `AnyBody` in `body`
219 |     dev::{BodySize, MessageBody, Service, ServiceRequest, ServiceResponse, Transform},
    |           ^^^^^^^^  ^^^^^^^^^^^ no `MessageBody` in `dev`
    |           |
    |           no `BodySize` in `dev`
220 |     http::{header::CONTENT_TYPE, HeaderValue, Method, StatusCode},
    |                                  ^^^^^^^^^^^ no `HeaderValue` in `http`

For more information about this error, try `rustc --explain E0432`.
error: could not compile `actix-web-prom` due to previous error

rustc:

rustc 1.58.0-nightly (dd549dcab 2021-11-25)
nlopes commented 2 years ago

I just verified this. Fix incoming within the next few days.

nlopes commented 2 years ago

Can you try with https://github.com/nlopes/actix-web-prom/pull/61?

Would love to see if it fixes your issues.

Ploppz commented 2 years ago

Thanks. I tried but I can't proceed to make it work due to https://github.com/actix/actix-web/issues/2533 And in my application I cannot seem to find a combination of version numbers (or git rev) for actix-web, actix-cors, actix-rt, actix-web-prom, and actix-web-middleware-keycloak-auth, that work together. Edit: I did find a combination that works per dependency resolution but still running into aforementioned issue.

Ploppz commented 2 years ago

Alright I got the dependency puzzle working. Now I have one problem with prometheus:

error[E0271]: type mismatch resolving `<<impl ServiceFactory<ServiceRequest> as ServiceFactory<ServiceRequest>>::Service as actix_web::dev::Service<ServiceRequest>>::Response == ServiceResponse`
   --> src/main.rs:194:19
    |
194 |             .wrap(prometheus.clone())
    |              ---- ^^^^^^^^^^^^^^^^^^ expected enum `EitherBody`, found struct `BoxBody`
    |              |
    |              required by a bound introduced by this call
    |
    = note: expected struct `ServiceResponse<EitherBody<BoxBody>>`
               found struct `ServiceResponse<BoxBody>`
    = note: required because of the requirements on the impl of `Transform<<impl ServiceFactory<ServiceRequest> as ServiceFactory<ServiceRequest>>::Service, ServiceRequest>` for `PrometheusMetrics`

(where prometheus: PrometheusMetrics)

Relevant dependencies:

actix-web = "=4.0.0-beta.14"
actix-http = "=3.0.0-beta.15" # Only in order to pin the dep - TODO remove when actix-web 4 is released
actix-web-prom = { git = "https://github.com/nlopes/actix-web-prom.git", branch = "nlopes/bump-to-actix-web-14" }
prometheus = "0.13.0"
mcronce commented 2 years ago

I don't know how helpful it is to mention, but I'm seeing the same issue as @Ploppz after resolving the dependency mismatches

AEnterprise commented 2 years ago

@nlopes is there any progress on this? also running into this

nlopes commented 2 years ago

@nlopes is there any progress on this? also running into this

Is this with the branch nlopes/bump-to-actix-web-14? If it is, would you mind pointing me to an open source repo where I can replicate the issue?

AEnterprise commented 2 years ago

@nlopes indeed there are 2 issues with it:

1) doesn't work with the latest beta, AnyBody no longer exists 2) trying to pass it as a middleware doesn't work, the type doesn't match

as for an open source repo, does this one count? the example in the readme is enough to trigger both problems

nlopes commented 2 years ago

@AEnterprise do you mind telling me your rust version? As it stands, I can run all of the examples. For ease of debugging this, I've added all 3 examples from the README to an examples/ folder. Can you run each one (and for which rust version) and provide the output?

AEnterprise commented 2 years ago

with the update to that branch the examples work again, thanks!

Ploppz commented 2 years ago

The examples run fine, but in my application I get

error[E0271]: type mismatch resolving `<<impl ServiceFactory<ServiceRequest> as ServiceFactory<ServiceRequest>>::Service as actix_web::dev::Service<ServiceRequest>>::Response == ServiceResponse`
   --> src/main.rs:194:19
    |
194 |             .wrap(prometheus.clone())
    |              ---- ^^^^^^^^^^^^^^^^^^ expected enum `EitherBody`, found struct `BoxBody`
    |              |
    |              required by a bound introduced by this call
    |
    = note: expected struct `ServiceResponse<EitherBody<BoxBody>>`
               found struct `ServiceResponse<BoxBody>`
    = note: required because of the requirements on the impl of `Transform<<impl ServiceFactory<ServiceRequest> as ServiceFactory<ServiceRequest>>::Service, ServiceRequest>` for `PrometheusMetrics`

I tried my best to lock the right versions of all relevant deps:

actix-web = "=4.0.0-beta.16"
actix-http = "=3.0.0-beta.17"
actix-web-prom = { git = "https://github.com/nlopes/actix-web-prom.git", branch = "nlopes/bump-to-actix-web-14" }
prometheus = "0.13.0"

Any idea how I can fix this?

nlopes commented 2 years ago

Can you show a bit more of the surrounding code there? Or point me to a commit somewhere where I can take a look if possible. Would love to help but I'm a bit at a loss at this point.

Ploppz commented 2 years ago

I managed to make a minimal example. The problem only arises when actix_cors::Cors is wrapped before the middleware of this crate.

[dependencies]
actix-web = "=4.0.0-beta.19"
actix-http = "=3.0.0-beta.18"
actix-rt = "2.5"
actix-web-prom = { git = "https://github.com/nlopes/actix-web-prom.git", branch = "nlopes/bump-to-actix-web-14" }
actix-cors = "0.6.0-beta.7"
prometheus = "0.13.0"

main.rs:

use actix_web::{App, HttpServer};
use actix_web_prom::PrometheusMetricsBuilder;
use prometheus::Registry;

#[actix_rt::main]
async fn main() {
    let registry = Registry::new();

    let prometheus = PrometheusMetricsBuilder::new("public_api")
        .endpoint("/api/v1/metrics")
        .registry(registry)
        .build()
        .unwrap();

    HttpServer::new(move || {
        App::new()
            .wrap(
                actix_cors::Cors::permissive()
                    .allowed_methods(vec!["GET", "POST", "PUT", "OPTIONS", "DELETE"])
                    .max_age(3600),
            )
            .wrap(prometheus.clone())
    })
    .bind("")
    .unwrap()
    .run()
    .await;
}
Ploppz commented 2 years ago

Fixed if I reorder my middlewares. I guess it's an issue in actix_cors?

nlopes commented 2 years ago

It might also be here. This is great to know. I'll take a look this weekend just to ensure I'm not doing something silly that's preventing another middleware from working.