Ppchelko has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/390211 )
Change subject: Logging improvements.
......................................................................
Logging improvements.
1. Catch exceptions trying to serialize events and log them as well.
2. Limit the log size to 8kb becasue anything bigger get truncated
by Logstash anyway.
Change-Id: Id216829f422cd96278d56e84b3d25280d54c859b
---
M EventBus.php
1 file changed, 28 insertions(+), 16 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/EventBus
refs/changes/11/390211/1
diff --git a/EventBus.php b/EventBus.php
index 4f2ced0..366bca0 100644
--- a/EventBus.php
+++ b/EventBus.php
@@ -105,14 +105,13 @@
// In case the event posted was too big we don't want
to log all the request body
// as it contains all
$context = [
- 'EventBus' => [
- 'events' => $events,
- 'response' => $res
- ]
+ 'events' => $events,
+ 'response' => $res
];
- // Limit the maximum size of the logged context to 1
megabyte
- if ( strlen( $body ) > 1048576 ) {
- $context['EventBus']['events'] = array_column(
$events, 'meta' );
+ // Limit the maximum size of the logged context to 8
kilobytes as that's where logstash
+ // truncates the JSON anyway
+ if ( strlen( $body ) > 8192 ) {
+ $context['events'] = array_column( $events,
'meta' );
}
self::logger()->error( "Unable to deliver all events:
${message}", $context );
}
@@ -128,22 +127,35 @@
* @return string JSON
*/
public static function serializeEvents( $events ) {
- $serializedEvents = FormatJson::encode( $events, false,
FormatJson::ALL_OK );
-
- if ( empty( $serializedEvents ) ) {
+ try {
+ $serializedEvents = FormatJson::encode( $events, false,
FormatJson::ALL_OK );
+ if ( empty( $serializedEvents ) ) {
+ $context = [
+ 'exception' => new Exception(),
+ 'events' => $events,
+ 'json_last_error' =>
json_last_error_msg()
+ ];
+ self::logger()->error(
+ 'FormatJson::encode($events) failed: '
. $context['json_last_error'] .
+ '. Aborting send.', $context
+ );
+ return;
+ }
+ return $serializedEvents;
+ } catch ( Exception $exception ) {
$context = [
- 'exception' => new Exception(),
- 'events' => $events,
- 'json_last_error' => json_last_error()
+ 'exception' => $exception,
+ // The exception during serializing mostly
happen when events are too big,
+ // so we will not be able to log a complete
thing, so truncate to only log meta
+ 'events' => array_column( $events, 'meta' ),
+ 'json_last_error' => json_last_error_msg()
];
self::logger()->error(
- 'FormatJson::encode($events) failed: ' .
$context['json_last_error'] .
+ 'FormatJson::encode($events) thrown exception:
' . $context['json_last_error'] .
'. Aborting send.', $context
);
return;
}
-
- return $serializedEvents;
}
/**
--
To view, visit https://gerrit.wikimedia.org/r/390211
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id216829f422cd96278d56e84b3d25280d54c859b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/EventBus
Gerrit-Branch: wmf/1.31.0-wmf.7
Gerrit-Owner: Ppchelko <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits