forked from cortexproject/cortex
/
main.go
100 lines (86 loc) · 3.18 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
package main
import (
"flag"
"net/http"
"os"
"github.com/go-kit/kit/log/level"
"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"google.golang.org/grpc"
"github.com/weaveworks/common/middleware"
"github.com/weaveworks/common/server"
"github.com/weaveworks/common/tracing"
"github.com/weaveworks/cortex/pkg/distributor"
"github.com/weaveworks/cortex/pkg/ring"
"github.com/weaveworks/cortex/pkg/util"
"github.com/weaveworks/promrus"
)
func main() {
// The pattern for main functions is a series of config objects, which are
// registered for command line flags, and then a series of components that
// are instantiated and composed. Some rules of thumb:
// - Config types should only contain 'simple' types (ints, strings, urls etc).
// - Flag validation should be done by the flag; use a flag.Value where
// appropriate.
// - Config types should map 1:1 with a component type.
// - Config types should define flags with a common prefix.
// - It's fine to nest configs within configs, but this should match the
// nesting of components within components.
// - Limit as much is possible sharing of configuration between config types.
// Where necessary, use a pointer for this - avoid repetition.
// - Where a nesting of components its not obvious, it's fine to pass
// references to other components constructors to compose them.
// - First argument for a components constructor should be its matching config
// object.
var (
serverConfig = server.Config{
MetricsNamespace: "cortex",
GRPCMiddleware: []grpc.UnaryServerInterceptor{
middleware.ServerUserHeaderInterceptor,
},
}
ringConfig ring.Config
distributorConfig distributor.Config
logLevel util.LogLevel
)
util.RegisterFlags(&serverConfig, &ringConfig, &distributorConfig, &logLevel)
flag.Parse()
util.InitLogger(logLevel.AllowedLevel)
// Setting the environment variable JAEGER_AGENT_HOST enables tracing
jaegerAgentHost := os.Getenv("JAEGER_AGENT_HOST")
trace := tracing.New(jaegerAgentHost, "distributor")
defer trace.Close()
log.AddHook(promrus.MustNewPrometheusHook())
r, err := ring.New(ringConfig)
if err != nil {
level.Error(util.Logger).Log("msg", "error initializing ring", "err", err)
os.Exit(1)
}
defer r.Stop()
dist, err := distributor.New(distributorConfig, r)
if err != nil {
level.Error(util.Logger).Log("msg", "error initializing distributor", "err", err)
os.Exit(1)
}
defer dist.Stop()
prometheus.MustRegister(dist)
server, err := server.New(serverConfig)
if err != nil {
level.Error(util.Logger).Log("msg", "error initializing server", "err", err)
os.Exit(1)
}
defer server.Shutdown()
server.HTTP.Handle("/ring", r)
operationNameFunc := nethttp.OperationNameFunc(func(r *http.Request) string {
return r.URL.RequestURI()
})
server.HTTP.Handle("/api/prom/push", middleware.Merge(
middleware.Func(func(handler http.Handler) http.Handler {
return nethttp.Middleware(opentracing.GlobalTracer(), handler, operationNameFunc)
}),
middleware.AuthenticateUser,
).Wrap(http.HandlerFunc(dist.PushHandler)))
server.Run()
}