frigus02 / opentelemetry-application-insights

OpenTelemetry exporter for Azure Application Insights
MIT License
22 stars 12 forks source link

Compatibility with actix-web #42

Closed soualid closed 2 years ago

soualid commented 2 years ago

Hello,

I am trying to use this crate with actix-web 4.0.0-beta.10 and actix-rt 2.3.0 but I'm not sure it's feasible.

If I try to setup it with install_simple() it panics because it seems it cannot use the actix underneath tokio runtime:

there is no reactor running, must be called from the context of a Tokio 1.x runtime

When trying to use .install_batch(opentelemetry::runtime::Tokio), it seems samples are not sent.

Span are created by the following actix middleware:

.wrap_fn(move |req, srv| {

    let r = req.path().to_string();
    debug!("appinsight {}", r);
    tracer.in_span("middleware", move |cx| {
        cx.span()
            .set_attribute(Key::new("path").string(r));
        srv.call(req).with_context(cx)
    })
})

Thank you if you can help on this!

frigus02 commented 2 years ago

Hi there. I can test it in more detail later but I noticed 2 things that might help:

soualid commented 2 years ago

Thanks, I'll check this out, I took this example here: https://github.com/open-telemetry/opentelemetry-rust/blob/main/examples/actix-http/src/main.rs#L37 which seems to work with jaeger.

frigus02 commented 2 years ago

That's a good call. The code actually seems to be correct. I just tried the example you mentioned with opentelemetry-application-insights and I can see spans in Azure.

These were the changes I did to the example:

diff --git a/examples/actix-http/Cargo.toml b/examples/actix-http/Cargo.toml
index 8302e7a..3c3e665 100644
--- a/examples/actix-http/Cargo.toml
+++ b/examples/actix-http/Cargo.toml
@@ -5,11 +5,12 @@ edition = "2018"
 publish = false

 [dependencies]
-opentelemetry = { path = "../../opentelemetry", features = ["rt-tokio"] }
-opentelemetry-jaeger = { path = "../../opentelemetry-jaeger", features = ["reqwest_collector_client", "rt-tokio"] }
+opentelemetry = { version = "0.16.0", features = ["rt-tokio"] }
+opentelemetry-application-insights = { version = "0.18.0", features = ["reqwest-client"] }
 thrift = "0.13"
 futures = "0.3"
 actix-web = "4.0.0-beta.4"
 actix-service = "2.0.0-beta.5"
 env_logger = "0.8.2"
 tokio = { version = "1", features = ["full"] }
+reqwest = "0.11"
diff --git a/examples/actix-http/src/main.rs b/examples/actix-http/src/main.rs
index 065f9d8..e20ad1c 100644
--- a/examples/actix-http/src/main.rs
+++ b/examples/actix-http/src/main.rs
@@ -9,10 +9,12 @@ use opentelemetry::{
 };

 fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
-    opentelemetry_jaeger::new_pipeline()
-        .with_collector_endpoint("http://127.0.0.1:14268/api/traces")
-        .with_service_name("trace-http-demo")
-        .install_batch(opentelemetry::runtime::Tokio)
+    Ok(opentelemetry_application_insights::new_pipeline(
+        std::env::var("INSTRUMENTATION_KEY").expect("env INSTRUMENTATION_KEY"),
+    )
+    .with_client(reqwest::Client::new())
+    .with_service_name("trace-http-demo")
+    .install_batch(opentelemetry::runtime::Tokio))
 }

 async fn index() -> &'static str {

Then I started it using:

INSTRUMENTATION_KEY=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx cargo run
soualid commented 2 years ago

Many thanks for a such detailed help!

Got it working in the example after applying the diff you provided, then in my application using the actix-web-opentelemetry crate you pointed, still wonder why it was not working using a wrap_fn middleware in my implementation, whereas the same is working in the opentelemetry actix example.

Anyway! I'll stick for now with the actix-web-opentelemetry which is working well with your crate, again, many thanks for your work on this and for your help! We really needed to integrate our rust microservice with the other stuff we log in Application Insight.

frigus02 commented 2 years ago

No worries. Glad it works now.