Page MenuHomePhabricator

Figure out XAnalytics stuff
Open, MediumPublic

Description

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

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.

Event Timeline

Krinkle renamed this task from Clean up XAnalytics stuff to Figure out XAnalytics stuff.Mar 22 2018, 2:49 AM
Krinkle updated the task description. (Show Details)

We think @Krinkle's question is better addressed by @Tgr

Krinkle triaged this task as Medium priority.Mar 26 2018, 8:07 PM

No I don't.

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

Don't do this. The API doesn't operate on a single page in the index.php sense and OutputPage shouldn't be used in API code at all.

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

Don't do this. The API doesn't operate on a single page in the index.php sense and OutputPage shouldn't be used in API code at all.

I agree.

Just to clarify, I wasn't proposing this pattern as something new. The "ApiModule with OutputPage title" approach is what XAnalytics already does, today (and currently produces ns=-1;special=Badtitle on most responses). I'm proposing someone should either remove that (assuming its unused), or (cited above) fix it and start using for ApiMobileView.

I assume you'd support removing this :)

I'm not familiar with XAnalytics but it seems like b9d8ead6485 broke it; either it should have fixed the buffer logic, or it should have removed the addedHeader check in addItem (the new header replaces the old one so it doesn't really matter how many times createHeader is called).

How WikimediaEvents uses the hook is completely unrelated to that. Presumably it shouldn't use it on API calls, and ApiMobileView should just replay the same logic instead of relying on WikimediaEvents (and it should stop overriding the title).

This comment was removed by Jdlrobson.

I'm not sure if it helps, but confusingly there is also some code in MobileFrontend, added a long time ago https://gerrit.wikimedia.org/r/#/c/52605/
It looks like this was added for beta/alpha/zero mode and I'm pretty sure it's redundant now (not used anywhere https://github.com/search?q=org%3Awikimedia+addAnalyticsLogItem&type=Code)

With respect to the API it was added in I178ecf as part of T92875. That might provide some background.

I confirm MobileFrontend has it's own X-Analytics header handling for regular requests (not API) doesn't use the XAnalytics extension. I'm not sure if stuff coded in MobileContext::addAnalyticsItem and MobileContext::getXAnalyticsHeader somehow doesn't modify the Application state when API endpoint is called ( handling is called on RequestContextCreateSkin hook and on BeforePageDisplay). I'll check whats going, and if possible I want to rewrite MF, and use the XAnalytics extension (see T217859)

This is less relevant every time, because we're are moving towards an event-centric analytics framework.
Moving to deprioritized.

@mforns Per T190381#5054344 when can we plan to remove this code. I'm not sure what to do with this task :)

@Jdlrobson there are bits of data in X-Analytics that are used in the pageview pipeline like for example the "special" page bit (when working well) so that we cannot remove.

https://github.com/wikimedia/analytics-refinery-source/blob/master/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/PageviewDefinition.java#L248

Now, there might be another usages of the X-Analytics that are no longer needed and it sounds like the MobileFrontend has a few of those. From this list : https://wikitech.wikimedia.org/wiki/X-Analytics the one that stands outs as not belonging is the first one.