Following T92875, a about three things were put in place of which aren't working exactly as they should.
The XAnalytics MediaWiki extension (source, review) is a way for MediaWiki extensions to add data to Wikimedia's X-Analytics header (which was originally only generated at the Varnish layer).
The MediaWiki extension part of it, exposes the following:
- A hook XAnalyticsSetHeader
- A method XAnalytics::addItem
Hook
The hook works as advertised. It fires once, and the WikimediaEvents extension adds properties through it. It also seems to specifically have logic to fire from API responses. But it's not clear what that should do, or whether that works.
Method
The method was later added to make adding things easier. The idea was that if its called early, it adds to a buffer added to the result from the hook at the main emission point. And if called late, it would create another header. However, the first part was never properly implemented. The remainder of which I removed in https://gerrit.wikimedia.org/r/#/c/338931/ (b9d8ead6485). At the time I found 0 uses of the method, and I was waiting for feedback at T92875 for whether it is even needed, given the hook seems to be working, and we should probably remove the malfunctioning method.
That feedback never came, and it seems MobileFrontend is now actually using the method. It uses it to add the same set of properties that WikimediaEvents adds, but for action=mobileview API requests.
Result
Page view
curl -I 'https://en.wikipedia.org/wiki/Star_Wars_Hyperspace_Mountain' ... x-analytics: ns=0;page_id=12996382;https=1;nocookies=1
- ns and page_id come from WikimediaEvents::onXAnalyticsHeader, which is called from the hook.
API response (MediaWiki core)
# Misc site data curl -I 'https://en.wikipedia.org/w/api.php?format=json&action=query&meta=siteinfo' ... x-analytics: ns=-1;special=Badtitle;loggedIn=1;... # Page meta data curl -I 'https://en.wikipedia.org/w/api.php?format=json&action=query&titles=Example' ... x-analytics: ns=-1;special=Badtitle;loggedIn=1;... # Wikitext parsing curl -I 'https://en.wikipedia.org/w/api.php?format=json&action=parse&page=Example' ... x-analytics: ns=-1;special=Badtitle;loggedIn=1;...
- When in API mode, the XAnalytics extension also triggers the hook (per XAnalyticsHooks), which means WikimediaEvents::onXAnalyticsHeader runs the same logic here as well.
- The ApiMain class implements RequestContext and has a (mostly unused) OutputPage and Title object associated, which defaults to Special:Badtitle for debugging purposes - which is reflected here.
Question: Can $apiMain->getOutput()->getTitle() ever return something other than Special:Badtitle? If not, we should probably remove the logic from XAnalytics that triggers the same hook during API responses. Right now it just produces bogus data claiming views for Special:Badtitle for our entire volume of API traffic, aside from the below case.
API response (MobileFrontend)
curl -I 'https://en.wikipedia.org/w/api.php?format=json&action=mobileview&page=Example' ... x-analytics: ns=0;page_id=1646233;...
- MobileFrontend's ApiMobileView (code) calls its addXAnalyticsItem utility which in turn calls XAnalytics::addItem
- Given its values seem to be reflected in the web response, it must mean that this code (luckily?) runs after the API hook for XAnalytics because the XAnalytics::addItem method only works if used after the hook has ran.
Question: Where do the values in the mobileview headers come from?
If they do indeed work through addItem(), we should see if it is possible to improve on this. Ideas:
- Fix the method to work reliably both before and after output.
- Or; Remove the current method and instead migrate MF to the XAnalyticsSetHeader hook.
- Or: Consider a way for MF/ApiMobileView to communicate the title in a more interoperable way to WikimediaEvents, e.g. by setting the $module->getOutput()->getTitle() to the appropiate title so that WE's code starts working automatically without needing the extra hook or instrumentation.