Skip to content

Commit

Permalink
Refactor metrics
Browse files Browse the repository at this point in the history
Signed-off-by: João Vilaça <jvilaca@redhat.com>
  • Loading branch information
machadovilaca committed Oct 25, 2023
1 parent e7adb42 commit 98d3955
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 330 deletions.
14 changes: 12 additions & 2 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"runtime"

"github.com/machadovilaca/operator-observability/pkg/operatormetrics"
osv1 "github.com/openshift/api/operator/v1"
"github.com/spf13/pflag"
apiruntime "k8s.io/apimachinery/pkg/runtime"
Expand All @@ -15,11 +16,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
controllerruntimemetrics "sigs.k8s.io/controller-runtime/pkg/metrics"

cnaov1 "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/v1"
cnaov1alpha1 "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/v1alpha1"
"github.com/kubevirt/cluster-network-addons-operator/pkg/controller"
"github.com/kubevirt/cluster-network-addons-operator/pkg/monitoring"
"github.com/kubevirt/cluster-network-addons-operator/pkg/monitoring/metrics"
"github.com/kubevirt/cluster-network-addons-operator/pkg/util/k8s"
)

Expand Down Expand Up @@ -65,14 +67,22 @@ func main() {
mgr, err := manager.New(cfg, manager.Options{
Scheme: scheme,
Namespace: namespace,
MetricsBindAddress: monitoring.GetMetricsAddress(),
MetricsBindAddress: controllerruntimemetrics.DefaultBindAddress,
MapperProvider: k8s.NewDynamicRESTMapper,
})
if err != nil {
log.Printf("failed to instantiate new operator manager: %v", err)
os.Exit(1)
}

// Setup Monitoring
operatormetrics.Register = controllerruntimemetrics.Registry.Register
err = metrics.SetupMetrics()
if err != nil {
log.Printf("failed to setup metrics: %v", err)
os.Exit(1)
}

log.Print("registering Components")

if err := osv1.Install(mgr.GetScheme()); err != nil {
Expand Down
17 changes: 11 additions & 6 deletions docs/metrics.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
# Cluster Network Addons Operator metrics
> This file is auto generated by metricsdocs, run 'make generate-doc' in order to update it.
# Cluster Network Addons Operator Metrics

This document aims to help users that are not familiar with metrics exposed by the Cluster Network Addons Operator.
All metrics documented here are auto-generated by the utility tool 'tools/metricsdocs' and reflects exactly what is being exposed.
## Cluster Network Addons Operator Metrics List
### kubevirt_cnao_cr_kubemacpool_aggregated
Total count of KubeMacPool manager pods deployed by CNAO CR. Type: Gauge.

### kubevirt_cnao_cr_kubemacpool_deployed
KubeMacpool is deployed by CNAO CR. Type: Gauge.

### kubevirt_cnao_cr_ready
CNAO CR Ready. Type: Gauge.

### kubevirt_cnao_kubemacpool_duplicate_macs
Total count of duplicate KubeMacPool MAC addresses. Type: Gauge.

### kubevirt_cnao_kubemacpool_manager_up
Total count of running KubeMacPool manager pods. Type: Gauge.

### kubevirt_cnao_operator_up
Total count of running CNAO operators. Type: Gauge.

## Developing new metrics
After developing new metrics or changing old ones, please run 'make generate-doc' to regenerate this document.

All metrics documented here are auto-generated and reflect exactly what is being
exposed. After developing new metrics or changing old ones please regenerate
this document.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
"github.com/kubevirt/cluster-network-addons-operator/pkg/apply"
"github.com/kubevirt/cluster-network-addons-operator/pkg/controller/statusmanager"
"github.com/kubevirt/cluster-network-addons-operator/pkg/eventemitter"
"github.com/kubevirt/cluster-network-addons-operator/pkg/monitoring"
"github.com/kubevirt/cluster-network-addons-operator/pkg/monitoring/metrics"
"github.com/kubevirt/cluster-network-addons-operator/pkg/names"
"github.com/kubevirt/cluster-network-addons-operator/pkg/network"
"github.com/kubevirt/cluster-network-addons-operator/pkg/util/k8s"
Expand Down Expand Up @@ -221,7 +221,7 @@ func (r *ReconcileNetworkAddonsConfig) Reconcile(ctx context.Context, request re
r.stopTrackingObjects()

if r.clusterInfo.MonitoringAvailable {
monitoring.ResetMonitoredComponents()
metrics.ResetMonitoredComponents()
}

// Owned objects are automatically garbage collected. Return and don't requeue
Expand Down Expand Up @@ -313,7 +313,7 @@ func (r *ReconcileNetworkAddonsConfig) Reconcile(ctx context.Context, request re
r.statusManager.SetFromPods()

if r.clusterInfo.MonitoringAvailable {
monitoring.TrackMonitoredComponents(&networkAddonsConfig.Spec, r.statusManager)
metrics.TrackMonitoredComponents(&networkAddonsConfig.Spec, r.statusManager)
}

// Kubernetes sometimes fails to apply objects while we remove and recreate
Expand Down
65 changes: 0 additions & 65 deletions pkg/monitoring/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ import (
"strconv"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/metrics"

cnao "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/shared"
"github.com/kubevirt/cluster-network-addons-operator/pkg/controller/statusmanager"
"github.com/kubevirt/cluster-network-addons-operator/pkg/render"
)

Expand All @@ -24,68 +21,6 @@ const (
runbookURLTemplateEnv = "RUNBOOK_URL_TEMPLATE"
)

type MetricsOpts struct {
Name string
Help string
Type string
}

type MetricsKey string

const (
ReadyGauge MetricsKey = "readyGauge"
KMPDeployGauge MetricsKey = "kmpDeployedGauge"
)

var MetricsOptsList = map[MetricsKey]MetricsOpts{
ReadyGauge: {
Name: "kubevirt_cnao_cr_ready",
Help: "CNAO CR Ready",
Type: "Gauge",
},
KMPDeployGauge: {
Name: "kubevirt_cnao_cr_kubemacpool_deployed",
Help: "KubeMacpool is deployed by CNAO CR",
Type: "Gauge",
},
}

var (
readyGauge = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: MetricsOptsList[ReadyGauge].Name,
Help: MetricsOptsList[ReadyGauge].Help,
})
kmpDeployedGauge = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: MetricsOptsList[KMPDeployGauge].Name,
Help: MetricsOptsList[KMPDeployGauge].Help,
})
)

