From d2b4a0af6eea01817d762105c2ed8397d0abdcf5 Mon Sep 17 00:00:00 2001 From: Gerrit Date: Thu, 7 Apr 2022 12:59:01 +0200 Subject: [PATCH] Debugging: Take out metrics filter. --- cmd/metal-api/internal/metrics/metrics.go | 20 +-- .../internal/service/image-service.go | 89 +--------- .../internal/service/machine-service.go | 2 +- .../internal/service/network-service.go | 154 +----------------- .../internal/service/partition-service.go | 108 +----------- cmd/metal-api/main.go | 2 +- 6 files changed, 27 insertions(+), 348 deletions(-) diff --git a/cmd/metal-api/internal/metrics/metrics.go b/cmd/metal-api/internal/metrics/metrics.go index a9c485eb8..e28bc5bd5 100644 --- a/cmd/metal-api/internal/metrics/metrics.go +++ b/cmd/metal-api/internal/metrics/metrics.go @@ -9,7 +9,7 @@ import ( ) var ( - machineLiveliness = prometheus.NewGaugeVec( + machineLiveliness = prometheus.NewGaugeVec( //nolint prometheus.GaugeOpts{ Namespace: "metal", Subsystem: "machine", @@ -41,7 +41,7 @@ var ( ) func init() { - prometheus.MustRegister(machineLiveliness, counter, duration) + // prometheus.MustRegister(machineLiveliness, counter, duration) } // PartitionLiveliness is a data container for the liveliness of different partitions. @@ -51,14 +51,14 @@ type PartitionLiveliness map[string]struct { Unknown int } -// ProvideLiveliness provides the given values as gauges so a scraper can collect them. -func ProvideLiveliness(lvness PartitionLiveliness) { - for p, l := range lvness { - machineLiveliness.WithLabelValues(p, "alive").Set(float64(l.Alive)) - machineLiveliness.WithLabelValues(p, "dead").Set(float64(l.Dead)) - machineLiveliness.WithLabelValues(p, "unknown").Set(float64(l.Unknown)) - } -} +// // ProvideLiveliness provides the given values as gauges so a scraper can collect them. +// func ProvideLiveliness(lvness PartitionLiveliness) { +// for p, l := range lvness { +// machineLiveliness.WithLabelValues(p, "alive").Set(float64(l.Alive)) +// machineLiveliness.WithLabelValues(p, "dead").Set(float64(l.Dead)) +// machineLiveliness.WithLabelValues(p, "unknown").Set(float64(l.Unknown)) +// } +// } func RestfulMetrics(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) { n := time.Now() diff --git a/cmd/metal-api/internal/service/image-service.go b/cmd/metal-api/internal/service/image-service.go index 9c53d59a6..d92b80d33 100644 --- a/cmd/metal-api/internal/service/image-service.go +++ b/cmd/metal-api/internal/service/image-service.go @@ -1,7 +1,6 @@ package service import ( - "context" "errors" "fmt" "net/http" @@ -12,7 +11,6 @@ import ( "github.com/metal-stack/metal-api/cmd/metal-api/internal/metal" v1 "github.com/metal-stack/metal-api/cmd/metal-api/internal/service/v1" "github.com/metal-stack/metal-api/cmd/metal-api/internal/utils" - "github.com/prometheus/client_golang/prometheus" "go.uber.org/zap" restfulspec "github.com/emicklei/go-restful-openapi/v2" @@ -32,11 +30,11 @@ func NewImage(ds *datastore.RethinkStore) *restful.WebService { ds: ds, }, } - iuc := imageUsageCollector{ir: &ir} - err := prometheus.Register(iuc) - if err != nil { - zapup.MustRootLogger().Error("Failed to register prometheus", zap.Error(err)) - } + // iuc := imageUsageCollector{ir: &ir} + // err := prometheus.Register(iuc) + // if err != nil { + // zapup.MustRootLogger().Error("Failed to register prometheus", zap.Error(err)) + // } return ir.webService() } @@ -414,80 +412,3 @@ func (ir imageResource) machinesByImage(machines metal.Machines, imageID string) } return machinesByImage } - -// networkUsageCollector implements the prometheus collector interface. -type imageUsageCollector struct { - ir *imageResource -} - -var usedImageDesc = prometheus.NewDesc( - "metal_image_used_total", - "The total number of machines using a image", - []string{"imageID", "name", "os", "classification", "created", "expirationDate", "base", "features"}, nil, -) - -func (iuc imageUsageCollector) Describe(ch chan<- *prometheus.Desc) { - prometheus.DescribeByCollect(iuc, ch) -} - -func (iuc imageUsageCollector) Collect(ch chan<- prometheus.Metric) { - // FIXME bad workaround to be able to run make spec - if iuc.ir == nil || iuc.ir.ds == nil { - return - } - - ctx, cancel := context.WithTimeout(context.Background(), datastore.DefaultQueryTimeout) - defer cancel() - - imgs, err := iuc.ir.ds.ListImages(ctx) - if err != nil { - return - } - images := make(map[string]metal.Image) - for _, i := range imgs { - images[i.ID] = i - } - // init with 0 - usage := make(map[string]int) - for _, i := range imgs { - usage[i.ID] = 0 - } - - ctx, cancel2 := context.WithTimeout(context.Background(), datastore.DefaultQueryTimeout) - defer cancel2() - - // loop over machines and count - machines, err := iuc.ir.ds.ListMachines(ctx) - if err != nil { - return - } - for _, m := range machines { - if m.Allocation == nil { - continue - } - usage[m.Allocation.ImageID]++ - } - - for i, count := range usage { - image := images[i] - - metric, err := prometheus.NewConstMetric( - usedImageDesc, - prometheus.CounterValue, - float64(count), - image.ID, - image.Name, - image.OS, - string(image.Classification), - fmt.Sprintf("%d", image.Created.Unix()), - fmt.Sprintf("%d", image.ExpirationDate.Unix()), - string(image.Base.ID), - image.ImageFeatureString(), - ) - if err != nil { - zapup.MustRootLogger().Error("Failed create metric for UsedImages", zap.Error(err)) - return - } - ch <- metric - } -} diff --git a/cmd/metal-api/internal/service/machine-service.go b/cmd/metal-api/internal/service/machine-service.go index 1140cf8fe..bc3205026 100644 --- a/cmd/metal-api/internal/service/machine-service.go +++ b/cmd/metal-api/internal/service/machine-service.go @@ -2189,7 +2189,7 @@ func MachineLiveliness(ctx context.Context, ds *datastore.RethinkStore, logger * liveliness[m.PartitionID] = p } - metrics.ProvideLiveliness(liveliness) + // metrics.ProvideLiveliness(liveliness) logger.Infow("machine liveliness evaluated", "alive", alive, "dead", dead, "unknown", unknown, "errors", errs) diff --git a/cmd/metal-api/internal/service/network-service.go b/cmd/metal-api/internal/service/network-service.go index 171f6266d..6b6e5ae00 100644 --- a/cmd/metal-api/internal/service/network-service.go +++ b/cmd/metal-api/internal/service/network-service.go @@ -5,8 +5,6 @@ import ( "errors" "fmt" "net/http" - "strconv" - "strings" "github.com/metal-stack/metal-api/cmd/metal-api/internal/utils" "go.uber.org/zap" @@ -22,7 +20,6 @@ import ( v1 "github.com/metal-stack/metal-api/cmd/metal-api/internal/service/v1" "github.com/metal-stack/metal-lib/httperrors" "github.com/metal-stack/metal-lib/zapup" - "github.com/prometheus/client_golang/prometheus" ) type networkResource struct { @@ -40,11 +37,11 @@ func NewNetwork(ds *datastore.RethinkStore, ipamer ipam.IPAMer, mdc mdm.Client) ipamer: ipamer, mdc: mdc, } - nuc := networkUsageCollector{r: &r} - err := prometheus.Register(nuc) - if err != nil { - zapup.MustRootLogger().Error("Failed to register prometheus", zap.Error(err)) - } + // nuc := networkUsageCollector{r: &r} + // err := prometheus.Register(nuc) + // if err != nil { + // zapup.MustRootLogger().Error("Failed to register prometheus", zap.Error(err)) + // } return r.webService() } @@ -784,144 +781,3 @@ func checkAnyIPOfPrefixesInUse(ips []metal.IP, prefixes metal.Prefixes) error { } return nil } - -// networkUsageCollector implements the prometheus collector interface. -type networkUsageCollector struct { - r *networkResource -} - -var ( - usedIpsDesc = prometheus.NewDesc( - "metal_network_ip_used", - "The total number of used IPs of the network", - []string{"networkId", "prefixes", "destPrefixes", "partitionId", "projectId", "parentNetworkID", "vrf", "isPrivateSuper", "useNat", "isUnderlay"}, nil, - ) - availableIpsDesc = prometheus.NewDesc( - "metal_network_ip_available", - "The total number of available IPs of the network", - []string{"networkId", "prefixes", "destPrefixes", "partitionId", "projectId", "parentNetworkID", "vrf", "isPrivateSuper", "useNat", "isUnderlay"}, nil, - ) - usedPrefixesDesc = prometheus.NewDesc( - "metal_network_prefix_used", - "The total number of used prefixes of the network", - []string{"networkId", "prefixes", "destPrefixes", "partitionId", "projectId", "parentNetworkID", "vrf", "isPrivateSuper", "useNat", "isUnderlay"}, nil, - ) - availablePrefixesDesc = prometheus.NewDesc( - "metal_network_prefix_available", - "The total number of available prefixes of the network", - []string{"networkId", "prefixes", "destPrefixes", "partitionId", "projectId", "parentNetworkID", "vrf", "isPrivateSuper", "useNat", "isUnderlay"}, nil, - ) -) - -func (nuc networkUsageCollector) Describe(ch chan<- *prometheus.Desc) { - prometheus.DescribeByCollect(nuc, ch) -} - -func (nuc networkUsageCollector) Collect(ch chan<- prometheus.Metric) { - // FIXME bad workaround to be able to run make spec - if nuc.r == nil || nuc.r.ds == nil { - return - } - - ctx, cancel := context.WithTimeout(context.Background(), datastore.DefaultQueryTimeout) - defer cancel() - - nws, err := nuc.r.ds.ListNetworks(ctx) - if err != nil { - zapup.MustRootLogger().Error("Failed to get network usage", zap.Error(err)) - return - } - - for i := range nws { - usage := getNetworkUsage(&nws[i], nuc.r.ipamer) - - privateSuper := fmt.Sprintf("%t", nws[i].PrivateSuper) - nat := fmt.Sprintf("%t", nws[i].Nat) - underlay := fmt.Sprintf("%t", nws[i].Underlay) - prefixes := strings.Join(nws[i].Prefixes.String(), ",") - destPrefixes := strings.Join(nws[i].DestinationPrefixes.String(), ",") - vrf := strconv.FormatUint(uint64(nws[i].Vrf), 3) - - metric, err := prometheus.NewConstMetric( - usedIpsDesc, - prometheus.CounterValue, - float64(usage.UsedIPs), - nws[i].ID, - prefixes, - destPrefixes, - nws[i].PartitionID, - nws[i].ProjectID, - nws[i].ParentNetworkID, - vrf, - privateSuper, - nat, - underlay, - ) - if err != nil { - zapup.MustRootLogger().Error("Failed create metric for UsedIPs", zap.Error(err)) - return - } - ch <- metric - - metric, err = prometheus.NewConstMetric( - availableIpsDesc, - prometheus.CounterValue, - float64(usage.AvailableIPs), - nws[i].ID, - prefixes, - destPrefixes, - nws[i].PartitionID, - nws[i].ProjectID, - nws[i].ParentNetworkID, - vrf, - privateSuper, - nat, - underlay, - ) - if err != nil { - zapup.MustRootLogger().Error("Failed create metric for AvailableIPs", zap.Error(err)) - return - } - ch <- metric - metric, err = prometheus.NewConstMetric( - usedPrefixesDesc, - prometheus.CounterValue, - float64(usage.UsedPrefixes), - nws[i].ID, - prefixes, - destPrefixes, - nws[i].PartitionID, - nws[i].ProjectID, - nws[i].ParentNetworkID, - vrf, - privateSuper, - nat, - underlay, - ) - if err != nil { - zapup.MustRootLogger().Error("Failed create metric for UsedPrefixes", zap.Error(err)) - return - } - ch <- metric - metric, err = prometheus.NewConstMetric( - availablePrefixesDesc, - prometheus.CounterValue, - float64(usage.AvailablePrefixes), - nws[i].ID, - prefixes, - destPrefixes, - nws[i].PartitionID, - nws[i].ProjectID, - nws[i].ParentNetworkID, - vrf, - privateSuper, - nat, - underlay, - ) - if err != nil { - zapup.MustRootLogger().Error("Failed create metric for AvailablePrefixes", zap.Error(err)) - return - } - ch <- metric - } -} diff --git a/cmd/metal-api/internal/service/partition-service.go b/cmd/metal-api/internal/service/partition-service.go index ae54648a1..a1dcf6585 100644 --- a/cmd/metal-api/internal/service/partition-service.go +++ b/cmd/metal-api/internal/service/partition-service.go @@ -16,7 +16,6 @@ import ( restful "github.com/emicklei/go-restful/v3" "github.com/metal-stack/metal-lib/httperrors" "github.com/metal-stack/metal-lib/zapup" - "github.com/prometheus/client_golang/prometheus" ) // TopicCreator creates a topic for messaging. @@ -37,11 +36,11 @@ func NewPartition(ds *datastore.RethinkStore, tc TopicCreator) *restful.WebServi }, topicCreator: tc, } - pcc := partitionCapacityCollector{r: &r} - err := prometheus.Register(pcc) - if err != nil { - zapup.MustRootLogger().Error("Failed to register prometheus", zap.Error(err)) - } + // pcc := partitionCapacityCollector{r: &r} + // err := prometheus.Register(pcc) + // if err != nil { + // zapup.MustRootLogger().Error("Failed to register prometheus", zap.Error(err)) + // } return r.webService() } @@ -444,100 +443,3 @@ func (r partitionResource) calcPartitionCapacity(ctx context.Context, pcr *v1.Pa return partitionCapacities, err } - -// partitionCapacityCollector implements the Collector interface. -type partitionCapacityCollector struct { - r *partitionResource -} - -var ( - capacityTotalDesc = prometheus.NewDesc( - "metal_partition_capacity_total", - "The total capacity of machines in the partition", - []string{"partition", "size"}, nil, - ) - capacityFreeDesc = prometheus.NewDesc( - "metal_partition_capacity_free", - "The capacity of free machines in the partition", - []string{"partition", "size"}, nil, - ) - capacityAllocatedDesc = prometheus.NewDesc( - "metal_partition_capacity_allocated", - "The capacity of allocated machines in the partition", - []string{"partition", "size"}, nil, - ) - capacityFaultyDesc = prometheus.NewDesc( - "metal_partition_capacity_faulty", - "The capacity of faulty machines in the partition", - []string{"partition", "size"}, nil, - ) -) - -func (pcc partitionCapacityCollector) Describe(ch chan<- *prometheus.Desc) { - prometheus.DescribeByCollect(pcc, ch) -} - -func (pcc partitionCapacityCollector) Collect(ch chan<- prometheus.Metric) { - ctx, cancel := context.WithTimeout(context.Background(), datastore.DefaultQueryTimeout) - defer cancel() - - pcs, err := pcc.r.calcPartitionCapacity(ctx, nil) - if err != nil { - zapup.MustRootLogger().Error("Failed to get partition capacity", zap.Error(err)) - return - } - - for _, pc := range pcs { - for _, sc := range pc.ServerCapacities { - metric, err := prometheus.NewConstMetric( - capacityTotalDesc, - prometheus.CounterValue, - float64(sc.Total), - pc.ID, - sc.Size, - ) - if err != nil { - zapup.MustRootLogger().Error("Failed to create metric for totalCapacity", zap.Error(err)) - return - } - ch <- metric - - metric, err = prometheus.NewConstMetric( - capacityFreeDesc, - prometheus.CounterValue, - float64(sc.Free), - pc.ID, - sc.Size, - ) - if err != nil { - zapup.MustRootLogger().Error("Failed to create metric for freeCapacity", zap.Error(err)) - return - } - ch <- metric - metric, err = prometheus.NewConstMetric( - capacityAllocatedDesc, - prometheus.CounterValue, - float64(sc.Allocated), - pc.ID, - sc.Size, - ) - if err != nil { - zapup.MustRootLogger().Error("Failed to create metric for allocatedCapacity", zap.Error(err)) - return - } - ch <- metric - metric, err = prometheus.NewConstMetric( - capacityFaultyDesc, - prometheus.CounterValue, - float64(sc.Faulty), - pc.ID, - sc.Size, - ) - if err != nil { - zapup.MustRootLogger().Error("Failed to create metric for faultyCapacity", zap.Error(err)) - return - } - ch <- metric - } - } -} diff --git a/cmd/metal-api/main.go b/cmd/metal-api/main.go index 44945f4bc..d8264663d 100644 --- a/cmd/metal-api/main.go +++ b/cmd/metal-api/main.go @@ -14,13 +14,13 @@ import ( "time" v1 "github.com/metal-stack/masterdata-api/api/v1" + "github.com/metal-stack/metal-api/cmd/metal-api/internal/metrics" "github.com/metal-stack/metal-api/cmd/metal-api/internal/service/s3client" "google.golang.org/protobuf/types/known/wrapperspb" "github.com/go-logr/zapr" "github.com/metal-stack/metal-api/cmd/metal-api/internal/grpc" - "github.com/metal-stack/metal-api/cmd/metal-api/internal/metrics" "github.com/metal-stack/metal-lib/rest" "github.com/prometheus/client_golang/prometheus/promhttp"