Skip to content
This repository has been archived by the owner on Dec 2, 2020. It is now read-only.

Addition of option to allow events to expire after a certain time #128

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/cube/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var mongodb = require("mongodb"),
tiers = require("./tiers"),
types = require("./types"),
bisect = require("./bisect"),
ObjectID = mongodb.ObjectID;
ObjectID = mongodb.ObjectID,
collector_config = require("../../bin/collector-config");

var type_re = /^[a-z][a-zA-Z0-9_]+$/,
invalidate = {$set: {i: true}},
Expand All @@ -22,6 +23,9 @@ var streamDelayDefault = 5000,
// How frequently to invalidate metrics after receiving events.
var invalidateInterval = 5000;

// Set the Time To Live interval in seconds from the collector config
var expireEventsAfterSeconds = collector_config.event_ttl_seconds;

exports.putter = function(db) {
var collection = types(db),
knownByType = {},
Expand Down Expand Up @@ -63,7 +67,13 @@ exports.putter = function(db) {
if (names.length) return saveEvents();

// Events are indexed by time.
events.ensureIndex({"t": 1}, handle);
if (expireEventsAfterSeconds) {
// Events are indexed by time and expired after a supplied period (expireEventsAfterSeconds).
events.ensureIndex({"t": 1}, { expireAfterSeconds: expireEventsAfterSeconds }, handle);
}
else {
events.ensureIndex({"t": 1}, handle);
}

// Create a capped collection for metrics. Three indexes are required: one
// for finding metrics, one (_id) for updating, and one for invalidation.
Expand Down