Gergő Tisza has uploaded a new change for review. https://gerrit.wikimedia.org/r/323638
Change subject: Rename 'type' field of authevents channel to 'eventType' ...................................................................... Rename 'type' field of authevents channel to 'eventType' 'type' is problematic as it conflicts with a default field name in logstash. Bug: T145133 Change-Id: Iab1eb47a6b6c98f3c84b4f8e2d16cbe2cdbf515b --- M includes/AuthManagerStatsdHandler.php M tests/phpunit/AuthManagerStatsdHandlerTest.php 2 files changed, 24 insertions(+), 5 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikimediaEvents refs/changes/38/323638/1 diff --git a/includes/AuthManagerStatsdHandler.php b/includes/AuthManagerStatsdHandler.php index 5d5f21d..4f315c3 100644 --- a/includes/AuthManagerStatsdHandler.php +++ b/includes/AuthManagerStatsdHandler.php @@ -31,7 +31,7 @@ * * Events can include the following data in their context: * - 'event': (string, required) the type of the event (e.g. 'login'). - * - 'type': (string) a subtype for more complex events. + * - 'eventType': (string) a subtype for more complex events. * - 'successful': (bool) whether the attempt was successful. Can be omitted if 'status' is * a Status or a StatusValue. * - 'status': (Status|StatusValue|string|int) attempt status (such as an error message key). @@ -51,11 +51,11 @@ * {@inheritdoc} */ public function handle( array $record ) { - $event = isset( $record['context']['event'] ) ? $record['context']['event'] : null; - $type = isset( $record['context']['type'] ) ? $record['context']['type'] : null; + $event = $this->getField( 'event', $record['context'] ); + $type = $this->getField( [ 'eventType', 'type' ] , $record['context'] ); $entrypoint = $this->getEntryPoint(); - $status = isset( $record['context']['status'] ) ? $record['context']['status'] : null; - $successful = isset( $record['context']['successful'] ) ? $record['context']['successful'] : null; + $status = $this->getField( 'status', $record['context'] ); + $successful = $this->getField( 'successful', $record['context'] ); $error = null; if ( $status instanceof Status || $status instanceof StatusValue ) { $status = Status::wrap( $status ); @@ -109,4 +109,19 @@ } return $entrypoint; } + + /** + * Get a field from an array without triggering errors if it does not exist + * @param string|array $field Field name or list of field name + fallbacks + * @param array $data + * @return mixed Field value, or null if field was missing + */ + protected function getField( $field, array $data ) { + foreach ( (array)$field as $key ) { + if ( isset( $data[$key] ) ) { + return $data[$key]; + } + } + return null; + } } diff --git a/tests/phpunit/AuthManagerStatsdHandlerTest.php b/tests/phpunit/AuthManagerStatsdHandlerTest.php index 8003f8d..5cc516b 100644 --- a/tests/phpunit/AuthManagerStatsdHandlerTest.php +++ b/tests/phpunit/AuthManagerStatsdHandlerTest.php @@ -57,6 +57,10 @@ ], 'authmanager.autocreate' ], 'type' => [ [ 'channel' => 'authevents', + 'context' => [ 'event' => 'autocreate', 'eventType' => 'session' ], + ], 'authmanager.autocreate.session' ], + 'type fallback' => [ [ + 'channel' => 'authevents', 'context' => [ 'event' => 'autocreate', 'type' => 'session' ], ], 'authmanager.autocreate.session' ], 'success' => [ [ -- To view, visit https://gerrit.wikimedia.org/r/323638 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iab1eb47a6b6c98f3c84b4f8e2d16cbe2cdbf515b Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/WikimediaEvents Gerrit-Branch: master Gerrit-Owner: Gergő Tisza <gti...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits