sentry-rust icon indicating copy to clipboard operation
sentry-rust copied to clipboard

Scope::set_user does not affect users reported in performance transactions

Open antifuchs opened this issue 1 year ago • 3 comments

Environment

I'm instrumenting an axum web app, using the default set of tower middleware, tags / user fields set using sentry::configure_scope land on error reporting events, but they are missing from Performance transactions.

Steps to Reproduce

  1. Set up the default sentry_tower layers:

         .layer(sentry_tower::NewSentryLayer::new_from_top())
         .layer(sentry_tower::SentryHttpLayer::with_transaction())
    
  2. In a handler (or another layer, it doesn't matter) sets user information or tags on the scope using the (docs-recommended) method, like:

      sentry::configure_scope(|scope| {
          scope.set_tag("lz_transaction", "woo weeee");
          scope.set_user(Some(User {
              id: Some(user.id.id().to_string()),
              username: Some(user.name.to_owned()),
              ..Default::default()
          }));
      });
  1. Call up a normally-succeeding handler
  2. Put panic!("oops"); in that handler to get an error event.

Expected Result

I'd like the transactions list in Performance to list Users and other tags, like sentry does in error events.

Actual Result

The error event has a user set, but the performance transaction doesn't.

The error event has the user: image

perf monitoring list: image

antifuchs avatar Apr 01 '24 16:04 antifuchs

This should have been implemented a long time ago in https://github.com/getsentry/sentry-rust/pull/596. It might be possible that the transaction is not using the correct Hub/Scope, or the problem might be elsewhere.

Swatinem avatar Apr 05 '24 14:04 Swatinem

Oh, I forgot to mention: using scope.set_transaction to name the transaction does make it to the performance list; but the rest of the data associated with the scope doesn't. (I did remove that set_transaction call when writing my tests, thinking it would help; it didn't...)

antifuchs avatar Apr 05 '24 14:04 antifuchs

I had a similar issue. I was setting the user in a spawned task. When I spawned the task, I was calling .bind_hub(Hub::new_from_top(Hub::current())) on it. After changing it to .bind_hub(Hub::current()) I could see user in performance traces.

I thought that creating a new Hub would also work and using the current Hub would not be required.

ollipa avatar May 06 '24 14:05 ollipa

Just tested this and the SDK works as intended in this aspect.

use axum::routing;
use axum::{extract::Request, Router};
use sentry::User;
use tower;

async fn handler() -> String {
    sentry::configure_scope(|scope| {
        scope.set_tag("lz_transaction", "woo weeee");
        scope.set_user(Some(User {
            id: Some("10".to_owned()),
            username: Some("lcian".to_owned()),
            ..Default::default()
        }));
    });

    let err = "NaN".parse::<usize>().unwrap_err();
    sentry::capture_error(&err);
    "".to_owned()
}

#[tokio::main]
async fn main() {
    let _guard = sentry::init((
        "https://[email protected]/4508694563782656",
        sentry::ClientOptions {
            release: sentry::release_name!(),
            traces_sample_rate: 1.0,
            debug: true,
            ..Default::default()
        },
    ));

    let app = Router::new().route("/", routing::get(handler)).layer(
        tower::ServiceBuilder::new()
            .layer(sentry_tower::NewSentryLayer::<Request>::new_from_top())
            .layer(sentry_tower::SentryHttpLayer::with_transaction()),
    );

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

Both the issue and the corresponding transaction have a user context.

The screenshot in the original post was showing the "Performance overview" page of the UI (now "Trace Explorer") which doesn't display this information, you need to navigate to the individual trace to see the user context.

Please reopen the issue and provide a full repro example if this is still relevant.

lcian avatar Mar 12 '25 09:03 lcian