From 1a0350adf188068ab11afc738ba5b1e5b4945401 Mon Sep 17 00:00:00 2001 From: Frank Kloeker Date: Mon, 27 Mar 2023 15:45:55 +0200 Subject: [PATCH] WIP: init prometheus --- api/api.go | 2 ++ main.go | 16 ++++++++++++++++ mesh/mesh.go | 2 ++ 3 files changed, 20 insertions(+) diff --git a/api/api.go b/api/api.go index 9c0aab5..1397383 100644 --- a/api/api.go +++ b/api/api.go @@ -39,6 +39,7 @@ import ( connect "github.com/bufbuild/connect-go" grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap" "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" + "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/telekom/canary-bot/data" h "github.com/telekom/canary-bot/helper" "github.com/telekom/canary-bot/proto/api/third_party" @@ -133,6 +134,7 @@ func StartApi(data data.Database, config *Configuration, log *zap.SugaredLogger) mux.Handle(apiv1connect.NewApiServiceHandler(a, interceptors)) mux.Handle("/api/v1/", gwmux) + mux.Handle("/metrics", promhttp.Handler()) server := &http.Server{ Addr: addr, Handler: h2c.NewHandler(mux, &http2.Server{}), diff --git a/main.go b/main.go index c5a080c..b9d5edb 100644 --- a/main.go +++ b/main.go @@ -33,12 +33,28 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) const ( envPrefix = "MESH" ) +// prometheus metrics for nodes and samples and cluster + +var ( + nodecounts = promauto.NewGauge(prometheus.GaugeOpts{ + Name: "mesh_nodes", + Help: "The total number of nodes", + }) + samplecounts = promauto.NewGauge(prometheus.GaugeOpts{ + Name: "mesh_samples", + Help: "The total number of samples", + }) +) + var cmd = &cobra.Command{ Use: "cbot", Short: "Canary Bot collecting & distributing sample data in a mesh", diff --git a/mesh/mesh.go b/mesh/mesh.go index b6392e6..c781855 100644 --- a/mesh/mesh.go +++ b/mesh/mesh.go @@ -145,6 +145,8 @@ func CreateCanaryMesh(routineConfig *RoutineConfiguration, setupConfig *SetupCon CaCert: setupConfig.CaCert, } + // init prometheus metrics to fetch data from database + // start the mesh API if err = api.StartApi(database, apiConfig, logger.Named("api")); err != nil { logger.Fatal("Could not start API - Error: %+v", err)