Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ide-metrics/cmd/run.go
2498 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"net/http"
9
10
"github.com/gitpod-io/gitpod/common-go/log"
11
"github.com/gitpod-io/gitpod/ide-metrics-api/config"
12
"github.com/gitpod-io/gitpod/ide-metrics/pkg/server"
13
"github.com/prometheus/client_golang/prometheus"
14
"github.com/prometheus/client_golang/prometheus/promhttp"
15
"github.com/spf13/cobra"
16
)
17
18
func init() {
19
rootCmd.AddCommand(runCommand)
20
}
21
22
var runCommand = &cobra.Command{
23
Use: "run",
24
Short: "Starts the service",
25
Version: Version,
26
Run: func(cmd *cobra.Command, args []string) {
27
cfg, err := config.Read(rootOpts.CfgFile)
28
if err != nil {
29
log.WithError(err).Fatal("cannot read configuration")
30
}
31
serviceRegistry := prometheus.NewRegistry()
32
metricsRegistry := prometheus.NewRegistry()
33
s := server.NewMetricsServer(cfg, serviceRegistry, metricsRegistry)
34
35
handler := http.NewServeMux()
36
handler.Handle("/metrics", promhttp.HandlerFor(prometheus.Gatherers{metricsRegistry, serviceRegistry}, promhttp.HandlerOpts{}))
37
38
go func() {
39
err := http.ListenAndServe(cfg.Prometheus.Addr, handler)
40
if err != nil {
41
log.WithError(err).Fatal("prometheus metrics server failed")
42
}
43
}()
44
log.WithField("addr", cfg.Prometheus.Addr).Info("started prometheus metrics server")
45
46
if err := s.Start(); err != nil {
47
log.WithError(err).Fatal("cannot start server")
48
}
49
},
50
}
51
52