Skip to content

use sentry_tracing multiple times? #744

Closed
@avioletheart

Description

@avioletheart

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions