-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (119 loc) · 2.88 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
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
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"time"
"github.com/patdowney/stopgap/metrics"
)
type config struct {
DryRun bool
Prefix string
ListItemKey string
MetricTime time.Time
Graphite metrics.GraphiteConfig
}
func graphiteConfig(cfg *metrics.GraphiteConfig) {
flag.StringVar(&cfg.Host, "graphite-host", "localhost", "graphite host")
flag.IntVar(&cfg.Port, "graphite-port", 2003, "graphite port")
}
type timeArg struct {
time.Time
}
func (t *timeArg) Set(value string) error {
var err error
t.Time, err = time.Parse(time.RFC3339, value)
return err
}
func (t *timeArg) String() string {
return fmt.Sprint(t.Time.Unix()) //Format(time.RFC3339)
}
func fetchConfig() *config {
c := &config{}
flag.BoolVar(&c.DryRun, "dry-run", false, "dry run")
flag.StringVar(&c.Prefix, "prefix", "", "metric prefix")
flag.StringVar(&c.ListItemKey, "list-item-key", "", "use item key instead of item index")
var t timeArg
t.Time = time.Now()
flag.Var(&t, "metric-timestamp", "metric time stamp (defaults to now)")
graphiteConfig(&c.Graphite)
flag.Parse()
c.MetricTime = t.Time
return c
}
func openHTTPReader(readerURL string) (io.ReadCloser, error) {
res, err := http.Get(readerURL)
if err != nil {
return nil, err
}
if res.StatusCode == http.StatusOK {
return res.Body, nil
}
return nil, fmt.Errorf("non-200 response from %v(%v)", readerURL, res.StatusCode)
}
func openURLReader(readerURL string) (io.ReadCloser, error) {
u, err := url.Parse(readerURL)
if err != nil {
return nil, err
}
switch u.Scheme {
case "":
return os.Open(u.String())
case "http", "https":
return openHTTPReader(u.String())
default:
return nil, fmt.Errorf("unsupported schema: %v", u.Scheme)
}
}
func openReader(readerURI string) (io.ReadCloser, error) {
if readerURI == "-" {
return os.Stdin, nil
}
return openURLReader(readerURI)
}
func openReaders(readerURIs []string) ([]io.ReadCloser, error) {
readers := make([]io.ReadCloser, 0, 1)
if len(readerURIs) == 0 {
readers = append(readers, os.Stdin)
} else {
for _, a := range readerURIs {
r, err := openReader(a)
if err != nil {
return nil, err
}
readers = append(readers, r)
}
}
return readers, nil
}
func closeReaders(readers []io.ReadCloser) {
for _, r := range readers {
r.Close()
}
}
func main() {
cfg := fetchConfig()
m := make([]metrics.Metric, 0, 1)
readers, err := openReaders(flag.Args())
defer closeReaders(readers)
for _, reader := range readers {
metricDecoder := metrics.NewDecoder(reader)
metricDecoder.MetricTime = cfg.MetricTime
metricDecoder.ListItemKey = cfg.ListItemKey
metricDecoder.KeyPrefix = metrics.GraphiteKey{cfg.Prefix}
err = metricDecoder.Decode(&m)
if err != nil {
log.Print(err.Error())
}
}
publisher, err := metrics.NewGraphitePublisher(
cfg.Graphite, cfg.DryRun)
if err != nil {
log.Fatal(err)
}
publisher.Publish(m)
}