-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (57 loc) · 1.25 KB
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"github.com/DataDog/datadog-go/statsd"
"io/ioutil"
"os"
"regexp"
yaml "gopkg.in/yaml.v2"
)
type Config struct {
Patterns []*Pattern `yaml:"patterns"`
}
type Pattern struct {
Pattern string `yaml:"pattern"`
Regex *regexp.Regexp
Metric string `yaml:"metric"`
Tags []string `yaml:"tags"`
}
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
// parse flags
config_path := flag.String("config_path", "logs_to_datadog_metrics.yaml", "path to config_path file")
flag.Parse()
// read config_path
var config Config
content, err := ioutil.ReadFile(*config_path)
check(err)
err = yaml.Unmarshal(content, &config)
check(err)
for _, c := range config.Patterns {
c.Regex = regexp.MustCompile(c.Pattern)
}
// connect to statsd
statsd, err := statsd.New(os.Getenv("STATSD_HOST") + ":" + os.Getenv("STATSD_PORT"))
check(err)
defer func() {
statsd.Flush() // https://github.com/DataDog/datadog-go/issues/138
statsd.Close()
}()
// read stdin and send metrics
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Bytes()
fmt.Println(string(line))
for _, c := range config.Patterns {
if c.Regex.Match(line) {
statsd.Incr(c.Metric, c.Tags, 1)
}
}
}
}