Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use sentry_tracing multiple times? #744

Open
avioletheart opened this issue Feb 23, 2025 · 3 comments
Open

use sentry_tracing multiple times? #744

avioletheart opened this issue Feb 23, 2025 · 3 comments
Labels
area: tracing Also known as performance monitoring, stuff like transactions and spans question

Comments

@avioletheart
Copy link

i have a function such as this:

pub fn init_logging(component: &str) {
    let sentry_layer = sentry_tracing::layer().event_filter(|md| match md.level() {
        &tracing::Level::ERROR => EventFilter::Event,
        _ => EventFilter::Ignore,
    });

    if config.json_log {
        tracing_subscriber::registry()
            .with(json_subscriber::layer())
            .with(sentry_layer)
            .with(EnvFilter::from_default_env())
            .init();
    } else {
        tracing_subscriber::registry()
            .with(tracing_subscriber::fmt::layer())
            .with(sentry_layer)
            .init();
    }
}

if i remove the sentry_layer in one of the two branches, it works. but with the code above the compile fails with an odd error:

error[E0277]: the trait bound `SentryLayer<Layered<json_subscriber::fmt::Layer, Registry>>: __tracing_subscriber_Layer<Layered<tracing_subscriber::fmt::Layer<Registry>, Registry>>` is not satisfied
    --> crates/libpk/src/lib.rs:39:19
     |
39   |             .with(sentry_layer)
     |              ---- ^^^^^^^^^^^^ the trait `__tracing_subscriber_Layer<Layered<tracing_subscriber::fmt::Layer<Registry>, Registry>>` is not implemented for `SentryLayer<Layered<json_subscriber::fmt::Layer, Registry>>`
     |              |
     |              required by a bound introduced by this call
     |
     = help: the trait `Layer<Layered<tracing_subscriber::fmt::Layer<Registry>, _>>` is not implemented for `SentryLayer<Layered<json_subscriber::fmt::Layer, Registry>>`
             but trait `Layer<Layered<json_subscriber::fmt::Layer, _>>` is implemented for it
     = help: for that trait implementation, expected `json_subscriber::fmt::Layer`, found `tracing_subscriber::fmt::Layer<Registry>`
note: required by a bound in `with`
    --> /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tracing-subscriber-0.3.18/src/layer/mod.rs:1503:12
     |
1501 |     fn with<L>(self, layer: L) -> Layered<L, Self>
     |        ---- required by a bound in this associated function
1502 |     where
1503 |         L: Layer<Self>,
     |            ^^^^^^^^^^^ required by this bound in `__tracing_subscriber_SubscriberExt::with`

error[E0599]: the method `init` exists for struct `Layered<SentryLayer<Layered<Layer, Registry>>, Layered<Layer<Registry>, Registry>>`, but its trait bounds were not satisfied
  --> crates/libpk/src/lib.rs:40:14
   |
37 | /         tracing_subscriber::registry()
38 | |             .with(tracing_subscriber::fmt::layer())
39 | |             .with(sentry_layer)
40 | |             .init();
   | |             -^^^^ method cannot be called due to unsatisfied trait bounds
   | |_____________|
   |
   |
  ::: /root/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/tracing-subscriber-0.3.18/src/layer/layered.rs:22:1
   |
22 |   pub struct Layered<L, I, S = I> {
   |   ------------------------------- doesn't satisfy `_: Into<Dispatch>` or `_: SubscriberInitExt`
   |
   = note: the following trait bounds were not satisfied:
           `Layered<SentryLayer<Layered<json_subscriber::fmt::Layer, Registry>>, Layered<tracing_subscriber::fmt::Layer<Registry>, Registry>>: Into<tracing::Dispatch>`
           which is required by `Layered<SentryLayer<Layered<json_subscriber::fmt::Layer, Registry>>, Layered<tracing_subscriber::fmt::Layer<Registry>, Registry>>: SubscriberInitExt`
           `&Layered<SentryLayer<Layered<json_subscriber::fmt::Layer, Registry>>, Layered<tracing_subscriber::fmt::Layer<Registry>, Registry>>: Into<tracing::Dispatch>`
           which is required by `&Layered<SentryLayer<Layered<json_subscriber::fmt::Layer, Registry>>, Layered<tracing_subscriber::fmt::Layer<Registry>, Registry>>: SubscriberInitExt`
           `&mut Layered<SentryLayer<Layered<json_subscriber::fmt::Layer, Registry>>, Layered<tracing_subscriber::fmt::Layer<Registry>, Registry>>: Into<tracing::Dispatch>`
           which is required by `&mut Layered<SentryLayer<Layered<json_subscriber::fmt::Layer, Registry>>, Layered<tracing_subscriber::fmt::Layer<Registry>, Registry>>: SubscriberInitExt`

what is the correct way of doing this?

@lcian
Copy link
Member

lcian commented Feb 24, 2025

Hey @avioletheart, I should spend some time investigating why that doesn't work, I'm not sure if there's something that might need a fix on our end.

However, if you reorder the layers and put the sentry layer first, your example compiles fine.
e.g. like this:

pub fn init_logging(component: &str) {
    let sentry_layer = sentry_tracing::layer().event_filter(|md| match md.level() {
        &tracing::Level::ERROR => EventFilter::Event,
        _ => EventFilter::Ignore,
    });

    if config.json_log {
        tracing_subscriber::registry()
            .with(sentry_layer)
            .with(json_subscriber::layer())
            .with(EnvFilter::from_default_env())
            .init();
    } else {
        tracing_subscriber::registry()
            .with(sentry_layer)
            .with(tracing_subscriber::fmt::layer())
            .init();
    }
}

Does that work for your usecase?

@avioletheart
Copy link
Author

huh, so it does work, i swear i'd tried that. thanks!

@lcian
Copy link
Member

lcian commented Feb 24, 2025

Nice!
I'll keep this open so that I remember to look into it again.
I'm not sure if this behaviour is intended or if we're maybe missing some impls on SentryLayer that should be there so that it can be applied in whatever order.

@lcian lcian added the question label Mar 6, 2025
@lcian lcian added the area: tracing Also known as performance monitoring, stuff like transactions and spans label Mar 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: tracing Also known as performance monitoring, stuff like transactions and spans question
Projects
None yet
Development

No branches or pull requests

2 participants