Skip to content

Commit

Permalink
Debugging: Take out metrics filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Apr 7, 2022
1 parent 4885c78 commit d2b4a0a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 348 deletions.
20 changes: 10 additions & 10 deletions cmd/metal-api/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var (
machineLiveliness = prometheus.NewGaugeVec(
machineLiveliness = prometheus.NewGaugeVec( //nolint
prometheus.GaugeOpts{
Namespace: "metal",
Subsystem: "machine",
Expand Down Expand Up @@ -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.
Expand All @@ -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()
Expand Down
89 changes: 5 additions & 84 deletions cmd/metal-api/internal/service/image-service.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package service

import (
"context"
"errors"
"fmt"
"net/http"
Expand All @@ -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"
Expand All @@ -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()
}

Expand Down Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion cmd/metal-api/internal/service/machine-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
154 changes: 5 additions & 149 deletions cmd/metal-api/internal/service/network-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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()
}

Expand Down Expand Up @@ -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
}
}
Loading

0 comments on commit d2b4a0a

Please sign in to comment.