-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
98 lines (86 loc) · 3.61 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
// Copyright 2024 The Perses Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"time"
"github.com/perses/common/app"
"github.com/perses/metrics-usage/config"
"github.com/perses/metrics-usage/database"
"github.com/perses/metrics-usage/source/grafana"
"github.com/perses/metrics-usage/source/labels"
"github.com/perses/metrics-usage/source/metric"
"github.com/perses/metrics-usage/source/perses"
"github.com/perses/metrics-usage/source/rules"
"github.com/sirupsen/logrus"
)
func main() {
configFile := flag.String("config", "", "Path to the YAML configuration file for the API. Configuration settings can be overridden when using environment variables.")
pprof := flag.Bool("pprof", false, "Enable pprof")
flag.Parse()
// load the config from file or/and from environment
conf, err := config.Resolve(*configFile)
if err != nil {
logrus.WithError(err).Fatalf("error reading configuration from file %q or from environment", *configFile)
}
db := database.New(conf.Database)
runner := app.NewRunner().WithDefaultHTTPServer("metrics_usage")
if conf.MetricCollector.Enable {
metricCollectorConfig := conf.MetricCollector
metricCollector, collectorErr := metric.NewCollector(db, metricCollectorConfig)
if collectorErr != nil {
logrus.WithError(collectorErr).Fatal("unable to create the metric collector")
}
runner.WithTimerTasks(time.Duration(metricCollectorConfig.Period), metricCollector)
}
for i, rulesCollectorConfig := range conf.RulesCollectors {
if rulesCollectorConfig.Enable {
rulesCollector, collectorErr := rules.NewCollector(db, rulesCollectorConfig)
if collectorErr != nil {
logrus.WithError(collectorErr).Fatalf("unable to create the rules collector number %d", i)
}
runner.WithTimerTasks(time.Duration(rulesCollectorConfig.Period), rulesCollector)
}
}
for i, labelsCollectorConfig := range conf.LabelsCollectors {
if labelsCollectorConfig.Enable {
labelsCollector, collectorErr := labels.NewCollector(db, labelsCollectorConfig)
if collectorErr != nil {
logrus.WithError(collectorErr).Fatalf("unable to create the labels collector number %d", i)
}
runner.WithTimerTasks(time.Duration(labelsCollectorConfig.Period), labelsCollector)
}
}
if conf.PersesCollector.Enable {
persesCollectorConfig := conf.PersesCollector
persesCollector, collectorErr := perses.NewCollector(db, persesCollectorConfig)
if collectorErr != nil {
logrus.WithError(collectorErr).Fatal("unable to create the perses collector")
}
runner.WithTimerTasks(time.Duration(persesCollectorConfig.Period), persesCollector)
}
if conf.GrafanaCollector.Enable {
grafanaCollectorConfig := conf.GrafanaCollector
grafanaCollector, collectorErr := grafana.NewCollector(db, grafanaCollectorConfig)
if collectorErr != nil {
logrus.WithError(collectorErr).Fatal("unable to create the grafana collector")
}
runner.WithTimerTasks(time.Duration(grafanaCollectorConfig.Period), grafanaCollector)
}
runner.HTTPServerBuilder().
ActivatePprof(*pprof).
APIRegistration(metric.NewAPI(db)).
APIRegistration(rules.NewAPI(db)).
APIRegistration(labels.NewAPI(db))
runner.Start()
}