From Wikitech
Jump to: navigation, search

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 mwdebug1002.eqiad.wmnet:

$ curl -H 'X-Wikimedia-Debug: backend=mwdebug1002.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=mwdebug1002.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=mwdebug1002.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 mwdebug1002.eqiad.wmnet
you@mwdebug1002:~$ 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