Provide a unique request identifier
authorOri Livneh <ori@wikimedia.org>
Fri, 25 Mar 2016 01:43:23 +0000 (18:43 -0700)
committerBryanDavis <bdavis@wikimedia.org>
Fri, 25 Mar 2016 16:09:05 +0000 (16:09 +0000)
commit5360a3497f31ce5af167746bf33375da5f32e061
tree3f383fac1bfbe2805b46945a68fc80f70c31552b
parente3bd13db0c285f312e31bb1b7271af4628cca80c
Provide a unique request identifier

When MediaWiki encounters an unhandled exception, the error message it produces
includes a randomly-generated token, which allows the exception details to be
looked up in the error logs. This is useful but narrow: would it not be useful
to have the ability to retrieve all log records associated with a particular
request, rather than just exception details? (Hint: yes.)

So: introduce the notion of a request-global unique ID, retrievable via
WebRequest::getRequestId(). When MediaWiki is behind Apache + mod_unique_id
(which provides the same facility) or some other software which sets a
UNIQUE_ID envvar, the value of that envvar is used as the request ID.
Otherwise, it is a randomly-generated 24-character string.

The request ID supplants exception-specific IDs; MWExceptionHandler::getLogId()
is deprecated, accordingly. The request ID is also added as an annotation to
all Monolog-processed log records, and is exposed client-side as 'wgRequestId'.
This allows developers to associate a page view with log records even when the
page view does not result in an unhandled exception. (For the WMF, I also
intend to add it as an annotation to profiling data).

The request ID is not a tracking token; it does not persist, and it is
associated with a backend request, not with a particular user or a particular
session. Like the data in the NewPP report, the request ID is designed to be
cacheable, so that if, for example, a developer notices something weird in the
HTML, s/he can associate the output with a backend request regardless of
whether the response was served from the cache or directly from the backend.

Some prior art:
* https://httpd.apache.org/docs/2.4/mod/mod_unique_id.html
* http://api.rubyonrails.org/classes/ActionDispatch/RequestId.html
* https://github.com/dabapps/django-log-request-id
* https://packagist.org/packages/php-middleware/request-id
* https://github.com/rhyselsmore/flask-request-id

Change-Id: Iaf90c20c330e0470b9b98627a0228cadefd301d1
RELEASE-NOTES-1.27
includes/OutputPage.php
includes/WebRequest.php
includes/api/ApiMain.php
includes/debug/logger/monolog/WikiProcessor.php
includes/exception/MWException.php
includes/exception/MWExceptionHandler.php