Skip to content

Commit 229c8c0

Browse files
authored
Merge pull request #2148 from vimalk78/add-node-name-to-metrics
feat(node): add node_name to power metrics
2 parents 4d24510 + 4e120f7 commit 229c8c0

File tree

8 files changed

+191
-35
lines changed

8 files changed

+191
-35
lines changed

cmd/kepler/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ func createPrometheusExporter(logger *slog.Logger, cfg *config.Config, apiServer
210210
prometheus.WithLogger(logger),
211211
prometheus.WithCollectors(collectors),
212212
prometheus.WithDebugCollectors(debugCollectors),
213+
prometheus.WithNodeName(cfg.Kube.Node),
213214
)
214215

215216
return promExporter, nil

docs/metrics/metrics.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ These metrics provide energy and power information at the node level.
2424
- **Labels**:
2525
- `zone`
2626
- `path`
27+
- **Constant Labels**:
28+
- `node_name`
2729

2830
#### kepler_node_cpu_active_watts
2931

@@ -32,6 +34,8 @@ These metrics provide energy and power information at the node level.
3234
- **Labels**:
3335
- `zone`
3436
- `path`
37+
- **Constant Labels**:
38+
- `node_name`
3539

3640
#### kepler_node_cpu_idle_joules_total
3741

@@ -40,6 +44,8 @@ These metrics provide energy and power information at the node level.
4044
- **Labels**:
4145
- `zone`
4246
- `path`
47+
- **Constant Labels**:
48+
- `node_name`
4349

4450
#### kepler_node_cpu_idle_watts
4551

@@ -48,6 +54,8 @@ These metrics provide energy and power information at the node level.
4854
- **Labels**:
4955
- `zone`
5056
- `path`
57+
- **Constant Labels**:
58+
- `node_name`
5159

5260
#### kepler_node_cpu_info
5361

@@ -67,11 +75,15 @@ These metrics provide energy and power information at the node level.
6775
- **Labels**:
6876
- `zone`
6977
- `path`
78+
- **Constant Labels**:
79+
- `node_name`
7080

7181
#### kepler_node_cpu_usage_ratio
7282

7383
- **Type**: GAUGE
7484
- **Description**: CPU usage ratio of a node (value between 0.0 and 1.0)
85+
- **Constant Labels**:
86+
- `node_name`
7587

7688
#### kepler_node_cpu_watts
7789

@@ -80,6 +92,8 @@ These metrics provide energy and power information at the node level.
8092
- **Labels**:
8193
- `zone`
8294
- `path`
95+
- **Constant Labels**:
96+
- `node_name`
8397

8498
### Container Metrics
8599

@@ -95,6 +109,8 @@ These metrics provide energy and power information for containers.
95109
- `runtime`
96110
- `zone`
97111
- `pod_id`
112+
- **Constant Labels**:
113+
- `node_name`
98114

99115
#### kepler_container_cpu_watts
100116

@@ -106,6 +122,8 @@ These metrics provide energy and power information for containers.
106122
- `runtime`
107123
- `zone`
108124
- `pod_id`
125+
- **Constant Labels**:
126+
- `node_name`
109127

110128
### Process Metrics
111129

@@ -123,6 +141,8 @@ These metrics provide energy and power information for individual processes.
123141
- `container_id`
124142
- `vm_id`
125143
- `zone`
144+
- **Constant Labels**:
145+
- `node_name`
126146

127147
#### kepler_process_cpu_seconds_total
128148

@@ -135,6 +155,8 @@ These metrics provide energy and power information for individual processes.
135155
- `type`
136156
- `container_id`
137157
- `vm_id`
158+
- **Constant Labels**:
159+
- `node_name`
138160

139161
#### kepler_process_cpu_watts
140162

@@ -148,6 +170,8 @@ These metrics provide energy and power information for individual processes.
148170
- `container_id`
149171
- `vm_id`
150172
- `zone`
173+
- **Constant Labels**:
174+
- `node_name`
151175

152176
### Virtual Machine Metrics
153177

