From Wikitech
Jump to navigation Jump to search
Screenshot of WikimediaDebug extension
Main interface of the WikimediaDebug extension

X-Wikimedia-Debug is an HTTP request header you can use to aid problem determination in production. You can use the header to control several aspects of backend request processing:

  • Backend selection: force Varnish to pass your request to a specific backend application server.
  • Caching behavior: requests that bear an X-Wikimedia-Debug header are always handled as a Varnish cache miss.
  • Request profiling: record a trace of request-processing code for performance analysis.
  • Debug logs: enable MediaWiki's verbose debug log group.
  • Read-only mode: request a page with MediaWiki set to read-only mode.

There is a WikimediaDebug browser extension for any WebExtension-supported browser that make working with the header easier: Chromium and Firefox (source code).

Command-line usage

Force Varnish to skip the cache and pass request to mwdebug1001.eqiad.wmnet:

$ curl -H 'X-Wikimedia-Debug: backend=mwdebug1001.eqiad.wmnet' https://meta.wikimedia.org/wiki/Main_Page

Same as above, but profile request using XHProf and log profile to XHGui:

$ curl -H 'X-Wikimedia-Debug: backend=mwdebug1001.eqiad.wmnet; profile' https://meta.wikimedia.org/wiki/Main_Page

Request a page with MediaWiki configured for read-only mode:

$ curl -H 'X-Wikimedia-Debug: backend=mwdebug1001.eqiad.wmnet; readonly' https://meta.wikimedia.org/wiki/Main_Page

Staging changes

This section will walk you through a common X-Wikimedia-Debug debugging workflow.

It worked on your machine. It worked on the Beta Cluster. And the unit tests pass. But will it work in production? You can reduce the risk of unexpected breakage by deploying your change to a debugging server in production and using the X-Wikimedia-Debug header to verify its behavior.

Follow the instructions on How to deploy code, but stop after you have completed step 2 ("Get the code on the deployment host"). Now sync your change to one of the debug backends in a new tap by SSHing into it and running scap pull:

you@laptop:~$ ssh mwdebug1001.eqiad.wmnet
you@mwdebug1001:~$ scap pull

Now use the X-Wikimedia-Debug extension to enable header injection, selecting the same backend you just synced your code to:

X-Wikimedia-Debug demo.png

Your requests will be routed to the backend on which you staged your changes, allowing you to verify that is working correctly.

Request profiling

The "Profiling Data" link is in the footer.

Setting the profile attribute in the X-Wikimedia-Debug header will use XHProf to capture a profile and send it to XHGui. If you are using the browser extension, there will be a link in the page footer with the label Profiling Data. Clicking or tapping on it will take you directly to a list of profiles matching your Request ID. Then click on the method (e.g. "GET") or timestamp link to show the recorded profile.

The Firefox add-on adds a menu item named X-Wikimedia-Debug profile to the context menu, shown when you right-click on the page body.

XHGui feature:

  • Overview dashboard (e.g. listing functions that used the most time and/or most memory).
  • Complete call tree with timing, counts, sub/parent for each function.
  • Flamegraph visualisation. (example)
  • Callgraph visualisation.
  • and more...

To get an idea of what XHGui provides, view this example request.

Plaintext request profile

In addition to obtaining a profiler via XHGui (see above), it is also possible to get a plaintext-format profile directly from the web server.

Enable X-Wikimedia-Debug (profile attribute not required) and set a forceprofile=1 query parameter. On any MediaWiki web request (including index.php, api.php, and load.php), this will result in a plain text profile being appended to the web response.

With an X-Wikimedia-Debug header present, view this example request. Toward the end of the response is a call-graph summary, that should look roughly like this:

 100.00% 1437.125      1 - main()
  87.21% 1253.268      1 - ResourceLoader::respond
  79.31% 1139.756   1509 - ResourceLoaderModule::getVersionHash
  77.88% 1119.292      3 - ResourceLoader::getCombinedVersion

Example using cURL:

# Production
$ curl -H 'X-Wikimedia-Debug: 1' 'https://test.wikipedia.org/w/load.php?debug=false&modules=startup&only=scripts&forceprofile=1'

# Beta Cluster
$ curl -H 'X-Wikimedia-Debug: 1' 'https://en.wikipedia.beta.wmflabs.org/w/load.php?debug=false&modules=startup&only=scripts&forceprofile=1'

For maintenance scripts, this can be triggered using the --profiler=text option (which is a built-in from MediaWiki). This currently requires PHP=hhvm to be set, given the php5 run-time does not currently have xhprof installed:

krinkle@deployment-tin:~$ PHP=hhvm mwscript showJobs.php --wiki testwiki --profiler text
100.00% 282.967      1 - main()
 89.56% 253.419      1 - section.Setup.php
 57.12% 161.627    139 - AutoLoader::autoload
 49.59% 140.324      1 - ExtensionRegistry::loadFromQueue

Debug logging

Setting the log attribute in the X-Wikimedia-Debug header will cause MediaWiki to be maximally verbose, recording all log messages on all channels (regardless of whether or not they are otherwise enabled in wmf-config).

These messages will end up in Logstash at https://logstash.wikimedia.org/app/kibana#/dashboard/mwdebug and in /srv/mw-log/XWikimediaDebug.log on mwlog1001. See Logs#mw-log for more information.

To view the logs of a specific web request only, the browser extension adds a "Debug Logs" link in the footer on any wiki as well, which opens goes to Logstash with a filter for the request ID of the current page view. You can also construct this URL manually, by typing the following code into your browser's JavaScript console:

'https://logstash.wikimedia.org/#/dashboard/elasticsearch/request-id?id=' + mw.config.get('wgRequestId');

Available backends

The following application servers are reserved for X-Wikimedia-Debug use:

  • mwdebug1001.eqiad.wmnet
  • mwdebug1002.eqiad.wmnet
  • mwdebug2001.codfw.wmnet
  • mwdebug2002.codfw.wmnet

Browser extensions

The pop-out interface of the WikimediaDebug extension.


X-Wikimedia-Debug does not currently work on the Wikitech wiki, which is served by servers separated from the rest of the Wikimedia Foundation production cluster. Try it on Wikipedia instead!

See also