forked from go-graphite/graphite-clickhouse
/
config.go
153 lines (128 loc) · 3.15 KB
/
config.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package config
import (
"bytes"
"fmt"
"io/ioutil"
"time"
"github.com/BurntSushi/toml"
"github.com/lomik/graphite-clickhouse/helper/rollup"
)
// Duration wrapper time.Duration for TOML
type Duration struct {
time.Duration
}
var _ toml.TextMarshaler = &Duration{}
// UnmarshalText from TOML
func (d *Duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
// MarshalText encode text with TOML format
func (d *Duration) MarshalText() ([]byte, error) {
return []byte(d.Duration.String()), nil
}
// Value return time.Duration value
func (d *Duration) Value() time.Duration {
return d.Duration
}
type Common struct {
Listen string `toml:"listen"`
// MetricPrefix string `toml:"metric-prefix"`
// MetricInterval *Duration `toml:"metric-interval"`
// MetricEndpoint string `toml:"metric-endpoint"`
MaxCPU int `toml:"max-cpu"`
}
type ClickHouse struct {
Url string `toml:"url"`
DataTable string `toml:"data-table"`
DataTimeout *Duration `toml:"data-timeout"`
TreeTable string `toml:"tree-table"`
TreeTimeout *Duration `toml:"tree-timeout"`
TagTable string `toml:"tag-table"`
RollupConf string `toml:"rollup-conf"`
ExtraPrefix string `toml:"extra-prefix"`
}
type Tags struct {
Rules string `toml:"rules"`
Date string `toml:"date"`
InputFile string `toml:"input-file"`
OutputFile string `toml:"output-file"`
}
type Logging struct {
File string `toml:"file"`
Level string `toml:"level"`
}
// Config ...
type Config struct {
Common Common `toml:"common"`
ClickHouse ClickHouse `toml:"clickhouse"`
Tags Tags `toml:"tags"`
Logging Logging `toml:"logging"`
Rollup *rollup.Rollup `toml:"-"`
}
// NewConfig ...
func New() *Config {
cfg := &Config{
Common: Common{
Listen: ":9090",
// MetricPrefix: "carbon.graphite-clickhouse.{host}",
// MetricInterval: &Duration{
// Duration: time.Minute,
// },
// MetricEndpoint: MetricEndpointLocal,
MaxCPU: 1,
},
ClickHouse: ClickHouse{
Url: "http://localhost:8123",
DataTable: "graphite",
DataTimeout: &Duration{
Duration: time.Minute,
},
TreeTable: "graphite_tree",
TreeTimeout: &Duration{
Duration: time.Minute,
},
RollupConf: "/etc/graphite-clickhouse/rollup.xml",
TagTable: "",
},
Tags: Tags{
Date: "2016-11-01",
Rules: "/etc/graphite-clickhouse/tag.d/*.conf",
},
Logging: Logging{
File: "/var/log/graphite-clickhouse/graphite-clickhouse.log",
Level: "info",
},
}
return cfg
}
// PrintConfig ...
func Print(cfg interface{}) error {
buf := new(bytes.Buffer)
encoder := toml.NewEncoder(buf)
encoder.Indent = ""
if err := encoder.Encode(cfg); err != nil {
return err
}
fmt.Print(buf.String())
return nil
}
// Parse ...
func Parse(filename string, cfg *Config) error {
if filename != "" {
if _, err := toml.DecodeFile(filename, cfg); err != nil {
return err
}
}
rollupConfBody, err := ioutil.ReadFile(cfg.ClickHouse.RollupConf)
if err != nil {
return err
}
r, err := rollup.ParseXML(rollupConfBody)
if err != nil {
return err
}
cfg.Rollup = r
return nil
}