forked from grafana/metrictank
/
main.go
269 lines (244 loc) · 6.8 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package main
import (
"crypto/tls"
"encoding/base64"
"flag"
"fmt"
"io"
"io/ioutil"
"math"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/grafana/metrictank/conf"
"github.com/grafana/metrictank/logger"
"github.com/grafana/metrictank/mdata/importer"
"github.com/kisielk/whisper-go/whisper"
log "github.com/sirupsen/logrus"
)
var (
httpEndpoint = flag.String(
"http-endpoint",
"http://127.0.0.1:8080/metrics/import",
"The http endpoint to send the data to",
)
namePrefix = flag.String(
"name-prefix",
"",
"Prefix to prepend before every metric name, should include the '.' if necessary",
)
threads = flag.Int(
"threads",
10,
"Number of workers threads to process and convert .wsp files",
)
writeUnfinishedChunks = flag.Bool(
"write-unfinished-chunks",
false,
"Defines if chunks that have not completed their chunk span should be written",
)
insecureSSL = flag.Bool(
"insecure-ssl",
false,
"Disables ssl certificate verification",
)
whisperDirectory = flag.String(
"whisper-directory",
"/opt/graphite/storage/whisper",
"The directory that contains the whisper file structure",
)
httpAuth = flag.String(
"http-auth",
"",
"The credentials used to authenticate in the format \"user:password\"",
)
dstSchemas = flag.String(
"dst-schemas",
"",
"The filename of the output schemas definition file",
)
nameFilterPattern = flag.String(
"name-filter",
"",
"A regex pattern to be applied to all metric names, only matching ones will be imported",
)
importUntil = flag.Uint(
"import-until",
math.MaxUint32,
"Only import up to, but not including, the specified timestamp",
)
importFrom = flag.Uint(
"import-from",
0,
"Only import starting from the specified timestamp",
)
positionFile = flag.String(
"position-file",
"",
"file to store position and load position from",
)
verbose = flag.Bool(
"verbose",
false,
"More detailed logging",
)
schemas conf.Schemas
nameFilter *regexp.Regexp
processedCount uint32
skippedCount uint32
)
func init() {
formatter := &logger.TextFormatter{}
formatter.TimestampFormat = "2006-01-02 15:04:05.000"
log.SetFormatter(formatter)
log.SetLevel(log.InfoLevel)
}
func main() {
var err error
flag.Parse()
if *verbose {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
nameFilter = regexp.MustCompile(*nameFilterPattern)
schemas, err = conf.ReadSchemas(*dstSchemas)
if err != nil {
panic(fmt.Sprintf("Error when parsing schemas file: %q", err))
}
var pos *posTracker
if len(*positionFile) > 0 {
pos, err = NewPositionTracker(*positionFile)
if err != nil {
log.Fatalf("Error instantiating position tracker: %s", err.Error())
}
defer pos.Close()
}
fileChan := make(chan string)
wg := &sync.WaitGroup{}
wg.Add(*threads)
for i := 0; i < *threads; i++ {
go processFromChan(pos, fileChan, wg)
}
getFileListIntoChan(pos, fileChan)
wg.Wait()
}
func processFromChan(pos *posTracker, files chan string, wg *sync.WaitGroup) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: *insecureSSL},
}
client := &http.Client{Transport: tr}
for file := range files {
fd, err := os.Open(file)
if err != nil {
log.Errorf("Failed to open whisper file %q: %q\n", file, err.Error())
continue
}
w, err := whisper.OpenWhisper(fd)
if err != nil {
log.Errorf("Failed to open whisper file %q: %q\n", file, err.Error())
continue
}
name := getMetricName(file)
log.Debugf("Processing file %s (%s)", file, name)
data, err := importer.NewArchiveRequest(w, schemas, file, name, uint32(*importFrom), uint32(*importUntil), *writeUnfinishedChunks)
if err != nil {
log.Errorf("Failed to get metric: %q", err.Error())
continue
}
if log.GetLevel() >= log.DebugLevel {
details := fmt.Sprintf("Name: %s\nId: %s\nLastUpdate: %d\n", data.MetricData.Name, data.MetricData.Id, data.MetricData.Time)
for _, cwr := range data.ChunkWriteRequests {
details += fmt.Sprintf("Chunk write request T0: %d TTL: %d Size: %d\n", cwr.T0, cwr.TTL, len(cwr.Data))
}
log.Debugf("Sending archive request:\n%s", details)
}
success := false
attempts := 0
for !success {
b, err := data.MarshalCompressed()
if err != nil {
log.Errorf("Failed to encode metric: %q", err.Error())
continue
}
size := b.Len()
req, err := http.NewRequest("POST", *httpEndpoint, io.Reader(b))
if err != nil {
log.Fatalf("Cannot construct request to http endpoint %q: %q", *httpEndpoint, err.Error())
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Encoding", "gzip")
if len(*httpAuth) > 0 {
req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(*httpAuth)))
}
pre := time.Now()
resp, err := client.Do(req)
passed := time.Now().Sub(pre).Seconds()
if err != nil || resp.StatusCode >= 300 {
if err != nil {
log.Warningf("Error posting %s (%d bytes), to endpoint %q (attempt %d/%fs, retrying): %s", name, size, *httpEndpoint, attempts, passed, err.Error())
attempts++
continue
} else {
log.Warningf("Error posting %s (%d bytes) to endpoint %q status %d (attempt %d/%fs, retrying)", name, size, *httpEndpoint, resp.StatusCode, attempts, passed)
}
attempts++
} else {
log.Debugf("Posted %s (%d bytes) to endpoint %q in %f seconds", name, size, *httpEndpoint, passed)
success = true
}
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
if pos != nil {
pos.Done(file)
}
processed := atomic.AddUint32(&processedCount, 1)
if processed%100 == 0 {
skipped := atomic.LoadUint32(&skippedCount)
log.Infof("Processed %d files, %d skipped", processed, skipped)
}
}
wg.Done()
}
// generate the metric name based on the file name and given prefix
func getMetricName(file string) string {
// remove all leading '/' from file name
file = strings.TrimPrefix(file, *whisperDirectory)
for file[0] == '/' {
file = file[1:]
}
return *namePrefix + strings.Replace(strings.TrimSuffix(file, ".wsp"), "/", ".", -1)
}
// scan a directory and feed the list of whisper files relative to base into the given channel
func getFileListIntoChan(pos *posTracker, fileChan chan string) {
filepath.Walk(
*whisperDirectory,
func(path string, info os.FileInfo, err error) error {
if path == *whisperDirectory {
return nil
}
name := getMetricName(path)
if !nameFilter.Match([]byte(getMetricName(name))) {
log.Debugf("Skipping file %s with name %s", path, name)
atomic.AddUint32(&skippedCount, 1)
return nil
}
if len(path) < 4 || path[len(path)-4:] != ".wsp" {
return nil
}
if pos != nil && pos.IsDone(path) {
log.Debugf("Skipping file %s because it was listed as already done", path)
return nil
}
fileChan <- path
return nil
},
)
close(fileChan)
}