Skip to content

Commit 0ed9296

Browse files
Merge pull request #527 from vyzigold/nodeExporter-hostname
[OSPRH-9848] enhance NodeExporter metrics
2 parents d01484b + 5130e45 commit 0ed9296

File tree

4 files changed

+120
-34
lines changed

4 files changed

+120
-34
lines changed

controllers/metricstorage_controller.go

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ type MetricStorageReconciler struct {
9696
Cache cache.Cache
9797
}
9898

99+
// ConnectionInfo holds information about connection to a compute node
100+
type ConnectionInfo struct {
101+
IP string
102+
Hostname string
103+
TLS bool
104+
}
105+
99106
// GetLogger returns a logger object with a prefix of "conroller.name" and aditional controller context fields
100107
func (r *MetricStorageReconciler) GetLogger(ctx context.Context) logr.Logger {
101108
return log.FromContext(ctx).WithName("Controllers").WithName("MetricStorage")
@@ -506,7 +513,7 @@ func (r *MetricStorageReconciler) createServiceScrapeConfig(
506513
log logr.Logger,
507514
description string,
508515
serviceName string,
509-
targets []string,
516+
targets interface{},
510517
tlsEnabled bool,
511518
) error {
512519
scrapeConfig := &monv1alpha1.ScrapeConfig{
@@ -593,30 +600,31 @@ func (r *MetricStorageReconciler) createScrapeConfigs(
593600
return ctrl.Result{}, err
594601
}
595602

596-
// ScrapeConfigs for NodeExporters
597-
endpointsNonTLS, endpointsTLS, err := getMetricExporterTargets(instance, helper, telemetryv1.DefaultNodeExporterPort)
603+
connectionInfo, err := getComputeNodesConnectionInfo(instance, helper)
598604
if err != nil {
599-
Log.Info(fmt.Sprintf("Cannot get node exporter targets. Scrape configs not created. Error: %s", err))
605+
Log.Info(fmt.Sprintf("Cannot get compute node connection info. Scrape configs not created. Error: %s", err))
600606
}
601607

608+
// ScrapeConfigs for NodeExporters
609+
neTargetsTLS, neTargetsNonTLS := getNodeExporterTargets(connectionInfo)
602610
// ScrapeConfig for non-tls nodes
603611
neServiceName := fmt.Sprintf("%s-node-exporter", telemetry.ServiceName)
604612
err = r.createServiceScrapeConfig(ctx, instance, Log, "Node Exporter",
605-
neServiceName, endpointsNonTLS, false)
613+
neServiceName, neTargetsNonTLS, false)
606614
if err != nil {
607615
return ctrl.Result{}, err
608616
}
609617

610618
// ScrapeConfig for tls nodes
611619
neServiceName = fmt.Sprintf("%s-node-exporter-tls", telemetry.ServiceName)
612620
err = r.createServiceScrapeConfig(ctx, instance, Log, "Node Exporter",
613-
neServiceName, endpointsTLS, true)
621+
neServiceName, neTargetsTLS, true)
614622
if err != nil {
615623
return ctrl.Result{}, err
616624
}
617625

618626
// kepler scrape endpoints
619-
_, keplerEndpoints, err := getMetricExporterTargets(instance, helper, telemetryv1.DefaultKeplerPort)
627+
keplerEndpoints, _ := getKeplerTargets(connectionInfo)
620628
if err != nil {
621629
Log.Info(fmt.Sprintf("Cannot get Kepler targets. Scrape configs not created. Error: %s", err))
622630
}
@@ -633,6 +641,37 @@ func (r *MetricStorageReconciler) createScrapeConfigs(
633641
return ctrl.Result{}, err
634642
}
635643

644+
func getNodeExporterTargets(nodes []ConnectionInfo) ([]metricstorage.LabeledTarget, []metricstorage.LabeledTarget) {
645+
tls := []metricstorage.LabeledTarget{}
646+
nonTLS := []metricstorage.LabeledTarget{}
647+
for _, node := range nodes {
648+
target := metricstorage.LabeledTarget{
649+
IP: fmt.Sprintf("%s:%d", node.IP, telemetryv1.DefaultNodeExporterPort),
650+
Hostname: node.Hostname,
651+
}
652+
if node.TLS {
653+
tls = append(tls, target)
654+
} else {
655+
nonTLS = append(nonTLS, target)
656+
}
657+
}
658+
return tls, nonTLS
659+
}
660+
661+
func getKeplerTargets(nodes []ConnectionInfo) ([]string, []string) {
662+
tls := []string{}
663+
nonTLS := []string{}
664+
for _, node := range nodes {
665+
target := fmt.Sprintf("%s:%d", node.IP, telemetryv1.DefaultKeplerPort)
666+
if node.TLS {
667+
tls = append(tls, target)
668+
} else {
669+
nonTLS = append(nonTLS, target)
670+
}
671+
}
672+
return tls, nonTLS
673+
}
674+
636675
func (r *MetricStorageReconciler) createDashboardObjects(ctx context.Context, instance *telemetryv1.MetricStorage, eventHandler handler.EventHandler) (ctrl.Result, error) {
637676
Log := r.GetLogger(ctx)
638677
uiPluginObj := &obsui.UIPlugin{
@@ -795,26 +834,24 @@ func (r *MetricStorageReconciler) ensureWatches(
795834
return err
796835
}
797836

798-
func getMetricExporterTargets(
837+
func getComputeNodesConnectionInfo(
799838
instance *telemetryv1.MetricStorage,
800839
helper *helper.Helper,
801-
defaultMetricExporterPort int32,
802-
) ([]string, []string, error) {
840+
) ([]ConnectionInfo, error) {
803841
ipSetList, err := getIPSetList(instance, helper)
804842
if err != nil {
805-
return []string{}, []string{}, err
843+
return []ConnectionInfo{}, err
806844
}
807845
inventorySecretList, err := getInventorySecretList(instance, helper)
808846
if err != nil {
809-
return []string{}, []string{}, err
847+
return []ConnectionInfo{}, err
810848
}
811849
var address string
812-
addressesNonTLS := []string{}
813-
addressesTLS := []string{}
850+
connectionInfo := []ConnectionInfo{}
814851
for _, secret := range inventorySecretList.Items {
815852
inventory, err := ansible.UnmarshalYAML(secret.Data["inventory"])
816853
if err != nil {
817-
return []string{}, []string{}, err
854+
return []ConnectionInfo{}, err
818855
}
819856
nodeSetGroup := inventory.Groups[secret.Labels["openstackdataplanenodeset"]]
820857
containsTelemetry := false
@@ -842,20 +879,28 @@ func getMetricExporterTargets(
842879
address, _ = getAddressFromAnsibleHost(&item)
843880
} else {
844881
// we were unable to find an IP or HostName for a node, so we do not go further
845-
return addressesNonTLS, addressesTLS, fmt.Errorf("failed to find an IP or HostName for node %s", name)
882+
return connectionInfo, fmt.Errorf("failed to find an IP or HostName for node %s", name)
846883
}
847884
if address == "" {
848885
// we were unable to find an IP or HostName for a node, so we do not go further
849-
return addressesNonTLS, addressesTLS, fmt.Errorf("failed to find an IP or HostName for node %s", name)
886+
return connectionInfo, fmt.Errorf("failed to find an IP or HostName for node %s", name)
850887
}
851888
if TLSEnabled, ok := nodeSetGroup.Vars["edpm_tls_certs_enabled"].(bool); ok && TLSEnabled {
852-
addressesTLS = append(addressesTLS, fmt.Sprintf("%s:%d", address, defaultMetricExporterPort))
889+
connectionInfo = append(connectionInfo, ConnectionInfo{
890+
IP: address,
891+
Hostname: name,
892+
TLS: true,
893+
})
853894
} else {
854-
addressesNonTLS = append(addressesNonTLS, fmt.Sprintf("%s:%d", address, defaultMetricExporterPort))
895+
connectionInfo = append(connectionInfo, ConnectionInfo{
896+
IP: address,
897+
Hostname: name,
898+
TLS: false,
899+
})
855900
}
856901
}
857902
}
858-
return addressesNonTLS, addressesTLS, nil
903+
return connectionInfo, nil
859904
}
860905

861906
func getIPSetList(instance *telemetryv1.MetricStorage, helper *helper.Helper) (*infranetworkv1.IPSetList, error) {

pkg/metricstorage/scrape_config.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@ import (
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
)
2929

30+
type LabeledTarget struct {
31+
IP string
32+
Hostname string
33+
}
34+
3035
// ScrapeConfig creates a ScrapeConfig CR
3136
func ScrapeConfig(
3237
instance *telemetryv1.MetricStorage,
3338
labels map[string]string,
34-
targets []string,
39+
targets interface{},
3540
tlsEnabled bool,
3641
) *monv1alpha1.ScrapeConfig {
3742
var scrapeInterval monv1.Duration
@@ -46,10 +51,28 @@ func ScrapeConfig(
4651
scrapeInterval = telemetryv1.DefaultScrapeInterval
4752
}
4853

49-
sort.Strings(targets)
50-
var convertedTargets []monv1alpha1.Target
51-
for _, t := range targets {
52-
convertedTargets = append(convertedTargets, monv1alpha1.Target(t))
54+
var staticConfigs []monv1alpha1.StaticConfig
55+
if ips, ok := targets.([]string); ok {
56+
sort.Strings(ips)
57+
var convertedTargets []monv1alpha1.Target
58+
for _, t := range ips {
59+
convertedTargets = append(convertedTargets, monv1alpha1.Target(t))
60+
}
61+
staticConfigs = append(staticConfigs, monv1alpha1.StaticConfig{
62+
Targets: convertedTargets,
63+
})
64+
} else if labeledTargets, ok := targets.([]LabeledTarget); ok {
65+
sort.Slice(labeledTargets, func(i, j int) bool {
66+
return labeledTargets[i].IP < labeledTargets[j].IP
67+
})
68+
for _, t := range labeledTargets {
69+
staticConfigs = append(staticConfigs, monv1alpha1.StaticConfig{
70+
Targets: []monv1alpha1.Target{monv1alpha1.Target(t.IP)},
71+
Labels: map[monv1.LabelName]string{
72+
"hostname": t.Hostname,
73+
},
74+
})
75+
}
5376
}
5477

5578
scrapeConfig := &monv1alpha1.ScrapeConfig{
@@ -82,11 +105,7 @@ func ScrapeConfig(
82105
},
83106
},
84107
ScrapeInterval: &scrapeInterval,
85-
StaticConfigs: []monv1alpha1.StaticConfig{
86-
{
87-
Targets: convertedTargets,
88-
},
89-
},
108+
StaticConfigs: staticConfigs,
90109
},
91110
}
92111

tests/kuttl/suites/metricstorage/tests/01-assert.yaml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,19 @@ metadata:
8181
- kind: MetricStorage
8282
name: telemetry-kuttl
8383
spec:
84-
staticConfigs:
85-
- {}
84+
scrapeInterval: 30s
85+
---
86+
apiVersion: monitoring.rhobs/v1alpha1
87+
kind: ScrapeConfig
88+
metadata:
89+
labels:
90+
service: metricStorage
91+
name: telemetry-node-exporter-tls
92+
ownerReferences:
93+
- kind: MetricStorage
94+
name: telemetry-kuttl
95+
spec:
96+
scrapeInterval: 30s
8697
---
8798
apiVersion: monitoring.rhobs/v1alpha1
8899
kind: ScrapeConfig

tests/kuttl/suites/metricstorage/tests/04-assert.yaml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,19 @@ metadata:
9393
- kind: MetricStorage
9494
name: telemetry-kuttl
9595
spec:
96-
staticConfigs:
97-
- {}
96+
scrapeInterval: 40s
97+
---
98+
apiVersion: monitoring.rhobs/v1alpha1
99+
kind: ScrapeConfig
100+
metadata:
101+
labels:
102+
service: metricStorage
103+
name: telemetry-node-exporter-tls
104+
ownerReferences:
105+
- kind: MetricStorage
106+
name: telemetry-kuttl
107+
spec:
108+
scrapeInterval: 40s
98109
---
99110
apiVersion: monitoring.rhobs/v1alpha1
100111
kind: ScrapeConfig

0 commit comments

Comments
 (0)