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

feat: optional console log message filter #241

Merged
merged 22 commits into from
Oct 2, 2024
Merged

feat: optional console log message filter #241

merged 22 commits into from
Oct 2, 2024

Conversation

rvanasa
Copy link
Collaborator

@rvanasa rvanasa commented Jul 18, 2024

Resolves #201 by adding an optional consoleFilter install arg.

Show all log messages (default):

dfx deploy evm_rpc --argument "(record { consoleFilter = opt variant { ShowAll } })"

Hide all log messages:

dfx deploy evm_rpc --argument "(record { consoleFilter = opt variant { HideAll } })"

Only show INFO ... messages:

dfx deploy evm_rpc --argument "(record { consoleFilter = opt variant { ShowPattern = "^INFO" } })"

Hide TRACE_HTTP ... messages:

dfx deploy evm_rpc --argument "(record { consoleFilter = opt variant { HidePattern = "^TRACE_HTTP" } })"

Only show messages related to eth_getLogs:

dfx deploy evm_rpc --argument "(record { consoleFilter = opt variant { ShowPattern = "eth_getLogs" } })"

Note that ShowPattern and HidePattern accept regular expressions and are evaluated using the regex crate.

@rvanasa rvanasa marked this pull request as draft July 18, 2024 18:38
@rvanasa rvanasa marked this pull request as ready for review July 23, 2024 15:29
@rvanasa rvanasa marked this pull request as draft July 23, 2024 15:29
@rvanasa rvanasa marked this pull request as ready for review September 25, 2024 20:07
candid/evm_rpc.did Outdated Show resolved Hide resolved
src/rpc_client/eth_rpc/mod.rs Show resolved Hide resolved
src/logs/mod.rs Outdated Show resolved Hide resolved
candid/evm_rpc.did Outdated Show resolved Hide resolved
Cargo.toml Show resolved Hide resolved
Copy link
Member

@gregorydemay gregorydemay left a comment

Choose a reason for hiding this comment

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

Thanks a lot @rvanasa for this PR and the changes regarding to logging. I have one more comment regarding the mix of test logic with the productive code, but otherwise looks good to me!

candid/evm_rpc.did Outdated Show resolved Hide resolved
src/logs/mod.rs Outdated
Comment on lines 43 to 44
#[cfg(test)]
DISPLAYED_LOG_ENTRIES.with_borrow_mut(|entries| entries.push(entry.clone()));
Copy link
Member

Choose a reason for hiding this comment

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

mixing test code with productive code is usually not a good idea and I think here you actually don't need it, so I would remove this and the declaration L24-27.

Here is an alternative of how it could look like in the test file src/logs/tests.rs:

...

declare_log_buffer!(name = TEST_BUF, capacity = 1000);
const TEST: PrintProxySink = PrintProxySink(&Priority::Debug, &TEST_BUF);

...

#[test]
fn should_show_all() {
    set_console_filter(ConsoleFilter::ShowAll);
    log!(TEST, "ABC");
    log!(TEST, "123");
    log!(TEST, "!@#");
    assert_eq!(get_messages(), vec!["ABC", "123", "!@#"]);
}

...

fn get_messages() -> Vec<String> {
    export(&TEST_BUF)
        .into_iter()
        .map(|entry| entry.message)
        .collect()
}

Does-it make sense?

Copy link
Collaborator Author

@rvanasa rvanasa Sep 30, 2024

Choose a reason for hiding this comment

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

The problem I'm running into with this approach is that the messages are always added to the buffer regardless of the filter, which only affects whether the messages are printed to the console.

The simplest solution would be to change the logic so that the console messages are the same as the logs. I also looked into intercepting the println!() output, but compared to this, it seemed cleaner to add the DISPLAYED_LOG_ENTRIES data structure to simulate this in a more controlled way. A third option could be to call ConsoleFilter::is_valid() (now renamed try_is_valid()) directly instead of going through a PrintProxySink. Do you have a preference for any of these solutions or something else?

Copy link
Member

Choose a reason for hiding this comment

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

the messages are always added to the buffer regardless of the filter, which only affects whether the messages are printed to the console.

good point, I missed this. I should have tried with more tests :)

The simplest solution would be to change the logic so that the console messages are the same as the logs.

I have the impression this would indeed the simplest. I guess for this it would then maybe make sense to rename ConsoleFilter to something like LogFilter?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done!

src/types.rs Outdated Show resolved Hide resolved
src/types.rs Outdated
pub fn is_match(&self, value: &str) -> bool {
Regex::new(&self.0)
.map(|regex| regex.is_match(value))
.unwrap_or(false)
Copy link
Member

Choose a reason for hiding this comment

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

nit: I think we should panic in case the regex is not valid instead of just silently returning false

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I suppose we can do this because it's only used in the local replica. Updated.

Copy link
Member

Choose a reason for hiding this comment

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

I suppose we can do this because it's only used in the local replica.

By local replica you mean running locally in dfx? If yes, I think we can and should also do this even if we were to use that feature productively. This could only be set during an upgrade and so if the regex is wrong then the panic will revert the upgrade which is IMO the correct thing to do.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that makes sense. This is now implemented.

gregorydemay added a commit that referenced this pull request Sep 30, 2024
Remove all productive dependencies on the [IC
repository](https://github.com/dfinity/ic/tree/master):

1. `ic-canisters-http-types`: is removed and the corresponding types
(`HttpRequest` and `HttpResponse`) were copied over since they amount to
a few lines of code.
2. `ic-crypto-sha3`: is replaced by `ic-sha3`, available on
[crates.io](https://crates.io/crates/ic-sha3).
3. `ic-ethereum-types` is replaced by a crate of the same name available
on[ crates.io](https://crates.io/crates/ic-ethereum-types).
4. `ic-nervous-system-common`: is removed since it was only used to
serve metrics.

The only remaining productive dependency from the IC repository is
`ic-canister-log` and will be removed by #241.
@rvanasa rvanasa merged commit a043a32 into main Oct 2, 2024
3 checks passed
@rvanasa rvanasa deleted the optional-logs branch October 2, 2024 16:10
@letmejustputthishere
Copy link
Member

We should make sure this is properly documented with a new release. Do we expect projects that deploy their own evm rpc canister to only deploy the latest release as well?

@rvanasa
Copy link
Collaborator Author

rvanasa commented Oct 3, 2024

We should make sure this is properly documented with a new release.

For sure. I'll work with @gregorydemay and @jessiemongeon1 on making sure the documentation is updated for the new changes.

Do we expect projects that deploy their own evm rpc canister to only deploy the latest release as well?

We can recommend this, although the previous version should continue working as well.

@letmejustputthishere
Copy link
Member

We can recommend this, although the previous version should continue working as well.

what about deploying main?

@rvanasa
Copy link
Collaborator Author

rvanasa commented Oct 4, 2024

If I understand the question correctly, this is a work in progress since it requires an NNS proposal. We also want to give a window of time for developers to migrate to the new version before redeploying.

@rvanasa
Copy link
Collaborator Author

rvanasa commented Oct 4, 2024

Documentation PR: dfinity/portal#3575

@letmejustputthishere
Copy link
Member

what i meant was should we advise projects not to deploy from main, but only from releases instead :)

@rvanasa
Copy link
Collaborator Author

rvanasa commented Oct 5, 2024

Haha, okay, I see what you're saying. Yes, that is what we should recommend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add init arg for optional canister logs
4 participants