func init() {
metrics.Registry.MustRegister(readyGauge, kmpDeployedGauge)
}

func setGaugeParam(setTrueFlag bool, gaugeParam *prometheus.Gauge) {
if setTrueFlag {
(*gaugeParam).Set(1)
} else {
(*gaugeParam).Set(0)
}
}

func ResetMonitoredComponents() {
setGaugeParam(false, &readyGauge)
setGaugeParam(false, &kmpDeployedGauge)
}

func TrackMonitoredComponents(conf *cnao.NetworkAddonsConfigSpec, statusManager *statusmanager.StatusManager) {
isKubemacpoolDeployed := conf.KubeMacPool != nil
setGaugeParam(isKubemacpoolDeployed, &kmpDeployedGauge)
setGaugeParam(statusManager.IsStatusAvailable(), &readyGauge)
}

func RenderMonitoring(manifestDir string, monitoringAvailable bool) ([]*unstructured.Unstructured, error) {
if !monitoringAvailable {
return nil, nil
Expand Down
17 changes: 17 additions & 0 deletions pkg/monitoring/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package metrics

import "github.com/machadovilaca/operator-observability/pkg/operatormetrics"

var (
metrics = [][]operatormetrics.Metric{
operatorMetrics,
}
)

func SetupMetrics() error {
return operatormetrics.RegisterMetrics(metrics...)
}

func ListMetrics() []operatormetrics.Metric {
return operatormetrics.ListMetrics()
}
48 changes: 48 additions & 0 deletions pkg/monitoring/metrics/operator_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package metrics

import (
"github.com/machadovilaca/operator-observability/pkg/operatormetrics"

cnao "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/shared"
"github.com/kubevirt/cluster-network-addons-operator/pkg/controller/statusmanager"
)

var (
operatorMetrics = []operatormetrics.Metric{
cnaoCrReady,
kmpDeployed,
}

cnaoCrReady = operatormetrics.NewGauge(
operatormetrics.MetricOpts{
Name: "kubevirt_cnao_cr_ready",
Help: "CNAO CR Ready",
},
)

kmpDeployed = operatormetrics.NewGauge(
operatormetrics.MetricOpts{
Name: "kubevirt_cnao_cr_kubemacpool_deployed",
Help: "KubeMacpool is deployed by CNAO CR",
},
)
)

func ResetMonitoredComponents() {
setGaugeParam(false, cnaoCrReady)
setGaugeParam(false, kmpDeployed)
}

func TrackMonitoredComponents(conf *cnao.NetworkAddonsConfigSpec, statusManager *statusmanager.StatusManager) {
isKubemacpoolDeployed := conf.KubeMacPool != nil
setGaugeParam(isKubemacpoolDeployed, kmpDeployed)
setGaugeParam(statusManager.IsStatusAvailable(), cnaoCrReady)
}

func setGaugeParam(setTrueFlag bool, gaugeParam *operatormetrics.Gauge) {
if setTrueFlag {
gaugeParam.Set(1)
} else {
gaugeParam.Set(0)
}
}
29 changes: 14 additions & 15 deletions tools/metrics-parser/metrics_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,12 @@ import (
"strings"
"text/template"

"github.com/machadovilaca/operator-observability/pkg/operatormetrics"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/yaml.v3"

"github.com/kubevirt/cluster-network-addons-operator/pkg/monitoring"

"sigs.k8s.io/controller-tools/pkg/markers"
)

func MetricsOptsToMetricList(Metrics map[monitoring.MetricsKey]monitoring.MetricsOpts, result MetricList) MetricList {
for _, opts := range Metrics {
result = append(result, MetricDescriptionToMetric(opts))
}
return result
}

type PrometheusCR struct {
Spec struct {
Groups []struct {
Expand Down Expand Up @@ -111,19 +103,26 @@ func ParseTemplateFile() []byte {
}

type Metric struct {
operatormetrics.Metric

Name string
Description string
MType string
}

func MetricDescriptionToMetric(rrd monitoring.MetricsOpts) Metric {
return Metric{
Name: rrd.Name,
Description: rrd.Help,
MType: rrd.Type,
func (m Metric) getCollector() prometheus.Collector { return nil }

func (m Metric) GetOpts() operatormetrics.MetricOpts {
return operatormetrics.MetricOpts{
Name: m.Name,
Help: m.Description,
}
}

func (m Metric) GetType() operatormetrics.MetricType {
return operatormetrics.MetricType(m.MType)
}

func (m Metric) WriteOut() {
fmt.Println("###", m.Name)

Expand Down
64 changes: 36 additions & 28 deletions tools/metricsdocs/metricsdocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,51 @@ package main

import (
"fmt"
"sort"

"github.com/kubevirt/cluster-network-addons-operator/pkg/monitoring"
"github.com/machadovilaca/operator-observability/pkg/docs"

"github.com/kubevirt/cluster-network-addons-operator/pkg/monitoring/metrics"
metricsparser "github.com/kubevirt/cluster-network-addons-operator/tools/metrics-parser"
)

const (
opening = `# Cluster Network Addons Operator metrics
> This file is auto generated by metricsdocs, run 'make generate-doc' in order to update it.
const tpl = `# Cluster Network Addons Operator Metrics
This document aims to help users that are not familiar with metrics exposed by the Cluster Network Addons Operator.
All metrics documented here are auto-generated by the utility tool 'tools/metricsdocs' and reflects exactly what is being exposed.
## Cluster Network Addons Operator Metrics List
`
{{- range . }}
footer = `## Developing new metrics
After developing new metrics or changing old ones, please run 'make generate-doc' to regenerate this document.`
)
{{ $deprecatedVersion := "" -}}
{{- with index .ExtraFields "DeprecatedVersion" -}}
{{- $deprecatedVersion = printf " in %s" . -}}
{{- end -}}
func main() {
metricsList := getMetrics()
writeToFile(metricsList)
}
{{- $stabilityLevel := "" -}}
{{- if and (.ExtraFields.StabilityLevel) (ne .ExtraFields.StabilityLevel "STABLE") -}}
{{- $stabilityLevel = printf "[%s%s] " .ExtraFields.StabilityLevel $deprecatedVersion -}}
{{- end -}}
func getMetrics() metricsparser.MetricList {
metricsList := metricsparser.ReadFromPrometheusCR()
metricsList = metricsparser.MetricsOptsToMetricList(monitoring.MetricsOptsList, metricsList)
### {{ .Name }}
{{ print $stabilityLevel }}{{ .Help }}. Type: {{ .Type -}}.
sort.Slice(metricsList, func(i, j int) bool {
return metricsList[i].Name < metricsList[j].Name
})
{{- end }}
return metricsList
}
## Developing new metrics
All metrics documented here are auto-generated and reflect exactly what is being
exposed. After developing new metrics or changing old ones please regenerate
this document.
`

func main() {
err := metrics.SetupMetrics()
if err != nil {
panic(err)
}

metricsList := metrics.ListMetrics()

for _, metric := range metricsparser.ReadFromPrometheusCR() {
metricsList = append(metricsList, metric)
}

func writeToFile(metricsList metricsparser.MetricList) {
fmt.Print(opening)
metricsList.WriteOut()
fmt.Print(footer)
docsString := docs.BuildMetricsDocsWithCustomTemplate(metricsList, nil, tpl)
fmt.Print(docsString)
}
Loading

0 comments on commit 98d3955

Please sign in to comment.