Skip to content

Commit

Permalink
Container: Add configurable RW timeout for redis scripts
Browse files Browse the repository at this point in the history
Also: get rid of configurable bucketDB
  • Loading branch information
Vladimir Dombrovski authored and fatpat committed Aug 25, 2020
1 parent fe799c8 commit e3ab56b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
26 changes: 12 additions & 14 deletions cmd/container.plugin/container.plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ package main

import (
"flag"
"log"
"github.com/open-io/openio-netdata-plugins/collector"
"github.com/open-io/openio-netdata-plugins/container"
"github.com/open-io/openio-netdata-plugins/netdata"
"log"
"os"
"strings"
"time"

"github.com/go-redis/redis"
)
Expand All @@ -39,15 +40,15 @@ func main() {
var limit int64
var threshold int64
var fast bool
var bucketdb string
var timeout time.Duration

fs := flag.NewFlagSet("", flag.ExitOnError)
fs.StringVar(&ns, "ns", "OPENIO", "List of namespaces delimited by semicolons (:)")
fs.StringVar(&conf, "conf", "/etc/oio/sds/", "Path to SDS config directory")
fs.StringVar(&addr, "addr", "", "Force redis IP:PORT for each namespace")
fs.StringVar(&bucketdb, "bucketdb", "", "BucketDB redis IP:PORT for each namespace")
fs.Int64Var(&limit, "limit", -1, "Amount of processed containers in a single request, -1 for unlimited")
fs.Int64Var(&threshold, "threshold", 3e5, "Minimal number of objects in container to report it")
fs.DurationVar(&timeout, "timeout", time.Duration(30*time.Second), "Read/Write timeout on redis requests")
fs.BoolVar(&fast, "fast", false, "Use fast account listing")
err := fs.Parse(os.Args[2:])
if err != nil {
Expand All @@ -57,10 +58,10 @@ func main() {

namespaces := strings.Split(ns, ":")
redisAddr := strings.Split(addr, ",")
collector.Run(intervalSeconds, makeCollect(conf, redisAddr, strings.Split(bucketdb, ","), namespaces, limit, threshold, fast))
collector.Run(intervalSeconds, makeCollect(conf, redisAddr, namespaces, limit, threshold, fast, timeout))
}

func makeCollect(basePath string, addr, bucketdb, namespaces []string, l int64, t int64, f bool) (collect collector.Collect) {
func makeCollect(basePath string, addr, namespaces []string, l int64, t int64, f bool, timeout time.Duration) (collect collector.Collect) {

return func(c chan netdata.Metric) error {
errors := make(map[string]error)
Expand All @@ -76,16 +77,13 @@ func makeCollect(basePath string, addr, bucketdb, namespaces []string, l int64,
return err
}
}
client := redis.NewClient(&redis.Options{Addr: redisAddr})

var bucketDBClient *redis.Client
if i < len(bucketdb) && bucketdb[i] != "" {
bucketDBClient = redis.NewClient(&redis.Options{Addr: bucketdb[i]})
} else {
bucketDBClient = client
}
client := redis.NewClient(&redis.Options{
Addr: redisAddr,
ReadTimeout: timeout,
WriteTimeout: timeout,
})

errors[ns] = container.Collect(client, bucketDBClient, ns, l, t, f, c)
errors[ns] = container.Collect(client, ns, l, t, f, c)
}
for _, err := range errors {
if err != nil {
Expand Down
30 changes: 14 additions & 16 deletions container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ package container
import (
"encoding/json"
"fmt"
"github.com/go-redis/redis"
"github.com/open-io/openio-netdata-plugins/netdata"
"github.com/open-io/openio-netdata-plugins/util"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/go-redis/redis"
)

var scriptGetAccounts = redis.NewScript(`
Expand Down Expand Up @@ -110,21 +110,19 @@ type bucketInfoStruct struct {
}

// Collect -- collect container metrics
func Collect(client, bucketdb *redis.Client, ns string, l int64, t int64, f bool, c chan netdata.Metric) error {
if bucketdb != nil {
bucketInfoStr, err := scriptBucketInfo.Run(bucketdb, []string{}, 0).Result()
if err != nil {
return err
}
bucketInfo := map[string]bucketInfoStruct{}
if err := json.Unmarshal([]byte(bucketInfoStr.(string)), &bucketInfo); err != nil {
return err
}
for name, info := range bucketInfo {
bucket := info.Account + "." + strings.Split(name, ":")[1]
netdata.Update("account_bucket_kilobytes", bucket, fmt.Sprintf("%d", info.Bytes/1000), c)
netdata.Update("account_bucket_objects", bucket, fmt.Sprintf("%d", info.Objects), c)
}
func Collect(client *redis.Client, ns string, l int64, t int64, f bool, c chan netdata.Metric) error {
bucketInfoStr, err := scriptBucketInfo.Run(client, []string{}, 0).Result()
if err != nil {
return err
}
bucketInfo := map[string]bucketInfoStruct{}
if err := json.Unmarshal([]byte(bucketInfoStr.(string)), &bucketInfo); err != nil {
return err
}
for name, info := range bucketInfo {
bucket := info.Account + "." + strings.Split(name, ":")[1]
netdata.Update("account_bucket_kilobytes", bucket, fmt.Sprintf("%d", info.Bytes/1000), c)
netdata.Update("account_bucket_objects", bucket, fmt.Sprintf("%d", info.Objects), c)
}

accounts, err := scriptGetAccounts.Run(client, []string{}, 0).Result()
Expand Down

0 comments on commit e3ab56b

Please sign in to comment.