-
-
Notifications
You must be signed in to change notification settings - Fork 96
Add a gRPC logger (Restored) #389
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
base: master
Are you sure you want to change the base?
Conversation
changing the color of some log messages.
Reviewer's GuideThis PR introduces a new Sequence diagram for gRPC unary call logging with TalkerGrpcLoggersequenceDiagram
participant Client
participant TalkerGrpcLogger
participant Talker
participant gRPC_Server
Client->>TalkerGrpcLogger: Make unary gRPC call
TalkerGrpcLogger->>Talker: logTyped(GrpcRequestLog)
TalkerGrpcLogger->>gRPC_Server: Forward request
gRPC_Server-->>TalkerGrpcLogger: Response or Error
alt Success
TalkerGrpcLogger->>Talker: logTyped(GrpcResponseLog)
TalkerGrpcLogger-->>Client: Return response
else Error
TalkerGrpcLogger->>Talker: logTyped(GrpcErrorLog)
TalkerGrpcLogger-->>Client: Return error
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Frezyx - I've reviewed your changes - here's some feedback:
- The generated test in test/talker_grpc_logger_test.dart still references a non‐existent
Awesome
class — replace this placeholder with real unit tests that verify TalkerGrpcLogger’s behavior. - The interceptStreaming method only prints to console; consider either implementing streaming RPC logging or throwing a clear “not supported” error rather than leaving a no-op.
- You’re passing a
title
into each Grpc*Log constructor but then overriding thetitle
getter with a constant string; remove the unused constructor parameter or make the getter use it to avoid confusion.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The generated test in test/talker_grpc_logger_test.dart still references a non‐existent `Awesome` class — replace this placeholder with real unit tests that verify TalkerGrpcLogger’s behavior.
- The interceptStreaming method only prints to console; consider either implementing streaming RPC logging or throwing a clear “not supported” error rather than leaving a no-op.
- You’re passing a `title` into each Grpc*Log constructor but then overriding the `title` getter with a constant string; remove the unused constructor parameter or make the getter use it to avoid confusion.
## Individual Comments
### Comment 1
<location> `packages/talker_grpc_logger/lib/src/grpc_logs.dart:38` </location>
<code_context>
+ // Add the headers to the log message, but obfuscate the token if
+ // necessary.
+ final Map<String, String> headers = {};
+ options.metadata.forEach((key, value) {
+ if (obfuscateToken && key.toLowerCase() == 'authorization') {
+ headers[key] = 'Bearer [obfuscated]';
+ } else {
+ headers[key] = value;
+ }
+ });
+
+ try {
</code_context>
<issue_to_address>
Assumes all metadata values are strings, which may not always be the case.
gRPC metadata values can be non-strings (e.g., List<String>), so this code may fail or log incorrectly if such values are present. Please add type checks or handle non-string values appropriately.
</issue_to_address>
### Comment 2
<location> `packages/talker_grpc_logger/lib/src/grpc_logs.dart:51` </location>
<code_context>
+ final prettyHeaders = encoder.convert(headers);
+ msg += '\nHeaders: $prettyHeaders';
+ }
+ } catch (_) {
+ // TODO: add handling can`t convert
+ }
+ return msg;
</code_context>
<issue_to_address>
Catching all exceptions without handling or logging may obscure errors.
Please log the exception or provide a fallback message in the catch block to aid debugging if header conversion fails.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
} catch (_) {
// TODO: add handling can`t convert
}
return msg;
=======
} catch (e, stackTrace) {
msg += '\nHeaders: <Failed to convert headers: $e>';
// Optionally, log the stack trace if a logger is available:
// logger.error('Failed to convert headers', e, stackTrace);
}
return msg;
>>>>>>> REPLACE
</suggested_fix>
### Comment 3
<location> `packages/talker_grpc_logger/lib/src/talker_grpc_logger_base.dart:29` </location>
<code_context>
+ DateTime startTime = DateTime.now();
+ final response = invoker(method, request, options);
+
+ response.then((r) {
+ Duration elapsedTime = DateTime.now().difference(startTime);
+ _talker.logTyped(GrpcResponseLog(method.path,
+ method: method, response: r, durationMs: elapsedTime.inMilliseconds));
+ }).catchError((e) {
+ Duration elapsedTime = DateTime.now().difference(startTime);
+ _talker.logTyped(GrpcErrorLog(method.path,
</code_context>
<issue_to_address>
Error handling assumes all errors are GrpcError instances.
If 'e' is not a GrpcError, passing it directly to GrpcErrorLog may cause a runtime error. Please add a type check and handle unexpected error types accordingly.
</issue_to_address>
### Comment 4
<location> `packages/talker_grpc_logger/lib/src/talker_grpc_logger_base.dart:52` </location>
<code_context>
+ Stream<Q> requests,
+ CallOptions options,
+ ClientStreamingInvoker<Q, R> invoker) {
+ print('interceptStreaming');
+
+ return invoker(method, requests, options);
</code_context>
<issue_to_address>
Debug print statement left in production code.
Please remove the print statement or replace it with appropriate logging if necessary.
</issue_to_address>
### Comment 5
<location> `packages/talker_grpc_logger/README.md:98` </location>
<code_context>
+false`:
+
+```dart
+TalkerGrpcLogger(talker: talker, obfuscateToken: true)
+```
</code_context>
<issue_to_address>
Code snippet shows enabling obfuscation, not disabling as described.
Update the code snippet to use `obfuscateToken: false` to match the description above.
</issue_to_address>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
options.metadata.forEach((key, value) { | ||
if (obfuscateToken && key.toLowerCase() == 'authorization') { | ||
headers[key] = 'Bearer [obfuscated]'; | ||
} else { | ||
headers[key] = value; | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Assumes all metadata values are strings, which may not always be the case.
gRPC metadata values can be non-strings (e.g., List), so this code may fail or log incorrectly if such values are present. Please add type checks or handle non-string values appropriately.
} catch (_) { | ||
// TODO: add handling can`t convert | ||
} | ||
return msg; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (bug_risk): Catching all exceptions without handling or logging may obscure errors.
Please log the exception or provide a fallback message in the catch block to aid debugging if header conversion fails.
} catch (_) { | |
// TODO: add handling can`t convert | |
} | |
return msg; | |
} catch (e, stackTrace) { | |
msg += '\nHeaders: <Failed to convert headers: $e>'; | |
// Optionally, log the stack trace if a logger is available: | |
// logger.error('Failed to convert headers', e, stackTrace); | |
} | |
return msg; |
response.then((r) { | ||
Duration elapsedTime = DateTime.now().difference(startTime); | ||
_talker.logTyped(GrpcResponseLog(method.path, | ||
method: method, response: r, durationMs: elapsedTime.inMilliseconds)); | ||
}).catchError((e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Error handling assumes all errors are GrpcError instances.
If 'e' is not a GrpcError, passing it directly to GrpcErrorLog may cause a runtime error. Please add a type check and handle unexpected error types accordingly.
Stream<Q> requests, | ||
CallOptions options, | ||
ClientStreamingInvoker<Q, R> invoker) { | ||
print('interceptStreaming'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: Debug print statement left in production code.
Please remove the print statement or replace it with appropriate logging if necessary.
false`: | ||
|
||
```dart | ||
TalkerGrpcLogger(talker: talker, obfuscateToken: true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: Code snippet shows enabling obfuscation, not disabling as described.
Update the code snippet to use obfuscateToken: false
to match the description above.
As discussed in this issue: #201 (comment).
This PR adds a gRPC logger. This logger was inspired by the http logger and works very similarly. The logger can be used to instrument a gRPC client, allowing to transparently log gRPC calls.
The logger obfuscates authorization tokens by default (I find it really insecure to write tokens to the logs), but this can be disabled if needs be.
For now, it only supports unary RPCs, but adding streaming RPCs shouldn't be too difficult.
Summary by Sourcery
Add a standalone gRPC logger package for Talker to transparently log unary RPC calls with request, response, and error details, including optional authorization token obfuscation.
New Features:
Build:
Documentation:
Tests:
Chores: