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

Make Debug impl for newtypes passthrough to inner type #77

Merged
merged 1 commit into from
Sep 13, 2024

Conversation

rukai
Copy link
Collaborator

@rukai rukai commented Sep 12, 2024

kafka-protocol has generated newtypes, which wrap integers and strings to give some extra type safety.
For example:

  • BrokerId wraps i32
  • GroupId wraps StrBytes

Currently the Debug impl for these types is derived by #[derive(Debug)].
This results in debug output like:

  • BrokerId(2)
  • GroupId("some group")

This PR changes the debug output to instead directly display the type:

  • 2
  • "some group"

This should result in more concise logs.
Here are some example usages:

tracing::info!("Received request for group {group_id:?}");
// before
// Received request for group GroupId("foo")
// after
// Received request for group "foo"
//
// assuming the user specifies that its a group the log is easier to read

tracing::info!("{fetch_request:?}");
// before
//Fetch(FetchRequest { cluster_id: None, replica_id: BrokerId(-1), replica_state: ReplicaState { replica_id: BrokerId(-1), replica_epoch: -1, unknown_tagged_fields: {} }, max_wait_ms: 500, min_bytes: 1, max_bytes: 52428800, isolation_level: 0, session_id: 0, session_epoch: 0, topics: [FetchTopic { topic: TopicName(""), topic_id: 4033c2f1-9daf-4473-85f6-a786bf0dd5da, partitions: [FetchPartition { partition: 0, current_leader_epoch: 0, fetch_offset: 0, last_fetched_epoch: -1, log_start_offset: -1, partition_max_bytes: 1048576, unknown_tagged_fields: {} }], unknown_tagged_fields: {} }], forgotten_topics_data: [], rack_id: "", unknown_tagged_fields: {} })
// after
//Fetch(FetchRequest { cluster_id: None, replica_id: -1, replica_state: ReplicaState { replica_id: -1, replica_epoch: -1, unknown_tagged_fields: {} }, max_wait_ms: 500, min_bytes: 1, max_bytes: 52428800, isolation_level: 0, session_id: 0, session_epoch: 0, topics: [FetchTopic { topic: "", topic_id: 4033c2f1-9daf-4473-85f6-a786bf0dd5da, partitions: [FetchPartition { partition: 0, current_leader_epoch: 0, fetch_offset: 0, last_fetched_epoch: -1, log_start_offset: -1, partition_max_bytes: 1048576, unknown_tagged_fields: {} }], unknown_tagged_fields: {} }], forgotten_topics_data: [], rack_id: "", unknown_tagged_fields: {} })
//
// A downside is that its slightly less clear that `replica_id` refers to a broker and that `topic` refers to the name of the topic
// However I think that in order to make sense of the log you need to be familiar with the protocol or refer to protocol documentation anyway, so I think its worth it for making the log easier to scan.

Alternatives

As an alternative we could implement Display to skip the newtype name.
But in the case of a string type Display should not have quotes and Debug should be wrapped in quotes.
So I don't think this is appropriate for logs where we need to clearly show where a name begins and ends.
e.g.
tracing::log("the group {group} hit an issue")
could output: the group did not hit an issue

For this reason I dont think the string newtypes should implement Display at all, if the user really wants to they can get the inner type and display that.

Copy link
Owner

@tychedelia tychedelia left a comment

Choose a reason for hiding this comment

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

Okay, I think I'm pretty convinced, especially with regard to including the item in the middle of a larger log. Thanks! Looks like there are just some merge conflicts to resolve.

@rukai rukai force-pushed the newtype_debug_passthrough branch from b33f8ed to c7595d9 Compare September 12, 2024 21:53
@tychedelia tychedelia merged commit 94cc428 into tychedelia:main Sep 13, 2024
3 checks passed
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.

2 participants