Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1347 from erikvold/959461
Browse files Browse the repository at this point in the history
Bug 959461 - Create telemetry module r=@Gozala
  • Loading branch information
Gozala committed Apr 7, 2015
2 parents f9bff95 + 597814d commit 052fd98
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/sdk/addon/telemetry.js
@@ -0,0 +1,27 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';

module.metadata = {
"stability": "unstable"
};

const { Cu } = require("chrome");
const { Measurement: CoreMeasurement } = require("../telemetry/measurement");
const { Class } = require("../core/heritage");
const { id } = require("../self");
const { Services } = Cu.import("resource://gre/modules/Services.jsm", {});

exports.Measurement = Class({
extends: CoreMeasurement,
initialize: function(options) {
options.id = id;
CoreMeasurement.prototype.initialize.call(this, options);
}
});

function unregister() {
Services.telemetry.unregisterAddonHistograms(id);
}
exports.unregister = unregister;
29 changes: 29 additions & 0 deletions lib/sdk/system/telemetry.js
@@ -0,0 +1,29 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';

module.metadata = {
"stability": "unstable"
};

const { Measurement: CoreMeasurement } = require("../telemetry/measurement");
const { Class } = require("../core/heritage");

const Measurement = Class({
extends: CoreMeasurement,
initialize: function(options) {
CoreMeasurement.prototype.initialize.call(this, {
name: options.name
});
}
});

exports.add = function add(options) {
try {
Measurement({ name: options.name }).add(options.value);
}
catch(e) {
throw Error("options are invalid");
}
}
75 changes: 75 additions & 0 deletions lib/sdk/telemetry/measurement.js
@@ -0,0 +1,75 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';

module.metadata = {
"stability": "unstable"
};

const { Cu } = require("chrome");
const { Class } = require("../core/heritage");
const { contract } = require("../util/contract");
const { merge } = require("../util/object");

const { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
const Telemetry = Services.telemetry;

const model = WeakMap();

const TYPES = {
"linear": Telemetry.HISTOGRAM_LINEAR,
"exponential": Telemetry.HISTOGRAM_EXPONENTIAL,
}

exports.Measurement = Class({
initialize: function(options) {
options = merge({
id: undefined,
name: undefined,
min: 0,
max: undefined,
count: 5,
type: "linear"
}, options);
model.set(this, options);
register(options);
},
add: function add(value) {
let options = model.get(this);
if (!options || !options.name)
throw Error("options are invalid");

if (options.id) {
register(options);
let h = Telemetry.getAddonHistogram(options.id, options.name);
h.add(value);
}
else {
Telemetry.getHistogramById(options.name).add(value);
}
return undefined;
},
clear: function clear() {
let hist;
let { id, name } = model.get(this);
try {
Telemetry.getAddonHistogram(id, name).clear();
} catch(e) {}
}
});

function register(options) {
// if no id is provided then assume the histogram is a global
// declared in http://mxr.mozilla.org/mozilla-central/source/toolkit/components/telemetry/Histograms.json
if (options.id) {
try {
Telemetry.registerAddonHistogram(options.id,
options.name,
options.min,
options.max,
options.count,
TYPES[options.type]);
} catch(e) {}
}
}
57 changes: 57 additions & 0 deletions test/test-addon-telemetry.js
@@ -0,0 +1,57 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';

const { Cu } = require("chrome");
const { id } = require("sdk/self");
const { Measurement, unregister } = require('sdk/addon/telemetry');

const { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
const Telemetry = Services.telemetry;

exports.testAddClearUnregister = function(assert) {
let options = {
id: id,
name: "testing-histogram1",
min: 1,
max: 5,
count: 6
};
let m = Measurement(options);

assert.equal(options.id, id, 'options.id did not change');
assert.equal(options.name, "testing-histogram1", 'options.name did not change');
assert.throws(function() {
Telemetry.registerAddonHistogram(id, options.name, 1, 3, 5, 1);
}, /nsITelemetry.registerAddonHistogram/, 'registered telemetry measurement properly');
assert.throws(function() {
let h1 = Telemetry.getAddonHistogram(id + "X", options.name);
}, /nsITelemetry.getAddonHistogram/, 'fake id fails');
let hist = Telemetry.getAddonHistogram(id, options.name);
assert.equal(hist.snapshot().counts[0], 0, 'data does not exist');
m.add(0);
assert.equal(hist.snapshot().counts[0], 1, 'data was added');
m.clear();
assert.equal(hist.snapshot().counts[0], 0, 'data was added');
hist = Telemetry.getAddonHistogram(id, options.name);
unregister();
assert.throws(function() {
hist = Telemetry.getAddonHistogram(id, options.name);
}, /nsITelemetry.getAddonHistogram/, 'clear worked');

// try re-add afterwards
m.add(0);
hist = Telemetry.getAddonHistogram(id, options.name);
assert.equal(hist.snapshot().counts[0], 1, 'data was added');
m.clear();
assert.equal(hist.snapshot().counts[0], 0, 'data was added');
hist = Telemetry.getAddonHistogram(id, options.name);
unregister();
assert.throws(function() {
hist = Telemetry.getAddonHistogram(id, options.name);
}, /nsITelemetry.getAddonHistogram/, 'unregister worked');

}

require('sdk/test').run(exports);
38 changes: 38 additions & 0 deletions test/test-system-telemetry.js
@@ -0,0 +1,38 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';

const { add } = require('sdk/system/telemetry');

exports.testAddInvalid = function(assert) {
assert.throws(function() {
add({
name: 'UNKNOWN',
value: 200
});
}, /options are invalid/, 'cannot add to unknown');

assert.throws(function() {
add({
value: 200
});
}, /options are invalid/, 'cannot add to undefined');

assert.throws(function() {
add({
name: null,
value: 200
});
}, /options are invalid/, 'cannot add to null');

assert.throws(function() {
add({});
}, /options are invalid/, 'cannot add with blank options');

assert.throws(function() {
add();
}, /options are invalid/, 'cannot add with undefined options');
}

require('sdk/test').run(exports);

0 comments on commit 052fd98

Please sign in to comment.