Skip to content

Commit

Permalink
remove shared metrics (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwennrich authored May 6, 2022
1 parent 8872b9b commit 7f87922
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 356 deletions.
27 changes: 1 addition & 26 deletions cmd/metal-api/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ import (
)

var (
machineLiveliness = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "metal",
Subsystem: "machine",
Name: "liveliness_total",
Help: "The liveliness of the machines which are available in the system",
},
[]string{"partition", "status"})

counter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "metal",
Expand All @@ -41,23 +32,7 @@ var (
)

func init() {
prometheus.MustRegister(machineLiveliness, counter, duration)
}

// PartitionLiveliness is a data container for the liveliness of different partitions.
type PartitionLiveliness map[string]struct {
Alive int
Dead int
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))
}
prometheus.MustRegister(counter, duration)
}

func RestfulMetrics(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
Expand Down
76 changes: 1 addition & 75 deletions cmd/metal-api/internal/service/image-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,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 @@ -31,11 +30,7 @@ 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))
}

return ir.webService()
}

Expand Down Expand Up @@ -413,72 +408,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
}
imgs, err := iuc.ir.ds.ListImages()
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
}
// loop over machines and count
machines, err := iuc.ir.ds.ListMachines()
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
}
}
10 changes: 0 additions & 10 deletions cmd/metal-api/internal/service/machine-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/dustin/go-humanize"
restfulspec "github.com/emicklei/go-restful-openapi/v2"
"github.com/emicklei/go-restful/v3"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/metrics"
"github.com/metal-stack/metal-lib/bus"
)

Expand Down Expand Up @@ -2201,14 +2200,11 @@ func MachineLiveliness(ds *datastore.RethinkStore, logger *zap.SugaredLogger) er
return err
}

liveliness := make(metrics.PartitionLiveliness)

unknown := 0
alive := 0
dead := 0
errs := 0
for _, m := range machines {
p := liveliness[m.PartitionID]
lvlness, err := evaluateMachineLiveliness(ds, m)
if err != nil {
logger.Errorw("cannot update liveliness", "error", err, "machine", m)
Expand All @@ -2218,19 +2214,13 @@ func MachineLiveliness(ds *datastore.RethinkStore, logger *zap.SugaredLogger) er
switch lvlness {
case metal.MachineLivelinessAlive:
alive++
p.Alive++
case metal.MachineLivelinessDead:
dead++
p.Dead++
case metal.MachineLivelinessUnknown:
unknown++
p.Unknown++
}
liveliness[m.PartitionID] = p
}

metrics.ProvideLiveliness(liveliness)

logger.Infow("machine liveliness evaluated", "alive", alive, "dead", dead, "unknown", unknown, "errors", errs)

return nil
Expand Down
146 changes: 1 addition & 145 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,7 @@ 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))
}

return r.webService()
}

Expand Down Expand Up @@ -795,140 +788,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
}
nws, err := nuc.r.ds.ListNetworks()
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), 10)

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 7f87922

Please sign in to comment.