Skip to content

Commit

Permalink
feat: handling retention policy in influxdb
Browse files Browse the repository at this point in the history
docs: Adding storage_driver_influxdb_retention_policy flag to the influxdb documentation

fix: using rentention policy variable in test
  • Loading branch information
Vincent Daniel committed Apr 28, 2017
1 parent 4e25a79 commit ef0f4d1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
2 changes: 2 additions & 0 deletions docs/storage/influxdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Specify what InfluxDB instance to push data to:
-storage_driver_password
# Use secure connection with database. False by default
-storage_driver_secure
# retention policy. Default is '' which corresponds to the default retention policy of the influxdb database
-storage_driver_influxdb_retention_policy
```

# Examples
Expand Down
27 changes: 17 additions & 10 deletions storage/influxdb/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package influxdb

import (
"flag"
"fmt"
"net/url"
"os"
Expand All @@ -32,6 +33,8 @@ func init() {
storage.RegisterStorageDriver("influxdb", new)
}

var argDbRetentionPolicy = flag.String("storage_driver_influxdb_retention_policy", "", "retention policy")

type influxdbStorage struct {
client *influxdb.Client
machineName string
Expand Down Expand Up @@ -82,6 +85,7 @@ func new() (storage.StorageDriver, error) {
hostname,
*storage.ArgDbTable,
*storage.ArgDbName,
*argDbRetentionPolicy,
*storage.ArgDbUsername,
*storage.ArgDbPassword,
*storage.ArgDbHost,
Expand Down Expand Up @@ -243,10 +247,11 @@ func (self *influxdbStorage) AddStats(ref info.ContainerReference, stats *info.C

batchTags := map[string]string{tagMachineName: self.machineName}
bp := influxdb.BatchPoints{
Points: points,
Database: self.database,
Tags: batchTags,
Time: stats.Timestamp,
Points: points,
Database: self.database,
RetentionPolicy: self.retentionPolicy,
Tags: batchTags,
Time: stats.Timestamp,
}
response, err := self.client.Write(bp)
if err != nil || checkResponseForErrors(response) != nil {
Expand All @@ -268,6 +273,7 @@ func newStorage(
machineName,
tablename,
database,
retentionPolicy,
username,
password,
influxdbHost string,
Expand All @@ -294,12 +300,13 @@ func newStorage(
}

ret := &influxdbStorage{
client: client,
machineName: machineName,
database: database,
bufferDuration: bufferDuration,
lastWrite: time.Now(),
points: make([]*influxdb.Point, 0),
client: client,
machineName: machineName,
database: database,
retentionPolicy: retentionPolicy,
bufferDuration: bufferDuration,
lastWrite: time.Now(),
points: make([]*influxdb.Point, 0),
}
ret.readyToFlush = ret.defaultReadyToFlush
return ret, nil
Expand Down
13 changes: 10 additions & 3 deletions storage/influxdb/influxdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func runStorageTest(f func(test.TestStorageDriver, *testing.T), t *testing.T, bu
username := "root"
password := "root"
hostname := "localhost:8086"
retentionPolicy := "cadvisor_test_rp"
// percentilesDuration := 10 * time.Minute

config := influxdb.Config{
Expand All @@ -103,7 +104,7 @@ func runStorageTest(f func(test.TestStorageDriver, *testing.T), t *testing.T, bu
}

// Re-create the database first.
if err := prepareDatabase(client, database); err != nil {
if err := prepareDatabase(client, database, retentionPolicy); err != nil {
t.Fatal(err)
}

Expand All @@ -113,6 +114,7 @@ func runStorageTest(f func(test.TestStorageDriver, *testing.T), t *testing.T, bu
driver, err := newStorage(machineName,
table,
database,
retentionPolicy,
username,
password,
hostname,
Expand All @@ -133,6 +135,7 @@ func runStorageTest(f func(test.TestStorageDriver, *testing.T), t *testing.T, bu
driverForAnotherMachine, err := newStorage("machineB",
table,
database,
retentionPolicy,
username,
password,
hostname,
Expand All @@ -150,7 +153,7 @@ func runStorageTest(f func(test.TestStorageDriver, *testing.T), t *testing.T, bu
f(testDriver, t)
}

func prepareDatabase(client *influxdb.Client, database string) error {
func prepareDatabase(client *influxdb.Client, database string, retentionPolicy string) error {
dropDbQuery := influxdb.Query{
Command: fmt.Sprintf("drop database \"%v\"", database),
}
Expand All @@ -161,7 +164,7 @@ func prepareDatabase(client *influxdb.Client, database string) error {
// Depending on the InfluxDB configuration it may be created automatically with the database or not.
// TODO create ret. policy only if not present
createPolicyQuery := influxdb.Query{
Command: fmt.Sprintf("create retention policy \"default\" on \"%v\" duration 1h replication 1 default", database),
Command: fmt.Sprintf("create retention policy \"%v\" on \"%v\" duration 1h replication 1 default", retentionPolicy, database),
}
_, err := client.Query(dropDbQuery)
if err != nil {
Expand All @@ -181,13 +184,15 @@ func TestContainerFileSystemStatsToPoints(t *testing.T) {
machineName := "testMachine"
table := "cadvisor_table"
database := "cadvisor_test"
retentionPolicy := "cadvisor_test_rp"
username := "root"
password := "root"
influxdbHost := "localhost:8086"

storage, err := newStorage(machineName,
table,
database,
retentionPolicy,
username,
password,
influxdbHost,
Expand Down Expand Up @@ -252,13 +257,15 @@ func createTestStorage() (*influxdbStorage, error) {
machineName := "testMachine"
table := "cadvisor_table"
database := "cadvisor_test"
retentionPolicy := "cadvisor_test_rp"
username := "root"
password := "root"
influxdbHost := "localhost:8086"

storage, err := newStorage(machineName,
table,
database,
retentionPolicy,
username,
password,
influxdbHost,
Expand Down

0 comments on commit ef0f4d1

Please sign in to comment.