tokio-rs/tokio-metrics

compatibility with tokio

daixiang0 opened this issue · 4 comments

I want to print metrics of Tokio example with master HEAD, then I get below error:


error[E0308]: mismatched types
    --> examples/tinyhttp.rs:40:51
     |
40   |         let runtime_monitor = RuntimeMonitor::new(&handle);
     |                               ------------------- ^^^^^^^ expected struct `tokio::runtime::handle::Handle`, found struct `Handle`
     |                               |
     |                               arguments to this function are incorrect
     |
     = note: expected reference `&tokio::runtime::handle::Handle`
                found reference `&Handle`
     = note: perhaps two different versions of crate `tokio` are being used?
note: associated function defined here
    --> /root/github/tokio-metrics/src/runtime.rs:1015:12
     |
1015 |     pub fn new(runtime: &runtime::Handle) -> RuntimeMonitor {
     |            ^^^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `examples` due to previous error

Full change in Tokio:

diff --git a/.cargo/config b/.cargo/config
index df885898..71097e3c 100644
--- a/.cargo/config
+++ b/.cargo/config
@@ -1,2 +1,5 @@
+[build]
+rustflags = ["--cfg", "tokio_unstable"]
+rustdocflags = ["--cfg", "tokio_unstable"]
 # [build]
-# rustflags = ["--cfg", "tokio_unstable"]
\ No newline at end of file
+# rustflags = ["--cfg", "tokio_unstable"]
diff --git a/examples/Cargo.toml b/examples/Cargo.toml
index b35c587b..e628ceb2 100644
--- a/examples/Cargo.toml
+++ b/examples/Cargo.toml
@@ -10,7 +10,7 @@ edition = "2018"
 tokio = { version = "1.0.0", path = "../tokio", features = ["full", "tracing"] }
 tokio-util = { version = "0.7.0", path = "../tokio-util", features = ["full"] }
 tokio-stream = { version = "0.1", path = "../tokio-stream" }
-
+tokio-metrics = { version = "0.1.0", path = "../../tokio-metrics" }
 tracing = "0.1"
 tracing-subscriber = { version = "0.3.1", default-features = false, features = ["fmt", "ansi", "env-filter", "tracing-log"] }
 bytes = "1.0.0"
@@ -24,6 +24,9 @@ httpdate = "1.0"
 once_cell = "1.5.2"
 rand = "0.8.3"

+
+
+
 [target.'cfg(windows)'.dev-dependencies.windows-sys]
 version = "0.42.0"

diff --git a/examples/tinyhttp.rs b/examples/tinyhttp.rs
index fa0bc669..0457406a 100644
--- a/examples/tinyhttp.rs
+++ b/examples/tinyhttp.rs
@@ -18,8 +18,10 @@ use futures::SinkExt;
 use http::{header::HeaderValue, Request, Response, StatusCode};
 #[macro_use]
 extern crate serde_derive;
+use std::time::Duration;
 use std::{env, error::Error, fmt, io};
 use tokio::net::{TcpListener, TcpStream};
+use tokio_metrics::RuntimeMonitor;
 use tokio_stream::StreamExt;
 use tokio_util::codec::{Decoder, Encoder, Framed};

@@ -33,6 +35,18 @@ async fn main() -> Result<(), Box<dyn Error>> {
     let server = TcpListener::bind(&addr).await?;
     println!("Listening on: {}", addr);

+    let handle = tokio::runtime::Handle::current();
+    {
+        let runtime_monitor = RuntimeMonitor::new(&handle);
+        tokio::spawn(async move {
+            for interval in runtime_monitor.intervals() {
+                // pretty-print the metric interval
+                println!("{:?}", interval);
+                // wait 500ms
+                tokio::time::sleep(Duration::from_secs(1)).await;
+            }
+        });
+    }
     loop {
         let (stream, _) = server.accept().await?;
         tokio::spawn(async move {

Command:

RUSTFLAGS="--cfg tokio_unstable" cargo run --example tinyhttp

When working on the main Tokio repository, you can end up with two versions of Tokio: the one locally on disk, and one from crates.io, even if both are 1.x.y.

This isn't a tokio-metrics bug, so I'll close this.

@Darksonn thanks, how can I make it work?

It usually requires some work to work around these errors. In my opinion, it is not worth the work to use tokio-metrics in the examples from the main Tokio crate.

If you just need it for testing locally, you can copy the example into a separate repository and only use the crates.io versions of things.

Thanks a lot, I will try.