-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
97 lines (82 loc) · 2.1 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
package main
import (
"context"
"fmt"
"os"
"golang.org/x/sync/errgroup"
log "github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
)
func runPoller(ctx context.Context, sourceType string,
source string, minfoChan chan *metricInfo) error {
var err error
switch sourceType {
case "pubsub":
err = runPubSubPoller(ctx, source, minfoChan)
break
case "file":
err = runFilePoller(ctx, source, minfoChan)
break
default:
err = fmt.Errorf("Unknown source type: '%s'", sourceType)
}
if err != nil {
ctx.Done()
}
return err
}
func runTheDaemon(source string, sourceType string, port int) {
pio := promIO{
scrapeSignalChan: make(chan bool),
messageChan: make(chan promMessage),
}
ctx, cancel := context.WithCancel(context.Background())
minfoChan := make(chan *metricInfo)
var g errgroup.Group
go exposePrometheusEndpoint(port, &pio)
g.Go(func() error {
if err := runPoller(ctx, sourceType, source, minfoChan); err != nil {
cancel()
return err
}
return nil
})
g.Go(func() error {
if err := launchAggregator(ctx, minfoChan, &pio); err != nil {
cancel()
return err
}
return nil
})
if err := g.Wait(); err != nil {
log.Errorf("Error: %v", err)
}
}
func run(cmd *cobra.Command, args []string) {
port, perr := cmd.PersistentFlags().GetInt("port")
sourceType, serr := cmd.PersistentFlags().GetString("source")
if perr != nil || serr != nil || port < 0 || sourceType == "" || len(args) != 1 {
cmd.Usage()
os.Exit(-1)
}
verbose, err := cmd.PersistentFlags().GetBool("verbose")
if err == nil && verbose {
log.SetLevel(log.DebugLevel)
}
runTheDaemon(args[0], sourceType, port)
}
func main() {
cmd := &cobra.Command{
Use: "mflowd [-p port] <-s source-type> <source>",
Short: "Metrics Flow Prometheus Proxy",
Run: run,
}
cmd.PersistentFlags().StringP("source", "s", "",
"Type of metric update event messages source."+
" Can be either 'pubsub' or 'file'")
cmd.PersistentFlags().IntP("port", "p", 6221,
"Port to expose for prometheus to scrap the metrics")
cmd.PersistentFlags().BoolP("verbose", "v", false,
"Turn on verbose mode")
cmd.Execute()
}