|
| 1 | +# RRD Package |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +Package rrd provides a Round Robin Database (RRD) implementation using BoltDB. |
| 6 | + |
| 7 | +The RRD is designed to store monotonically increasing counter values, making it easy to compute the increase of a metric over a fixed time window. Data is stored in fixed-size time slots, and older data is automatically deleted after the retention duration. |
| 8 | + |
| 9 | +## Structure |
| 10 | + |
| 11 | +**Interfaces:** |
| 12 | + - `RRD` : the main API for interacting with the DB |
| 13 | + - `Slot` : represents one time window bucket |
| 14 | + - `Printer` : for printing DB contents |
| 15 | + |
| 16 | +**Structs:** |
| 17 | + - `rrdBolt` implements `RRD` |
| 18 | + - `rrdSlot` implements `Slot` |
| 19 | + |
| 20 | +### 1- RRD Interface |
| 21 | +```go |
| 22 | +type RRD interface { |
| 23 | + // Slot returns the current window (slot) to store values. |
| 24 | + Slot() (Slot, error) |
| 25 | + |
| 26 | + // Counters, return all stored counters since the given time (since) until now. |
| 27 | + Counters(since time.Time) (map[string]float64, error) |
| 28 | + |
| 29 | + // Last returns the last reported value for a metric given the metric name |
| 30 | + Last(key string) (value float64, ok bool, err error) |
| 31 | + |
| 32 | + // Close the db |
| 33 | + Close() error |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +#### Constructor |
| 38 | + |
| 39 | +```go |
| 40 | +// NewRRDBolt creates a new rrd database that uses bolt as a storage |
| 41 | +// if window or retention are 0, the function will panic. |
| 42 | +// If retnetion is smaller then window the function will panic. |
| 43 | +// retention and window must be multiple of 1 minute. |
| 44 | +func NewRRDBolt(path string, window time.Duration, retention time.Duration) (RRD, error) |
| 45 | +``` |
| 46 | + |
| 47 | +#### rrdBolt Struct |
| 48 | +`rrdBolt` implements `RRD` and also implements the `Printer` interface |
| 49 | + |
| 50 | +```go |
| 51 | +type rrdBolt struct { |
| 52 | + db *bolt.DB // BoltDB database handle |
| 53 | + window uint64 // Window size in seconds |
| 54 | + retention uint64 // Retention duration in seconds |
| 55 | +} |
| 56 | + |
| 57 | +// RDD interface methods |
| 58 | +Slot() (Slot, error) |
| 59 | +Counters(since time.Time) (map[string]float64, error) |
| 60 | +Last(key string) (value float64, ok bool, err error) |
| 61 | +Close() error |
| 62 | + |
| 63 | +// Printer interface methods |
| 64 | +Print(out io.Writer) error |
| 65 | + |
| 66 | +// extra methods |
| 67 | +// Slots return all the slot timestamps stored in database |
| 68 | +Slots() ([]uint64, error) |
| 69 | +``` |
| 70 | + |
| 71 | +### 2- Slot Interface |
| 72 | + |
| 73 | +```go |
| 74 | +type Slot interface { |
| 75 | + // Counter sets (or overrides) the current stored value for this key, with the passed value |
| 76 | + Counter(key string, value float64) error |
| 77 | + |
| 78 | + // Key return the key of the slot which is the window timestamp |
| 79 | + Key() uint64 |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +#### rrdSlot Struct |
| 84 | +`rrdSlot` implements `Slot` |
| 85 | + |
| 86 | +```go |
| 87 | +type rrdSlot struct { |
| 88 | + db *bolt.DB // BoltDB database handle |
| 89 | + key uint64 // slot timestamp |
| 90 | +} |
| 91 | + |
| 92 | +// Slot interface methods |
| 93 | +Counter(key string, value float64) error |
| 94 | +Key() uint64 |
| 95 | +``` |
| 96 | + |
| 97 | +### 3- Printer Interface |
| 98 | + |
| 99 | +```go |
| 100 | +type Printer interface { |
| 101 | + // Print writes the content of the RRD to the passed writer |
| 102 | + Print(w io.Writer) error |
| 103 | +} |
| 104 | +``` |
0 commit comments