Skip to content

Commit 9a0f5bd

Browse files
committed
use base struct instead of tagdesc
1 parent 646a7e9 commit 9a0f5bd

File tree

7 files changed

+43
-16
lines changed

7 files changed

+43
-16
lines changed

carbon/app.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/url"
77
"os"
88
"path/filepath"
9+
"regexp"
910
"runtime"
1011
"strings"
1112
"sync"
@@ -192,6 +193,8 @@ func (app *App) Start() (err error) {
192193

193194
app.writeChan = make(chan *RowBinary.WriteBuffer)
194195

196+
validationRegex := regexp.MustCompile(app.Config.Common.ValidationRegex)
197+
195198
/* WRITER start */
196199
uploaders := make([]string, 0, len(conf.Upload))
197200
for t := range conf.Upload {
@@ -256,6 +259,7 @@ func (app *App) Start() (err error) {
256259
receiver.DropPast(uint32(conf.Tcp.DropPast.Value().Seconds())),
257260
receiver.DropLongerThan(conf.Tcp.DropLongerThan),
258261
receiver.ReadTimeout(uint32(conf.Tcp.ReadTimeout.Value().Seconds())),
262+
receiver.ValidationRegex(validationRegex),
259263
)
260264

261265
if err != nil {
@@ -274,6 +278,7 @@ func (app *App) Start() (err error) {
274278
receiver.DropFuture(uint32(conf.Udp.DropFuture.Value().Seconds())),
275279
receiver.DropPast(uint32(conf.Udp.DropPast.Value().Seconds())),
276280
receiver.DropLongerThan(conf.Udp.DropLongerThan),
281+
receiver.ValidationRegex(validationRegex),
277282
)
278283

279284
if err != nil {
@@ -292,6 +297,7 @@ func (app *App) Start() (err error) {
292297
receiver.DropFuture(uint32(conf.Pickle.DropFuture.Value().Seconds())),
293298
receiver.DropPast(uint32(conf.Pickle.DropPast.Value().Seconds())),
294299
receiver.DropLongerThan(conf.Pickle.DropLongerThan),
300+
receiver.ValidationRegex(validationRegex),
295301
)
296302

297303
if err != nil {
@@ -309,6 +315,7 @@ func (app *App) Start() (err error) {
309315
receiver.DropFuture(uint32(conf.Grpc.DropFuture.Value().Seconds())),
310316
receiver.DropPast(uint32(conf.Grpc.DropPast.Value().Seconds())),
311317
receiver.DropLongerThan(conf.Grpc.DropLongerThan),
318+
receiver.ValidationRegex(validationRegex),
312319
)
313320

314321
if err != nil {
@@ -326,6 +333,7 @@ func (app *App) Start() (err error) {
326333
receiver.DropFuture(uint32(conf.Prometheus.DropFuture.Value().Seconds())),
327334
receiver.DropPast(uint32(conf.Prometheus.DropPast.Value().Seconds())),
328335
receiver.DropLongerThan(conf.Prometheus.DropLongerThan),
336+
receiver.ValidationRegex(validationRegex),
329337
)
330338

331339
if err != nil {
@@ -344,6 +352,7 @@ func (app *App) Start() (err error) {
344352
receiver.DropPast(uint32(conf.TelegrafHttpJson.DropPast.Value().Seconds())),
345353
receiver.DropLongerThan(conf.TelegrafHttpJson.DropLongerThan),
346354
receiver.ConcatChar(conf.TelegrafHttpJson.Concat),
355+
receiver.ValidationRegex(validationRegex),
347356
)
348357

349358
if err != nil {

carbon/config.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"io/ioutil"
7+
"regexp"
78
"strings"
89
"time"
910

@@ -20,11 +21,12 @@ const (
2021
)
2122

2223
type commonConfig struct {
23-
MetricPrefix string `toml:"metric-prefix"`
24-
MetricInterval *config.Duration `toml:"metric-interval"`
25-
MetricEndpoint string `toml:"metric-endpoint"`
26-
MaxCPU int `toml:"max-cpu"`
27-
Enabled bool `toml:"enabled"`
24+
MetricPrefix string `toml:"metric-prefix"`
25+
MetricInterval *config.Duration `toml:"metric-interval"`
26+
MetricEndpoint string `toml:"metric-endpoint"`
27+
MaxCPU int `toml:"max-cpu"`
28+
Enabled bool `toml:"enabled"`
29+
ValidationRegex string `toml:"validation-regex"`
2830
}
2931

3032
type clickhouseConfig struct {
@@ -279,6 +281,12 @@ func ReadConfig(filename string, exactConfig bool) (*Config, error) {
279281
}
280282
}
281283

284+
if cfg.Common.ValidationRegex != "" {
285+
if _, err := regexp.Compile(cfg.Common.ValidationRegex); err != nil {
286+
return nil, err
287+
}
288+
}
289+
282290
if cfg.Logging == nil {
283291
cfg.Logging = make([]zapwriter.Config, 0)
284292
}

helper/tags/graphite.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,12 @@ type TemplateDesc struct {
190190
}
191191

192192
type TagConfig struct {
193-
Enabled bool `toml:"enabled"`
194-
Separator string `toml:"separator"`
195-
ValidationRegex string `toml:"validation-regex"`
196-
ValidationRegexCompiled *regexp.Regexp `toml:"-"`
197-
Tags []string `toml:"tags"`
198-
TagMap map[string]string `toml:"-"`
199-
Templates []string `toml:"templates"`
200-
TemplateDescs []TemplateDesc `toml:"-"`
193+
Enabled bool `toml:"enabled"`
194+
Separator string `toml:"separator"`
195+
Tags []string `toml:"tags"`
196+
TagMap map[string]string `toml:"-"`
197+
Templates []string `toml:"templates"`
198+
TemplateDescs []TemplateDesc `toml:"-"`
201199
}
202200

203201
func DisabledTagConfig() TagConfig {
@@ -227,7 +225,6 @@ func (cfg *TagConfig) Configure() error {
227225
makeTagMap(cfg.TagMap, cfg.Tags)
228226

229227
var err error
230-
cfg.ValidationRegexCompiled, err = regexp.Compile(cfg.ValidationRegex)
231228
if err != nil {
232229
return err
233230
}

receiver/base.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package receiver
33
import (
44
"fmt"
55
"net/http"
6+
"regexp"
67
"sort"
78
"sync"
89
"sync/atomic"
@@ -36,6 +37,7 @@ type Base struct {
3637
dropPastSeconds uint32
3738
dropTooLongLimit uint16
3839
readTimeoutSeconds uint32
40+
validationRegex *regexp.Regexp
3941
writeChan chan *RowBinary.WriteBuffer
4042
logger *zap.Logger
4143
Tags tags.TagConfig

receiver/plain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (base *Base) PlainParseLine(p []byte, now uint32, buf *tags.GraphiteBuf) ([
7575
value float64
7676
)
7777

78-
if base.Tags.ValidationRegexCompiled != nil && base.Tags.ValidationRegexCompiled.Match(p[:i1]) {
78+
if base.validationRegex != nil && base.validationRegex.Match(p[:i1]) {
7979
return nil, 0, 0, errors.New("message contains invalid characters: '" + unsafeString(p) + "'")
8080
}
8181

receiver/plain_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func TestPlainParseLine(t *testing.T) {
309309
{"app:service:metric;env=prod:primary 42.15 1422642189\n", "app:service:metric?env=prod%3Aprimary", 42.15, 1422642189},
310310
}
311311

312-
baseWithValidation := &Base{Tags: tags.TagConfig{ValidationRegexCompiled: regexp.MustCompile(`[^a-zA-Z0-9.;\-_:=]{1}`)}}
312+
baseWithValidation := &Base{validationRegex: regexp.MustCompile(`[^a-zA-Z0-9.;\-_:=]{1}`)}
313313
for _, p := range tableWithValidation {
314314
name, value, timestamp, err := baseWithValidation.PlainParseLine([]byte(p.b), now, &tagBuf)
315315
if p.name == "" {

receiver/receiver.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net"
66
"net/http"
77
"net/url"
8+
"regexp"
89
"strings"
910

1011
"github.com/lomik/carbon-clickhouse/helper/RowBinary"
@@ -90,6 +91,16 @@ func ConcatChar(concat string) Option {
9091
}
9192
}
9293

94+
// ConcatChar creates option for New constructor
95+
func ValidationRegex(regex *regexp.Regexp) Option {
96+
return func(r interface{}) error {
97+
if t, ok := r.(*Base); ok {
98+
t.validationRegex = regex
99+
}
100+
return nil
101+
}
102+
}
103+
93104
// New creates udp, tcp, pickle receiver
94105
func New(dsn string, config tags.TagConfig, opts ...Option) (Receiver, error) {
95106
u, err := url.Parse(dsn)

0 commit comments

Comments
 (0)