This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
main.go
118 lines (104 loc) · 3.71 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
package main
import (
"flag"
"fmt"
"net/http"
"net/url"
"os"
"runtime"
"strings"
"time"
"github.com/grafana/globalconf"
"github.com/grafana/metrictank/logger"
"github.com/grafana/metrictank/stats"
log "github.com/sirupsen/logrus"
)
var (
version = "(none)"
showVersion = flag.Bool("version", false, "print version string")
addr = flag.String("addr", ":6059", "http service address")
confFile = flag.String("config", "/etc/metrictank/mt-gateway.ini", "configuration file path")
metrictankUrl = flag.String("metrictank-url", "http://localhost:6060", "metrictank address")
graphiteURL = flag.String("graphite-url", "http://localhost:8080", "graphite-api address")
importerURL = flag.String("importer-url", "", "mt-whisper-importer-writer address")
defaultOrgId = flag.Int("default-org-id", -1, "default org ID to send to downstream services if none is provided")
brokers = flag.String("kafka-tcp-addr", "localhost:9092", "kafka tcp address(es) for metrics, in csv host[:port] format")
logLevel = flag.String("log-level", "info", "log level. panic|fatal|error|warning|info|debug")
// stats
statsEnabled = flag.Bool("stats-enabled", false, "enable sending graphite messages for instrumentation")
statsPrefix = flag.String("stats-prefix", "mt-gateway.stats.default.$hostname", "stats prefix (will add trailing dot automatically if needed)")
statsAddr = flag.String("stats-addr", "localhost:2003", "graphite address")
statsInterval = flag.Int("stats-interval", 10, "interval in seconds to send statistics")
statsBufferSize = flag.Int("stats-buffer-size", 20000, "how many messages (holding all measurements from one interval) to buffer up in case graphite endpoint is unavailable.")
statsTimeout = flag.Duration("stats-timeout", time.Second*10, "timeout after which a write is considered not successful")
)
type Urls struct {
metrictank *url.URL
graphite *url.URL
bulkImporter *url.URL
kafkaBrokers string
}
func init() {
formatter := &logger.TextFormatter{}
formatter.TimestampFormat = "2006-01-02 15:04:05.000"
log.SetFormatter(formatter)
}
func main() {
flag.Usage = func() {
fmt.Println("mt-gateway")
fmt.Println("Provides an HTTP gateway for interacting with metrictank, including metrics ingestion")
fmt.Println()
fmt.Println("Usage:")
fmt.Println()
fmt.Println(" mt-gateway [flags]")
fmt.Println()
fmt.Println("Flags:")
flag.PrintDefaults()
}
// Only try and parse the conf file if it exists
path := ""
if _, err := os.Stat(*confFile); err == nil {
path = *confFile
}
conf, err := globalconf.NewWithOptions(&globalconf.Options{
Filename: path,
EnvPrefix: "MT_",
})
if err != nil {
log.Fatalf("error with configuration file: %s", err)
}
conf.ParseAll()
lvl, err := log.ParseLevel(*logLevel)
if err != nil {
log.Fatalf("failed to parse log-level, %s", err.Error())
}
log.SetLevel(lvl)
if *showVersion {
fmt.Printf("mt-gateway (version: %s - runtime: %s)\n", version, runtime.Version())
return
}
if *statsEnabled {
stats.NewMemoryReporter()
hostname, _ := os.Hostname()
prefix := strings.Replace(*statsPrefix, "$hostname", strings.Replace(hostname, ".", "_", -1), -1)
stats.NewGraphite(prefix, *statsAddr, *statsInterval, *statsBufferSize, *statsTimeout)
} else {
stats.NewDevnull()
}
urls := Urls{}
urls.kafkaBrokers = *brokers
urls.metrictank, err = url.Parse(*metrictankUrl)
if err != nil {
log.Fatal(err)
}
urls.graphite, err = url.Parse(*graphiteURL)
if err != nil {
log.Fatal(err)
}
urls.bulkImporter, err = url.Parse(*importerURL)
if err != nil {
log.Fatal(err)
}
log.WithField("addr", *addr).Info("starting server")
log.WithError(http.ListenAndServe(*addr, NewApi(urls).Mux())).Fatal("terminating")
}