From dbf1b6acdfaaf6361326c6915a14d0a878499ad8 Mon Sep 17 00:00:00 2001 From: Bruno Ledesma Date: Mon, 22 Jul 2024 10:38:47 -0300 Subject: [PATCH] Infra prometheus collector Signed-off-by: Bruno Ledesma --- chaoscenter/graphql/server/go.mod | 7 +++ chaoscenter/graphql/server/go.sum | 9 ++++ .../graph/chaos_infrastructure_collector.go | 47 +++++++++++++++++++ chaoscenter/graphql/server/graph/resolver.go | 10 ++++ .../model/mocks/service.go | 5 ++ .../pkg/chaos_infrastructure/service.go | 6 +++ .../chaos_infrastructure/operations.go | 21 +++++++++ .../graphql/server/pkg/gitops/gitops.go | 8 ---- chaoscenter/graphql/server/server.go | 2 + 9 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 chaoscenter/graphql/server/graph/chaos_infrastructure_collector.go diff --git a/chaoscenter/graphql/server/go.mod b/chaoscenter/graphql/server/go.mod index 47ecd58ce44..8adcab61d1b 100644 --- a/chaoscenter/graphql/server/go.mod +++ b/chaoscenter/graphql/server/go.mod @@ -18,6 +18,7 @@ require ( github.com/litmuschaos/chaos-scheduler v0.0.0-20220714173615-d7513d616a71 github.com/mrz1836/go-sanitize v1.3.2 github.com/pkg/errors v0.9.1 + github.com/prometheus/client_golang v1.12.1 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.9.0 github.com/tidwall/gjson v1.17.1 @@ -41,8 +42,10 @@ require ( github.com/PuerkitoBio/purell v1.1.1 // indirect github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect github.com/agnivade/levenshtein v1.1.1 // indirect + github.com/beorn7/perks v1.0.1 // indirect github.com/bytedance/sonic v1.11.6 // indirect github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/cloudflare/circl v1.3.7 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect @@ -79,6 +82,7 @@ require ( github.com/leodido/go-urn v1.4.0 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect @@ -86,6 +90,9 @@ require ( github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/pjbgf/sha1cd v0.3.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/prometheus/client_model v0.2.0 // indirect + github.com/prometheus/common v0.32.1 // indirect + github.com/prometheus/procfs v0.7.3 // indirect github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect github.com/skeema/knownhosts v1.2.2 // indirect github.com/sosodev/duration v1.3.1 // indirect diff --git a/chaoscenter/graphql/server/go.sum b/chaoscenter/graphql/server/go.sum index bd159a79a2b..ac56d5fdade 100644 --- a/chaoscenter/graphql/server/go.sum +++ b/chaoscenter/graphql/server/go.sum @@ -161,6 +161,7 @@ github.com/bazelbuild/rules_go v0.0.0-20190719190356-6dae44dc5cab/go.mod h1:MC23 github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bifurcation/mint v0.0.0-20180715133206-93c51c6ce115/go.mod h1:zVt7zX3K/aDCk9Tj+VM7YymsX66ERvzCJzw8rFCX2JU= @@ -186,10 +187,13 @@ github.com/campoy/embedmd v1.0.0/go.mod h1:oxyr9RCiSXg0M3VJ3ks0UGfp98BpSSGr0kpiX github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/prettybench v0.0.0-20150116022406-03b8cfe5406c/go.mod h1:Xe6ZsFhtM8HrDku0pxJ3/Lr51rwykrzgFwpmTzleatY= +github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.0/go.mod h1:dgIUBU3pDso/gPgZ1osOZ0iQf77oPR28Tjxl5dIMyVM= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chai2010/gettext-go v0.0.0-20160711120539-c6fed771bfd5/go.mod h1:/iP1qXHoty45bqomnu2LM+VVyAEdWN+vtSHGlQgyxbw= github.com/checkpoint-restore/go-criu v0.0.0-20190109184317-bdb7599cd87b/go.mod h1:TrMrLQfeENAPYPRsJuq3jsqdlRh3lvi6trTZJG8+tho= github.com/cheekybits/genny v0.0.0-20170328200008-9127e812e1e9/go.mod h1:+tQajlRqAUrPI7DOSpB0XAqZYtQakVtB7wXkRAgjxjQ= @@ -831,6 +835,7 @@ github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOq github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI= github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/maxbrunsfeld/counterfeiter/v6 v6.2.1/go.mod h1:F9YacGpnZbLQMzuPI0rR6op21YvNu/RjL705LJJpM3k= github.com/maxbrunsfeld/counterfeiter/v6 v6.2.2/go.mod h1:eD9eIE7cdwcMi9rYluz88Jz2VyhSmden33/aXg4oVIY= @@ -984,11 +989,13 @@ github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQ github.com/prometheus/client_golang v1.2.1/go.mod h1:XMU6Z2MjaRKVu/dC1qupJI9SiNkDYzz3xecMgSW/F+U= github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M= github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0= +github.com/prometheus/client_golang v1.12.1 h1:ZiaPsmm9uiBeaSMRznKsCDNtPCS0T3JVDGF+06gjBzk= github.com/prometheus/client_golang v1.12.1/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M= github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= @@ -999,6 +1006,7 @@ github.com/prometheus/common v0.6.0/go.mod h1:eBmuwkDJBwy6iBfxCBob6t6dR6ENT/y+J+ github.com/prometheus/common v0.7.0/go.mod h1:DjGbpBbp5NYNiECxcL/VnbXCCaQpKd3tt26CguLLsqA= github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo= github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc= +github.com/prometheus/common v0.32.1 h1:hWIdL3N2HoUx3B8j3YN9mWor0qhY/NlEKZEaXxuIRh4= github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= @@ -1011,6 +1019,7 @@ github.com/prometheus/procfs v0.0.5/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDa github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= +github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU= github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/prometheus v2.3.2+incompatible/go.mod h1:oAIUtOny2rjMX0OWN5vPR5/q/twIROJvdqnQKDdil/s= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= diff --git a/chaoscenter/graphql/server/graph/chaos_infrastructure_collector.go b/chaoscenter/graphql/server/graph/chaos_infrastructure_collector.go new file mode 100644 index 00000000000..e3fec7e7c2f --- /dev/null +++ b/chaoscenter/graphql/server/graph/chaos_infrastructure_collector.go @@ -0,0 +1,47 @@ +package graph + +import ( + "github.com/prometheus/client_golang/prometheus" +) + +// Define a struct for you collector that contains pointers +// to prometheus descriptors for each metric you wish to expose. +// Note you can also include fields of other types if they provide utility +// but we just won't be exposing them as metrics. + +type Infra struct { + Name string + InfraNamespace string + isActive bool + PlatformName string +} + +type infraCollector struct { + infraMetric *prometheus.Desc + AllInfras func() []Infra +} + +type InfraProvider func() []Infra + +// You must create a constructor for you collector that +// initializes every desinfrasProvidercriptor and returns a pointer to the collector +func newInfraCollector(fn InfraProvider) *infraCollector { + return &infraCollector{ + infraMetric: prometheus.NewDesc("infra_metric", "Shows chaos infra status and info", []string{"name", "namespace"}, nil), + AllInfras: fn, + } +} + +func (collector *infraCollector) Describe(ch chan<- *prometheus.Desc) { + ch <- collector.infraMetric +} + +func (collector *infraCollector) Collect(ch chan<- prometheus.Metric) { + for _, infra := range collector.AllInfras() { + var metricValue float64 = 0 + if infra.isActive { + metricValue = 1 + } + ch <- prometheus.MustNewConstMetric(collector.infraMetric, prometheus.GaugeValue, metricValue, infra.Name, infra.InfraNamespace) + } +} diff --git a/chaoscenter/graphql/server/graph/resolver.go b/chaoscenter/graphql/server/graph/resolver.go index e08b658a214..2532834042e 100644 --- a/chaoscenter/graphql/server/graph/resolver.go +++ b/chaoscenter/graphql/server/graph/resolver.go @@ -3,7 +3,9 @@ package graph import ( "context" + "github.com/jinzhu/copier" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/authConfig" + "github.com/prometheus/client_golang/prometheus" chaos_experiment2 "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment/ops" @@ -84,6 +86,14 @@ func NewConfig(mongodbOperator mongodb.MongoOperator) generated.Config { probeService: probeService, }} + infra := newInfraCollector(func() []Infra { + result, _ := chaosInfrastructureService.ListAll() + var infras = []Infra{} + copier.Copy(&infras, &result) + return infras + }) + prometheus.MustRegister(infra) + config.Directives.Authorized = func(ctx context.Context, obj interface{}, next graphql.Resolver) (interface{}, error) { token := ctx.Value(authorization.AuthKey).(string) salt, err := authConfig.NewAuthConfigOperator(mongodb.Operator).GetAuthConfig(context.Background()) diff --git a/chaoscenter/graphql/server/pkg/chaos_infrastructure/model/mocks/service.go b/chaoscenter/graphql/server/pkg/chaos_infrastructure/model/mocks/service.go index c20b5242771..2e7d68c299d 100644 --- a/chaoscenter/graphql/server/pkg/chaos_infrastructure/model/mocks/service.go +++ b/chaoscenter/graphql/server/pkg/chaos_infrastructure/model/mocks/service.go @@ -39,6 +39,11 @@ func (s *InfraService) ListInfras(projectID string, request *model.ListInfraRequ return args.Get(0).(*model.ListInfraResponse), args.Error(1) } +func (s *InfraService) ListAll() ([]*dbChaosInfra.ChaosInfra, error) { + args := s.Called() + return args.Get(0).([]*dbChaosInfra.ChaosInfra), args.Error(1) +} + func (s *InfraService) GetInfraDetails(ctx context.Context, infraID string, projectID string) (*model.Infra, error) { args := s.Called(ctx, infraID, projectID) return args.Get(0).(*model.Infra), args.Error(1) diff --git a/chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go b/chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go index 94d093e7693..7e8b08304c6 100644 --- a/chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go +++ b/chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go @@ -43,6 +43,7 @@ type Service interface { DeleteInfra(ctx context.Context, projectID string, infraId string, r store.StateData) (string, error) ListInfras(projectID string, request *model.ListInfraRequest) (*model.ListInfraResponse, error) GetInfraDetails(ctx context.Context, infraID string, projectID string) (*model.Infra, error) + ListAll() ([]*dbChaosInfra.ChaosInfra, error) SendInfraEvent(eventType, eventName, description string, infra model.Infra, r store.StateData) GetManifestWithInfraID(host string, infraID string, accessKey string) ([]byte, error) GetInfra(ctx context.Context, projectID string, infraID string) (*model.Infra, error) @@ -60,6 +61,11 @@ type infraService struct { envOperator *dbEnvironments.Operator } +// List all infrastructures +func (in *infraService) ListAll() ([]*dbChaosInfra.ChaosInfra, error) { + return in.infraOperator.GetAll(); +} + // NewChaosInfrastructureService returns a new instance of Service func NewChaosInfrastructureService(infraOperator *dbChaosInfra.Operator, envOperator *dbEnvironments.Operator) Service { return &infraService{ diff --git a/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure/operations.go b/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure/operations.go index 19fce69b492..0a7114d437c 100644 --- a/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure/operations.go +++ b/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure/operations.go @@ -53,6 +53,27 @@ func (c *Operator) GetInfra(infraID string) (ChaosInfra, error) { return infra, nil } +// Get All non removed infras +func (c *Operator) GetAll() ([]*ChaosInfra, error) { + ctx, cancel := context.WithTimeout(backgroundContext, 10*time.Second) + defer cancel() + + query := bson.D{{"is_removed", false}} + + var infras []*ChaosInfra + results, err := c.operator.List(ctx, mongodb.ChaosInfraCollection, query) + if err != nil { + return []*ChaosInfra{}, err + } + + err = results.All(ctx, &infras) + if err != nil { + return []*ChaosInfra{}, err + } + + return infras, nil +} + // GetInfraDetails takes a infraName and projectID to retrieve the chaos_infra details from the database func (c *Operator) GetInfraDetails(ctx context.Context, infraID string, projectID string) (ChaosInfra, error) { query := bson.D{ diff --git a/chaoscenter/graphql/server/pkg/gitops/gitops.go b/chaoscenter/graphql/server/pkg/gitops/gitops.go index c7b2afda308..b629e600d79 100644 --- a/chaoscenter/graphql/server/pkg/gitops/gitops.go +++ b/chaoscenter/graphql/server/pkg/gitops/gitops.go @@ -14,7 +14,6 @@ import ( "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" - "github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" "github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/go-git/go-git/v5/plumbing/transport/ssh" @@ -173,13 +172,6 @@ func (c GitConfig) GitClone() (*git.Repository, error) { // getAuthMethod returns the AuthMethod instance required for the current repo access [read/writes] func (c GitConfig) getAuthMethod() (transport.AuthMethod, error) { - // Azure DevOps requires the 'multi_ack' and 'multi_ack_detailed' capabilities, - // which are not fully implemented in the go-git package. By default, these - // capabilities are included in 'transport.UnsupportedCapabilities'. - transport.UnsupportedCapabilities = []capability.Capability{ - capability.ThinPack, - } - switch c.AuthType { case model.AuthTypeToken: diff --git a/chaoscenter/graphql/server/server.go b/chaoscenter/graphql/server/server.go index bf6a3df67f9..e53ad36fdae 100644 --- a/chaoscenter/graphql/server/server.go +++ b/chaoscenter/graphql/server/server.go @@ -13,6 +13,7 @@ import ( "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb" dbSchemaChaosHub "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/projects" + "github.com/prometheus/client_golang/prometheus/promhttp" "context" "fmt" @@ -81,6 +82,7 @@ func validateVersion() error { func setupGin() *gin.Engine { gin.SetMode(gin.ReleaseMode) router := gin.New() + router.GET("/metrics", gin.WrapH(promhttp.Handler())) router.Use(middleware.DefaultStructuredLogger()) router.Use(gin.Recovery()) router.Use(middleware.ValidateCors())