Skip to content

[JS] Create Instrument class/newInstrument API

Phuedx requested to merge work/phuedx/submitter into main

Instruments using MetricsClient#submitInteraction() and other methods will need to repeat the streamName and schemaID parameters on every invocation. In order to allow instrument maintainers to keep their instruments DRY, I propose adding the Instrument class, a class that has a MetricsClient-like interface but is bound to a specific {streamName,schemaID} pair.

Example

type Action = [ 'scroll', 'scroll-to-top' ];

const m = createMetricsClient();
const instrument = m.newInstrument(
  'mediawiki.web_ui_scroll_migrated',
  '/analytics/mediawiki/product_metrics/web_ui_actions/1.0.1'
);

// …

/**
 * @param {Action} action
 */
function log( action ) {
    instrument.submitInteraction( action );
}

// …

// Is the instrument enabled?
//if ( instrument.isStreamInSample() ) {
if ( instrument.isEnabled() ) {
}

c.f. mediawiki/extensions/WikimediaEvents/modules/ext.wikimediaEvents/webUIScroll.js

Future Improvements

  1. Allow MetricsClient#newInstrument() to accept a {streamName,schemaVersion} pair. Stream configs must have a schema_title property. Combining that property with a version creates a schema ID

  2. Allow MetricsClient#newInstrument() to accept only instrumentName. In future, the MetricsPlatform MediaWiki extension will be updated to put more information about instruments inside of the relevant stream config. When this is done, we can update MetricsClient to use this information when constructing Instrument instances


Notes

  1. The Mobile Web team (who became the Web team) had the same idea with the mw.eventLog.Schema class.

Depends-On: !42 (merged)

Bug: T366827

Edited by Phuedx

Merge request reports

Loading