@@ -162,6 +186,8 @@ These metrics provide energy and power information for virtual machines.
162186
- `vm_name`
163187
- `hypervisor`
164188
- `zone`
189+
- **Constant Labels**:
190+
- `node_name`
165191

166192
#### kepler_vm_cpu_watts
167193

@@ -172,6 +198,8 @@ These metrics provide energy and power information for virtual machines.
172198
- `vm_name`
173199
- `hypervisor`
174200
- `zone`
201+
- **Constant Labels**:
202+
- `node_name`
175203

176204
### Pod Metrics
177205

@@ -186,6 +214,8 @@ These metrics provide energy and power information for pods.
186214
- `pod_name`
187215
- `pod_namespace`
188216
- `zone`
217+
- **Constant Labels**:
218+
- `node_name`
189219

190220
#### kepler_pod_cpu_watts
191221

@@ -196,6 +226,8 @@ These metrics provide energy and power information for pods.
196226
- `pod_name`
197227
- `pod_namespace`
198228
- `zone`
229+
- **Constant Labels**:
230+
- `node_name`
199231

200232
### Other Metrics
201233

hack/gen-metric-docs/main.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type MetricInfo struct {
2525
Type string
2626
Description string
2727
Labels []string
28+
ConstLabels map[string]string
2829
}
2930

3031
// MockMonitor implements the minimal interface needed by collectors
@@ -70,6 +71,7 @@ func extractMetricsInfo(collector prometheus.Collector) ([]MetricInfo, error) {
7071
fqNameRegex := regexp.MustCompile(`fqName: "([^"]+)"`)
7172
helpRegex := regexp.MustCompile(`help: "([^"]+)"`)
7273
variableLabelsRegex := regexp.MustCompile(`variableLabels: \{([^}]*)\}`)
74+
constLabelsRegex := regexp.MustCompile(`constLabels: \{([^}]*)\}`)
7375

7476
for desc := range ch {
7577
descStr := desc.String()
@@ -99,6 +101,20 @@ func extractMetricsInfo(collector prometheus.Collector) ([]MetricInfo, error) {
99101
}
100102
}
101103

104+
constLabels := make(map[string]string)
105+
constLabelsMatch := constLabelsRegex.FindStringSubmatch(descStr)
106+
if len(constLabelsMatch) >= 2 && constLabelsMatch[1] != "" {
107+
constLabelsStr := constLabelsMatch[1]
108+
// Parse const labels which are in format: labelName="labelValue"
109+
labelPairRegex := regexp.MustCompile(`(\w+)="([^"]*)"`)
110+
matches := labelPairRegex.FindAllStringSubmatch(constLabelsStr, -1)
111+
for _, match := range matches {
112+
if len(match) >= 3 {
113+
constLabels[match[1]] = match[2]
114+
}
115+
}
116+
}
117+
102118
metricType := "GAUGE"
103119
if strings.HasSuffix(name, "_total") {
104120
metricType = "COUNTER"
@@ -109,6 +125,7 @@ func extractMetricsInfo(collector prometheus.Collector) ([]MetricInfo, error) {
109125
Type: metricType,
110126
Description: help,
111127
Labels: labels,
128+
ConstLabels: constLabels,
112129
})
113130
}
114131

