Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/273008

Change subject: Submit server-side events via HTTP POST to the beacon endpoint
......................................................................

Submit server-side events via HTTP POST to the beacon endpoint

Bug: T127209
Change-Id: I94a592f4652bc5a74e7c402f3aca9a8fac79f7cb
---
M EventLogging.php
M includes/EventLogging.php
M includes/EventLoggingHooks.php
3 files changed, 19 insertions(+), 38 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/EventLogging 
refs/changes/08/273008/1

diff --git a/EventLogging.php b/EventLogging.php
index 3f88e18..cfb3316 100644
--- a/EventLogging.php
+++ b/EventLogging.php
@@ -60,22 +60,6 @@
 $wgEventLoggingBaseUri = false;
 
 /**
- * @var bool|string: File name or UDP address; false if not set.
- * Server-side events will be logged to this location.
- *
- * The syntax for UDP addresses is `udp://host:port/prefix`. The prefix
- * (followed by a space) is included at the start of each message. By
- * convention it specifies which log bucket the message should be routed
- * to. It is best if the prefix is simply "EventLogging".
- *
- * @see MWLoggerLegacyLogger::emit()
- *
- * @example string: 'udp://127.0.0.1:9000/EventLogging'
- * @example string: '/var/log/mediawiki/events.log'
- */
-$wgEventLoggingFile = false;
-
-/**
  * @var bool|string: URI or false if not set.
  * URI of api.php on schema wiki.
  *
diff --git a/includes/EventLogging.php b/includes/EventLogging.php
index 64de3ff..57ba8b3 100644
--- a/includes/EventLogging.php
+++ b/includes/EventLogging.php
@@ -1,6 +1,4 @@
 <?php
-
-use MediaWiki\Logger\LegacyLogger;
 /**
  * PHP API for logging events
  *
@@ -17,13 +15,22 @@
        const OMIT_USER_AGENT = 2;
 
        /**
-        * Writes an event to a file descriptor or socket.
-        * Takes an event ID and an event, encodes it as query string,
-        * and writes it to the UDP / TCP address or file specified by
-        * $wgEventLoggingFile. If $wgEventLoggingFile is not set, returns
-        * false without logging anything.
+        * Transfer small data asynchronously using an HTTP POST.
+        * This is meant to match the Navigator.sendBeacon() API.
         *
-        * @see LegacyLogger::emit
+        * @see https://w3c.github.io/beacon/#sec-sendBeacon-method
+        */
+       public static function sendBeacon( $url, array $data = [] ) {
+               DeferredUpdates::addCallableUpdate( function() use ( $url, 
$data ) {
+                       $options = $data ? [ 'postData' => $data ] : [];
+                       return Http::post( $url, $options );
+               } );
+
+               return true;
+       }
+
+       /**
+        * Emit an event via a sendBeacon POST to the event beacon endpoint.
         *
         * @param string $schemaName Schema name.
         * @param int $revId revision ID of schema.
@@ -32,11 +39,7 @@
         * @return bool: Whether the event was logged.
         */
        static function logEvent( $schemaName, $revId, $event, $options = 0 ) {
-               global $wgDBname, $wgEventLoggingFile;
-
-               if ( !$wgEventLoggingFile ) {
-                       return false;
-               }
+               global $wgEventLoggingBaseUri;
 
                $remoteSchema = new RemoteSchema( $schemaName, $revId );
                $schema = $remoteSchema->get();
@@ -52,23 +55,18 @@
                        'schema'           => $schemaName,
                        'revision'         => $revId,
                        'clientValidated'  => $isValid,
-                       'wiki'             => $wgDBname,
-                       'recvFrom'         => gethostname(),
-                       'timestamp'        => $_SERVER[ 'REQUEST_TIME' ],
+                       'wiki'             => wfWikiID(),
                );
                if ( isset( $_SERVER[ 'HTTP_HOST' ] ) ) {
                        $encapsulated[ 'webHost' ] = $_SERVER[ 'HTTP_HOST' ];
                }
-
                if ( !( $options & self::OMIT_USER_AGENT ) && !empty( $_SERVER[ 
'HTTP_USER_AGENT' ] ) ) {
                        $encapsulated[ 'userAgent' ] = $_SERVER[ 
'HTTP_USER_AGENT' ];
                }
 
                $json = static::serializeEvent( $encapsulated );
-
-               LegacyLogger::emit( $json . "\n", $wgEventLoggingFile );
-
-               return true;
+               $url = $wgEventLoggingBaseUri . '?' . rawurlencode( $json );
+               return self::sendBeacon( $url );
        }
 
        /**
diff --git a/includes/EventLoggingHooks.php b/includes/EventLoggingHooks.php
index 70b67c2..15860f4 100644
--- a/includes/EventLoggingHooks.php
+++ b/includes/EventLoggingHooks.php
@@ -26,7 +26,6 @@
                foreach ( array(
                        'wgEventLoggingBaseUri',
                        'wgEventLoggingDBname',
-                       'wgEventLoggingFile',
                        'wgEventLoggingSchemaApiUri'
                ) as $configVar ) {
                        if ( !isset( $GLOBALS[ $configVar ] ) || $GLOBALS[ 
$configVar ] === false ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/273008
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I94a592f4652bc5a74e7c402f3aca9a8fac79f7cb
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/EventLogging
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to