@segment/analytics-core
Version:
This package represents core 'shared' functionality that is shared by analytics packages. This is not designed to be used directly, but internal to analytics-node and analytics-browser.
96 lines • 2.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NullStats = exports.CoreStats = void 0;
var tslib_1 = require("tslib");
var compactMetricType = function (type) {
var enums = {
gauge: 'g',
counter: 'c',
};
return enums[type];
};
var CoreStats = /** @class */ (function () {
function CoreStats() {
this.metrics = [];
}
CoreStats.prototype.increment = function (metric, by, tags) {
if (by === void 0) { by = 1; }
this.metrics.push({
metric: metric,
value: by,
tags: tags !== null && tags !== void 0 ? tags : [],
type: 'counter',
timestamp: Date.now(),
});
};
CoreStats.prototype.gauge = function (metric, value, tags) {
this.metrics.push({
metric: metric,
value: value,
tags: tags !== null && tags !== void 0 ? tags : [],
type: 'gauge',
timestamp: Date.now(),
});
};
CoreStats.prototype.flush = function () {
var formatted = this.metrics.map(function (m) { return (tslib_1.__assign(tslib_1.__assign({}, m), { tags: m.tags.join(',') })); });
// ie doesn't like console.table
if (console.table) {
console.table(formatted);
}
else {
console.log(formatted);
}
this.metrics = [];
};
/**
* compact keys for smaller payload
*/
CoreStats.prototype.serialize = function () {
return this.metrics.map(function (m) {
return {
m: m.metric,
v: m.value,
t: m.tags,
k: compactMetricType(m.type),
e: m.timestamp,
};
});
};
return CoreStats;
}());
exports.CoreStats = CoreStats;
var NullStats = /** @class */ (function (_super) {
tslib_1.__extends(NullStats, _super);
function NullStats() {
return _super !== null && _super.apply(this, arguments) || this;
}
NullStats.prototype.gauge = function () {
var _args = [];
for (var _i = 0; _i < arguments.length; _i++) {
_args[_i] = arguments[_i];
}
};
NullStats.prototype.increment = function () {
var _args = [];
for (var _i = 0; _i < arguments.length; _i++) {
_args[_i] = arguments[_i];
}
};
NullStats.prototype.flush = function () {
var _args = [];
for (var _i = 0; _i < arguments.length; _i++) {
_args[_i] = arguments[_i];
}
};
NullStats.prototype.serialize = function () {
var _args = [];
for (var _i = 0; _i < arguments.length; _i++) {
_args[_i] = arguments[_i];
}
return [];
};
return NullStats;
}(CoreStats));
exports.NullStats = NullStats;
//# sourceMappingURL=index.js.map