@@ -203,6 +220,18 @@ func writeMetricsSection(md *strings.Builder, metrics []MetricInfo) {
203220
fmt.Fprintf(md, " - `%s`\n", label)
204221
}
205222
}
223+
if len(metric.ConstLabels) > 0 {
224+
md.WriteString("- **Constant Labels**:\n")
225+
// Sort constant labels for consistent output
226+
var keys []string
227+
for key := range metric.ConstLabels {
228+
keys = append(keys, key)
229+
}
230+
sort.Strings(keys)
231+
for _, key := range keys {
232+
fmt.Fprintf(md, " - `%s`\n", key)
233+
}
234+
}
206235
md.WriteString("\n")
207236
}
208237
}
@@ -231,7 +260,7 @@ func main() {
231260
fmt.Println("Creating collectors...")
232261
// Create a logger for the collectors
233262
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
234-
powerCollector := collector.NewPowerCollector(mockMonitor, logger)
263+
powerCollector := collector.NewPowerCollector(mockMonitor, "test-node", logger)
235264
fmt.Println("Created power collector")
236265
buildInfoCollector := collector.NewKeplerBuildInfoCollector()
237266
fmt.Println("Created build info collector")

hack/gen-metric-docs/main_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,37 @@ func TestExtractMetricsInfo(t *testing.T) {
5252
Type: "COUNTER",
5353
Description: "Test counter metric",
5454
Labels: []string{"label1", "label2"},
55+
ConstLabels: map[string]string{},
5556
},
5657
{
5758
Name: "test_gauge",
5859
Type: "GAUGE",
5960
Description: "Test gauge metric",
6061
Labels: []string{"label3"},
62+
ConstLabels: map[string]string{},
6163
},
6264
{
6365
Name: "test_no_labels",
6466
Type: "GAUGE",
6567
Description: "Test metric without labels",
6668
Labels: nil,
69+
ConstLabels: map[string]string{},
70+
},
71+
},
72+
},
73+
{
74+
name: "MetricsWithConstLabels",
75+
descs: []*prometheus.Desc{
76+
prometheus.NewDesc("test_const_labels", "Test metric with constant labels", []string{"var_label"}, prometheus.Labels{"node_name": "test-node", "region": "us-west-1"}),
77+
},
78+
expectedMetricsLen: 1,
79+
expectedMetrics: []MetricInfo{
80+
{
81+
Name: "test_const_labels",
82+
Type: "GAUGE",
83+
Description: "Test metric with constant labels",
84+
Labels: []string{"var_label"},
85+
ConstLabels: map[string]string{"node_name": "test-node", "region": "us-west-1"},
6786
},
6887
},
6988
},
@@ -121,6 +140,29 @@ func TestGenerateMarkdown(t *testing.T) {
121140
"- `path`",
122141
},
123142
},
143+
{
144+
name: "MetricsWithConstLabels",
145+
metrics: []MetricInfo{
146+
{
147+
Name: "kepler_node_cpu_watts",
148+
Type: "GAUGE",
149+
Description: "Power consumption of cpu at node level in watts",
150+
Labels: []string{"zone", "path"},
151+
ConstLabels: map[string]string{"node_name": "test-node"},
152+
},
153+
},
154+
expectedMarkdown: []string{
155+
"### Node Metrics",
156+
"#### kepler_node_cpu_watts",
157+
"- **Type**: GAUGE",
158+
"- **Description**: Power consumption of cpu at node level in watts",
159+
"- **Labels**:",
160+
"- `zone`",
161+
"- `path`",
162+
"- **Constant Labels**:",
163+
"- `node_name`",
164+
},
165+
},
124166
{
125167
name: "ContainerMetrics",
126168
metrics: []MetricInfo{
@@ -253,6 +295,33 @@ func TestWriteMetricsSection(t *testing.T) {
253295
"- `path`",
254296
},
255297
},
298+
{
299+
name: "MetricWithConstLabels",
300+
metrics: []MetricInfo{
301+
{
302+
Name: "kepler_node_cpu_watts",
303+
Type: "GAUGE",
304+
Description: "Power consumption of cpu at node level in watts",
305+
Labels: []string{"zone", "path"},
306+
ConstLabels: map[string]string{"node_name": "test-node", "region": "us-west-1"},
307+
},
308+
},
309+
expectedMarkdown: []string{
310+
"#### kepler_node_cpu_watts",
311+
"- **Type**: GAUGE",
312+
"- **Description**: Power consumption of cpu at node level in watts",
313+
"- **Labels**:",
314+
"- `zone`",
315+
"- `path`",
316+
"- **Constant Labels**:",
317+
"- `node_name`",
318+
"- `region`",
319+
},
320+
notExpectedMarkdown: []string{
321+
"- `node_name=test-node`",
322+
"- `region=us-west-1`",
323+
},
324+
},
256325
{
257326
name: "MetricWithoutLabels",
258327
metrics: []MetricInfo{

0 commit comments

Comments
 (0)