Skip to content

[FEAT] Add YAML support for default and custom metrics definition #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"sync"
"time"

"github.com/BurntSushi/toml"
_ "github.com/godror/godror"
"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -59,7 +58,7 @@ func NewExporter(logger *slog.Logger, m *MetricsConfiguration) *Exporter {
// set to a blank value.
for _, dbconfig := range m.Databases {
for label, _ := range dbconfig.Labels {
if (!slices.Contains(allConstLabels, label)) {
if !slices.Contains(allConstLabels, label) {
allConstLabels = append(allConstLabels, label)
}
}
Expand Down Expand Up @@ -400,12 +399,14 @@ func (e *Exporter) reloadMetrics() {
if len(e.CustomMetricsFiles()) > 0 {
for _, _customMetrics := range e.CustomMetricsFiles() {
metrics := &Metrics{}
if _, err := toml.DecodeFile(_customMetrics, metrics); err != nil {

if err := loadMetricsConfig(_customMetrics, metrics); err != nil {
e.logger.Error("failed to load custom metrics", "error", err)
panic(errors.New("Error while loading " + _customMetrics))
} else {
e.logger.Info("Successfully loaded custom metrics from " + _customMetrics)
}

e.metricsToScrape.Metric = append(e.metricsToScrape.Metric, metrics.Metric...)
}
} else {
Expand Down
41 changes: 41 additions & 0 deletions collector/data_loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package collector
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the copyright header:

// Copyright (c) 2025, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.


import (
"fmt"
"os"
"strings"

"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2"
)

func loadYamlMetricsConfig(_metricsFileName string, metrics *Metrics) error {
yamlBytes, err := os.ReadFile(_metricsFileName)
if err != nil {
return fmt.Errorf("cannot read the metrics config %s: %w", _metricsFileName, err)
}
if err := yaml.Unmarshal(yamlBytes, metrics); err != nil {
return fmt.Errorf("cannot unmarshal the metrics config %s: %w", _metricsFileName, err)
}
return nil
}

func loadTomlMetricsConfig(_customMetrics string, metrics *Metrics) error {
if _, err := toml.DecodeFile(_customMetrics, metrics); err != nil {
return fmt.Errorf("cannot read the metrics config %s: %w", _customMetrics, err)
}
return nil
}

func loadMetricsConfig(_customMetrics string, metrics *Metrics) error {
if strings.HasSuffix(_customMetrics, "toml") {
if err := loadTomlMetricsConfig(_customMetrics, metrics); err != nil {
return fmt.Errorf("cannot load toml based metrics: %w", err)
}
} else {
if err := loadYamlMetricsConfig(_customMetrics, metrics); err != nil {
return fmt.Errorf("cannot load yaml based metrics: %w", err)
}
}
return nil
}
2 changes: 1 addition & 1 deletion collector/default_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var defaultMetricsToml string
func (e *Exporter) DefaultMetrics() Metrics {
var metricsToScrape Metrics
if e.Metrics.Default != "" {
if _, err := toml.DecodeFile(filepath.Clean(e.Metrics.Default), &metricsToScrape); err != nil {
if err := loadMetricsConfig(filepath.Clean(e.Metrics.Default), &metricsToScrape); err != nil {
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."),
"error", err)
}
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/prometheus/client_golang v1.22.0
github.com/prometheus/common v0.65.0
github.com/prometheus/exporter-toolkit v0.14.0
gopkg.in/yaml.v2 v2.4.0
)

require (
Expand All @@ -30,7 +31,6 @@ require (
github.com/golang-jwt/jwt/v5 v5.2.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/klauspost/compress v1.18.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mdlayher/socket v0.4.1 // indirect
github.com/mdlayher/vsock v1.2.1 // indirect
Expand All @@ -50,5 +50,4 @@ require (
golang.org/x/sys v0.33.0 // indirect
golang.org/x/text v0.25.0 // indirect
google.golang.org/protobuf v1.36.6 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)