summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/redis/init.go
blob: 6fcf4379d16f9eae5118ada0418aea00f1f4b64b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// SPDX-License-Identifier: GPL-3.0-or-later

package redis

import (
	"errors"

	"github.com/netdata/netdata/go/go.d.plugin/agent/module"
	"github.com/netdata/netdata/go/go.d.plugin/pkg/tlscfg"

	"github.com/go-redis/redis/v8"
)

func (r *Redis) validateConfig() error {
	if r.Address == "" {
		return errors.New("'address' not set")
	}
	return nil
}

func (r *Redis) initRedisClient() (*redis.Client, error) {
	opts, err := redis.ParseURL(r.Address)
	if err != nil {
		return nil, err
	}

	tlsConfig, err := tlscfg.NewTLSConfig(r.TLSConfig)
	if err != nil {
		return nil, err
	}

	if opts.TLSConfig != nil && tlsConfig != nil {
		tlsConfig.ServerName = opts.TLSConfig.ServerName
	}

	if opts.Username == "" && r.Username != "" {
		opts.Username = r.Username
	}
	if opts.Password == "" && r.Password != "" {
		opts.Password = r.Password
	}

	opts.PoolSize = 1
	opts.TLSConfig = tlsConfig
	opts.DialTimeout = r.Timeout.Duration()
	opts.ReadTimeout = r.Timeout.Duration()
	opts.WriteTimeout = r.Timeout.Duration()

	return redis.NewClient(opts), nil
}

func (r *Redis) initCharts() (*module.Charts, error) {
	return redisCharts.Copy(), nil
}