Skip to content

Commit 244e15a

Browse files
authored
Add YAML support for default and custom metrics definition (#302)
Introduce YAML support for exporter metrics files
1 parent 40663ff commit 244e15a

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

collector/collector.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"sync"
2121
"time"
2222

23-
"github.com/BurntSushi/toml"
2423
_ "github.com/godror/godror"
2524
"github.com/prometheus/client_golang/prometheus"
2625
)
@@ -59,7 +58,7 @@ func NewExporter(logger *slog.Logger, m *MetricsConfiguration) *Exporter {
5958
// set to a blank value.
6059
for _, dbconfig := range m.Databases {
6160
for label, _ := range dbconfig.Labels {
62-
if (!slices.Contains(allConstLabels, label)) {
61+
if !slices.Contains(allConstLabels, label) {
6362
allConstLabels = append(allConstLabels, label)
6463
}
6564
}
@@ -400,12 +399,14 @@ func (e *Exporter) reloadMetrics() {
400399
if len(e.CustomMetricsFiles()) > 0 {
401400
for _, _customMetrics := range e.CustomMetricsFiles() {
402401
metrics := &Metrics{}
403-
if _, err := toml.DecodeFile(_customMetrics, metrics); err != nil {
402+
403+
if err := loadMetricsConfig(_customMetrics, metrics); err != nil {
404404
e.logger.Error("failed to load custom metrics", "error", err)
405405
panic(errors.New("Error while loading " + _customMetrics))
406406
} else {
407407
e.logger.Info("Successfully loaded custom metrics from " + _customMetrics)
408408
}
409+
409410
e.metricsToScrape.Metric = append(e.metricsToScrape.Metric, metrics.Metric...)
410411
}
411412
} else {

collector/data_loader.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2025, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package collector
5+
6+
import (
7+
"fmt"
8+
"os"
9+
"strings"
10+
11+
"github.com/BurntSushi/toml"
12+
"gopkg.in/yaml.v2"
13+
)
14+
15+
func loadYamlMetricsConfig(_metricsFileName string, metrics *Metrics) error {
16+
yamlBytes, err := os.ReadFile(_metricsFileName)
17+
if err != nil {
18+
return fmt.Errorf("cannot read the metrics config %s: %w", _metricsFileName, err)
19+
}
20+
if err := yaml.Unmarshal(yamlBytes, metrics); err != nil {
21+
return fmt.Errorf("cannot unmarshal the metrics config %s: %w", _metricsFileName, err)
22+
}
23+
return nil
24+
}
25+
26+
func loadTomlMetricsConfig(_customMetrics string, metrics *Metrics) error {
27+
if _, err := toml.DecodeFile(_customMetrics, metrics); err != nil {
28+
return fmt.Errorf("cannot read the metrics config %s: %w", _customMetrics, err)
29+
}
30+
return nil
31+
}
32+
33+
func loadMetricsConfig(_customMetrics string, metrics *Metrics) error {
34+
if strings.HasSuffix(_customMetrics, "toml") {
35+
if err := loadTomlMetricsConfig(_customMetrics, metrics); err != nil {
36+
return fmt.Errorf("cannot load toml based metrics: %w", err)
37+
}
38+
} else {
39+
if err := loadYamlMetricsConfig(_customMetrics, metrics); err != nil {
40+
return fmt.Errorf("cannot load yaml based metrics: %w", err)
41+
}
42+
}
43+
return nil
44+
}

collector/default_metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var defaultMetricsToml string
2020
func (e *Exporter) DefaultMetrics() Metrics {
2121
var metricsToScrape Metrics
2222
if e.Metrics.Default != "" {
23-
if _, err := toml.DecodeFile(filepath.Clean(e.Metrics.Default), &metricsToScrape); err != nil {
23+
if err := loadMetricsConfig(filepath.Clean(e.Metrics.Default), &metricsToScrape); err != nil {
2424
e.logger.Error(fmt.Sprintf("there was an issue while loading specified default metrics file at: "+e.Metrics.Default+", proceeding to run with default metrics."),
2525
"error", err)
2626
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ require (
3131
github.com/golang-jwt/jwt/v5 v5.2.2 // indirect
3232
github.com/google/uuid v1.6.0 // indirect
3333
github.com/jpillora/backoff v1.0.0 // indirect
34-
github.com/klauspost/compress v1.18.0 // indirect
3534
github.com/kylelemons/godebug v1.1.0 // indirect
3635
github.com/mdlayher/socket v0.4.1 // indirect
3736
github.com/mdlayher/vsock v1.2.1 // indirect

0 commit comments

Comments
 (0)