Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
Signed-off-by: yaroslavborbat <[email protected]>
  • Loading branch information
yaroslavborbat committed Sep 25, 2024
1 parent 0bf6e73 commit 1247b82
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ type Collector struct {
}

func (c Collector) Describe(ch chan<- *prometheus.Desc) {
for _, v := range diskMetrics {
ch <- v
for _, m := range diskMetrics {
ch <- m.Desc
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ limitations under the License.
package vd

import (
"strings"

"github.com/deckhouse/virtualization-controller/pkg/monitoring/metrics/util"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)

type dataMetric struct {
Name string
Namespace string
UID string
Phase virtv2.DiskPhase
Name string
Namespace string
UID string
Phase virtv2.DiskPhase
Labels map[string]string
Annotations map[string]string
}

// DO NOT mutate VirtualDisk!
Expand All @@ -38,5 +43,11 @@ func newDataMetric(vd *virtv2.VirtualDisk) *dataMetric {
Namespace: vd.Namespace,
UID: string(vd.UID),
Phase: vd.Status.Phase,
Labels: util.WrapPrometheusLabels(vd.GetLabels(), "label", func(key, value string) bool {
return false
}),
Annotations: util.WrapPrometheusLabels(vd.GetAnnotations(), "annotation", func(key, _ string) bool {
return strings.HasPrefix(key, "kubectl.kubernetes.io")
}),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (

const (
MetricDiskStatusPhase = "virtualdisk_status_phase"
MetricDiskLabels = "virtualdisk_labels"
MetricDiskAnnotations = "virtualdisk_annotations"
)

var baseLabels = []string{"name", "namespace", "uid"}
Expand All @@ -44,9 +46,26 @@ func WithBaseLabelsByMetric(m *dataMetric, labels ...string) []string {
return append(base, labels...)
}

var diskMetrics = map[string]*prometheus.Desc{
MetricDiskStatusPhase: prometheus.NewDesc(prometheus.BuildFQName(metrics.MetricNamespace, "", MetricDiskStatusPhase),
var diskMetrics = map[string]metrics.MetricInfo{
MetricDiskStatusPhase: metrics.NewMetricInfo(
MetricDiskStatusPhase,
"The virtualdisk current phase.",
prometheus.GaugeValue,
WithBaseLabels("phase"),
nil),
nil,
),

MetricDiskLabels: metrics.NewMetricInfo(MetricDiskLabels,
"Kubernetes labels converted to Prometheus labels.",
prometheus.GaugeValue,
WithBaseLabels(),
nil,
),

MetricDiskAnnotations: metrics.NewMetricInfo(MetricDiskAnnotations,
"Kubernetes annotations converted to Prometheus labels.",
prometheus.GaugeValue,
WithBaseLabels(),
nil,
),
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/prometheus/client_golang/prometheus"

metricutil "github.com/deckhouse/virtualization-controller/pkg/monitoring/metrics/util"
"github.com/deckhouse/virtualization-controller/pkg/util"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)
Expand All @@ -36,10 +37,12 @@ type scraper struct {
}

func (s *scraper) Report(m *dataMetric) {
s.updateDiskStatusPhaseMetrics(m)
s.updateMetricDiskStatusPhase(m)
s.updateMetricDiskLabels(m)
s.updateMetricDiskAnnotations(m)
}

func (s *scraper) updateDiskStatusPhaseMetrics(m *dataMetric) {
func (s *scraper) updateMetricDiskStatusPhase(m *dataMetric) {
phase := m.Phase
if phase == "" {
phase = virtv2.DiskPending
Expand All @@ -65,16 +68,40 @@ func (s *scraper) updateDiskStatusPhaseMetrics(m *dataMetric) {
}
}

func (s *scraper) updateMetricDiskLabels(m *dataMetric) {
s.updateDynamic(MetricDiskLabels, 1, m, nil, m.Labels)
}

func (s *scraper) updateMetricDiskAnnotations(m *dataMetric) {
s.updateDynamic(MetricDiskAnnotations, 1, m, nil, m.Annotations)
}

func (s *scraper) defaultUpdate(descName string, value float64, m *dataMetric, labels ...string) {
desc := diskMetrics[descName]
info := diskMetrics[descName]
metric, err := prometheus.NewConstMetric(
desc,
info.Desc,
prometheus.GaugeValue,
value,
WithBaseLabelsByMetric(m, labels...)...,
)
if err != nil {
s.log.Warn(fmt.Sprintf("Error creating the new const dataMetric for %s: %s", desc, err))
s.log.Warn(fmt.Sprintf("Error creating the new const dataMetric for %s: %s", info.Desc, err))
return
}
s.ch <- metric
}

func (s *scraper) updateDynamic(name string, value float64, m *dataMetric, labelValues []string, extraLabels prometheus.Labels) {
info := diskMetrics[name]
metric, err := metricutil.NewDynamicMetric(
info.Desc,
info.Type,
value,
WithBaseLabelsByMetric(m, labelValues...),
extraLabels,
)
if err != nil {
s.log.Warn(fmt.Sprintf("Error creating the new dynamic dataMetric for %s: %s", info.Desc, err))
return
}
s.ch <- metric
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ type Collector struct {
}

func (c Collector) Describe(ch chan<- *prometheus.Desc) {
for _, v := range vmbdaMetrics {
ch <- v
for _, m := range vmbdaMetrics {
ch <- m.Desc
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ limitations under the License.

package vmbda

import virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
import (
"strings"

"github.com/deckhouse/virtualization-controller/pkg/monitoring/metrics/util"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)

type dataMetric struct {
Name string
Namespace string
UID string
Phase virtv2.BlockDeviceAttachmentPhase
Name string
Namespace string
UID string
Phase virtv2.BlockDeviceAttachmentPhase
Labels map[string]string
Annotations map[string]string
}

// DO NOT mutate VirtualMachineBlockDeviceAttachment!
Expand All @@ -36,5 +43,11 @@ func newDataMetric(vmbda *virtv2.VirtualMachineBlockDeviceAttachment) *dataMetri
Namespace: vmbda.Namespace,
UID: string(vmbda.UID),
Phase: vmbda.Status.Phase,
Labels: util.WrapPrometheusLabels(vmbda.GetLabels(), "label", func(key, value string) bool {
return false
}),
Annotations: util.WrapPrometheusLabels(vmbda.GetAnnotations(), "annotation", func(key, _ string) bool {
return strings.HasPrefix(key, "kubectl.kubernetes.io")
}),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (

const (
MetricVMBDAStatusPhase = "virtualmachineblockdeviceattachment_status_phase"
MetricVMBDALabels = "virtualmachineblockdeviceattachment_labels"
MetricVMBDAAnnotations = "virtualmachineblockdeviceattachment_annotations"
)

var baseLabels = []string{"name", "namespace", "uid"}
Expand All @@ -44,9 +46,26 @@ func WithBaseLabelsByMetric(m *dataMetric, labels ...string) []string {
return append(base, labels...)
}

var vmbdaMetrics = map[string]*prometheus.Desc{
MetricVMBDAStatusPhase: prometheus.NewDesc(prometheus.BuildFQName(metrics.MetricNamespace, "", MetricVMBDAStatusPhase),
var vmbdaMetrics = map[string]metrics.MetricInfo{
MetricVMBDAStatusPhase: metrics.NewMetricInfo(
MetricVMBDAStatusPhase,
"The virtualmachineblockdeviceattachment current phase.",
prometheus.GaugeValue,
WithBaseLabels("phase"),
nil),
nil,
),

MetricVMBDALabels: metrics.NewMetricInfo(MetricVMBDALabels,
"Kubernetes labels converted to Prometheus labels.",
prometheus.GaugeValue,
WithBaseLabels(),
nil,
),

MetricVMBDAAnnotations: metrics.NewMetricInfo(MetricVMBDAAnnotations,
"Kubernetes annotations converted to Prometheus labels.",
prometheus.GaugeValue,
WithBaseLabels(),
nil,
),
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/prometheus/client_golang/prometheus"

metricutil "github.com/deckhouse/virtualization-controller/pkg/monitoring/metrics/util"
"github.com/deckhouse/virtualization-controller/pkg/util"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)
Expand All @@ -36,10 +37,12 @@ type scraper struct {
}

func (s *scraper) Report(m *dataMetric) {
s.updateVMBDAStatusPhaseMetrics(m)
s.updateMetricVMBDAStatusPhase(m)
s.updateMetricVMBDALabels(m)
s.updateMetricVMBDAAnnotations(m)
}

func (s *scraper) updateVMBDAStatusPhaseMetrics(m *dataMetric) {
func (s *scraper) updateMetricVMBDAStatusPhase(m *dataMetric) {
phase := m.Phase
if phase == "" {
phase = virtv2.BlockDeviceAttachmentPhasePending
Expand All @@ -61,16 +64,40 @@ func (s *scraper) updateVMBDAStatusPhaseMetrics(m *dataMetric) {
}
}

func (s *scraper) updateMetricVMBDALabels(m *dataMetric) {
s.updateDynamic(MetricVMBDALabels, 1, m, nil, m.Labels)
}

func (s *scraper) updateMetricVMBDAAnnotations(m *dataMetric) {
s.updateDynamic(MetricVMBDAAnnotations, 1, m, nil, m.Annotations)
}

func (s *scraper) defaultUpdate(descName string, value float64, m *dataMetric, labels ...string) {
desc := vmbdaMetrics[descName]
info := vmbdaMetrics[descName]
metric, err := prometheus.NewConstMetric(
desc,
info.Desc,
prometheus.GaugeValue,
value,
WithBaseLabelsByMetric(m, labels...)...,
)
if err != nil {
s.log.Warn(fmt.Sprintf("Error creating the new const dataMetric for %s: %s", desc, err))
s.log.Warn(fmt.Sprintf("Error creating the new const dataMetric for %s: %s", info.Desc, err))
return
}
s.ch <- metric
}

func (s *scraper) updateDynamic(name string, value float64, m *dataMetric, labelValues []string, extraLabels prometheus.Labels) {
info := vmbdaMetrics[name]
metric, err := metricutil.NewDynamicMetric(
info.Desc,
info.Type,
value,
WithBaseLabelsByMetric(m, labelValues...),
extraLabels,
)
if err != nil {
s.log.Warn(fmt.Sprintf("Error creating the new dynamic dataMetric for %s: %s", info.Desc, err))
return
}
s.ch <- metric
Expand Down

0 comments on commit 1247b82

Please sign in to comment.