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

force sentry to collect events with env dev for tests #122

Merged
merged 4 commits into from
Oct 10, 2024

Conversation

dfrankland
Copy link
Member

@dfrankland dfrankland commented Oct 9, 2024

I'm adding more Sentry events and I'm seeing these sometimes get sent using the prod env in tests 😬

This can happen when cargo test --release is used and Sentry defaults its environment based on the debug_assertions feature flag which is turned off/on by --release.

Comment on lines 4 to +5
"cli",
"cli-tests",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a fairly common pattern to split out a crate that's just for tests. For example, see tokio.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's interesting. How does this typically work with code owners? I assume that both directories will be marked as owned by the same team / group of individuals?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to mention that splitting out test crates is necessary when enabling different features for tests.

I don't see people using code owners that often, so I'm not exactly sure. Assigning code owners can be as simple as

cli*/ @cli-team

or

cli/ @cli-team
cli-*/ @cli-team

Comment on lines +24 to +35
lazy_static! {
static ref CARGO_MANIFEST_DIR: PathBuf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
static ref CARGO_RUN: CargoRun = CargoBuild::new()
.bin("trunk-analytics-cli")
.target_dir(CARGO_MANIFEST_DIR.join("../target"))
.manifest_path(CARGO_MANIFEST_DIR.join("../cli/Cargo.toml"))
.features("force-sentry-env-dev")
.current_release()
.current_target()
.run()
.unwrap();
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

escargot is a nice interface to build crates with features enabled or other things configured. It's recommended by assert_cmd.

Comment on lines -127 to +135
let _guard = sentry::init((
SENTRY_DSN,
sentry::ClientOptions {
release: sentry::release_name!(),
..Default::default()
},
));
let mut options = sentry::ClientOptions::default();
options.release = sentry::release_name!();

#[cfg(feature = "force-sentry-env-dev")]
{
options.environment = Some("development".into())
}

let _guard = sentry::init((SENTRY_DSN, options));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

environment defaults to "production" or "development" based on the debug_assertions feature flag. Our new feature force-sentry-env-dev forces it to "development" for tests.

Copy link

trunk-staging-io bot commented Oct 9, 2024

362 tests were run on d541df8f. ✅ 362 Passed. View Full Report ↗︎

settings

@dfrankland dfrankland force-pushed the dylan/force-sentry-env-dev branch from 2b5a31a to cf7460c Compare October 9, 2024 16:44
Comment on lines 4 to +5
"cli",
"cli-tests",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's interesting. How does this typically work with code owners? I assume that both directories will be marked as owned by the same team / group of individuals?

@@ -127,7 +144,7 @@ async fn upload_bundle() {
assert_eq!(bundle_meta.envs.get("CI"), Some(&String::from("1")));
let time_since_upload = chrono::Utc::now()
- chrono::DateTime::from_timestamp(bundle_meta.upload_time_epoch as i64, 0).unwrap();
assert_eq!(time_since_upload.num_minutes(), 0);
assert!(time_since_upload.num_minutes() <= 5);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This seems to be a fairly popular crate that has an explicit assert_lt
https://docs.rs/more-asserts/latest/more_asserts/#

I personally prefer being explicit about expectations in tests since it helps with debugging.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. I've been trying to keep things minimal until needed, but I have no issue with adding that. Thanks for doing the research 👍

@@ -155,7 +172,7 @@ async fn upload_bundle() {
);
let time_since_junit_modified = chrono::Utc::now()
- chrono::DateTime::from_timestamp_nanos(bundled_file.last_modified_epoch_ns as i64);
assert_eq!(time_since_junit_modified.num_minutes(), 0);
assert!(time_since_junit_modified.num_minutes() <= 5);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment to the above.

Copy link
Contributor

@max-trunk max-trunk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG! couple questions for my Rust learning

.bin("trunk-analytics-cli")
.target_dir(CARGO_MANIFEST_DIR.join("../target"))
.manifest_path(CARGO_MANIFEST_DIR.join("../cli/Cargo.toml"))
.features("force-sentry-env-dev")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is that every new test file in this crate that wants to invoke the CLI will need to do this? Not that it's a huge lift to copy it into new test files - but is there a Rust-y way to do this once and leverage it across modules?

Also - does enabling the force-sentry-env-dev feature in the dev dependencies not take care of this https://github.com/trunk-io/analytics-cli/pull/122/files#diff-48118bb96b547e6c79e43924f9db25f71065d526d458312f2c673458370dd4e3R19?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If by modules, you mean modules within cli-tests/src then we just need to add a pub to this static ref and reuse it in within the crate.

If you are thinking about how one might reuse this across crates in the workspace, then we could move this to test_utils with pub and reuse that here and elsewhere.

Enabling force-sentry-env-dev for the dependency only enables it for the lib.rs, not for the main.rs. Here we are using escargot to build the main.rs binary with the force-sentry-env-dev feature enabled.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If by modules, you mean modules within cli-tests/src then we just need to add a pub to this static ref and reuse it in within the crate.

Yep! this is what I meant. Thanks for the info 🫡

@dfrankland dfrankland force-pushed the dylan/force-sentry-env-dev branch from ccd775c to c049a37 Compare October 9, 2024 23:52
@dfrankland dfrankland merged commit f24b648 into main Oct 10, 2024
11 checks passed
@dfrankland dfrankland deleted the dylan/force-sentry-env-dev branch October 10, 2024 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants