forked from atlassian/gostatsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.go
54 lines (46 loc) · 1.57 KB
/
backend.go
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
package backend
import (
"fmt"
"github.com/atlassian/gostatsd/backend/backends/datadog"
"github.com/atlassian/gostatsd/backend/backends/graphite"
"github.com/atlassian/gostatsd/backend/backends/null"
"github.com/atlassian/gostatsd/backend/backends/statsdaemon"
"github.com/atlassian/gostatsd/backend/backends/stdout"
backendTypes "github.com/atlassian/gostatsd/backend/types"
log "github.com/Sirupsen/logrus"
"github.com/spf13/viper"
)
// All known backends.
var backends = map[string]backendTypes.Factory{
datadog.BackendName: datadog.NewClientFromViper,
graphite.BackendName: graphite.NewClientFromViper,
null.BackendName: null.NewClientFromViper,
statsdaemon.BackendName: statsdaemon.NewClientFromViper,
stdout.BackendName: stdout.NewClientFromViper,
}
// GetBackend creates an instance of the named backend, or nil if
// the name is not known. The error return is only used if the named backend
// was known but failed to initialize.
func GetBackend(name string, v *viper.Viper) (backendTypes.Backend, error) {
f, found := backends[name]
if !found {
return nil, nil
}
return f(v)
}
// InitBackend creates an instance of the named backend.
func InitBackend(name string, v *viper.Viper) (backendTypes.Backend, error) {
if name == "" {
log.Info("No backend specified.")
return nil, nil
}
backend, err := GetBackend(name, v)
if err != nil {
return nil, fmt.Errorf("could not init backend %q: %v", name, err)
}
if backend == nil {
return nil, fmt.Errorf("unknown backend %q", name)
}
log.Infof("Initialised backend %q", name)
return backend, nil
}